1 module sbylib.math.test;
2 
3 import sbylib.math;
4 
5 unittest {
6     vec2 a = vec2(1,2);
7 
8     // simple comparison
9     assert(a == vec2(1,2));
10 
11     // vector calculation
12     assert(a + vec2(2,3) == vec2(3,5));
13     assert(a - vec2(9,8) == vec2(-8, -6));
14 
15     // scalar calculation
16     assert(a + 1 == vec2(2,3));
17     assert(a - 2 == vec2(-1,0));
18     assert(a * 3 == vec2(3,6));
19     assert(a / 4 ==  vec2(0.25, 0.5));
20 
21     // assignment
22     a = vec2(4,-2);
23     assert(a == vec2(4,-2));
24     const(vec2) b = vec2(2,2);
25     a = b;
26     assert(a == vec2(2));
27 
28     // short expression
29     a = vec2(3);
30     assert(a == vec2(3,3));
31 
32     // operator assign
33     a += vec2(2,1);
34     assert(a == vec2(5,4));
35     a -= vec2(1);
36     assert(a == vec2(4,3));
37     a *= 2;
38     assert(a == vec2(8,6));
39     a /= 2;
40     assert(a == vec2(4,3));
41 }
42 
43 unittest {
44     vec2 a = vec2(1,2);
45     
46     // read partial vector
47     assert(a.xy == vec2(1,2));
48     assert(a.yx == vec2(2,1));
49     assert(a.xyyx == vec4(1,2,2,1));
50 
51     vec3 c;
52 
53     // write partial vector
54     c.yxz = vec3(3,2,1);
55     assert(c == vec3(2,3,1));
56     c.xy = a.yx;
57     assert(c == vec3(2,1,1));
58     c.zx = 3;
59     assert(c == vec3(3,1,3));
60     c.xy = c.yx;
61     assert(c == vec3(1,3,3));
62     //c.xy += vec2(1); // cannot modify partial vector
63     c.y--; // single member can be modified
64     assert(c == vec3(1,2,3));
65 }
66 
67 unittest {
68     static assert(__traits(hasMember, vec3, "undefinedMember") == false);
69     static assert(__traits(hasMember, vec3, "x") == true);
70     static assert(__traits(hasMember, vec3, "yxz") == true);
71 }
72 
73 unittest {
74     auto m1 = mat3(
75             1,4,7,
76             2,5,8,
77             3,6,9);
78     auto m2 = mat3(vec3(1,2,3),vec3(4,5,6),vec3(7,8,9));
79 
80     assert(m1 == m2);
81 
82     auto m3 = mat3(
83             1,0,1,
84             0,1,1,
85             0,0,1);
86     auto m4 = mat3(
87             1,2,3,
88             4,5,6,
89             1,1,1);
90 
91     auto m5 = mat3(
92             2,2,4,
93             4,6,7,
94             1,1,2);
95     auto m6 = mat3(
96             2,3,4,
97             5,6,7,
98             1,1,1);
99 
100     assert(m3 + m4 == m5);
101     assert(m3 * m4 == m6);
102 
103     auto p = vec4(0,1,2,1);
104     auto m = mat4(
105             1,0,0,100,
106             0,1,0,200,
107             0,0,1,300,
108             0,0,0,1);
109     assert(m * p == vec4(100,201,302,1));
110     auto t = mat4.translate(vec3(100,200,300));
111     assert(m == t);
112 }
113 
114 unittest {
115     struct S {
116         auto get() { return mat2(0); }
117         alias get this;
118     }
119 
120     S s;
121     vec2 a = s * vec2(0);
122 
123 }
124 
125 unittest {
126     vec4 v;
127     assert(!is(typeof({ v = mat4(0); })));
128 }