5f140666b8b0fe396c5b6a53539e0d6832d9c73d
[zxing.git] / core / test / src / com / google / zxing / common / BitMatrixTestCase.java
1 /*
2  * Copyright 2007 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.common;
18
19 import junit.framework.TestCase;
20
21 /**
22  * @author Sean Owen
23  * @author dswitkin@google.com (Daniel Switkin)
24  */
25 public final class BitMatrixTestCase extends TestCase {
26
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++) {
32         if (y * x % 3 == 0) {
33           matrix.set(x, y);
34         }
35       }
36     }
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));
40       }
41     }
42   }
43
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));
50       }
51     }
52   }
53
54   public void testRectangularMatrix() {
55     BitMatrix matrix = new BitMatrix(75, 20);
56     assertEquals(75, matrix.getWidth());
57     assertEquals(20, matrix.getHeight());
58     matrix.set(10, 0);
59     matrix.set(11, 1);
60     matrix.set(50, 2);
61     matrix.set(51, 3);
62     matrix.flip(74, 4);
63     matrix.flip(0, 5);
64
65     // Should all be on
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));
72
73     // Flip a couple back off
74     matrix.flip(50, 2);
75     matrix.flip(51, 3);
76     assertFalse(matrix.get(50, 2));
77     assertFalse(matrix.get(51, 3));
78   }
79
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);
85
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));
90       }
91     }
92   }
93
94   public void testGetRow() {
95     BitMatrix matrix = new BitMatrix(102, 5);
96     for (int x = 0; x < 102; x++) {
97       if ((x & 0x03) == 0) {
98         matrix.set(x, 2);
99       }
100     }
101
102     // Should allocate
103     BitArray array = matrix.getRow(2, null);
104     assertEquals(102, array.getSize());
105
106     // Should reallocate
107     BitArray array2 = new BitArray(60);
108     array2 = matrix.getRow(2, array2);
109     assertEquals(102, array2.getSize());
110
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());
115
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));
121     }
122   }
123
124 }