mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
enum exceptions
This commit is contained in:
@@ -1,258 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class BitArrayTestCase extends Assert {
|
||||
|
||||
@Test
|
||||
public void testGetSet() {
|
||||
BitArray array = new BitArray(33);
|
||||
for (int i = 0; i < 33; i++) {
|
||||
assertFalse(array.get(i));
|
||||
array.set(i);
|
||||
assertTrue(array.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSet1() {
|
||||
BitArray array = new BitArray(32);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
assertEquals(String.valueOf(i), 32, array.getNextSet(i));
|
||||
}
|
||||
array = new BitArray(33);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
assertEquals(String.valueOf(i), 33, array.getNextSet(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSet2() {
|
||||
BitArray array = new BitArray(33);
|
||||
array.set(31);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
assertEquals(String.valueOf(i), i <= 31 ? 31 : 33, array.getNextSet(i));
|
||||
}
|
||||
array = new BitArray(33);
|
||||
array.set(32);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
assertEquals(String.valueOf(i), 32, array.getNextSet(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSet3() {
|
||||
BitArray array = new BitArray(63);
|
||||
array.set(31);
|
||||
array.set(32);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
int expected;
|
||||
if (i <= 31) {
|
||||
expected = 31;
|
||||
} else if (i == 32) {
|
||||
expected = 32;
|
||||
} else {
|
||||
expected = 63;
|
||||
}
|
||||
assertEquals(String.valueOf(i), expected, array.getNextSet(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSet4() {
|
||||
BitArray array = new BitArray(63);
|
||||
array.set(33);
|
||||
array.set(40);
|
||||
for (int i = 0; i < array.getSize(); i++) {
|
||||
int expected;
|
||||
if (i <= 33) {
|
||||
expected = 33;
|
||||
} else if (i <= 40) {
|
||||
expected = 40;
|
||||
} else {
|
||||
expected = 63;
|
||||
}
|
||||
assertEquals(String.valueOf(i), expected, array.getNextSet(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNextSet5() {
|
||||
Random r = new Random(0xDEADBEEF);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
BitArray array = new BitArray(1 + r.nextInt(100));
|
||||
int numSet = r.nextInt(20);
|
||||
for (int j = 0; j < numSet; j++) {
|
||||
array.set(r.nextInt(array.getSize()));
|
||||
}
|
||||
int numQueries = r.nextInt(20);
|
||||
for (int j = 0; j < numQueries; j++) {
|
||||
int query = r.nextInt(array.getSize());
|
||||
int expected = query;
|
||||
while (expected < array.getSize() && !array.get(expected)) {
|
||||
expected++;
|
||||
}
|
||||
int actual = array.getNextSet(query);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSetBulk() {
|
||||
BitArray array = new BitArray(64);
|
||||
array.setBulk(32, 0xFFFF0000);
|
||||
for (int i = 0; i < 48; i++) {
|
||||
assertFalse(array.get(i));
|
||||
}
|
||||
for (int i = 48; i < 64; i++) {
|
||||
assertTrue(array.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetRange() {
|
||||
BitArray array = new BitArray(64);
|
||||
array.setRange(28, 36);
|
||||
assertFalse(array.get(27));
|
||||
for (int i = 28; i < 36; i++) {
|
||||
assertTrue(array.get(i));
|
||||
}
|
||||
assertFalse(array.get(36));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() {
|
||||
BitArray array = new BitArray(32);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
array.set(i);
|
||||
}
|
||||
array.clear();
|
||||
for (int i = 0; i < 32; i++) {
|
||||
assertFalse(array.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlip() {
|
||||
BitArray array = new BitArray(32);
|
||||
assertFalse(array.get(5));
|
||||
array.flip(5);
|
||||
assertTrue(array.get(5));
|
||||
array.flip(5);
|
||||
assertFalse(array.get(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetArray() {
|
||||
BitArray array = new BitArray(64);
|
||||
array.set(0);
|
||||
array.set(63);
|
||||
int[] ints = array.getBitArray();
|
||||
assertEquals(1, ints[0]);
|
||||
assertEquals(Integer.MIN_VALUE, ints[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsRange() {
|
||||
BitArray array = new BitArray(64);
|
||||
assertTrue(array.isRange(0, 64, false));
|
||||
assertFalse(array.isRange(0, 64, true));
|
||||
array.set(32);
|
||||
assertTrue(array.isRange(32, 33, true));
|
||||
array.set(31);
|
||||
assertTrue(array.isRange(31, 33, true));
|
||||
array.set(34);
|
||||
assertFalse(array.isRange(31, 35, true));
|
||||
for (int i = 0; i < 31; i++) {
|
||||
array.set(i);
|
||||
}
|
||||
assertTrue(array.isRange(0, 33, true));
|
||||
for (int i = 33; i < 64; i++) {
|
||||
array.set(i);
|
||||
}
|
||||
assertTrue(array.isRange(0, 64, true));
|
||||
assertFalse(array.isRange(0, 64, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reverseAlgorithmTest() {
|
||||
int[] oldBits = {128, 256, 512, 6453324, 50934953};
|
||||
for (int size = 1; size < 160; size++) {
|
||||
int[] newBitsOriginal = reverseOriginal(oldBits.clone(), size);
|
||||
BitArray newBitArray = new BitArray(oldBits.clone(), size);
|
||||
newBitArray.reverse();
|
||||
int[] newBitsNew = newBitArray.getBitArray();
|
||||
assertTrue(arraysAreEqual(newBitsOriginal, newBitsNew, size / 32 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClone() {
|
||||
BitArray array = new BitArray(32);
|
||||
array.clone().set(0);
|
||||
assertFalse(array.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
BitArray a = new BitArray(32);
|
||||
BitArray b = new BitArray(32);
|
||||
assertEquals(a, b);
|
||||
assertEquals(a.hashCode(), b.hashCode());
|
||||
assertNotEquals(a, new BitArray(31));
|
||||
a.set(16);
|
||||
assertNotEquals(a, b);
|
||||
assertNotEquals(a.hashCode(), b.hashCode());
|
||||
b.set(16);
|
||||
assertEquals(a, b);
|
||||
assertEquals(a.hashCode(), b.hashCode());
|
||||
}
|
||||
|
||||
private static int[] reverseOriginal(int[] oldBits, int size) {
|
||||
int[] newBits = new int[oldBits.length];
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (bitSet(oldBits, size - i - 1)) {
|
||||
newBits[i / 32] |= 1 << (i & 0x1F);
|
||||
}
|
||||
}
|
||||
return newBits;
|
||||
}
|
||||
|
||||
private static boolean bitSet(int[] bits, int i) {
|
||||
return (bits[i / 32] & (1 << (i & 0x1F))) != 0;
|
||||
}
|
||||
|
||||
private static boolean arraysAreEqual(int[] left, int[] right, int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (left[i] != right[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,347 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Sean Owen
|
||||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
public final class BitMatrixTestCase extends Assert {
|
||||
|
||||
private static final int[] BIT_MATRIX_POINTS = { 1, 2, 2, 0, 3, 1 };
|
||||
|
||||
@Test
|
||||
public void testGetSet() {
|
||||
BitMatrix matrix = new BitMatrix(33);
|
||||
assertEquals(33, matrix.getHeight());
|
||||
for (int y = 0; y < 33; y++) {
|
||||
for (int x = 0; x < 33; x++) {
|
||||
if (y * x % 3 == 0) {
|
||||
matrix.set(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int y = 0; y < 33; y++) {
|
||||
for (int x = 0; x < 33; x++) {
|
||||
assertEquals(y * x % 3 == 0, matrix.get(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetRegion() {
|
||||
BitMatrix matrix = new BitMatrix(5);
|
||||
matrix.setRegion(1, 1, 3, 3);
|
||||
for (int y = 0; y < 5; y++) {
|
||||
for (int x = 0; x < 5; x++) {
|
||||
assertEquals(y >= 1 && y <= 3 && x >= 1 && x <= 3, matrix.get(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnclosing() {
|
||||
BitMatrix matrix = new BitMatrix(5);
|
||||
assertNull(matrix.getEnclosingRectangle());
|
||||
matrix.setRegion(1, 1, 1, 1);
|
||||
assertArrayEquals(new int[] { 1, 1, 1, 1 }, matrix.getEnclosingRectangle());
|
||||
matrix.setRegion(1, 1, 3, 2);
|
||||
assertArrayEquals(new int[] { 1, 1, 3, 2 }, matrix.getEnclosingRectangle());
|
||||
matrix.setRegion(0, 0, 5, 5);
|
||||
assertArrayEquals(new int[] { 0, 0, 5, 5 }, matrix.getEnclosingRectangle());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnBit() {
|
||||
BitMatrix matrix = new BitMatrix(5);
|
||||
assertNull(matrix.getTopLeftOnBit());
|
||||
assertNull(matrix.getBottomRightOnBit());
|
||||
matrix.setRegion(1, 1, 1, 1);
|
||||
assertArrayEquals(new int[] { 1, 1 }, matrix.getTopLeftOnBit());
|
||||
assertArrayEquals(new int[] { 1, 1 }, matrix.getBottomRightOnBit());
|
||||
matrix.setRegion(1, 1, 3, 2);
|
||||
assertArrayEquals(new int[] { 1, 1 }, matrix.getTopLeftOnBit());
|
||||
assertArrayEquals(new int[] { 3, 2 }, matrix.getBottomRightOnBit());
|
||||
matrix.setRegion(0, 0, 5, 5);
|
||||
assertArrayEquals(new int[] { 0, 0 }, matrix.getTopLeftOnBit());
|
||||
assertArrayEquals(new int[] { 4, 4 }, matrix.getBottomRightOnBit());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRectangularMatrix() {
|
||||
BitMatrix matrix = new BitMatrix(75, 20);
|
||||
assertEquals(75, matrix.getWidth());
|
||||
assertEquals(20, matrix.getHeight());
|
||||
matrix.set(10, 0);
|
||||
matrix.set(11, 1);
|
||||
matrix.set(50, 2);
|
||||
matrix.set(51, 3);
|
||||
matrix.flip(74, 4);
|
||||
matrix.flip(0, 5);
|
||||
|
||||
// Should all be on
|
||||
assertTrue(matrix.get(10, 0));
|
||||
assertTrue(matrix.get(11, 1));
|
||||
assertTrue(matrix.get(50, 2));
|
||||
assertTrue(matrix.get(51, 3));
|
||||
assertTrue(matrix.get(74, 4));
|
||||
assertTrue(matrix.get(0, 5));
|
||||
|
||||
// Flip a couple back off
|
||||
matrix.flip(50, 2);
|
||||
matrix.flip(51, 3);
|
||||
assertFalse(matrix.get(50, 2));
|
||||
assertFalse(matrix.get(51, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRectangularSetRegion() {
|
||||
BitMatrix matrix = new BitMatrix(320, 240);
|
||||
assertEquals(320, matrix.getWidth());
|
||||
assertEquals(240, matrix.getHeight());
|
||||
matrix.setRegion(105, 22, 80, 12);
|
||||
|
||||
// Only bits in the region should be on
|
||||
for (int y = 0; y < 240; y++) {
|
||||
for (int x = 0; x < 320; x++) {
|
||||
assertEquals(y >= 22 && y < 34 && x >= 105 && x < 185, matrix.get(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetRow() {
|
||||
BitMatrix matrix = new BitMatrix(102, 5);
|
||||
for (int x = 0; x < 102; x++) {
|
||||
if ((x & 0x03) == 0) {
|
||||
matrix.set(x, 2);
|
||||
}
|
||||
}
|
||||
|
||||
// Should allocate
|
||||
BitArray array = matrix.getRow(2, null);
|
||||
assertEquals(102, array.getSize());
|
||||
|
||||
// Should reallocate
|
||||
BitArray array2 = new BitArray(60);
|
||||
array2 = matrix.getRow(2, array2);
|
||||
assertEquals(102, array2.getSize());
|
||||
|
||||
// Should use provided object, with original BitArray size
|
||||
BitArray array3 = new BitArray(200);
|
||||
array3 = matrix.getRow(2, array3);
|
||||
assertEquals(200, array3.getSize());
|
||||
|
||||
for (int x = 0; x < 102; x++) {
|
||||
boolean on = (x & 0x03) == 0;
|
||||
assertEquals(on, array.get(x));
|
||||
assertEquals(on, array2.get(x));
|
||||
assertEquals(on, array3.get(x));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRotate90Simple() {
|
||||
BitMatrix matrix = new BitMatrix(3, 3);
|
||||
matrix.set(0, 0);
|
||||
matrix.set(0, 1);
|
||||
matrix.set(1, 2);
|
||||
matrix.set(2, 1);
|
||||
|
||||
matrix.rotate90();
|
||||
|
||||
assertTrue(matrix.get(0, 2));
|
||||
assertTrue(matrix.get(1, 2));
|
||||
assertTrue(matrix.get(2, 1));
|
||||
assertTrue(matrix.get(1, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRotate180Simple() {
|
||||
BitMatrix matrix = new BitMatrix(3, 3);
|
||||
matrix.set(0, 0);
|
||||
matrix.set(0, 1);
|
||||
matrix.set(1, 2);
|
||||
matrix.set(2, 1);
|
||||
|
||||
matrix.rotate180();
|
||||
|
||||
assertTrue(matrix.get(2, 2));
|
||||
assertTrue(matrix.get(2, 1));
|
||||
assertTrue(matrix.get(1, 0));
|
||||
assertTrue(matrix.get(0, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRotate180() {
|
||||
testRotate180(7, 4);
|
||||
testRotate180(7, 5);
|
||||
testRotate180(8, 4);
|
||||
testRotate180(8, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() {
|
||||
BitMatrix emptyMatrix = new BitMatrix(3, 3);
|
||||
BitMatrix fullMatrix = new BitMatrix(3, 3);
|
||||
fullMatrix.setRegion(0, 0, 3, 3);
|
||||
BitMatrix centerMatrix = new BitMatrix(3, 3);
|
||||
centerMatrix.setRegion(1, 1, 1, 1);
|
||||
BitMatrix emptyMatrix24 = new BitMatrix(2, 4);
|
||||
|
||||
assertEquals(emptyMatrix, BitMatrix.parse(" \n \n \n", "x", " "));
|
||||
assertEquals(emptyMatrix, BitMatrix.parse(" \n \r\r\n \n\r", "x", " "));
|
||||
assertEquals(emptyMatrix, BitMatrix.parse(" \n \n ", "x", " "));
|
||||
|
||||
assertEquals(fullMatrix, BitMatrix.parse("xxx\nxxx\nxxx\n", "x", " "));
|
||||
|
||||
assertEquals(centerMatrix, BitMatrix.parse(" \n x \n \n", "x", " "));
|
||||
assertEquals(centerMatrix, BitMatrix.parse(" \n x \n \n", "x ", " "));
|
||||
try {
|
||||
assertEquals(centerMatrix, BitMatrix.parse(" \n xy\n \n", "x", " "));
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// good
|
||||
}
|
||||
|
||||
assertEquals(emptyMatrix24, BitMatrix.parse(" \n \n \n \n", "x", " "));
|
||||
|
||||
assertEquals(centerMatrix, BitMatrix.parse(centerMatrix.toString("x", "."), "x", "."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseBoolean() {
|
||||
BitMatrix emptyMatrix = new BitMatrix(3, 3);
|
||||
BitMatrix fullMatrix = new BitMatrix(3, 3);
|
||||
fullMatrix.setRegion(0, 0, 3, 3);
|
||||
BitMatrix centerMatrix = new BitMatrix(3, 3);
|
||||
centerMatrix.setRegion(1, 1, 1, 1);
|
||||
BitMatrix emptyMatrix24 = new BitMatrix(2, 4);
|
||||
|
||||
boolean[][] matrix = new boolean[3][3];
|
||||
assertEquals(emptyMatrix, BitMatrix.parse(matrix));
|
||||
matrix[1][1] = true;
|
||||
assertEquals(centerMatrix, BitMatrix.parse(matrix));
|
||||
for (boolean[] arr : matrix) {
|
||||
Arrays.fill(arr, true);
|
||||
}
|
||||
assertEquals(fullMatrix, BitMatrix.parse(matrix));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnset() {
|
||||
BitMatrix emptyMatrix = new BitMatrix(3, 3);
|
||||
BitMatrix matrix = emptyMatrix.clone();
|
||||
matrix.set(1, 1);
|
||||
assertNotEquals(emptyMatrix, matrix);
|
||||
matrix.unset(1, 1);
|
||||
assertEquals(emptyMatrix, matrix);
|
||||
matrix.unset(1, 1);
|
||||
assertEquals(emptyMatrix, matrix);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXOR() {
|
||||
BitMatrix emptyMatrix = new BitMatrix(3, 3);
|
||||
BitMatrix fullMatrix = new BitMatrix(3, 3);
|
||||
fullMatrix.setRegion(0, 0, 3, 3);
|
||||
BitMatrix centerMatrix = new BitMatrix(3, 3);
|
||||
centerMatrix.setRegion(1, 1, 1, 1);
|
||||
BitMatrix invertedCenterMatrix = fullMatrix.clone();
|
||||
invertedCenterMatrix.unset(1, 1);
|
||||
BitMatrix badMatrix = new BitMatrix(4, 4);
|
||||
|
||||
testXOR(emptyMatrix, emptyMatrix, emptyMatrix);
|
||||
testXOR(emptyMatrix, centerMatrix, centerMatrix);
|
||||
testXOR(emptyMatrix, fullMatrix, fullMatrix);
|
||||
|
||||
testXOR(centerMatrix, emptyMatrix, centerMatrix);
|
||||
testXOR(centerMatrix, centerMatrix, emptyMatrix);
|
||||
testXOR(centerMatrix, fullMatrix, invertedCenterMatrix);
|
||||
|
||||
testXOR(invertedCenterMatrix, emptyMatrix, invertedCenterMatrix);
|
||||
testXOR(invertedCenterMatrix, centerMatrix, fullMatrix);
|
||||
testXOR(invertedCenterMatrix, fullMatrix, centerMatrix);
|
||||
|
||||
testXOR(fullMatrix, emptyMatrix, fullMatrix);
|
||||
testXOR(fullMatrix, centerMatrix, invertedCenterMatrix);
|
||||
testXOR(fullMatrix, fullMatrix, emptyMatrix);
|
||||
|
||||
try {
|
||||
emptyMatrix.clone().xor(badMatrix);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// good
|
||||
}
|
||||
|
||||
try {
|
||||
badMatrix.clone().xor(emptyMatrix);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// good
|
||||
}
|
||||
}
|
||||
|
||||
public static String matrixToString(BitMatrix result) {
|
||||
assertEquals(1, result.getHeight());
|
||||
StringBuilder builder = new StringBuilder(result.getWidth());
|
||||
for (int i = 0; i < result.getWidth(); i++) {
|
||||
builder.append(result.get(i, 0) ? '1' : '0');
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private static void testXOR(BitMatrix dataMatrix, BitMatrix flipMatrix, BitMatrix expectedMatrix) {
|
||||
BitMatrix matrix = dataMatrix.clone();
|
||||
matrix.xor(flipMatrix);
|
||||
assertEquals(expectedMatrix, matrix);
|
||||
}
|
||||
|
||||
private static void testRotate180(int width, int height) {
|
||||
BitMatrix input = getInput(width, height);
|
||||
input.rotate180();
|
||||
BitMatrix expected = getExpected(width, height);
|
||||
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
assertEquals("(" + x + ',' + y + ')', expected.get(x, y), input.get(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static BitMatrix getExpected(int width, int height) {
|
||||
BitMatrix result = new BitMatrix(width, height);
|
||||
for (int i = 0; i < BIT_MATRIX_POINTS.length; i += 2) {
|
||||
result.set(width - 1 - BIT_MATRIX_POINTS[i], height - 1 - BIT_MATRIX_POINTS[i + 1]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static BitMatrix getInput(int width, int height) {
|
||||
BitMatrix result = new BitMatrix(width, height);
|
||||
for (int i = 0; i < BIT_MATRIX_POINTS.length; i += 2) {
|
||||
result.set(BIT_MATRIX_POINTS[i], BIT_MATRIX_POINTS[i + 1]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright 2008 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
/**
|
||||
* Class that lets one easily build an array of bytes by appending bits at a time.
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class BitSourceBuilder {
|
||||
|
||||
private final ByteArrayOutputStream output;
|
||||
private int nextByte;
|
||||
private int bitsLeftInNextByte;
|
||||
|
||||
public BitSourceBuilder() {
|
||||
output = new ByteArrayOutputStream();
|
||||
nextByte = 0;
|
||||
bitsLeftInNextByte = 8;
|
||||
}
|
||||
|
||||
public void write(int value, int numBits) {
|
||||
if (numBits <= bitsLeftInNextByte) {
|
||||
nextByte <<= numBits;
|
||||
nextByte |= value;
|
||||
bitsLeftInNextByte -= numBits;
|
||||
if (bitsLeftInNextByte == 0) {
|
||||
output.write(nextByte);
|
||||
nextByte = 0;
|
||||
bitsLeftInNextByte = 8;
|
||||
}
|
||||
} else {
|
||||
int bitsToWriteNow = bitsLeftInNextByte;
|
||||
int numRestOfBits = numBits - bitsToWriteNow;
|
||||
int mask = 0xFF >> (8 - bitsToWriteNow);
|
||||
int valueToWriteNow = (value >>> numRestOfBits) & mask;
|
||||
write(valueToWriteNow, bitsToWriteNow);
|
||||
write(value, numRestOfBits);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
if (bitsLeftInNextByte < 8) {
|
||||
write(0, bitsLeftInNextByte);
|
||||
}
|
||||
return output.toByteArray();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Tests {@link StringUtils}.
|
||||
*/
|
||||
public final class StringUtilsTestCase extends Assert {
|
||||
|
||||
@Test
|
||||
public void testRandom() {
|
||||
Random r = new Random(1234L);
|
||||
byte[] bytes = new byte[1000];
|
||||
r.nextBytes(bytes);
|
||||
assertEquals(Charset.defaultCharset(), StringUtils.guessCharset(bytes, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortShiftJIS1() {
|
||||
// 金魚
|
||||
doTest(new byte[] { (byte) 0x8b, (byte) 0xe0, (byte) 0x8b, (byte) 0x9b, }, StringUtils.SHIFT_JIS_CHARSET, "SJIS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortISO885911() {
|
||||
// båd
|
||||
doTest(new byte[] { (byte) 0x62, (byte) 0xe5, (byte) 0x64, }, StandardCharsets.ISO_8859_1, "ISO8859_1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShortUTF81() {
|
||||
// Español
|
||||
doTest(new byte[] { (byte) 0x45, (byte) 0x73, (byte) 0x70, (byte) 0x61, (byte) 0xc3,
|
||||
(byte) 0xb1, (byte) 0x6f, (byte) 0x6c },
|
||||
StandardCharsets.UTF_8, "UTF8");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedShiftJIS1() {
|
||||
// Hello 金!
|
||||
doTest(new byte[] { (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f,
|
||||
(byte) 0x20, (byte) 0x8b, (byte) 0xe0, (byte) 0x21, },
|
||||
StringUtils.SHIFT_JIS_CHARSET, "SJIS");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUTF16BE() {
|
||||
// 调压柜
|
||||
doTest(new byte[] { (byte) 0xFE, (byte) 0xFF, (byte) 0x8c, (byte) 0x03, (byte) 0x53, (byte) 0x8b,
|
||||
(byte) 0x67, (byte) 0xdc, },
|
||||
StandardCharsets.UTF_16,
|
||||
StandardCharsets.UTF_16.name());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUTF16LE() {
|
||||
// 调压柜
|
||||
doTest(new byte[] { (byte) 0xFF, (byte) 0xFE, (byte) 0x03, (byte) 0x8c, (byte) 0x8b, (byte) 0x53,
|
||||
(byte) 0xdc, (byte) 0x67, },
|
||||
StandardCharsets.UTF_16,
|
||||
StandardCharsets.UTF_16.name());
|
||||
}
|
||||
|
||||
private static void doTest(byte[] bytes, Charset charset, String encoding) {
|
||||
Charset guessedCharset = StringUtils.guessCharset(bytes, null);
|
||||
String guessedEncoding = StringUtils.guessEncoding(bytes, null);
|
||||
assertEquals(charset, guessedCharset);
|
||||
assertEquals(encoding, guessedEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility for printing out a string in given encoding as a Java statement, since it's better
|
||||
* to write that into the Java source file rather than risk character encoding issues in the
|
||||
* source file itself.
|
||||
*
|
||||
* @param args command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
String text = args[0];
|
||||
Charset charset = Charset.forName(args[1]);
|
||||
StringBuilder declaration = new StringBuilder();
|
||||
declaration.append("new byte[] { ");
|
||||
for (byte b : text.getBytes(charset)) {
|
||||
declaration.append("(byte) 0x");
|
||||
int value = b & 0xFF;
|
||||
if (value < 0x10) {
|
||||
declaration.append('0');
|
||||
}
|
||||
declaration.append(Integer.toHexString(value));
|
||||
declaration.append(", ");
|
||||
}
|
||||
declaration.append('}');
|
||||
System.out.println(declaration);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2008 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
/**
|
||||
* Encapsulates the result of one test over a batch of black-box images.
|
||||
*/
|
||||
public final class TestRXingResult {
|
||||
|
||||
private final int mustPassCount;
|
||||
private final int tryHarderCount;
|
||||
private final int maxMisreads;
|
||||
private final int maxTryHarderMisreads;
|
||||
private final float rotation;
|
||||
|
||||
public TestRXingResult(int mustPassCount, int tryHarderCount, int maxMisreads, int maxTryHarderMisreads, float rotation) {
|
||||
this.mustPassCount = mustPassCount;
|
||||
this.tryHarderCount = tryHarderCount;
|
||||
this.maxMisreads = maxMisreads;
|
||||
this.maxTryHarderMisreads = maxTryHarderMisreads;
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
public int getMustPassCount() {
|
||||
return mustPassCount;
|
||||
}
|
||||
|
||||
public int getTryHarderCount() {
|
||||
return tryHarderCount;
|
||||
}
|
||||
|
||||
public int getMaxMisreads() {
|
||||
return maxMisreads;
|
||||
}
|
||||
|
||||
public int getMaxTryHarderMisreads() {
|
||||
return maxTryHarderMisreads;
|
||||
}
|
||||
|
||||
public float getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user