Skip to content

Commit 2fbcba6

Browse files
committed
Initial push of one simple unittest
1 parent 77c44cd commit 2fbcba6

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

unittest/matrix_test.cc

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
///////////////////////////////////////////////////////////////////////
2+
// File: matrix_test.cc
3+
// Author: [email protected] (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.
15+
///////////////////////////////////////////////////////////////////////
16+
17+
#include "matrix.h"
18+
#include "gunit.h"
19+
#include "genericvector.h"
20+
#include "tprintf.h"
21+
22+
namespace {
23+
24+
class MatrixTest : public ::testing::Test {
25+
protected:
26+
// Fills src_ with data so it can pretend to be a tensor thus:
27+
// dims_=[5, 4, 3, 2]
28+
// array_=[0, 1, 2, ....119]
29+
// tensor=[[[[0, 1][2, 3][4, 5]]
30+
// [[6, 7][8, 9][10, 11]]
31+
// [[12, 13][14, 15][16, 17]]
32+
// [[18, 19][20, 21][22, 23]]]
33+
// [[[24, 25]...
34+
MatrixTest() {
35+
src_.Resize(1, kInputSize_, 0);
36+
for (int i = 0; i < kInputSize_; ++i) {
37+
src_.put(0, i, i);
38+
}
39+
for (int i = 0; i < kNumDims_; ++i) dims_[i] = 5 - i;
40+
}
41+
// Number of dimensions in src_.
42+
static const int kNumDims_ = 4;
43+
// Number of elements in src_.
44+
static const int kInputSize_ = 120;
45+
// Size of each dimension in src_;
46+
int dims_[kNumDims_];
47+
// Input array filled with [0,kInputSize).
48+
GENERIC_2D_ARRAY<int> src_;
49+
};
50+
51+
// Tests that the RotatingTranspose function does the right thing for various
52+
// transformations.
53+
// dims=[5, 4, 3, 2]->[5, 2, 4, 3]
54+
TEST_F(MatrixTest, RotatingTranspose_3_1) {
55+
GENERIC_2D_ARRAY<int> m;
56+
src_.RotatingTranspose(dims_, kNumDims_, 3, 1, &m);
57+
m.ResizeNoInit(kInputSize_ / 3, 3);
58+
// Verify that the result is:
59+
// output tensor=[[[[0, 2, 4][6, 8, 10][12, 14, 16][18, 20, 22]]
60+
// [[1, 3, 5][7, 9, 11][13, 15, 17][19, 21, 23]]]
61+
// [[[24, 26, 28]...
62+
EXPECT_EQ(0, m(0, 0));
63+
EXPECT_EQ(2, m(0, 1));
64+
EXPECT_EQ(4, m(0, 2));
65+
EXPECT_EQ(6, m(1, 0));
66+
EXPECT_EQ(1, m(4, 0));
67+
EXPECT_EQ(24, m(8, 0));
68+
EXPECT_EQ(26, m(8, 1));
69+
EXPECT_EQ(25, m(12, 0));
70+
}
71+
72+
// dims=[5, 4, 3, 2]->[3, 5, 4, 2]
73+
TEST_F(MatrixTest, RotatingTranspose_2_0) {
74+
GENERIC_2D_ARRAY<int> m;
75+
src_.RotatingTranspose(dims_, kNumDims_, 2, 0, &m);
76+
m.ResizeNoInit(kInputSize_ / 2, 2);
77+
// Verify that the result is:
78+
// output tensor=[[[[0, 1][6, 7][12, 13][18, 19]]
79+
// [[24, 25][30, 31][36, 37][42, 43]]
80+
// [[48, 49][54, 55][60, 61][66, 67]]
81+
// [[72, 73][78, 79][84, 85][90, 91]]
82+
// [[96, 97][102, 103][108, 109][114, 115]]]
83+
// [[[2,3]...
84+
EXPECT_EQ(0, m(0, 0));
85+
EXPECT_EQ(1, m(0, 1));
86+
EXPECT_EQ(6, m(1, 0));
87+
EXPECT_EQ(7, m(1, 1));
88+
EXPECT_EQ(24, m(4, 0));
89+
EXPECT_EQ(25, m(4, 1));
90+
EXPECT_EQ(30, m(5, 0));
91+
EXPECT_EQ(2, m(20, 0));
92+
}
93+
94+
// dims=[5, 4, 3, 2]->[5, 3, 2, 4]
95+
TEST_F(MatrixTest, RotatingTranspose_1_3) {
96+
GENERIC_2D_ARRAY<int> m;
97+
src_.RotatingTranspose(dims_, kNumDims_, 1, 3, &m);
98+
m.ResizeNoInit(kInputSize_ / 4, 4);
99+
// Verify that the result is:
100+
// output tensor=[[[[0, 6, 12, 18][1, 7, 13, 19]]
101+
// [[2, 8, 14, 20][3, 9, 15, 21]]
102+
// [[4, 10, 16, 22][5, 11, 17, 23]]]
103+
// [[[24, 30, 36, 42]...
104+
EXPECT_EQ(0, m(0, 0));
105+
EXPECT_EQ(6, m(0, 1));
106+
EXPECT_EQ(1, m(1, 0));
107+
EXPECT_EQ(2, m(2, 0));
108+
EXPECT_EQ(3, m(3, 0));
109+
EXPECT_EQ(4, m(4, 0));
110+
EXPECT_EQ(5, m(5, 0));
111+
EXPECT_EQ(24, m(6, 0));
112+
EXPECT_EQ(30, m(6, 1));
113+
}
114+
115+
// dims=[5, 4, 3, 2]->[4, 3, 5, 2]
116+
TEST_F(MatrixTest, RotatingTranspose_0_2) {
117+
GENERIC_2D_ARRAY<int> m;
118+
src_.RotatingTranspose(dims_, kNumDims_, 0, 2, &m);
119+
m.ResizeNoInit(kInputSize_ / 2, 2);
120+
// Verify that the result is:
121+
// output tensor=[[[[0, 1][24, 25][48, 49][72, 73][96, 97]]
122+
// [[2, 3][26, 27][50, 51][74, 75][98, 99]]
123+
// [[4, 5][28, 29][52, 53][76, 77][100, 101]]]
124+
// [[[6, 7]...
125+
EXPECT_EQ(0, m(0, 0));
126+
EXPECT_EQ(1, m(0, 1));
127+
EXPECT_EQ(24, m(1, 0));
128+
EXPECT_EQ(25, m(1, 1));
129+
EXPECT_EQ(96, m(4, 0));
130+
EXPECT_EQ(97, m(4, 1));
131+
EXPECT_EQ(2, m(5, 0));
132+
EXPECT_EQ(6, m(15, 0));
133+
}
134+
135+
} // namespace

0 commit comments

Comments
 (0)