6bdcb96eb88812765bb03744611a1d2df566c7a5
[debian/openrocket] / 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                 gm = new GenericModifier<TestGenericModifier.TestValue>("Test modifier", null,
26                                 UnitGroup.UNITS_NONE, 2.0, TestValue.class, "value") {
27                         @Override
28                         protected TestValue getModifiedObject(Simulation simulation) {
29                                 Assert.assertTrue(simulation == sim);
30                                 return value;
31                         }
32                 };
33                 
34                 gm.setMinValue(0.5);
35                 gm.setMaxValue(5.5);
36         }
37         
38         @Test
39         public void testGetCurrentValue() throws OptimizationException {
40                 value.d = 1.0;
41                 assertEquals(2.0, gm.getCurrentSIValue(sim), EPSILON);
42                 value.d = 2.0;
43                 assertEquals(4.0, gm.getCurrentSIValue(sim), EPSILON);
44         }
45         
46         @Test
47         public void testGetCurrentScaledValue() throws OptimizationException {
48                 value.d = 0.0;
49                 assertEquals(-0.1, gm.getCurrentScaledValue(sim), EPSILON);
50                 value.d = 1.0;
51                 assertEquals(0.3, gm.getCurrentScaledValue(sim), EPSILON);
52                 value.d = 2.0;
53                 assertEquals(0.7, gm.getCurrentScaledValue(sim), EPSILON);
54                 value.d = 3.0;
55                 assertEquals(1.1, gm.getCurrentScaledValue(sim), EPSILON);
56         }
57         
58         @Test
59         public void testModify() throws OptimizationException {
60                 value.d = 0.0;
61                 gm.modify(sim, -0.5);
62                 assertEquals(-1.0, value.d, EPSILON);
63                 
64                 gm.modify(sim, 0.0);
65                 assertEquals(0.25, value.d, EPSILON);
66                 
67                 gm.modify(sim, 0.5);
68                 assertEquals(1.5, value.d, EPSILON);
69                 
70                 gm.modify(sim, 1.0);
71                 assertEquals(2.75, value.d, EPSILON);
72                 
73                 gm.modify(sim, 1.5);
74                 assertEquals(4.0, value.d, EPSILON);
75         }
76         
77         public void testSingularRange() throws OptimizationException {
78                 gm.setMinValue(1.0);
79                 gm.setMaxValue(1.0);
80                 value.d = 0.5;
81                 assertEquals(0.0, gm.getCurrentScaledValue(sim), EPSILON);
82                 value.d = 1.0;
83                 assertEquals(0.5, gm.getCurrentScaledValue(sim), EPSILON);
84                 value.d = 1.00001;
85                 assertEquals(1.0, gm.getCurrentScaledValue(sim), EPSILON);
86         }
87         
88         public class TestValue {
89                 private double d;
90                 
91                 public double getValue() {
92                         return d;
93                 }
94                 
95                 public void setValue(double value) {
96                         this.d = value;
97                 }
98         }
99         
100 }