]> git.xonotic.org Git - xonotic/gmqcc.git/blob - tests/xor.qc
Does this fix it?
[xonotic/gmqcc.git] / tests / xor.qc
1 vector swap(float x, float y) {
2     vector ret = '0 0 0';
3     // everyone knows this trick
4     ret.x = x;
5     ret.y = y;
6
7     ret.x = ret.x ^ ret.y;
8     ret.y = ret.y ^ ret.x;
9     ret.x = ret.x ^ ret.y;
10
11     return ret;
12 }
13
14 float f(vector b) {
15     return b.x+b.y+b.z;
16 }
17
18 void main() {
19     float x = 5;
20     float y = 3;
21     float z = x ^ y; // 6
22
23     float a = 2;
24     float b = 10;
25     float c = a ^ b; // 8
26
27     print(ftos(z), "\n");
28     print(ftos(c), "\n");
29
30     // commutative?
31     if (x ^ y == y ^ x)
32         print("commutative\n");
33
34     // assocative?
35     if (x ^ (y ^ z) == (x ^ y) ^ z)
36         print("assocative\n");
37
38     // elements are their own inverse?
39     if (x ^ 0 == x)
40         print("inverse\n");
41
42     // vector ^ vector
43     // vector ^ float
44     // are legal in constant expressions (currently)
45     vector v1 = '5 2 5';
46     vector v2 = '3 10 3';
47
48     print("vv: ", vtos(v1 ^ v2), "\n");
49     print("vf: ", vtos(v1 ^ 10), "\n");
50
51     const vector v3 = '5 2 5' ^ '3 10 3';
52     const vector v4 = '5 2 5' ^ 10;
53
54     print("vv: ", vtos(v3), "\n");
55     print("vf: ", vtos(v4), "\n");
56
57     // good olde xor swap test too
58     float swap_x = 100;
59     float swap_y = 200;
60     vector swaps = swap(swap_x, swap_y);
61     print("100:200 swapped is: ", ftos(swaps.x), ":", ftos(swaps.y), "\n");
62
63     // good olde xor swap test too
64     vector swap_u = '1 2 3';
65     vector swap_v = '4 5 6';
66     swap_u ^= swap_v;
67     swap_v ^= swap_u;
68     swap_u ^= swap_v;
69     print("'1 2 3':'4 5 6' swapped is: ", vtos(swap_u), ":", vtos(swap_v), "\n");
70
71     // the one that showed us overlap bugs
72     print(vtos('1 2 3' ^ f('3 2 1') ^ f('1 1 1')), "\n");
73     print(vtos('1 2 3' ^ f('3 2 1') ^ 3),          "\n");
74     print(vtos('1 2 3' ^ 6          ^ 3),          "\n");
75     print(vtos('1 2 3' ^ 6          ^ f('1 1 1')), "\n");
76     print(vtos('1 2 3' ^ 5),                       "\n");
77 }