aboutsummaryrefslogtreecommitdiff
path: root/mons_math/tests/vec3_ops.c
blob: e29b39680d7a2e1edb253f2191af62693481e5fa (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
#include "mons_math/vec2.h"
#include "mons_math/vec3.h"
#include "mons_math/vec4.h"
#include "mons_math/util.h"
#include "test.h"

int main(void) {
    mons_vec3 a = {2.0, 3.0, 4.0};
    mons_vec3 b = {6.0, 5.0, 4.0};

    // Add
    mons_vec3 sum = mons_vec3_add(a, b);
    ASSERT(mons_vec3_equal(sum, (mons_vec3){8.0, 8.0, 8.0}));
    
    // Add in Place
    mons_vec3_add_inplace(&a, b);
    ASSERT(mons_vec3_equal(a, sum));

    // Subtract
    mons_vec3 diff = mons_vec3_sub(sum, b);
    ASSERT(mons_vec3_equal(diff, (mons_vec3){2.0, 3.0, 4.0}));

    // Subtract in Place
    mons_vec3_sub_inplace(&a, b);
    ASSERT(mons_vec3_equal(diff, a));

    // Multiply (float)
    mons_vec3 product_f = mons_vec3_mul_f(a, -3.0);
    ASSERT(mons_vec3_equal(product_f, (mons_vec3){-6.0, -9.0, -12.0}))
    
    // Multiply in Place (float)
    mons_vec3_mul_f_inplace(&a, 3.0);
    ASSERT(mons_vec3_equal(mons_vec3_negate(product_f), a))

    // Divide (float)
    mons_vec3 quotient_f = mons_vec3_div_f(product_f, 3.0);
    mons_vec3_negate_inplace(&quotient_f);
    ASSERT(mons_vec3_equal(quotient_f, (mons_vec3){2.0, 3.0, 4.0}));

    // Divide in Place (float)
    mons_vec3_div_f_inplace(&a, 3.0);
    ASSERT(mons_vec3_equal(quotient_f, a));

    // Multiply (int)
    mons_vec3 product_i = mons_vec3_mul_i(a, -3);
    ASSERT(mons_vec3_equal(product_i, (mons_vec3){-6.0, -9.0, -12.0}))
    
    // Multiply in Place (int)
    mons_vec3_mul_i_inplace(&a, 3);
    ASSERT(mons_vec3_equal(mons_vec3_negate(product_i), a))

    // Divide (int)
    mons_vec3 quotient_i = mons_vec3_div_i(product_i, 3);
    mons_vec3_negate_inplace(&quotient_i);
    ASSERT(mons_vec3_equal(quotient_i, (mons_vec3){2.0, 3.0, 4.0}));

    // Divide in Place (int)
    mons_vec3_div_i_inplace(&a, 3);
    ASSERT(mons_vec3_equal(quotient_i, a));

    // Get Length
    float a_len = mons_vec3_len(a);
    ASSERT(mons_float_approx_equal(a_len, 5.38516));

    // Dot Product
    float dot = mons_vec3_dot(a, b);
    ASSERT(mons_float_approx_equal(dot, 43.0));

    // Cross Product
    mons_vec3 cross = mons_vec3_cross(a, b);
    ASSERT(mons_vec3_equal(cross, (mons_vec3){-8.0, 16.0, -8.0}));

    // Extend
    mons_vec4 extended = mons_vec3_extend(a);
    ASSERT(mons_vec4_equal(extended, (mons_vec4){a.x, a.y, a.z, 0.0}));

    // Truncate
    mons_vec2 truncated = mons_vec3_truncate(a);
    ASSERT(mons_vec2_equal(truncated, (mons_vec2){a.x, a.y}));

    // Normalize
    mons_vec3 normalized = mons_vec3_normalize(a);
    ASSERT(mons_float_approx_equal(mons_vec3_len(normalized), 1.0));

    // Normalize in Place
    mons_vec3_normalize_inplace(&a);
    ASSERT(mons_float_approx_equal(mons_vec3_len(a), 1.0));

    return EXIT_SUCCESS;
}