2 * Copyright 2007 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.common;
19 import junit.framework.TestCase;
23 * @author dswitkin@google.com (Daniel Switkin)
25 public final class BitMatrixTestCase extends TestCase {
27 public void testGetSet() {
28 BitMatrix matrix = new BitMatrix(33);
29 assertEquals(33, matrix.getHeight());
30 for (int y = 0; y < 33; y++) {
31 for (int x = 0; x < 33; x++) {
37 for (int y = 0; y < 33; y++) {
38 for (int x = 0; x < 33; x++) {
39 assertEquals(y * x % 3 == 0, matrix.get(x, y));
44 public void testSetRegion() {
45 BitMatrix matrix = new BitMatrix(5);
46 matrix.setRegion(1, 1, 3, 3);
47 for (int y = 0; y < 5; y++) {
48 for (int x = 0; x < 5; x++) {
49 assertEquals(y >= 1 && y <= 3 && x >= 1 && x <= 3, matrix.get(x, y));
54 public void testRectangularMatrix() {
55 BitMatrix matrix = new BitMatrix(75, 20);
56 assertEquals(75, matrix.getWidth());
57 assertEquals(20, matrix.getHeight());
66 assertTrue(matrix.get(10, 0));
67 assertTrue(matrix.get(11, 1));
68 assertTrue(matrix.get(50, 2));
69 assertTrue(matrix.get(51, 3));
70 assertTrue(matrix.get(74, 4));
71 assertTrue(matrix.get(0, 5));
73 // Flip a couple back off
76 assertFalse(matrix.get(50, 2));
77 assertFalse(matrix.get(51, 3));
80 public void testRectangularSetRegion() {
81 BitMatrix matrix = new BitMatrix(320, 240);
82 assertEquals(320, matrix.getWidth());
83 assertEquals(240, matrix.getHeight());
84 matrix.setRegion(105, 22, 80, 12);
86 // Only bits in the region should be on
87 for (int y = 0; y < 240; y++) {
88 for (int x = 0; x < 320; x++) {
89 assertEquals(y >= 22 && y < 34 && x >= 105 && x < 185, matrix.get(x, y));
94 public void testGetRow() {
95 BitMatrix matrix = new BitMatrix(102, 5);
96 for (int x = 0; x < 102; x++) {
97 if ((x & 0x03) == 0) {
103 BitArray array = matrix.getRow(2, null);
104 assertEquals(102, array.getSize());
107 BitArray array2 = new BitArray(60);
108 array2 = matrix.getRow(2, array2);
109 assertEquals(102, array2.getSize());
111 // Should use provided object, with original BitArray size
112 BitArray array3 = new BitArray(200);
113 array3 = matrix.getRow(2, array3);
114 assertEquals(200, array3.getSize());
116 for (int x = 0; x < 102; x++) {
117 boolean on = ((x & 0x03) == 0);
118 assertEquals(on, array.get(x));
119 assertEquals(on, array2.get(x));
120 assertEquals(on, array3.get(x));