tesseract  5.0.0-alpha-619-ge9db
matrix_test.cc
Go to the documentation of this file.
1 // File: matrix_test.cc
3 // Author: rays@google.com (Ray Smith)
4 //
5 // Copyright 2016 Google Inc. All Rights Reserved.
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // 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.
16 
17 #include "matrix.h"
19 #include "include_gunit.h"
20 
21 namespace {
22 
23 class MatrixTest : public ::testing::Test {
24  protected:
25  void SetUp() override {
26  std::locale::global(std::locale(""));
27  }
28 
29  // Fills src_ with data so it can pretend to be a tensor thus:
30  // dims_=[5, 4, 3, 2]
31  // array_=[0, 1, 2, ....119]
32  // tensor=[[[[0, 1][2, 3][4, 5]]
33  // [[6, 7][8, 9][10, 11]]
34  // [[12, 13][14, 15][16, 17]]
35  // [[18, 19][20, 21][22, 23]]]
36  // [[[24, 25]...
37  MatrixTest() {
38  src_.Resize(1, kInputSize_, 0);
39  for (int i = 0; i < kInputSize_; ++i) {
40  src_.put(0, i, i);
41  }
42  for (int i = 0; i < kNumDims_; ++i) dims_[i] = 5 - i;
43  }
44  // Number of dimensions in src_.
45  static const int kNumDims_ = 4;
46  // Number of elements in src_.
47  static const int kInputSize_ = 120;
48  // Size of each dimension in src_;
49  int dims_[kNumDims_];
50  // Input array filled with [0,kInputSize).
52 };
53 
54 // Tests that the RotatingTranspose function does the right thing for various
55 // transformations.
56 // dims=[5, 4, 3, 2]->[5, 2, 4, 3]
57 TEST_F(MatrixTest, RotatingTranspose_3_1) {
59  src_.RotatingTranspose(dims_, kNumDims_, 3, 1, &m);
60  m.ResizeNoInit(kInputSize_ / 3, 3);
61  // Verify that the result is:
62  // output tensor=[[[[0, 2, 4][6, 8, 10][12, 14, 16][18, 20, 22]]
63  // [[1, 3, 5][7, 9, 11][13, 15, 17][19, 21, 23]]]
64  // [[[24, 26, 28]...
65  EXPECT_EQ(0, m(0, 0));
66  EXPECT_EQ(2, m(0, 1));
67  EXPECT_EQ(4, m(0, 2));
68  EXPECT_EQ(6, m(1, 0));
69  EXPECT_EQ(1, m(4, 0));
70  EXPECT_EQ(24, m(8, 0));
71  EXPECT_EQ(26, m(8, 1));
72  EXPECT_EQ(25, m(12, 0));
73 }
74 
75 // dims=[5, 4, 3, 2]->[3, 5, 4, 2]
76 TEST_F(MatrixTest, RotatingTranspose_2_0) {
78  src_.RotatingTranspose(dims_, kNumDims_, 2, 0, &m);
79  m.ResizeNoInit(kInputSize_ / 2, 2);
80  // Verify that the result is:
81  // output tensor=[[[[0, 1][6, 7][12, 13][18, 19]]
82  // [[24, 25][30, 31][36, 37][42, 43]]
83  // [[48, 49][54, 55][60, 61][66, 67]]
84  // [[72, 73][78, 79][84, 85][90, 91]]
85  // [[96, 97][102, 103][108, 109][114, 115]]]
86  // [[[2,3]...
87  EXPECT_EQ(0, m(0, 0));
88  EXPECT_EQ(1, m(0, 1));
89  EXPECT_EQ(6, m(1, 0));
90  EXPECT_EQ(7, m(1, 1));
91  EXPECT_EQ(24, m(4, 0));
92  EXPECT_EQ(25, m(4, 1));
93  EXPECT_EQ(30, m(5, 0));
94  EXPECT_EQ(2, m(20, 0));
95 }
96 
97 // dims=[5, 4, 3, 2]->[5, 3, 2, 4]
98 TEST_F(MatrixTest, RotatingTranspose_1_3) {
100  src_.RotatingTranspose(dims_, kNumDims_, 1, 3, &m);
101  m.ResizeNoInit(kInputSize_ / 4, 4);
102  // Verify that the result is:
103  // output tensor=[[[[0, 6, 12, 18][1, 7, 13, 19]]
104  // [[2, 8, 14, 20][3, 9, 15, 21]]
105  // [[4, 10, 16, 22][5, 11, 17, 23]]]
106  // [[[24, 30, 36, 42]...
107  EXPECT_EQ(0, m(0, 0));
108  EXPECT_EQ(6, m(0, 1));
109  EXPECT_EQ(1, m(1, 0));
110  EXPECT_EQ(2, m(2, 0));
111  EXPECT_EQ(3, m(3, 0));
112  EXPECT_EQ(4, m(4, 0));
113  EXPECT_EQ(5, m(5, 0));
114  EXPECT_EQ(24, m(6, 0));
115  EXPECT_EQ(30, m(6, 1));
116 }
117 
118 // dims=[5, 4, 3, 2]->[4, 3, 5, 2]
119 TEST_F(MatrixTest, RotatingTranspose_0_2) {
121  src_.RotatingTranspose(dims_, kNumDims_, 0, 2, &m);
122  m.ResizeNoInit(kInputSize_ / 2, 2);
123  // Verify that the result is:
124  // output tensor=[[[[0, 1][24, 25][48, 49][72, 73][96, 97]]
125  // [[2, 3][26, 27][50, 51][74, 75][98, 99]]
126  // [[4, 5][28, 29][52, 53][76, 77][100, 101]]]
127  // [[[6, 7]...
128  EXPECT_EQ(0, m(0, 0));
129  EXPECT_EQ(1, m(0, 1));
130  EXPECT_EQ(24, m(1, 0));
131  EXPECT_EQ(25, m(1, 1));
132  EXPECT_EQ(96, m(4, 0));
133  EXPECT_EQ(97, m(4, 1));
134  EXPECT_EQ(2, m(5, 0));
135  EXPECT_EQ(6, m(15, 0));
136 }
137 
138 } // namespace
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
GENERIC_2D_ARRAY< int >
genericvector.h
GENERIC_2D_ARRAY::ResizeNoInit
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:90
matrix.h
GENERIC_2D_ARRAY::RotatingTranspose
void RotatingTranspose(const int *dims, int num_dims, int src_dim, int dest_dim, GENERIC_2D_ARRAY< T > *result) const
Definition: matrix.h:417