Follow up on commit for issue #242

This commit is contained in:
Sean Owen 2014-10-30 21:40:02 +00:00
parent e0495aafd3
commit d876b3e178
9 changed files with 33 additions and 44 deletions

View file

@ -53,6 +53,7 @@ Isaac Potoczny-Jones
Ivan Poliakov Ivan Poliakov
Jacob Haynes (Google) Jacob Haynes (Google)
Jeff Breidenbach (Google) Jeff Breidenbach (Google)
jjYBdx4IL
Joan Montané (Softcatalà.cat) Joan Montané (Softcatalà.cat)
John Connolly (Bug Labs) John Connolly (Bug Labs)
Jonas Petersson (Prisjakt) Jonas Petersson (Prisjakt)

View file

@ -33,7 +33,7 @@ public abstract class ReaderException extends Exception {
} }
ReaderException(Throwable cause) { ReaderException(Throwable cause) {
super(cause); super(cause);
} }
// Prevent stack traces from being taken // Prevent stack traces from being taken

View file

@ -63,7 +63,6 @@ public final class BitMatrix implements Cloneable {
} }
public static BitMatrix parse(String stringRepresentation, String setString, String unsetString) { public static BitMatrix parse(String stringRepresentation, String setString, String unsetString) {
int pos = 0;
if (stringRepresentation == null) { if (stringRepresentation == null) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
@ -73,8 +72,10 @@ public final class BitMatrix implements Cloneable {
int rowStartPos = 0; int rowStartPos = 0;
int rowLength = -1; int rowLength = -1;
int nRows = 0; int nRows = 0;
int pos = 0;
while (pos < stringRepresentation.length()) { while (pos < stringRepresentation.length()) {
if (stringRepresentation.substring(pos, pos + 1).equals("\n") || stringRepresentation.substring(pos, pos + 1).equals("\r")) { if (stringRepresentation.charAt(pos) == '\n' ||
stringRepresentation.charAt(pos) == '\r') {
if (bitsPos > rowStartPos) { if (bitsPos > rowStartPos) {
if(rowLength == -1) { if(rowLength == -1) {
rowLength = bitsPos - rowStartPos; rowLength = bitsPos - rowStartPos;
@ -97,7 +98,8 @@ public final class BitMatrix implements Cloneable {
bits[bitsPos] = false; bits[bitsPos] = false;
bitsPos++; bitsPos++;
} else { } else {
throw new IllegalArgumentException("illegal character encountered: " + stringRepresentation.substring(pos)); throw new IllegalArgumentException(
"illegal character encountered: " + stringRepresentation.substring(pos));
} }
} }
@ -105,15 +107,14 @@ public final class BitMatrix implements Cloneable {
if (bitsPos > rowStartPos) { if (bitsPos > rowStartPos) {
if(rowLength == -1) { if(rowLength == -1) {
rowLength = bitsPos - rowStartPos; rowLength = bitsPos - rowStartPos;
} } else if (bitsPos - rowStartPos != rowLength) {
else if (bitsPos - rowStartPos != rowLength) {
throw new IllegalArgumentException("row lengths do not match"); throw new IllegalArgumentException("row lengths do not match");
} }
nRows++; nRows++;
} }
BitMatrix matrix = new BitMatrix(rowLength, nRows); BitMatrix matrix = new BitMatrix(rowLength, nRows);
for (int i=0; i<bitsPos; i++) { for (int i = 0; i < bitsPos; i++) {
if (bits[i]) { if (bits[i]) {
matrix.set(i % rowLength, i / rowLength); matrix.set(i % rowLength, i / rowLength);
} }
@ -161,17 +162,17 @@ public final class BitMatrix implements Cloneable {
} }
/** /**
* <p>XOR for {@link BitMatrix}.</p> * Exclusive-or (XOR): Flip the bit in this {@code BitMatrix} if the corresponding
* Flip the bit in this {@link BitMatrix} if the corresponding mask bit is set. * mask bit is set.
* *
* @param mask * @param mask XOR mask
*/ */
public void xor(BitMatrix mask) { public void xor(BitMatrix mask) {
if (width != mask.getWidth() || height != mask.getHeight() if (width != mask.getWidth() || height != mask.getHeight()
|| rowSize != mask.getRowSize()) { || rowSize != mask.getRowSize()) {
throw new IllegalArgumentException("input matrix dimensions do not match"); throw new IllegalArgumentException("input matrix dimensions do not match");
} }
BitArray rowArray = new BitArray(width/32+1); BitArray rowArray = new BitArray(width / 32 + 1);
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
int offset = y * rowSize; int offset = y * rowSize;
int[] row = mask.getRow(y, rowArray).getBitArray(); int[] row = mask.getRow(y, rowArray).getBitArray();

View file

@ -19,11 +19,9 @@ import com.google.zxing.FormatException;
import com.google.zxing.ResultPoint; import com.google.zxing.ResultPoint;
import com.google.zxing.aztec.AztecDetectorResult; import com.google.zxing.aztec.AztecDetectorResult;
import com.google.zxing.common.BitMatrix; import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.DecoderResult;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*;
public class DecoderTest { public final class DecoderTest {
private static final ResultPoint[] NO_POINTS = new ResultPoint[0]; private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
@ -36,8 +34,8 @@ public class DecoderTest {
* at com.google.zxing.aztec.decoder.Decoder.decode(Decoder.java:77) * at com.google.zxing.aztec.decoder.Decoder.decode(Decoder.java:77)
* at com.google.zxing.aztec.decoder.DecoderTest.testDecodeBug1(DecoderTest.java:66)</pre> * at com.google.zxing.aztec.decoder.DecoderTest.testDecodeBug1(DecoderTest.java:66)</pre>
*/ */
@Test @Test(expected = FormatException.class)
public void testDecodeTooManyErrors() { public void testDecodeTooManyErrors() throws FormatException {
BitMatrix matrix = BitMatrix.parse("" BitMatrix matrix = BitMatrix.parse(""
+ "X X . X . . . X X . . . X . . X X X . X . X X X X X . \n" + "X X . X . . . X X . . . X . . X X X . X . X X X X X . \n"
+ "X X . . X X . . . . . X X . . . X X . . . X . X . . X \n" + "X X . . X X . . . . . X X . . . X X . . . X . X . . X \n"
@ -68,15 +66,11 @@ public class DecoderTest {
+ "X X . X . X . . . X . X . . . . X X . X . . X X . . . \n", + "X X . X . X . . . X . X . . . . X X . X . . X X . . . \n",
"X ", ". "); "X ", ". ");
AztecDetectorResult r = new AztecDetectorResult(matrix, NO_POINTS, true, 16, 4); AztecDetectorResult r = new AztecDetectorResult(matrix, NO_POINTS, true, 16, 4);
try { new Decoder().decode(r);
DecoderResult res = new Decoder().decode(r);
fail();
} catch (FormatException ex) {
}
} }
@Test @Test(expected = FormatException.class)
public void testDecodeTooManyErrors2() { public void testDecodeTooManyErrors2() throws FormatException {
BitMatrix matrix = BitMatrix.parse("" BitMatrix matrix = BitMatrix.parse(""
+ ". X X . . X . X X . . . X . . X X X . . . X X . X X . \n" + ". X X . . X . X X . . . X . . X X X . . . X X . X X . \n"
+ "X X . X X . . X . . . X X . . . X X . X X X . X . X X \n" + "X X . X X . . X . . . X X . . . X X . X X X . X . X X \n"
@ -107,11 +101,7 @@ public class DecoderTest {
+ "X X . . . X X . . X . X . . . . X X . X . . X . X . X \n", + "X X . . . X X . . X . X . . . . X X . X . . X . X . X \n",
"X ", ". "); "X ", ". ");
AztecDetectorResult r = new AztecDetectorResult(matrix, NO_POINTS, true, 16, 4); AztecDetectorResult r = new AztecDetectorResult(matrix, NO_POINTS, true, 16, 4);
try { new Decoder().decode(r);
DecoderResult res = new Decoder().decode(r);
fail();
} catch (FormatException ex) {
}
} }
} }

View file

@ -27,17 +27,14 @@ import com.google.zxing.common.BitArray;
import com.google.zxing.common.BitMatrix; import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.DecoderResult; import com.google.zxing.common.DecoderResult;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.junit.Before;
/** /**
* Aztec 2D generator unit tests. * Aztec 2D generator unit tests.
@ -49,12 +46,6 @@ public final class EncoderTest extends Assert {
private static final Pattern DOTX = Pattern.compile("[^.X]"); private static final Pattern DOTX = Pattern.compile("[^.X]");
private static final ResultPoint[] NO_POINTS = new ResultPoint[0]; private static final ResultPoint[] NO_POINTS = new ResultPoint[0];
private static Random random;
@Before
public void beforeTest() {
random = new Random(0);
}
// real life tests // real life tests
@ -513,7 +504,7 @@ public final class EncoderTest extends Assert {
} }
private static Random getPseudoRandom() { private static Random getPseudoRandom() {
return random; return new Random(0xDEADBEEF);
} }
private static void testModeMessage(boolean compact, int layers, int words, String expected) { private static void testModeMessage(boolean compact, int layers, int words, String expected) {

View file

@ -101,7 +101,7 @@ public final class BitArrayTestCase extends Assert {
@Test @Test
public void testGetNextSet5() { public void testGetNextSet5() {
Random r = new SecureRandom(new byte[] {(byte) 0xDE, (byte) 0xAD, (byte) 0xBE, (byte) 0xEF}); Random r = new Random(0xDEADBEEF);
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
BitArray array = new BitArray(1 + r.nextInt(100)); BitArray array = new BitArray(1 + r.nextInt(100));
int numSet = r.nextInt(20); int numSet = r.nextInt(20);

View file

@ -173,7 +173,9 @@ public final class BitMatrixTestCase extends Assert {
try { try {
assertEquals(centerMatrix, BitMatrix.parse(" \n xy\n \n", "x", " ")); assertEquals(centerMatrix, BitMatrix.parse(" \n xy\n \n", "x", " "));
fail(); fail();
} catch (IllegalArgumentException ex) {} } catch (IllegalArgumentException ex) {
// good
}
assertEquals(emptyMatrix24, BitMatrix.parse(" \n \n \n \n", "x", " ")); assertEquals(emptyMatrix24, BitMatrix.parse(" \n \n \n \n", "x", " "));
@ -222,12 +224,16 @@ public final class BitMatrixTestCase extends Assert {
try { try {
emptyMatrix.clone().xor(badMatrix); emptyMatrix.clone().xor(badMatrix);
fail(); fail();
} catch(IllegalArgumentException ex) {} } catch(IllegalArgumentException ex) {
// good
}
try { try {
badMatrix.clone().xor(emptyMatrix); badMatrix.clone().xor(emptyMatrix);
fail(); fail();
} catch(IllegalArgumentException ex) {} } catch(IllegalArgumentException ex) {
// good
}
} }
private static void testXOR(BitMatrix dataMatrix, BitMatrix flipMatrix, BitMatrix expectedMatrix) { private static void testXOR(BitMatrix dataMatrix, BitMatrix flipMatrix, BitMatrix expectedMatrix) {

View file

@ -501,7 +501,7 @@ public final class ReedSolomonTestCase extends Assert {
} }
private static Random getPseudoRandom() { private static Random getPseudoRandom() {
return new SecureRandom(new byte[] {(byte) 0xDE, (byte) 0xAD, (byte) 0xBE, (byte) 0xEF}); return new Random(0xDEADBEEF);
} }
} }

View file

@ -49,7 +49,7 @@ abstract class AbstractErrorCorrectionTestCase extends Assert {
} }
static Random getRandom() { static Random getRandom() {
return new Random(0); return new Random(0xDEADBEEF);
} }
} }