]> git.xonotic.org Git - xonotic/gmqcc.git/blob - tests/xor.qc
Add the good old xor swap trick to the xor test, yes it works :P
[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 void main() {
15     float x = 5;
16     float y = 3;
17     float z = x ^ y; // 6
18     
19     float a = 2;
20     float b = 10;
21     float c = a ^ b; // 8
22     
23     print(ftos(z), "\n");
24     print(ftos(c), "\n");
25     
26     // commutative?
27     if (x ^ y == y ^ x)
28         print("commutative\n");
29     
30     // assocative?
31     if (x ^ (y ^ z) == (x ^ y) ^ z)
32         print("assocative\n");
33         
34     // elements are their own inverse?
35     if (x ^ 0 == x)
36         print("inverse\n");
37         
38     // vector ^ vector
39     // vector ^ float
40     // are legal in constant expressions (currently)
41     const vector v3 = '5 2 5' ^ '3 10 3';
42     const vector v4 = '5 2 5' ^ 10;
43     
44     print("vv: ", vtos(v3), "\n");
45     print("vf: ", vtos(v4), "\n");
46     
47     // good olde xor swap test too
48     float swap_x = 100;
49     float swap_y = 200;
50     vector swaps = swap(swap_x, swap_y);
51     print("100:200 swapped is: ", ftos(swaps.x), ":", ftos(swaps.y), "\n");
52 }