1 /*
2  * Copyright 2013 The Android Open Source Project
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 #define LOG_TAG "RegionTest"
18 
19 #include <stdlib.h>
20 #include <ui/Region.h>
21 #include <ui/Rect.h>
22 #include <gtest/gtest.h>
23 
24 #include <ui/mat4.h>
25 
26 namespace android {
27 
28 class MatTest : public testing::Test {
29 protected:
30 };
31 
TEST_F(MatTest,Basics)32 TEST_F(MatTest, Basics) {
33     mat4 m0;
34     EXPECT_EQ(sizeof(mat4), sizeof(float)*16);
35 }
36 
TEST_F(MatTest,ComparisonOps)37 TEST_F(MatTest, ComparisonOps) {
38     mat4 m0;
39     mat4 m1(2);
40 
41     EXPECT_TRUE(m0 == m0);
42     EXPECT_TRUE(m0 != m1);
43     EXPECT_FALSE(m0 != m0);
44     EXPECT_FALSE(m0 == m1);
45 }
46 
TEST_F(MatTest,Constructors)47 TEST_F(MatTest, Constructors) {
48     mat4 m0;
49     ASSERT_EQ(m0[0].x, 1);
50     ASSERT_EQ(m0[0].y, 0);
51     ASSERT_EQ(m0[0].z, 0);
52     ASSERT_EQ(m0[0].w, 0);
53     ASSERT_EQ(m0[1].x, 0);
54     ASSERT_EQ(m0[1].y, 1);
55     ASSERT_EQ(m0[1].z, 0);
56     ASSERT_EQ(m0[1].w, 0);
57     ASSERT_EQ(m0[2].x, 0);
58     ASSERT_EQ(m0[2].y, 0);
59     ASSERT_EQ(m0[2].z, 1);
60     ASSERT_EQ(m0[2].w, 0);
61     ASSERT_EQ(m0[3].x, 0);
62     ASSERT_EQ(m0[3].y, 0);
63     ASSERT_EQ(m0[3].z, 0);
64     ASSERT_EQ(m0[3].w, 1);
65 
66     mat4 m1(2);
67     mat4 m2(vec4(2));
68     mat4 m3(m2);
69 
70     EXPECT_EQ(m1, m2);
71     EXPECT_EQ(m2, m3);
72     EXPECT_EQ(m3, m1);
73 
74     mat4 m4(vec4(1), vec4(2), vec4(3), vec4(4));
75 }
76 
TEST_F(MatTest,ArithmeticOps)77 TEST_F(MatTest, ArithmeticOps) {
78     mat4 m0;
79     mat4 m1(2);
80     mat4 m2(vec4(2));
81 
82     m1 += m2;
83     EXPECT_EQ(mat4(4), m1);
84 
85     m2 -= m1;
86     EXPECT_EQ(mat4(-2), m2);
87 
88     m1 *= 2;
89     EXPECT_EQ(mat4(8), m1);
90 
91     m1 /= 2;
92     EXPECT_EQ(mat4(4), m1);
93 
94     m0 = -m0;
95     EXPECT_EQ(mat4(-1), m0);
96 }
97 
TEST_F(MatTest,UnaryOps)98 TEST_F(MatTest, UnaryOps) {
99     const mat4 identity;
100     mat4 m0;
101 
102     ++m0;
103     EXPECT_EQ(mat4( vec4(2,1,1,1), vec4(1,2,1,1), vec4(1,1,2,1), vec4(1,1,1,2) ), m0);
104     EXPECT_EQ(mat4( -vec4(2,1,1,1), -vec4(1,2,1,1), -vec4(1,1,2,1), -vec4(1,1,1,2) ), -m0);
105 
106     --m0;
107     EXPECT_EQ(identity, m0);
108 }
109 
TEST_F(MatTest,MiscOps)110 TEST_F(MatTest, MiscOps) {
111     const mat4 identity;
112     mat4 m0;
113     EXPECT_EQ(4, trace(m0));
114 
115     mat4 m1(vec4(1,2,3,4), vec4(5,6,7,8), vec4(9,10,11,12), vec4(13,14,15,16));
116     mat4 m2(vec4(1,5,9,13), vec4(2,6,10,14), vec4(3,7,11,15), vec4(4,8,12,16));
117     EXPECT_EQ(m1, transpose(m2));
118     EXPECT_EQ(m2, transpose(m1));
119     EXPECT_EQ(vec4(1,6,11,16), diag(m1));
120 
121     EXPECT_EQ(identity, inverse(identity));
122 
123     mat4 m3(vec4(4,3,0,0), vec4(3,2,0,0), vec4(0,0,1,0), vec4(0,0,0,1));
124     mat4 m3i(inverse(m3));
125     EXPECT_FLOAT_EQ(-2, m3i[0][0]);
126     EXPECT_FLOAT_EQ( 3, m3i[0][1]);
127     EXPECT_FLOAT_EQ( 3, m3i[1][0]);
128     EXPECT_FLOAT_EQ(-4, m3i[1][1]);
129 
130     mat4 m3ii(inverse(m3i));
131     EXPECT_FLOAT_EQ(m3[0][0], m3ii[0][0]);
132     EXPECT_FLOAT_EQ(m3[0][1], m3ii[0][1]);
133     EXPECT_FLOAT_EQ(m3[1][0], m3ii[1][0]);
134     EXPECT_FLOAT_EQ(m3[1][1], m3ii[1][1]);
135 
136     EXPECT_EQ(m1, m1*identity);
137 }
138 
139 }; // namespace android
140