- Implemented copying of custom expressions to other simulations in expression builde...
[debian/openrocket] / core / test / net / sf / openrocket / optimization / rocketoptimization / modifiers / TestGenericModifier.java
1 package net.sf.openrocket.optimization.rocketoptimization.modifiers;
2
3 import static net.sf.openrocket.util.MathUtil.EPSILON;
4 import static org.junit.Assert.assertEquals;
5 import net.sf.openrocket.document.OpenRocketDocument;
6 import net.sf.openrocket.document.Simulation;
7 import net.sf.openrocket.optimization.general.OptimizationException;
8 import net.sf.openrocket.rocketcomponent.Rocket;
9 import net.sf.openrocket.unit.UnitGroup;
10
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14
15 public class TestGenericModifier {
16         
17         private TestValue value;
18         private GenericModifier<TestValue> gm;
19         private Simulation sim;
20         
21         @Before
22         public void setup() {
23                 value = new TestValue();
24                 Rocket rocket = new Rocket();
25                 
26                 sim = new Simulation(new OpenRocketDocument(rocket), rocket);
27                 
28                 Object related = new Object();
29                 
30                 gm = new GenericModifier<TestGenericModifier.TestValue>("Test modifier", "Description", related,
31                                 UnitGroup.UNITS_NONE, 2.0, TestValue.class, "value") {
32                         @Override
33                         protected TestValue getModifiedObject(Simulation simulation) {
34                                 Assert.assertTrue(simulation == sim);
35                                 return value;
36                         }
37                 };
38                 
39                 gm.setMinValue(0.5);
40                 gm.setMaxValue(5.5);
41         }
42         
43         @Test
44         public void testGetCurrentValue() throws OptimizationException {
45                 value.d = 1.0;
46                 assertEquals(2.0, gm.getCurrentSIValue(sim), EPSILON);
47                 value.d = 2.0;
48                 assertEquals(4.0, gm.getCurrentSIValue(sim), EPSILON);
49         }
50         
51         @Test
52         public void testGetCurrentScaledValue() throws OptimizationException {
53                 value.d = 0.0;
54                 assertEquals(-0.1, gm.getCurrentScaledValue(sim), EPSILON);
55                 value.d = 1.0;
56                 assertEquals(0.3, gm.getCurrentScaledValue(sim), EPSILON);
57                 value.d = 2.0;
58                 assertEquals(0.7, gm.getCurrentScaledValue(sim), EPSILON);
59                 value.d = 3.0;
60                 assertEquals(1.1, gm.getCurrentScaledValue(sim), EPSILON);
61         }
62         
63         @Test
64         public void testModify() throws OptimizationException {
65                 value.d = 0.0;
66                 gm.modify(sim, -0.5);
67                 assertEquals(-1.0, value.d, EPSILON);
68                 
69                 gm.modify(sim, 0.0);
70                 assertEquals(0.25, value.d, EPSILON);
71                 
72                 gm.modify(sim, 0.5);
73                 assertEquals(1.5, value.d, EPSILON);
74                 
75                 gm.modify(sim, 1.0);
76                 assertEquals(2.75, value.d, EPSILON);
77                 
78                 gm.modify(sim, 1.5);
79                 assertEquals(4.0, value.d, EPSILON);
80         }
81         
82         public void testSingularRange() throws OptimizationException {
83                 gm.setMinValue(1.0);
84                 gm.setMaxValue(1.0);
85                 value.d = 0.5;
86                 assertEquals(0.0, gm.getCurrentScaledValue(sim), EPSILON);
87                 value.d = 1.0;
88                 assertEquals(0.5, gm.getCurrentScaledValue(sim), EPSILON);
89                 value.d = 1.00001;
90                 assertEquals(1.0, gm.getCurrentScaledValue(sim), EPSILON);
91         }
92         
93         public class TestValue {
94                 private double d;
95                 
96                 public double getValue() {
97                         return d;
98                 }
99                 
100                 public void setValue(double value) {
101                         this.d = value;
102                 }
103         }
104         
105 }