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 org.junit.Assert;
20 import org.junit.Test;
24 * @author dswitkin@google.com (Daniel Switkin)
26 public final class BitMatrixTestCase extends Assert {
29 public void testGetSet() {
30 BitMatrix matrix = new BitMatrix(33);
31 assertEquals(33, matrix.getHeight());
32 for (int y = 0; y < 33; y++) {
33 for (int x = 0; x < 33; x++) {
39 for (int y = 0; y < 33; y++) {
40 for (int x = 0; x < 33; x++) {
41 assertEquals(y * x % 3 == 0, matrix.get(x, y));
47 public void testSetRegion() {
48 BitMatrix matrix = new BitMatrix(5);
49 matrix.setRegion(1, 1, 3, 3);
50 for (int y = 0; y < 5; y++) {
51 for (int x = 0; x < 5; x++) {
52 assertEquals(y >= 1 && y <= 3 && x >= 1 && x <= 3, matrix.get(x, y));
58 public void testRectangularMatrix() {
59 BitMatrix matrix = new BitMatrix(75, 20);
60 assertEquals(75, matrix.getWidth());
61 assertEquals(20, matrix.getHeight());
70 assertTrue(matrix.get(10, 0));
71 assertTrue(matrix.get(11, 1));
72 assertTrue(matrix.get(50, 2));
73 assertTrue(matrix.get(51, 3));
74 assertTrue(matrix.get(74, 4));
75 assertTrue(matrix.get(0, 5));
77 // Flip a couple back off
80 assertFalse(matrix.get(50, 2));
81 assertFalse(matrix.get(51, 3));
85 public void testRectangularSetRegion() {
86 BitMatrix matrix = new BitMatrix(320, 240);
87 assertEquals(320, matrix.getWidth());
88 assertEquals(240, matrix.getHeight());
89 matrix.setRegion(105, 22, 80, 12);
91 // Only bits in the region should be on
92 for (int y = 0; y < 240; y++) {
93 for (int x = 0; x < 320; x++) {
94 assertEquals(y >= 22 && y < 34 && x >= 105 && x < 185, matrix.get(x, y));
100 public void testGetRow() {
101 BitMatrix matrix = new BitMatrix(102, 5);
102 for (int x = 0; x < 102; x++) {
103 if ((x & 0x03) == 0) {
109 BitArray array = matrix.getRow(2, null);
110 assertEquals(102, array.getSize());
113 BitArray array2 = new BitArray(60);
114 array2 = matrix.getRow(2, array2);
115 assertEquals(102, array2.getSize());
117 // Should use provided object, with original BitArray size
118 BitArray array3 = new BitArray(200);
119 array3 = matrix.getRow(2, array3);
120 assertEquals(200, array3.getSize());
122 for (int x = 0; x < 102; x++) {
123 boolean on = ((x & 0x03) == 0);
124 assertEquals(on, array.get(x));
125 assertEquals(on, array2.get(x));
126 assertEquals(on, array3.get(x));