create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / unit / FractionalUnitTest.java
1 package net.sf.openrocket.unit;
2
3 import static org.junit.Assert.*;
4
5 import org.junit.Test;
6
7 public class FractionalUnitTest {
8
9         private final static Unit testUnit = new FractionalUnit(1, "unit", "unit", 4, 0.5);
10         private final static Unit testUnitApprox = new FractionalUnit(1, "unit", "unit", 16, 0.5, 0.02);
11
12         private final static Unit inchUnit = new FractionalUnit(0.0254, "in/64", "in", 64, 1d/16d);
13         
14         @Test
15         public void testRound() {
16
17                 assertEquals(-1d, testUnit.round(-1.125), 0.0); // rounds to -1 since mod is even
18                 assertEquals(-1d, testUnit.round(-1.0), 0.0);
19                 assertEquals(-1d, testUnit.round(-.875), 0.0); // rounds to -1 since mod is even
20
21                 assertEquals(-0.75d, testUnit.round(-.874), 0.0);
22                 assertEquals(-0.75d, testUnit.round(-.75), 0.0);
23                 assertEquals(-0.75d, testUnit.round(-.626), 0.0);
24
25                 assertEquals(-0.5d, testUnit.round(-.625), 0.0); // rounds to -.5 since mod is even
26                 assertEquals(-0.5d, testUnit.round(-.5), 0.0);
27                 assertEquals(-0.5d, testUnit.round(-.375), 0.0); // rounds to -.5 since mod is even
28
29                 assertEquals(-0.25d, testUnit.round(-.374), 0.0);
30                 assertEquals(-0.25d, testUnit.round(-.25), 0.0);
31                 assertEquals(-0.25d, testUnit.round(-.126), 0.0);
32
33                 assertEquals(0d, testUnit.round(-.125), 0.0);
34                 assertEquals(0d, testUnit.round(0), 0.0);
35                 assertEquals(0d, testUnit.round(.125), 0.0);
36
37                 assertEquals(0.25d, testUnit.round(.126), 0.0);
38                 assertEquals(0.25d, testUnit.round(.25), 0.0);
39                 assertEquals(0.25d, testUnit.round(.374), 0.0);
40
41                 assertEquals(0.5d, testUnit.round(.375), 0.0); // rounds to .5 since mod is even
42                 assertEquals(0.5d, testUnit.round(.5), 0.0);
43                 assertEquals(0.5d, testUnit.round(.625), 0.0); // rounds to .5 since mod is even
44
45                 assertEquals(0.75d, testUnit.round(.626), 0.0);
46                 assertEquals(0.75d, testUnit.round(.75), 0.0);
47                 assertEquals(0.75d, testUnit.round(.874), 0.0);
48
49                 assertEquals(1d, testUnit.round(.875), 0.0); // rounds to 1 since mod is even
50                 assertEquals(1d, testUnit.round(1.0), 0.0);
51                 assertEquals(1d, testUnit.round(1.125), 0.0); // rounds to 1 since mod is even
52
53         }
54
55         @Test
56         public void testIncrement() {
57
58                 assertEquals( -1d, testUnit.getNextValue(-1.2), 0.0);
59                 assertEquals( -1d, testUnit.getNextValue(-1.4), 0.0);
60
61                 assertEquals( -0.5d, testUnit.getNextValue(-0.7), 0.0);
62                 assertEquals( -0.5d, testUnit.getNextValue(-0.9), 0.0);
63                 assertEquals( -0.5d, testUnit.getNextValue(-1.0), 0.0);
64
65                 assertEquals( 0.0d, testUnit.getNextValue(-0.05), 0.0 );
66                 assertEquals( 0.0d, testUnit.getNextValue(-0.062), 0.0 );
67                 assertEquals( 0.0d, testUnit.getNextValue(-0.07), 0.0 );
68                 assertEquals( 0.0d, testUnit.getNextValue(-0.11), 0.0 );
69
70                 assertEquals( 0.5d, testUnit.getNextValue(0), 0.0 );
71                 assertEquals( 0.5d, testUnit.getNextValue(0.01), 0.0 );
72                 assertEquals( 0.5d, testUnit.getNextValue(0.062), 0.0 );
73                 assertEquals( 0.5d, testUnit.getNextValue(0.0625), 0.0);
74
75                 assertEquals( 1d, testUnit.getNextValue(0.51), 0.0);
76                 assertEquals( 1d, testUnit.getNextValue(0.7), 0.0);
77         }
78
79         @Test
80         public void testDecrement() {
81
82                 assertEquals( -1.5d, testUnit.getPreviousValue(-1.2), 0.0);
83                 assertEquals( -1.5d, testUnit.getPreviousValue(-1.4), 0.0);
84                 assertEquals( -1.5d, testUnit.getPreviousValue(-1.0), 0.0);
85
86                 assertEquals( -1d, testUnit.getPreviousValue(-0.7), 0.0);
87                 assertEquals( -1d, testUnit.getPreviousValue(-0.9), 0.0);
88
89                 assertEquals( -0.5d, testUnit.getPreviousValue(-0.01), 0.0 );
90                 assertEquals( -0.5d, testUnit.getPreviousValue(-0.05), 0.0 );
91                 assertEquals( -0.5d, testUnit.getPreviousValue(-0.062), 0.0 );
92                 assertEquals( -0.5d, testUnit.getPreviousValue(-0.07), 0.0 );
93                 assertEquals( -0.5d, testUnit.getPreviousValue(0), 0.0 );
94
95                 assertEquals( 0.0d, testUnit.getPreviousValue(0.49), 0.0 );
96                 assertEquals( 0.0d, testUnit.getPreviousValue(0.262), 0.0 );
97                 assertEquals( 0.0d, testUnit.getPreviousValue(0.51), 0.0);
98
99                 assertEquals( 0.5d, testUnit.getPreviousValue(0.7), 0.0);
100
101                 assertEquals( 1.0d, testUnit.getPreviousValue(1.2), 0.0);
102         }
103
104         @Test
105         public void testToStringDefaultPrecision() {
106
107                 // default epsilon is 0.025
108                 assertEquals("-1.2", testUnit.toString(-1.2)); 
109                 assertEquals("-1 \u00B9\u2044\u2084", testUnit.toString(-1.225));
110                 assertEquals("-1 \u00B9\u2044\u2084", testUnit.toString(-1.227));
111                 assertEquals("-1 \u00B9\u2044\u2084", testUnit.toString(-1.25));
112                 assertEquals("-1 \u00B9\u2044\u2084", testUnit.toString(-1.25));
113                 assertEquals("-1 \u00B9\u2044\u2084", testUnit.toString(-1.275));
114                 assertEquals("-1.3", testUnit.toString(-1.3));
115
116                 assertEquals("-0.2", testUnit.toString(-.2));
117                 assertEquals("-\u00B9\u2044\u2084", testUnit.toString(-.225));
118                 assertEquals("-\u00B9\u2044\u2084", testUnit.toString(-.25));
119                 assertEquals("-\u00B9\u2044\u2084", testUnit.toString(-.274));
120                 //assertEquals("-1/4", testUnit.toString(-.275)); // this has roundoff error which pushes it over epsilon
121                 assertEquals("-0.3", testUnit.toString(-.3));
122
123                 assertEquals("-0.1", testUnit.toString(-.1));
124                 assertEquals("0", testUnit.toString(-0.024));
125                 assertEquals("0", testUnit.toString(0));
126                 assertEquals("0", testUnit.toString(.024));
127                 assertEquals("0.1", testUnit.toString(.1));
128
129                 assertEquals("0.2", testUnit.toString(.2));
130                 assertEquals("\u00B9\u2044\u2084", testUnit.toString(.225));
131                 assertEquals("\u00B9\u2044\u2084", testUnit.toString(.25));
132                 assertEquals("\u00B9\u2044\u2084", testUnit.toString(.274));
133                 assertEquals("0.3", testUnit.toString(.3));
134
135                 assertEquals("1.2", testUnit.toString(1.2));
136                 assertEquals("1 \u00B9\u2044\u2084", testUnit.toString(1.225));
137                 assertEquals("1 \u00B9\u2044\u2084", testUnit.toString(1.25));
138                 assertEquals("1 \u00B9\u2044\u2084", testUnit.toString(1.275));
139                 assertEquals("1.3", testUnit.toString(1.3));
140
141         }
142
143         @Test
144         public void testToStringWithPrecision() {
145
146                 // epsilon is .02
147                 assertEquals("-1 \u00B3\u2044\u2081\u2086", testUnitApprox.toString(-1.2));
148                 assertEquals("-1.225", testUnitApprox.toString(-1.225));
149                 assertEquals("-1 \u00B9\u2044\u2084", testUnitApprox.toString(-1.25));
150                 assertEquals("-1.275", testUnitApprox.toString(-1.275));
151                 assertEquals("-1 \u2075\u2044\u2081\u2086", testUnitApprox.toString(-1.3));
152
153                 assertEquals("-\u00B3\u2044\u2081\u2086", testUnitApprox.toString(-.2));
154                 assertEquals("-0.225", testUnitApprox.toString(-.225));
155                 assertEquals("-\u00B9\u2044\u2084", testUnitApprox.toString(-.25));
156                 assertEquals("-0.275", testUnitApprox.toString(-.275));
157                 assertEquals("-\u2075\u2044\u2081\u2086", testUnitApprox.toString(-.3));
158
159                 assertEquals("-0.1", testUnitApprox.toString(-.1));
160                 assertEquals("-0.024", testUnitApprox.toString(-0.024));
161                 assertEquals("0", testUnitApprox.toString(0));
162                 assertEquals("0.024", testUnitApprox.toString(.024));
163                 assertEquals("0.1", testUnitApprox.toString(.1));
164
165                 assertEquals("\u00B3\u2044\u2081\u2086", testUnitApprox.toString(.2));
166                 assertEquals("0.225", testUnitApprox.toString(.225));
167                 assertEquals("\u00B9\u2044\u2084", testUnitApprox.toString(.25));
168                 assertEquals("0.275", testUnitApprox.toString(.275));
169                 assertEquals("\u2075\u2044\u2081\u2086", testUnitApprox.toString(.3));
170
171                 assertEquals("1 \u00B3\u2044\u2081\u2086", testUnitApprox.toString(1.2));
172                 assertEquals("1.225", testUnitApprox.toString(1.225));
173                 assertEquals("1 \u00B9\u2044\u2084", testUnitApprox.toString(1.25));
174                 assertEquals("1.275", testUnitApprox.toString(1.275));
175                 assertEquals("1 \u2075\u2044\u2081\u2086", testUnitApprox.toString(1.3));
176
177         }
178         
179         @Test
180         public void testInchToString() {
181                 
182                 // Just some random test points.
183                 assertEquals( "\u00B9\u2044\u2086\u2084", inchUnit.toString( 1d/64d*0.0254));
184                 
185                 assertEquals( "-\u2075\u2044\u2086\u2084", inchUnit.toString( -5d/64d*0.0254));
186                 
187                 assertEquals( "4 \u00B9\u2044\u2082", inchUnit.toString( 9d/2d*0.0254));
188                 
189                 assertEquals( "0.002", inchUnit.toString( 0.002*0.0254));
190                 
191                 // default body tube length:
192                 double length = 8d * 0.025;
193                 
194                 assertEquals( "7 \u2077\u2044\u2088", inchUnit.toString( length) );
195                 
196                 // had problems with roundoff in decrement.
197                 
198                 double v = inchUnit.toUnit(length);
199                 for ( int i = 0; i< 15; i++ ) {
200                         assertTrue( v > inchUnit.getPreviousValue(v) );
201                         v = inchUnit.getPreviousValue(v);
202                 }
203                 
204         }
205
206 }