|
5 | 5 | import static org.junit.jupiter.api.Assertions.*; |
6 | 6 |
|
7 | 7 | class MathSymbolTest { |
8 | | - @Test |
9 | | - void test_fmt() { |
10 | | - MathSymbol sym = getBigExpression(); |
11 | | - assertEquals("(0.2 + 8.1) * (2.7 * -2.1 + 0.1) + (7.9 + -2.3 + (9.9 + 2.3 * -1.1 + 2.1))", sym.fmt()); |
| 8 | + void assertExprFmt(ObjStringPair testcase) { |
| 9 | + assertEquals(/*expected*/testcase.str(), testcase.obj().fmt()); |
12 | 10 | } |
13 | | - |
14 | | - @Test |
15 | | - void test_calculateValue() { |
16 | | - MathSymbol sym = getBigExpression(); |
17 | | - assertEquals(-31.161, sym.calculateValue()); |
| 11 | + void assertExprValue(double expected, ObjStringPair testcase) { |
| 12 | + assertEquals(expected, testcase.obj().calculateValue()); |
18 | 13 | } |
19 | 14 |
|
20 | 15 | @Test |
21 | | - void test_fmtBigMixed2() { |
22 | | - MathSymbol sym = getBigMixedExpr2(); |
23 | | - assertEquals("(68.0 + 93.5 * -85.5 / 41.1 - (42.3 + 66.8) * ((77.8 / 45.3 + -10.7 * 65.6) / (0.4 + (84.5 + -31.1 / 90.6)))) / ((-37.6 + 59.5 * ((-80.9 - -72.2) * (84.1 - -68.0 / -67.8))) * ((-96.2 + -1.2) / (2.6 + 36.7) / 40.6 * -57.4))", |
24 | | - sym.fmt()); |
| 16 | + void test_fmt() { |
| 17 | + assertExprFmt(CommonData.getBigData1_minimumParens()); |
| 18 | + assertExprFmt(CommonData.getBigData2_minimumParens()); |
25 | 19 | } |
26 | 20 |
|
27 | 21 | @Test |
28 | | - void test_calculateValueBigMixed2() { |
29 | | - MathSymbol sym = getBigMixedExpr2(); |
30 | | - assertEquals(-0.0051502812181513195, sym.calculateValue()); |
31 | | - } |
32 | | - |
33 | | - MathSymbol getBigExpression() { |
34 | | - return new AddOperation( |
35 | | - new MulOperation( |
36 | | - new AddOperation( |
37 | | - new BasicDoubleSymbol(0.2), |
38 | | - new BasicDoubleSymbol(8.1)), |
39 | | - new AddOperation( |
40 | | - new MulOperation( |
41 | | - new BasicDoubleSymbol(2.7), |
42 | | - new BasicDoubleSymbol(-2.1) |
43 | | - ), |
44 | | - new BasicDoubleSymbol(0.1) |
45 | | - ) |
46 | | - ), |
47 | | - new AddOperation( |
48 | | - new AddOperation( |
49 | | - new BasicDoubleSymbol(7.9), |
50 | | - new BasicDoubleSymbol(-2.3)), |
51 | | - new AddOperation( |
52 | | - new AddOperation( |
53 | | - new BasicDoubleSymbol(9.9), |
54 | | - new MulOperation( |
55 | | - new BasicDoubleSymbol(2.3), |
56 | | - new BasicDoubleSymbol(-1.1))), |
57 | | - new BasicDoubleSymbol(2.1) |
58 | | - ) |
59 | | - ) |
60 | | - ); |
61 | | - } |
62 | | - |
63 | | - |
64 | | - /** |
65 | | - * Tests involving this are more like snapshot tests - this just returns this randomly generated MathSymbol |
66 | | - * (small python script). You could call this a form of fuzzing. |
67 | | - * <pre>{@code |
68 | | - * import random |
69 | | - * |
70 | | - * OPTIONS = [s + 'Operation' for s in ('Add', 'Sub', 'Mul', 'Div')] |
71 | | - * |
72 | | - * |
73 | | - * def happens(prob: float): |
74 | | - * return random.random() < prob |
75 | | - * |
76 | | - * |
77 | | - * def gen_random_double_sym(curr_depth: int, is_arg1: bool): |
78 | | - * indent = ' ' * curr_depth * 4 |
79 | | - * return (f'{indent}new BasicDoubleSymbol(' |
80 | | - * f'{random.randint(-999, 999) / 10:.1f})' |
81 | | - * + (',' if is_arg1 else '')) |
82 | | - * |
83 | | - * |
84 | | - * def get_leaf_prob(min_depth: int, max_depth: int, depth: int): |
85 | | - * if depth >= max_depth: |
86 | | - * return 1 |
87 | | - * if depth < min_depth: |
88 | | - * return 0 |
89 | | - * w = max_depth - min_depth |
90 | | - * o = depth - min_depth |
91 | | - * return o / w |
92 | | - * |
93 | | - * |
94 | | - * def gen_random(min_depth: int, max_depth: int, |
95 | | - * curr_depth: int = 0, is_arg1=False) -> str: |
96 | | - * indent = ' ' * curr_depth * 4 |
97 | | - * if happens(get_leaf_prob(min_depth, max_depth, curr_depth)): |
98 | | - * return gen_random_double_sym(curr_depth, is_arg1) |
99 | | - * name = random.choice(OPTIONS) |
100 | | - * arg1 = gen_random(min_depth, max_depth, curr_depth + 1, True) |
101 | | - * arg2 = gen_random(min_depth, max_depth, curr_depth + 1, False) |
102 | | - * result = (f'{indent}new {name}(\n' |
103 | | - * f'{arg1}\n' |
104 | | - * f'{arg2}\n' |
105 | | - * f'{indent})' + (',' if is_arg1 else '')) |
106 | | - * return result |
107 | | - * |
108 | | - * |
109 | | - * if __name__ == '__main__': |
110 | | - * print(gen_random(2, 7)) |
111 | | - * }</pre> |
112 | | - */ |
113 | | - MathSymbol getBigMixedExpr2() { |
114 | | - return new DivOperation( |
115 | | - new SubOperation( |
116 | | - new AddOperation( |
117 | | - new BasicDoubleSymbol(68.0), |
118 | | - new DivOperation( |
119 | | - new MulOperation( |
120 | | - new BasicDoubleSymbol(93.5), |
121 | | - new BasicDoubleSymbol(-85.5) |
122 | | - ), |
123 | | - new BasicDoubleSymbol(41.1) |
124 | | - ) |
125 | | - ), |
126 | | - new MulOperation( |
127 | | - new AddOperation( |
128 | | - new BasicDoubleSymbol(42.3), |
129 | | - new BasicDoubleSymbol(66.8) |
130 | | - ), |
131 | | - new DivOperation( |
132 | | - new AddOperation( |
133 | | - new DivOperation( |
134 | | - new BasicDoubleSymbol(77.8), |
135 | | - new BasicDoubleSymbol(45.3) |
136 | | - ), |
137 | | - new MulOperation( |
138 | | - new BasicDoubleSymbol(-10.7), |
139 | | - new BasicDoubleSymbol(65.6) |
140 | | - ) |
141 | | - ), |
142 | | - new AddOperation( |
143 | | - new BasicDoubleSymbol(0.4), |
144 | | - new AddOperation( |
145 | | - new BasicDoubleSymbol(84.5), |
146 | | - new DivOperation( |
147 | | - new BasicDoubleSymbol(-31.1), |
148 | | - new BasicDoubleSymbol(90.6) |
149 | | - ) |
150 | | - ) |
151 | | - ) |
152 | | - ) |
153 | | - ) |
154 | | - ), |
155 | | - new MulOperation( |
156 | | - new AddOperation( |
157 | | - new BasicDoubleSymbol(-37.6), |
158 | | - new MulOperation( |
159 | | - new BasicDoubleSymbol(59.5), |
160 | | - new MulOperation( |
161 | | - new SubOperation( |
162 | | - new BasicDoubleSymbol(-80.9), |
163 | | - new BasicDoubleSymbol(-72.2) |
164 | | - ), |
165 | | - new SubOperation( |
166 | | - new BasicDoubleSymbol(84.1), |
167 | | - new DivOperation( |
168 | | - new BasicDoubleSymbol(-68.0), |
169 | | - new BasicDoubleSymbol(-67.8) |
170 | | - ) |
171 | | - ) |
172 | | - ) |
173 | | - ) |
174 | | - ), |
175 | | - new MulOperation( |
176 | | - new DivOperation( |
177 | | - new DivOperation( |
178 | | - new AddOperation( |
179 | | - new BasicDoubleSymbol(-96.2), |
180 | | - new BasicDoubleSymbol(-1.2) |
181 | | - ), |
182 | | - new AddOperation( |
183 | | - new BasicDoubleSymbol(2.6), |
184 | | - new BasicDoubleSymbol(36.7) |
185 | | - ) |
186 | | - ), |
187 | | - new BasicDoubleSymbol(40.6) |
188 | | - ), |
189 | | - new BasicDoubleSymbol(-57.4) |
190 | | - ) |
191 | | - ) |
192 | | - ); |
| 22 | + void test_calculateValue() { |
| 23 | + assertExprValue(-31.161, CommonData.getBigData1_minimumParens()); |
| 24 | + assertExprValue(-0.0051502812181513195, CommonData.getBigData2_minimumParens()); |
193 | 25 | } |
194 | 26 | } |
0 commit comments