Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Interpreter/Expressions/FloatExpressionTests.cs @ 14398

Last change on this file since 14398 was 14398, checked in by pkimmesw, 7 years ago

#2665 Expressions are splitted into StatefullExpressions and StatelessExpressions, Added traits for tests

File size: 10.7 KB
Line 
1using System;
2using HeuristicLab.Algorithms.PushGP.Expressions;
3using HeuristicLab.Algorithms.PushGP.Stack;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5
6namespace 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        [TestProperty("Time", "Short")]
17        [TestCategory("ExpressionTest")]
18        [TestCategory("FloatExpressionTest")]
19        public void TestAdd()
20        {
21            interpreter.FloatStack.Push(5.3, 5.3);
22            interpreter.Interpret(new FloatAddExpression());
23
24            Assert.AreEqual(10.6, interpreter.FloatStack.Top, delta);
25
26            TestStackCounts(floatStack: 1);
27        }
28
29        [TestMethod]
30        [TestProperty("Time", "Short")]
31        [TestCategory("ExpressionTest")]
32        [TestCategory("FloatExpressionTest")]
33        public void TestAddWithInsufficientArguments()
34        {
35            TestWithInsufficientArguments("+", 1);
36        }
37
38        [TestMethod]
39        [TestProperty("Time", "Short")]
40        [TestCategory("ExpressionTest")]
41        [TestCategory("FloatExpressionTest")]
42        public void TestSubtract()
43        {
44            interpreter.FloatStack.Push(10.2, 5.3);
45            interpreter.Interpret(new FloatSubtractExpression());
46
47            Assert.AreEqual(4.9, interpreter.FloatStack.Top, delta);
48
49            TestStackCounts(floatStack: 1);
50        }
51
52        [TestMethod]
53        [TestProperty("Time", "Short")]
54        [TestCategory("ExpressionTest")]
55        [TestCategory("FloatExpressionTest")]
56        public void TestSubractWithInsufficientArguments()
57        {
58            TestWithInsufficientArguments("-", 1);
59        }
60
61        [TestMethod]
62        [TestProperty("Time", "Short")]
63        [TestCategory("ExpressionTest")]
64        [TestCategory("FloatExpressionTest")]
65        public void TestMultiply()
66        {
67            interpreter.FloatStack.Push(9.9, 3.3);
68            interpreter.Interpret(new FloatMultiplyExpression());
69
70            Assert.AreEqual(32.67, interpreter.FloatStack.Top, delta);
71
72            TestStackCounts(floatStack: 1);
73        }
74
75        [TestMethod]
76        [TestProperty("Time", "Short")]
77        [TestCategory("ExpressionTest")]
78        [TestCategory("FloatExpressionTest")]
79        public void TestMultiplyWithInsufficientArguments()
80        {
81            TestWithInsufficientArguments("*", 1);
82        }
83
84        [TestMethod]
85        [TestProperty("Time", "Short")]
86        [TestCategory("ExpressionTest")]
87        [TestCategory("FloatExpressionTest")]
88        public void TestDivide()
89        {
90            interpreter.FloatStack.Push(9.9, 3.3);
91            interpreter.Interpret(new FloatDivideExpression());
92
93            Assert.AreEqual(3.0, interpreter.FloatStack.Top, delta);
94
95            TestStackCounts(floatStack: 1);
96        }
97
98        [TestMethod]
99        [TestProperty("Time", "Short")]
100        [TestCategory("ExpressionTest")]
101        [TestCategory("FloatExpressionTest")]
102        public void TestDivideWithInsufficientArguments()
103        {
104            TestWithInsufficientArguments("/", 1);
105        }
106
107        [TestMethod]
108        [TestProperty("Time", "Short")]
109        [TestCategory("ExpressionTest")]
110        [TestCategory("FloatExpressionTest")]
111        public void TestModulo()
112        {
113            interpreter.FloatStack.Push(11.6, 5.3);
114            interpreter.Interpret(new FloatModuloExpression());
115
116            Assert.AreEqual(1, interpreter.FloatStack.Top, delta);
117
118            TestStackCounts(floatStack: 1);
119        }
120
121        [TestMethod]
122        [TestProperty("Time", "Short")]
123        [TestCategory("ExpressionTest")]
124        [TestCategory("FloatExpressionTest")]
125        public void TestModuloWithInsufficientArguments()
126        {
127            TestWithInsufficientArguments("%", 1);
128        }
129
130        [TestMethod]
131        [TestProperty("Time", "Short")]
132        [TestCategory("ExpressionTest")]
133        [TestCategory("FloatExpressionTest")]
134        public void TestMin()
135        {
136            interpreter.FloatStack.Push(10.2, 5.3);
137            interpreter.Interpret(new FloatMinExpression());
138
139            Assert.AreEqual(5.3, interpreter.FloatStack.Top, delta);
140
141            TestStackCounts(floatStack: 1);
142        }
143
144        [TestMethod]
145        [TestProperty("Time", "Short")]
146        [TestCategory("ExpressionTest")]
147        [TestCategory("FloatExpressionTest")]
148        public void TestMinWithInsufficientArguments()
149        {
150            TestWithInsufficientArguments("MIN", 1);
151        }
152
153        [TestMethod]
154        [TestProperty("Time", "Short")]
155        [TestCategory("ExpressionTest")]
156        [TestCategory("FloatExpressionTest")]
157        public void TestMax()
158        {
159            interpreter.FloatStack.Push(10.3, 5.3);
160            interpreter.Interpret(new FloatMaxExpression());
161
162            Assert.AreEqual(10.3, interpreter.FloatStack.Top, delta);
163
164            TestStackCounts(floatStack: 1);
165        }
166
167        [TestMethod]
168        [TestProperty("Time", "Short")]
169        [TestCategory("ExpressionTest")]
170        [TestCategory("FloatExpressionTest")]
171        public void TestMaxWithInsufficientArguments()
172        {
173            TestWithInsufficientArguments("MAX", 1);
174        }
175
176        [TestMethod]
177        [TestProperty("Time", "Short")]
178        [TestCategory("ExpressionTest")]
179        [TestCategory("FloatExpressionTest")]
180        public void TestSmallerThan()
181        {
182            interpreter.FloatStack.Push(10.2, 5.3);
183            interpreter.Interpret(new FloatSmallerThanExpression());
184
185            Assert.AreEqual(false, interpreter.BooleanStack.Top);
186
187            TestStackCounts(booleanStack: 1);
188        }
189
190        [TestMethod]
191        [TestProperty("Time", "Short")]
192        [TestCategory("ExpressionTest")]
193        [TestCategory("FloatExpressionTest")]
194        public void TestSmallerThanWithInsufficientArguments()
195        {
196            TestWithInsufficientArguments("<", 1);
197        }
198
199        [TestMethod]
200        [TestProperty("Time", "Short")]
201        [TestCategory("ExpressionTest")]
202        [TestCategory("FloatExpressionTest")]
203        public void TestGreaterThan()
204        {
205            interpreter.FloatStack.Push(10.2, 5.3);
206            interpreter.Interpret(new FloatGreaterThanExpression());
207
208            Assert.AreEqual(true, interpreter.BooleanStack.Top);
209
210            TestStackCounts(booleanStack: 1);
211        }
212
213        [TestMethod]
214        [TestProperty("Time", "Short")]
215        [TestCategory("ExpressionTest")]
216        [TestCategory("FloatExpressionTest")]
217        public void TestGreaterThanWithInsufficientArguments()
218        {
219            TestWithInsufficientArguments(">", 1);
220        }
221
222        [TestMethod]
223        [TestProperty("Time", "Short")]
224        [TestCategory("ExpressionTest")]
225        [TestCategory("FloatExpressionTest")]
226        public void TestFromBooleanTrue()
227        {
228            interpreter.BooleanStack.Push(true);
229            interpreter.Interpret(new FloatFromBooleanExpression());
230
231            Assert.AreEqual(1d, interpreter.FloatStack.Top, delta);
232
233            TestStackCounts(floatStack: 1);
234        }
235
236        [TestMethod]
237        [TestProperty("Time", "Short")]
238        [TestCategory("ExpressionTest")]
239        [TestCategory("FloatExpressionTest")]
240        public void TestFromBooleanFalse()
241        {
242            interpreter.BooleanStack.Push(false);
243            interpreter.Interpret(new FloatFromBooleanExpression());
244
245            Assert.AreEqual(0d, interpreter.FloatStack.Top, delta);
246
247            TestStackCounts(floatStack: 1);
248        }
249
250        [TestMethod]
251        [TestProperty("Time", "Short")]
252        [TestCategory("ExpressionTest")]
253        [TestCategory("FloatExpressionTest")]
254        public void TestFromBooleanWithInsufficientArguments()
255        {
256            TestWithInsufficientArguments("FROMBOOLEAN");
257        }
258
259        [TestMethod]
260        [TestProperty("Time", "Short")]
261        [TestCategory("ExpressionTest")]
262        [TestCategory("FloatExpressionTest")]
263        public void TestFromInteger()
264        {
265            interpreter.IntegerStack.Push(5);
266            interpreter.Interpret(new FloatFromIntegerExpression());
267
268            Assert.AreEqual(5d, interpreter.FloatStack.Top, delta);
269
270            TestStackCounts(floatStack: 1);
271        }
272
273        [TestMethod]
274        [TestProperty("Time", "Short")]
275        [TestCategory("ExpressionTest")]
276        [TestCategory("FloatExpressionTest")]
277        public void TestFromIntegerWithInsufficientArguments()
278        {
279            TestWithInsufficientArguments("FROMINTEGER");
280        }
281
282        [TestMethod]
283        [TestProperty("Time", "Short")]
284        [TestCategory("ExpressionTest")]
285        [TestCategory("FloatExpressionTest")]
286        public void TestSine()
287        {
288            interpreter.FloatStack.Push(Math.PI / 2);
289            interpreter.Interpret(new FloatSineExpression());
290
291            Assert.AreEqual(1, interpreter.FloatStack.Top, delta);
292
293            TestStackCounts(floatStack: 1);
294        }
295
296        [TestMethod]
297        [TestProperty("Time", "Short")]
298        [TestCategory("ExpressionTest")]
299        [TestCategory("FloatExpressionTest")]
300        public void TestSineWithInsufficientArguments()
301        {
302            TestWithInsufficientArguments("SIN");
303        }
304
305        [TestMethod]
306        [TestProperty("Time", "Short")]
307        [TestCategory("ExpressionTest")]
308        [TestCategory("FloatExpressionTest")]
309        public void TestCosine()
310        {
311            interpreter.FloatStack.Push(Math.PI);
312            interpreter.Interpret(new FloatCosineExpression());
313
314            Assert.AreEqual(-1, interpreter.FloatStack.Top, delta);
315
316            TestStackCounts(floatStack: 1);
317        }
318
319        [TestMethod]
320        [TestProperty("Time", "Short")]
321        [TestCategory("ExpressionTest")]
322        [TestCategory("FloatExpressionTest")]
323        public void TestCosineWithInsufficientArguments()
324        {
325            TestWithInsufficientArguments("COS");
326        }
327
328        protected override double[] GetValues(int count)
329        {
330            var values = new double[count];
331
332            for (var i = 0; i < count; i++)
333            {
334                values[i] = i * 0.9;
335            }
336
337            return values;
338        }
339
340        protected override void CheckOtherStacksAreEmpty()
341        {
342            TestStackCounts(floatStack: null);
343        }
344    }
345}
Note: See TracBrowser for help on using the repository browser.