summaryrefslogtreecommitdiff
path: root/libs/cglm/test/src/test_common.c
blob: 46b6e63aaa11e252d00cd028a51c460f8e2c9f57 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (c), Recep Aslantas.
 * MIT License (MIT), http://opensource.org/licenses/MIT
 */

#include "test_common.h"
#include <time.h>

void
test_rand_mat4(mat4 dest) {
  glm_mat4_copy(GLM_MAT4_IDENTITY, dest);

  /* random position */
  dest[3][0] = drand48();
  dest[3][1] = drand48();
  dest[3][2] = drand48();

  /* random rotatation around random axis with random angle */
  glm_rotate(dest, drand48(), (vec3){drand48(), drand48(), drand48()});

  /* random scale */
  /* glm_scale(dest, (vec3){drand48(), drand48(), drand48()}); */
}

void
test_rand_mat3(mat3 dest) {
  mat4 m4;

  /* random rotatation around random axis with random angle */
  glm_rotate_make(m4, drand48(), (vec3){drand48(), drand48(), drand48()});
  glm_mat4_pick3(m4, dest);
}

void
test_rand_mat2(mat2 dest) {
  dest[0][0] = drand48();
  dest[0][1] = drand48();
  dest[1][0] = drand48();
  dest[1][1] = drand48();
}

void
test_rand_vec3(vec3 dest) {
  dest[0] = drand48();
  dest[1] = drand48();
  dest[2] = drand48();
}

vec3s
test_rand_vec3s(void) {
  vec3s r;
  test_rand_vec3(r.raw);
  return r;
}

void
test_rand_vec4(vec4 dest) {
  dest[0] = drand48();
  dest[1] = drand48();
  dest[2] = drand48();
  dest[3] = drand48();
}

vec4s
test_rand_vec4s(void) {
  vec4s r;
  test_rand_vec4(r.raw);
  return r;
}

float
test_rand(void) {
  return drand48();
}

void
test_rand_quat(versor q) {
  glm_quat(q, drand48(), drand48(), drand48(), drand48());
  glm_quat_normalize(q);
}

test_status_t
test_assert_mat4_eq(mat4 m1, mat4 m2) {
  int i, j;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      ASSERT(fabsf(m1[i][j] - m2[i][j]) <= 0.0000009)
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat4_eqt(mat4 m1, mat4 m2) {
  int i, j;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      ASSERT(fabsf(m1[j][i] - m2[i][j]) <= 0.0000009)
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat4_eq2(mat4 m1, mat4 m2, float eps) {
  int i, j;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      ASSERT(fabsf(m1[i][j] - m2[i][j]) <= eps);
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat2_eqt(mat2 m1, mat2 m2) {
  int i, j;

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) {
      ASSERT(fabsf(m1[j][i] - m2[i][j]) <= 0.0000009);
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat2_eq(mat2 m1, mat2 m2) {
  int i, j;

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) {
      ASSERT(fabsf(m1[i][j] - m2[i][j]) <= 0.0000009);
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat2_eq_identity(mat2 m2) {
  int i, j;

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) {
      if (i == j) {
        ASSERT(test_eq(m2[i][j], 1.0f))
      } else {
        ASSERT(test_eq(m2[i][j], 0.0f))
      }
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat2_eq_zero(mat2 m2) {
  int i, j;

  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) {
      ASSERT(test_eq(m2[i][j], 0.0f))
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat3_eq(mat3 m1, mat3 m2) {
  int i, j;

  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      ASSERT(fabsf(m1[i][j] - m2[i][j]) <= 0.0000009);
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat3_eqt(mat3 m1, mat3 m2) {
  int i, j;

  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      ASSERT(fabsf(m1[j][i] - m2[i][j]) <= 0.0000009);
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat3_eq_identity(mat3 m3) {
  int i, j;

  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      if (i == j) {
        ASSERT(test_eq(m3[i][j], 1.0f))
      } else {
        ASSERT(test_eq(m3[i][j], 0.0f))
      }
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat3_eq_zero(mat3 m3) {
  int i, j;

  for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
      ASSERT(test_eq(m3[i][j], 0.0f))
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat4_eq_identity(mat4 m4) {
  int i, j;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      if (i == j) {
        ASSERT(test_eq(m4[i][j], 1.0f))
      } else {
        ASSERT(test_eq(m4[i][j], 0.0f))
      }
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_mat4_eq_zero(mat4 m4) {
  int i, j;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      ASSERT(test_eq(m4[i][j], 0.0f))
    }
  }

  TEST_SUCCESS
}

test_status_t
test_assert_eqf(float a, float b) {
  ASSERT(fabsf(a - b) <= 0.000009); /* rounding errors */

  TEST_SUCCESS
}

test_status_t
test_assert_vec2_eq(vec2 v1, vec2 v2) {
  ASSERT(fabsf(v1[0] - v2[0]) <= 0.000009); /* rounding errors */
  ASSERT(fabsf(v1[1] - v2[1]) <= 0.000009);

  TEST_SUCCESS
}

test_status_t
test_assert_vec3_eq(vec3 v1, vec3 v2) {
  ASSERT(fabsf(v1[0] - v2[0]) <= 0.000009); /* rounding errors */
  ASSERT(fabsf(v1[1] - v2[1]) <= 0.000009);
  ASSERT(fabsf(v1[2] - v2[2]) <= 0.000009);

  TEST_SUCCESS
}

test_status_t
test_assert_vec3s_eq(vec3s v1, vec3s v2) {
  test_assert_vec3_eq(v1.raw, v2.raw);

  TEST_SUCCESS
}

test_status_t
test_assert_vec4_eq(vec4 v1, vec4 v2) {
  ASSERT(fabsf(v1[0] - v2[0]) <= 0.000009); /* rounding errors */
  ASSERT(fabsf(v1[1] - v2[1]) <= 0.000009);
  ASSERT(fabsf(v1[2] - v2[2]) <= 0.000009);
  ASSERT(fabsf(v1[3] - v2[3]) <= 0.000009);

  TEST_SUCCESS
}

test_status_t
test_assert_vec4s_eq(vec4s v1, vec4s v2) {
  test_assert_vec4_eq(v1.raw, v2.raw);

  TEST_SUCCESS
}

test_status_t
test_assert_quat_eq_abs(versor v1, versor v2) {
  ASSERT(fabsf(fabsf(v1[0]) - fabsf(v2[0])) <= 0.0009); /* rounding errors */
  ASSERT(fabsf(fabsf(v1[1]) - fabsf(v2[1])) <= 0.0009);
  ASSERT(fabsf(fabsf(v1[2]) - fabsf(v2[2])) <= 0.0009);
  ASSERT(fabsf(fabsf(v1[3]) - fabsf(v2[3])) <= 0.0009);

  TEST_SUCCESS
}

test_status_t
test_assert_quat_eq(versor v1, versor v2) {
  ASSERT(fabsf(v1[0] - v2[0]) <= 0.000009); /* rounding errors */
  ASSERT(fabsf(v1[1] - v2[1]) <= 0.000009);
  ASSERT(fabsf(v1[2] - v2[2]) <= 0.000009);
  ASSERT(fabsf(v1[3] - v2[3]) <= 0.000009);

  TEST_SUCCESS
}

test_status_t
test_assert_quat_eq_identity(versor q) {
  versor p = GLM_QUAT_IDENTITY_INIT;

  ASSERT(fabsf(q[0] - p[0]) <= 0.000009); /* rounding errors */
  ASSERT(fabsf(q[1] - p[1]) <= 0.000009);
  ASSERT(fabsf(q[2] - p[2]) <= 0.000009);
  ASSERT(fabsf(q[3] - p[3]) <= 0.000009);

  TEST_SUCCESS
}