-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestNoOps.sc
127 lines (99 loc) · 3.06 KB
/
TestNoOps.sc
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
/*
TestNoOps.run
UnitTest.gui
*/
TestNoOps : UnitTest {
test_array {
var ops, check, n = 300;
var a, b, r1, r2;
check = { |func, inval|
func.value(func.value(inval)) == inval
};
a = Array.rand(300, 0, 100);
b = a.clump(10);
r1 = b.size.rand;
r2 = b.size.rand;
this.assert(n.even, "for this test, array must have even number of elements");
ops = (
\reverse: { |x| x.reverse },
\rotate: { |x| x.rotate(x.size div: 2) },
\swap: {|x| x.swap(r1, r2) },
\flop: {|x| x.flop }
);
ops.keysValuesDo { |name, func|
this.assert(check.value(func, b), "the operation '%' should be no-op when applied twice".format(name))
};
this.assert(b.flat == a, "flat should return a flat array of the same order");
this.assert(b.flatten(1) == a, "flatten should return a flat array of the same order");
this.assert(b.sum.sum == a.sum, "summing should be invariant with sub arrays");
this.assert({ |...args| args }.flop.value(*b) == b.flop,
"function flop should behave in analogy to array.flop");
}
test_numerical_homomorphisms {
var n = [
10.rand2, // integers
10.0.rand2, // floats
{ 100.rand2 }.dup(8), // arrays
{ 100.0.rand2 }.dup(8),
Complex(100.rand2, 100.rand2), // complex
{ 1 }, // function
];
var numbers = n.dup(2).allTuples.flatten(1);
var test = { |name, f, h, a, b|
var x = f.(h.(a, b));
var y = h.(f.(a), f.(b));
this.assert(this.deepEval(x) == this.deepEval(y),
name + "should be a homomorphism for" + [a, b]);
};
var c = 0.5;
numbers.pairsDo { |n1, n2|
test.value("add-mul", c * _, _ + _, n1, n2);
test.value("add-div", _ / c, _ + _, n1, n2); test.value("bubble-mul", _.bubble, _ * _, n1, n2);
}
}
deepEval { |item|
^if(item.isSequenceableCollection) {
item.collect(this.deepEval(_))
} {
item.value
}
}
assertEqualValue { |a, b, comment, report = true, onFailure|
var diff, size;
a = a.value;
b = b.value;
diff = absdif(a, b).asArray.maxItem;
// faliures with 1e-13 in new build
^this.assert(diff < 1e-12, comment, report, { onFailure.value(diff) })
}
test_arithmetics {
var n = [
rrand(2, 4), // integers
1.0.rand + 2, // floats
{ rrand(2, 4) }.dup(4), // arrays
{ 1.0.rand + 2 }.dup(4),
{ 2.0 }, // function
];
var numbers = n.dup(3).allTuples;
var farmulae = [
"{|a,b,c| (a ** b) * (a ** c) }", "{|a,b,c| a ** (b + c) }",
// "{|a,b,c| (a ** b ** c) }", "{|a,b,c| a ** (b * c) }",
"{|a,b,c| (a ** c) * (b ** c) }", "{|a,b,c| (a * b) ** c }",
"{|a,b,c| (a ** c) / (b ** c) }", "{|a,b,c| (a / b) ** c }",
"{|a,b,c| (a ** c.neg) }", "{|a,b,c| 1 / (a ** c) }",
"{|a,b,c| log(a * b) }", "{|a,b,c| log(a) + log(b) }",
"{|a,b,c| log(a ** c) }", "{|a,b,c| c * log(a) }"
];
numbers.do { |triple|
farmulae.pairsDo { |a, b, i|
var x = a.interpret.value(*triple);
var y = b.interpret.value(*triple);
this.assertEqualValue(x, y, "arithmetic test " ++ i,
onFailure:
postf("%\nshould equal close enough\n%\n"
"For the values: %\n. Difference: %\n", a, b, triple, _)
);
};
}
}
}