package com.google.zxing.qrcode.encoder;
-import com.google.zxing.common.ByteMatrix;
import com.google.zxing.WriterException;
+import com.google.zxing.common.BitArray;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
-import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Test;
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author mysen@google.com (Chris Mysen) - ported from C++
*/
-public final class MatrixUtilTestCase extends TestCase {
+public final class MatrixUtilTestCase extends Assert {
public void testtoString() {
ByteMatrix array = new ByteMatrix(3, 3);
array.set(0, 0, 0);
- array.set(0, 1, 1);
- array.set(0, 2, 0);
array.set(1, 0, 1);
+ array.set(2, 0, 0);
+ array.set(0, 1, 1);
array.set(1, 1, 0);
- array.set(1, 2, 1);
- array.set(2, 0, -1);
- array.set(2, 1, -1);
+ array.set(2, 1, 1);
+ array.set(0, 2, -1);
+ array.set(1, 2, -1);
array.set(2, 2, -1);
String expected = " 0 1 0\n" + " 1 0 1\n" + " \n";
assertEquals(expected, array.toString());
}
+ @Test
public void testClearMatrix() {
ByteMatrix matrix = new ByteMatrix(2, 2);
MatrixUtil.clearMatrix(matrix);
assertEquals(-1, matrix.get(0, 0));
- assertEquals(-1, matrix.get(0, 1));
assertEquals(-1, matrix.get(1, 0));
+ assertEquals(-1, matrix.get(0, 1));
assertEquals(-1, matrix.get(1, 1));
}
+ @Test
public void testEmbedBasicPatterns() throws WriterException {
{
// Version 1.
String expected =
- " 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
- " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" +
- " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
- " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
- " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
- " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" +
- " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
- " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
- " 1 \n" +
- " 0 \n" +
- " 1 \n" +
- " 0 \n" +
- " 1 \n" +
- " 0 0 0 0 0 0 0 0 1 \n" +
- " 1 1 1 1 1 1 1 0 \n" +
- " 1 0 0 0 0 0 1 0 \n" +
- " 1 0 1 1 1 0 1 0 \n" +
- " 1 0 1 1 1 0 1 0 \n" +
- " 1 0 1 1 1 0 1 0 \n" +
- " 1 0 0 0 0 0 1 0 \n" +
- " 1 1 1 1 1 1 1 0 \n";
+ " 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
+ " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1\n" +
+ " 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1\n" +
+ " 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1\n" +
+ " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
+ " 1 \n" +
+ " 0 \n" +
+ " 1 \n" +
+ " 0 \n" +
+ " 1 \n" +
+ " 0 0 0 0 0 0 0 0 1 \n" +
+ " 1 1 1 1 1 1 1 0 \n" +
+ " 1 0 0 0 0 0 1 0 \n" +
+ " 1 0 1 1 1 0 1 0 \n" +
+ " 1 0 1 1 1 0 1 0 \n" +
+ " 1 0 1 1 1 0 1 0 \n" +
+ " 1 0 0 0 0 0 1 0 \n" +
+ " 1 1 1 1 1 1 1 0 \n";
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedBasicPatterns(1, matrix);
// Version 2. Position adjustment pattern should apppear at right
// bottom corner.
String expected =
- " 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1\n" +
- " 1 0 0 0 0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" +
+ " 0 \n" +
+ " 1 1 1 1 1 1 \n" +
+ " 0 0 0 0 0 0 0 0 1 1 0 0 0 1 \n" +
+ " 1 1 1 1 1 1 1 0 1 0 1 0 1 \n" +
+ " 1 0 0 0 0 0 1 0 1 0 0 0 1 \n" +
+ " 1 0 1 1 1 0 1 0 1 1 1 1 1 \n" +
+ " 1 0 1 1 1 0 1 0 \n" +
+ " 1 0 1 1 1 0 1 0 \n" +
+ " 1 0 0 0 0 0 1 0 \n" +
+ " 1 1 1 1 1 1 1 0 \n";
ByteMatrix matrix = new ByteMatrix(25, 25);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedBasicPatterns(2, matrix);
}
}
+ @Test
public void testEmbedTypeInfo() throws WriterException {
// Type info bits = 100000011001110.
String expected =
assertEquals(expected, matrix.toString());
}
+ @Test
public void testEmbedVersionInfo() throws WriterException {
// Version info bits = 000111 110010 010100
String expected =
assertEquals(expected, matrix.toString());
}
+ @Test
public void testEmbedDataBits() throws WriterException {
// Cells other than basic patterns should be filled with zero.
String expected =
" 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" +
" 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n";
- BitVector bits = new BitVector();
+ BitArray bits = new BitArray();
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.clearMatrix(matrix);
MatrixUtil.embedBasicPatterns(1, matrix);
assertEquals(expected, matrix.toString());
}
+ @Test
public void testBuildMatrix() throws WriterException {
// From http://www.swetake.com/qr/qr7.html
String expected =
" 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0\n" +
" 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0\n" +
" 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 0\n";
- char bytes[] = {32, 65, 205, 69, 41, 220, 46, 128, 236,
- 42, 159, 74, 221, 244, 169, 239, 150, 138,
- 70, 237, 85, 224, 96, 74, 219 , 61};
- BitVector bits = new BitVector();
+ char[] bytes = {32, 65, 205, 69, 41, 220, 46, 128, 236,
+ 42, 159, 74, 221, 244, 169, 239, 150, 138,
+ 70, 237, 85, 224, 96, 74, 219 , 61};
+ BitArray bits = new BitArray();
for (char c: bytes) {
bits.appendBits(c, 8);
}
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.buildMatrix(bits,
- ErrorCorrectionLevel.H,
- 1, // Version 1
- 3, // Mask pattern 3
- matrix);
+ ErrorCorrectionLevel.H,
+ 1, // Version 1
+ 3, // Mask pattern 3
+ matrix);
}
+ @Test
public void testFindMSBSet() {
assertEquals(0, MatrixUtil.findMSBSet(0));
assertEquals(1, MatrixUtil.findMSBSet(1));
assertEquals(32, MatrixUtil.findMSBSet(0x80000000));
}
+ @Test
public void testCalculateBCHCode() {
// Encoding of type information.
// From Appendix C in JISX0510:2004 (p 65)
// We don't test a lot of cases in this function since we've already
// tested them in TEST(calculateBCHCode).
+ @Test
public void testMakeVersionInfoBits() throws WriterException {
// From Appendix D in JISX0510:2004 (p 68)
- BitVector bits = new BitVector();
+ BitArray bits = new BitArray();
MatrixUtil.makeVersionInfoBits(7, bits);
- assertEquals("000111110010010100", bits.toString());
+ assertEquals(" ...XXXXX ..X..X.X ..", bits.toString());
}
// We don't test a lot of cases in this function since we've already
// tested them in TEST(calculateBCHCode).
+ @Test
public void testMakeTypeInfoInfoBits() throws WriterException {
// From Appendix C in JISX0510:2004 (p 65)
- BitVector bits = new BitVector();
- MatrixUtil.makeTypeInfoBits(ErrorCorrectionLevel.M,
- 5, bits);
- assertEquals("100000011001110", bits.toString());
+ BitArray bits = new BitArray();
+ MatrixUtil.makeTypeInfoBits(ErrorCorrectionLevel.M, 5, bits);
+ assertEquals(" X......X X..XXX.", bits.toString());
}
}