Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Added Problem.ProgramSynthesis Project, Fixed Expression Issues, Fixed Code Generation

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