1 | using System;
|
---|
2 | using HeuristicLab.Algorithms.PushGP.Expressions;
|
---|
3 | using HeuristicLab.Algorithms.PushGP.Stack;
|
---|
4 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Tests.Interpreter.Expressions
|
---|
7 | {
|
---|
8 | [TestClass]
|
---|
9 | public class FloatExpressionTests : CommonTests<double>
|
---|
10 | {
|
---|
11 | private const double delta = 0.00001;
|
---|
12 | protected override string TypeName { get { return "FLOAT"; } }
|
---|
13 | protected override IStack<double> Stack { get { return interpreter.FloatStack; } }
|
---|
14 |
|
---|
15 | [TestMethod]
|
---|
16 | public void TestAdd()
|
---|
17 | {
|
---|
18 | interpreter.FloatStack.Push(5.3, 5.3);
|
---|
19 | interpreter.Interpret(new FloatAddExpression());
|
---|
20 |
|
---|
21 | Assert.AreEqual(10.6, interpreter.FloatStack.Top, delta);
|
---|
22 |
|
---|
23 | this.TestStackCounts(floatStack: 1);
|
---|
24 | }
|
---|
25 |
|
---|
26 | [TestMethod]
|
---|
27 | public void TestAddWithInsufficientArguments()
|
---|
28 | {
|
---|
29 | this.TestWithInsufficientArguments("+", 1);
|
---|
30 | }
|
---|
31 |
|
---|
32 | [TestMethod]
|
---|
33 | public void TestSubtract()
|
---|
34 | {
|
---|
35 | interpreter.FloatStack.Push(10.2, 5.3);
|
---|
36 | interpreter.Interpret(new FloatSubtractExpression());
|
---|
37 |
|
---|
38 | Assert.AreEqual(4.9, interpreter.FloatStack.Top, delta);
|
---|
39 |
|
---|
40 | this.TestStackCounts(floatStack: 1);
|
---|
41 | }
|
---|
42 |
|
---|
43 | [TestMethod]
|
---|
44 | public void TestSubractWithInsufficientArguments()
|
---|
45 | {
|
---|
46 | this.TestWithInsufficientArguments("-", 1);
|
---|
47 | }
|
---|
48 |
|
---|
49 | [TestMethod]
|
---|
50 | public void TestMultiply()
|
---|
51 | {
|
---|
52 | interpreter.FloatStack.Push(9.9, 3.3);
|
---|
53 | interpreter.Interpret(new FloatMultiplyExpression());
|
---|
54 |
|
---|
55 | Assert.AreEqual(32.67, interpreter.FloatStack.Top, delta);
|
---|
56 |
|
---|
57 | this.TestStackCounts(floatStack: 1);
|
---|
58 | }
|
---|
59 |
|
---|
60 | [TestMethod]
|
---|
61 | public void TestMultiplyWithInsufficientArguments()
|
---|
62 | {
|
---|
63 | this.TestWithInsufficientArguments("*", 1);
|
---|
64 | }
|
---|
65 |
|
---|
66 | [TestMethod]
|
---|
67 | public void TestDivide()
|
---|
68 | {
|
---|
69 | interpreter.FloatStack.Push(9.9, 3.3);
|
---|
70 | interpreter.Interpret(new FloatDivideExpression());
|
---|
71 |
|
---|
72 | Assert.AreEqual(3.0, interpreter.FloatStack.Top, delta);
|
---|
73 |
|
---|
74 | this.TestStackCounts(floatStack: 1);
|
---|
75 | }
|
---|
76 |
|
---|
77 | [TestMethod]
|
---|
78 | public void TestDivideWithInsufficientArguments()
|
---|
79 | {
|
---|
80 | this.TestWithInsufficientArguments("/", 1);
|
---|
81 | }
|
---|
82 |
|
---|
83 | [TestMethod]
|
---|
84 | public void TestModulo()
|
---|
85 | {
|
---|
86 | interpreter.FloatStack.Push(11.6, 5.3);
|
---|
87 | interpreter.Interpret(new FloatModuloExpression());
|
---|
88 |
|
---|
89 | Assert.AreEqual(1, interpreter.FloatStack.Top, delta);
|
---|
90 |
|
---|
91 | this.TestStackCounts(floatStack: 1);
|
---|
92 | }
|
---|
93 |
|
---|
94 | [TestMethod]
|
---|
95 | public void TestModuloWithInsufficientArguments()
|
---|
96 | {
|
---|
97 | this.TestWithInsufficientArguments("%", 1);
|
---|
98 | }
|
---|
99 |
|
---|
100 | [TestMethod]
|
---|
101 | public void TestMin()
|
---|
102 | {
|
---|
103 | interpreter.FloatStack.Push(10.2, 5.3);
|
---|
104 | interpreter.Interpret(new FloatMinExpression());
|
---|
105 |
|
---|
106 | Assert.AreEqual(5.3, interpreter.FloatStack.Top, delta);
|
---|
107 |
|
---|
108 | this.TestStackCounts(floatStack: 1);
|
---|
109 | }
|
---|
110 |
|
---|
111 | [TestMethod]
|
---|
112 | public void TestMinWithInsufficientArguments()
|
---|
113 | {
|
---|
114 | this.TestWithInsufficientArguments("MIN", 1);
|
---|
115 | }
|
---|
116 |
|
---|
117 | [TestMethod]
|
---|
118 | public void TestMax()
|
---|
119 | {
|
---|
120 | interpreter.FloatStack.Push(10.3, 5.3);
|
---|
121 | interpreter.Interpret(new FloatMaxExpression());
|
---|
122 |
|
---|
123 | Assert.AreEqual(10.3, interpreter.FloatStack.Top, delta);
|
---|
124 |
|
---|
125 | this.TestStackCounts(floatStack: 1);
|
---|
126 | }
|
---|
127 |
|
---|
128 | [TestMethod]
|
---|
129 | public void TestMaxWithInsufficientArguments()
|
---|
130 | {
|
---|
131 | this.TestWithInsufficientArguments("MAX", 1);
|
---|
132 | }
|
---|
133 |
|
---|
134 | [TestMethod]
|
---|
135 | public void TestSmallerThan()
|
---|
136 | {
|
---|
137 | interpreter.FloatStack.Push(10.2, 5.3);
|
---|
138 | interpreter.Interpret(new FloatSmallerThanExpression());
|
---|
139 |
|
---|
140 | Assert.AreEqual(false, interpreter.BooleanStack.Top);
|
---|
141 |
|
---|
142 | this.TestStackCounts(booleanStack: 1);
|
---|
143 | }
|
---|
144 |
|
---|
145 | [TestMethod]
|
---|
146 | public void TestSmallerThanWithInsufficientArguments()
|
---|
147 | {
|
---|
148 | this.TestWithInsufficientArguments("<", 1);
|
---|
149 | }
|
---|
150 |
|
---|
151 | [TestMethod]
|
---|
152 | public void TestGreaterThan()
|
---|
153 | {
|
---|
154 | interpreter.FloatStack.Push(10.2, 5.3);
|
---|
155 | interpreter.Interpret(new FloatGreaterThanExpression());
|
---|
156 |
|
---|
157 | Assert.AreEqual(true, interpreter.BooleanStack.Top);
|
---|
158 |
|
---|
159 | this.TestStackCounts(booleanStack: 1);
|
---|
160 | }
|
---|
161 |
|
---|
162 | [TestMethod]
|
---|
163 | public void TestGreaterThanWithInsufficientArguments()
|
---|
164 | {
|
---|
165 | this.TestWithInsufficientArguments(">", 1);
|
---|
166 | }
|
---|
167 |
|
---|
168 | [TestMethod]
|
---|
169 | public void TestFromBooleanTrue()
|
---|
170 | {
|
---|
171 | interpreter.BooleanStack.Push(true);
|
---|
172 | interpreter.Interpret(new FloatFromBooleanExpression());
|
---|
173 |
|
---|
174 | Assert.AreEqual(1d, interpreter.FloatStack.Top, delta);
|
---|
175 |
|
---|
176 | this.TestStackCounts(floatStack: 1);
|
---|
177 | }
|
---|
178 |
|
---|
179 | [TestMethod]
|
---|
180 | public void TestFromBooleanFalse()
|
---|
181 | {
|
---|
182 | interpreter.BooleanStack.Push(false);
|
---|
183 | interpreter.Interpret(new FloatFromBooleanExpression());
|
---|
184 |
|
---|
185 | Assert.AreEqual(0d, interpreter.FloatStack.Top, delta);
|
---|
186 |
|
---|
187 | this.TestStackCounts(floatStack: 1);
|
---|
188 | }
|
---|
189 |
|
---|
190 | [TestMethod]
|
---|
191 | public void TestFromBooleanWithInsufficientArguments()
|
---|
192 | {
|
---|
193 | this.TestWithInsufficientArguments("FROMBOOLEAN");
|
---|
194 | }
|
---|
195 |
|
---|
196 | [TestMethod]
|
---|
197 | public void TestFromInteger()
|
---|
198 | {
|
---|
199 | interpreter.IntegerStack.Push(5);
|
---|
200 | interpreter.Interpret(new FloatFromIntegerExpression());
|
---|
201 |
|
---|
202 | Assert.AreEqual(5d, interpreter.FloatStack.Top, delta);
|
---|
203 |
|
---|
204 | this.TestStackCounts(floatStack: 1);
|
---|
205 | }
|
---|
206 |
|
---|
207 | [TestMethod]
|
---|
208 | public void TestFromIntegerWithInsufficientArguments()
|
---|
209 | {
|
---|
210 | this.TestWithInsufficientArguments("FROMINTEGER");
|
---|
211 | }
|
---|
212 |
|
---|
213 | [TestMethod]
|
---|
214 | public void TestSine()
|
---|
215 | {
|
---|
216 | interpreter.FloatStack.Push(Math.PI / 2);
|
---|
217 | interpreter.Interpret(new FloatSineExpression());
|
---|
218 |
|
---|
219 | Assert.AreEqual(1, interpreter.FloatStack.Top, delta);
|
---|
220 |
|
---|
221 | this.TestStackCounts(floatStack: 1);
|
---|
222 | }
|
---|
223 |
|
---|
224 | [TestMethod]
|
---|
225 | public void TestSineWithInsufficientArguments()
|
---|
226 | {
|
---|
227 | this.TestWithInsufficientArguments("SIN");
|
---|
228 | }
|
---|
229 |
|
---|
230 | [TestMethod]
|
---|
231 | public void TestCosine()
|
---|
232 | {
|
---|
233 | interpreter.FloatStack.Push(Math.PI);
|
---|
234 | interpreter.Interpret(new FloatCosineExpression());
|
---|
235 |
|
---|
236 | Assert.AreEqual(-1, interpreter.FloatStack.Top, delta);
|
---|
237 |
|
---|
238 | this.TestStackCounts(floatStack: 1);
|
---|
239 | }
|
---|
240 |
|
---|
241 | [TestMethod]
|
---|
242 | public void TestCosineWithInsufficientArguments()
|
---|
243 | {
|
---|
244 | this.TestWithInsufficientArguments("COS");
|
---|
245 | }
|
---|
246 |
|
---|
247 | protected override double[] GetValues(int count)
|
---|
248 | {
|
---|
249 | var values = new double[count];
|
---|
250 |
|
---|
251 | for (var i = 0; i < count; i++)
|
---|
252 | {
|
---|
253 | values[i] = i * 0.9;
|
---|
254 | }
|
---|
255 |
|
---|
256 | return values;
|
---|
257 | }
|
---|
258 |
|
---|
259 | protected override void CheckOtherStacksAreEmpty()
|
---|
260 | {
|
---|
261 | this.TestStackCounts(floatStack: null);
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|