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