At last update to JUnit 4.x
[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 org.junit.Assert;
20 import org.junit.Test;
21
22 /**
23  * @author Sean Owen
24  * @author dswitkin@google.com (Daniel Switkin)
25  */
26 public final class BitMatrixTestCase extends Assert {
27
28   @Test
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++) {
34         if (y * x % 3 == 0) {
35           matrix.set(x, y);
36         }
37       }
38     }
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));
42       }
43     }
44   }
45
46   @Test
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));
53       }
54     }
55   }
56
57   @Test
58   public void testRectangularMatrix() {
59     BitMatrix matrix = new BitMatrix(75, 20);
60     assertEquals(75, matrix.getWidth());
61     assertEquals(20, matrix.getHeight());
62     matrix.set(10, 0);
63     matrix.set(11, 1);
64     matrix.set(50, 2);
65     matrix.set(51, 3);
66     matrix.flip(74, 4);
67     matrix.flip(0, 5);
68
69     // Should all be on
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));
76
77     // Flip a couple back off
78     matrix.flip(50, 2);
79     matrix.flip(51, 3);
80     assertFalse(matrix.get(50, 2));
81     assertFalse(matrix.get(51, 3));
82   }
83
84   @Test
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);
90
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));
95       }
96     }
97   }
98
99   @Test
100   public void testGetRow() {
101     BitMatrix matrix = new BitMatrix(102, 5);
102     for (int x = 0; x < 102; x++) {
103       if ((x & 0x03) == 0) {
104         matrix.set(x, 2);
105       }
106     }
107
108     // Should allocate
109     BitArray array = matrix.getRow(2, null);
110     assertEquals(102, array.getSize());
111
112     // Should reallocate
113     BitArray array2 = new BitArray(60);
114     array2 = matrix.getRow(2, array2);
115     assertEquals(102, array2.getSize());
116
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());
121
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));
127     }
128   }
129
130 }