lin
2025-08-21 57113df3a0e2be01232281fad9a5f2c060567981
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
 
#include <gtest/gtest.h>
#include <math.h>
 
#include "Matrix.h"
 
class MatrixTest: public testing::Test {
public:
 
};
 
void checkValues(const float* arr1, const float* arr2, const int size) {
    for (int i = 0; i < size; i++) {
        ASSERT_FLOAT_EQ(arr1[i], arr2[i]);
    }
}
 
TEST(MatrixTest, matrixEqualityTest) {
    // Create two identity matrixes.
    Matrix m1;
    Matrix m2;
    // Change some random values.
    m1.mData[4] = 9;
    m2.mData[4] = 9;
    // Check they are the same.
    ASSERT_TRUE(m1.equals(m2));
    Matrix* clone = new Matrix(m1);
    ASSERT_TRUE(clone != NULL);
    ASSERT_TRUE(m1.equals(*clone));
    delete clone;
}
 
TEST(MatrixTest, matrixIdentityTest) {
    // Create an identity matrix.
    Matrix m;
    float expected[] = {
        1.0f, 0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 0.0f, 1.0f};
    // Check values
    checkValues(m.mData, expected, Matrix::MATRIX_SIZE);
}
 
TEST(MatrixTest, matrixLoadWithTest) {
    // Create a matrix.
    Matrix m1;
    float* d1 = m1.mData;
    float data[Matrix::MATRIX_SIZE];
 
    // Fill with rubbish
    for (int i = 0; i < Matrix::MATRIX_SIZE; i++) {
        d1[i] = i;
        data[i] = i;
    }
 
    // Create another matrix
    Matrix m2;
 
    // Load second matrix with first
    m2.loadWith(m1);
 
    // Check values
    checkValues(m2.mData, data, Matrix::MATRIX_SIZE);
}
 
TEST(MatrixTest, matrixTranslateTest) {
    Matrix m1;
    m1.translate(10, 5, 6);
    Matrix* m2 = Matrix::newTranslate(10, 5, 6);
    ASSERT_TRUE(m2 != NULL);
    ASSERT_TRUE(m1.equals(*m2));
    delete m2;
}
 
TEST(MatrixTest, matrixScaleTest) {
    Matrix m1;
    m1.scale(10, 5, 6);
    Matrix* m2 = Matrix::newScale(10, 5, 6);
    ASSERT_TRUE(m2 != NULL);
    ASSERT_TRUE(m1.equals(*m2));
    delete m2;
}
 
TEST(MatrixTest, matrixRotateTest) {
    Matrix m1;
    m1.rotate(180, 1, 0, 1);
    Matrix* m2 = Matrix::newRotate(180, 1, 0, 1);
    ASSERT_TRUE(m2 != NULL);
    ASSERT_TRUE(m1.equals(*m2));
    delete m2;
}
 
TEST(MatrixTest, matrixMultiplyTest) {
    // Create three identity matrixes.
    Matrix m1;
    Matrix m2;
    Matrix m3;
    float* d1 = m1.mData;
    float* d2 = m2.mData;
 
    m3.multiply(m1, m2);
    // Multiplication of identity matrixes should give identity
    ASSERT_TRUE(m3.equals(m1));
 
    // Fill with ascending numbers
    for (int i = 0; i < Matrix::MATRIX_SIZE; i++) {
        d1[i] = i;
        d2[i] = i;
    }
    m3.multiply(m1, m2);
 
    // Check against expected
    float expected[] = {
        56, 62, 68, 74,
        152, 174, 196, 218,
        248, 286, 324, 362,
        344, 398, 452, 506};
    checkValues(m3.mData, expected, Matrix::MATRIX_SIZE);
}
 
TEST(MatrixTest, matrixNewLookAtTest) {
    // Position the eye in front of the origin.
    float eyeX = 0.0f;
    float eyeY = 0.0f;
    float eyeZ = 6.0f;
 
    // We are looking at the origin
    float centerX = 0.0f;
    float centerY = 0.0f;
    float centerZ = 0.0f;
 
    // Set our up vector. This is where our head would be pointing were we holding the camera.
    float upX = 0.0f;
    float upY = 1.0f;
    float upZ = 0.0f;
 
    // Set the view matrix. This matrix can be said to represent the camera position.
    Matrix* m = Matrix::newLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ,
            upX, upY, upZ);
    ASSERT_TRUE(m != NULL);
    float expected[] = {
        1.0f, 0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, -6.0f, 1.0f};
    // Check values
    checkValues(m->mData, expected, Matrix::MATRIX_SIZE);
    delete m;
}
 
TEST(MatrixTest, matrixNewFrustumTest) {
    float ratio = (float) 800 / 600;
    float left = -ratio;
    float right = ratio;
    float bottom = -1.0f;
    float top = 1.0f;
    float near = 1.0f;
    float far = 8.0f;
 
    Matrix* m = Matrix::newFrustum(left, right, bottom, top, near, far);
    ASSERT_TRUE(m != NULL);
    float expected[] = {
        0.75f, 0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 9.0f / -7.0f, -1.0f,
        0.0f, 0.0f, 16.0f / -7.0f, 0.0f};
    // Check values
    checkValues(m->mData, expected, Matrix::MATRIX_SIZE);
    delete m;
}
 
TEST(MatrixTest, matrixNewTranslateTest) {
    Matrix* m = Matrix::newTranslate(5, 6, 8);
    ASSERT_TRUE(m != NULL);
    float expected[] = {
        1.0f, 0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 1.0f, 0.0f,
        5.0f, 6.0f, 8.0f, 1.0f};
    // Check values
    checkValues(m->mData, expected, Matrix::MATRIX_SIZE);
    delete m;
}
 
TEST(MatrixTest, matrixNewScaleTest) {
    Matrix* m = Matrix::newScale(3, 7, 2);
    ASSERT_TRUE(m != NULL);
    float expected[] = {
        3.0f, 0.0f, 0.0f, 0.0f,
        0.0f, 7.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 2.0f, 0.0f,
        0.0f, 0.0f, 0.0f, 1.0f};
    // Check values
    checkValues(m->mData, expected, Matrix::MATRIX_SIZE);
    delete m;
}
 
TEST(MatrixTest, matrixNewRotateTest) {
    Matrix* m = Matrix::newRotate(45.0f, 0.0f, 1.0f, 0.0f);
    ASSERT_TRUE(m != NULL);
    float radians = 45.0f * (M_PI / 180.0f);
    float sin = sinf(radians);
    float cos = cosf(radians);
    float expected[] = {
        cos, 0.0f, -sin, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f,
        sin, 0.0f, cos, 0.0f,
        0.0f, 0.0f, 0.0f, 1.0f};
    // Check values
    checkValues(m->mData, expected, Matrix::MATRIX_SIZE);
    delete m;
}
 
TEST(MatrixTest, matrixMultiplyVectorTest) {
    float in[] = {2, 4, 6, 8};
    float out[4];
    Matrix m;
    float* d = m.mData;
    // Fill with rubbish
    for (int i = 0; i < Matrix::MATRIX_SIZE; i++) {
        d[i] = i;
    }
    float expected[] = {40, 120, 200, 280};
    Matrix::multiplyVector(out, m, in);
    checkValues(out, expected, 4);
}