GreyscaleRotatedLuminanceSource: implemented getMatrix()
[zxing.git] / cpp / core / tests / src / common / BitMatrixTest.cpp
1 /*
2  *  BitMatrixTest.cpp
3  *  zxing
4  *
5  *  Created by Christian Brunschen on 12/05/2008.
6  *  Copyright 2008 Google UK. All rights reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include "BitMatrixTest.h"
22 #include <limits>
23 #include <stdlib.h>
24
25 namespace zxing {
26 using namespace std;
27
28 CPPUNIT_TEST_SUITE_REGISTRATION(BitMatrixTest);
29
30 BitMatrixTest::BitMatrixTest() {
31   srand(getpid());
32 }
33
34 void BitMatrixTest::testGetSet() {
35   size_t bits = numeric_limits<unsigned int>::digits;
36   BitMatrix matrix(bits + 1);
37   CPPUNIT_ASSERT_EQUAL(bits + 1, matrix.getDimension());
38   for (size_t i = 0; i < bits + 1; i++) {
39     for (size_t j = 0; j < bits + 1; j++) {
40       if (i * j % 3 == 0) {
41         matrix.set(i, j);
42       }
43     }
44   }
45   for (size_t i = 0; i < bits + 1; i++) {
46     for (size_t j = 0; j < bits + 1; j++) {
47       CPPUNIT_ASSERT_EQUAL(i * j % 3 == 0, matrix.get(i, j));
48     }
49   }
50 }
51
52 void BitMatrixTest::testSetRegion() {
53   BitMatrix matrix(5);
54   matrix.setRegion(1, 1, 3, 3);
55   for (int i = 0; i < 5; i++) {
56     for (int j = 0; j < 5; j++) {
57       CPPUNIT_ASSERT_EQUAL(i >= 1 && i <= 3 && j >= 1 && j <= 3,
58                            matrix.get(i, j));
59     }
60   }
61 }
62
63 void BitMatrixTest::testGetBits() {
64   BitMatrix matrix(6);
65   matrix.set(0, 0);
66   matrix.set(5, 5);
67   unsigned int* bits = matrix.getBits();
68   CPPUNIT_ASSERT_EQUAL(1u, bits[0]);
69   CPPUNIT_ASSERT_EQUAL(8u, bits[1]);
70 }
71
72 void BitMatrixTest::testGetRow1() {
73   const int width = 98;
74   const int height = 76;
75   runBitMatrixGetRowTest(width, height);
76 }
77
78 void BitMatrixTest::testGetRow2() {
79   const int width = 320;
80   const int height = 320;
81   runBitMatrixGetRowTest(width, height);
82 }
83
84 void BitMatrixTest::testGetRow3() {
85   const int width = 17;
86   const int height = 23;
87   runBitMatrixGetRowTest(width, height);
88 }
89
90 void BitMatrixTest::runBitMatrixGetRowTest(int width, int height) {
91   BitMatrix mat(width, height);
92   for (int y = 0; y < height; y++) {
93     for (int x = 0; x < width; x++) {
94       if ((rand() & 0x01) != 0) {
95         mat.set(x, y);
96       }
97     }
98   }
99   Ref<BitArray> row(new BitArray(width));
100   for (int y = 0; y < height; y++) {
101     row = mat.getRow(y, row);
102     for (int x = 0; x < width; x++) {
103       CPPUNIT_ASSERT_EQUAL(row->get(x), mat.get(x,y));
104     }
105   }
106 }
107 }