Begin versioning.
[fits.git] / src / nom / tam / util / test / ByteFormatParseTest.java
1 package nom.tam.util.test;
2
3 import org.junit.Test;
4 import static org.junit.Assert.assertEquals;
5 import junit.framework.JUnit4TestAdapter;
6
7 /** This class tests the ByteFormatter and ByteParser classes.
8  */
9 import nom.tam.util.*;
10 import java.util.Arrays;
11
12 public class ByteFormatParseTest {
13
14     byte[] buffer = new byte[100000];
15     ByteFormatter bf = new ByteFormatter();
16     ByteParser bp = new ByteParser(buffer);
17     int offset = 0;
18     int cnt = 0;
19
20     @Test
21     public void testInt() throws Exception {
22
23         for (int i = 0; i < 10; i += 1) {
24             buffer[i] = (byte) ' ';
25         }
26         bp.setOffset(0);
27         assertEquals("IntBlank", 0, bp.getInt(10));
28
29         bf.setAlign(true);
30         bf.setTruncationThrow(false);
31
32         int[] tint = new int[100];
33
34         tint[0] = Integer.MIN_VALUE;
35         tint[1] = Integer.MAX_VALUE;
36         tint[2] = 0;
37
38         for (int i = 0; i < tint.length; i += 1) {
39             tint[i] = (int) (Integer.MAX_VALUE * (2 * (Math.random() - .5)));
40         }
41
42
43         // Write 100 numbers
44         int colSize = 12;
45         while (cnt < tint.length) {
46             offset = bf.format(tint[cnt], buffer, offset, colSize);
47             cnt += 1;
48             if (cnt % 8 == 0) {
49                 offset = bf.format("\n", buffer, offset, 1);
50             }
51         }
52
53         // Now see if we can get them back
54         bp.setOffset(0);
55         for (int i = 0; i < tint.length; i += 1) {
56
57             int chk = bp.getInt(colSize);
58
59             assertEquals("IntegersRA", chk, tint[i]);
60             if ((i + 1) % 8 == 0) {
61                 bp.skip(1);
62             }
63         }
64
65         // Now do it with left-aligned numbers.
66         bf.setAlign(false);
67         bp.setFillFields(true);
68         offset = 0;
69         colSize = 12;
70         cnt = 0;
71         offset = 0;
72         while (cnt < tint.length) {
73             int oldOffset = offset;
74             offset = bf.format(tint[cnt], buffer, offset, colSize);
75             int nb = colSize - (offset - oldOffset);
76             if (nb > 0) {
77                 offset = bf.alignFill(buffer, offset, nb);
78             }
79             cnt += 1;
80             if (cnt % 8 == 0) {
81                 offset = bf.format("\n", buffer, offset, 1);
82             }
83         }
84
85         // Now see if we can get them back
86         bp.setOffset(0);
87         for (int i = 0; i < tint.length; i += 1) {
88
89             int chk = bp.getInt(colSize);
90
91             assertEquals("IntegersLA", chk, tint[i]);
92             if ((i + 1) % 8 == 0) {
93                 bp.skip(1);
94             }
95         }
96
97         offset = 0;
98         colSize = 12;
99         cnt = 0;
100         offset = 0;
101         while (cnt < tint.length) {
102             offset = bf.format(tint[cnt], buffer, offset, colSize);
103             cnt += 1;
104             if (cnt % 8 == 0) {
105                 offset = bf.format("\n", buffer, offset, 1);
106             }
107         }
108
109         String myStr = new String(buffer, 0, offset);
110         assertEquals("No spaces", -1, myStr.indexOf(" "));
111
112         bf.setAlign(false);
113
114         offset = 0;
115         colSize = 12;
116         cnt = 0;
117         offset = 0;
118         while (cnt < tint.length) {
119             offset = bf.format(tint[cnt], buffer, offset, colSize);
120             offset = bf.format(" ", buffer, offset, 1);
121             cnt += 1;
122         }
123         myStr = new String(buffer, 0, offset);
124         String[] array = myStr.split(" ");
125
126         assertEquals("Split size", 100, array.length);
127
128         for (int i = 0; i < array.length; i += 1) {
129             assertEquals("Parse token", tint[i], Integer.parseInt(array[i]));
130         }
131
132
133         bf.setTruncationThrow(false);
134
135         int val = 1;
136         Arrays.fill(buffer, (byte) ' ');
137
138         for (int i = 0; i < 10; i += 1) {
139             offset = bf.format(val, buffer, 0, 6);
140             String test = (val + "      ").substring(0, 6);
141             if (i < 6) {
142                 assertEquals("TestTrunc" + i, test, new String(buffer, 0, 6));
143             } else {
144                 assertEquals("TestTrunc" + i, "******", new String(buffer, 0, 6));
145             }
146             val *= 10;
147         }
148
149         bf.setTruncationThrow(true);
150         val = 1;
151         for (int i = 0; i < 10; i += 1) {
152             boolean thrown = false;
153             try {
154                 offset = bf.format(val, buffer, 0, 6);
155             } catch (TruncationException e) {
156                 thrown = true;
157             }
158             if (i < 6) {
159                 assertEquals("TestTruncThrow" + i, false, thrown);
160             } else {
161                 assertEquals("TestTruncThrow" + i, true, thrown);
162             }
163             val *= 10;
164         }
165     }
166
167     @Test
168     public void testLong() throws Exception {
169
170         for (int i = 0; i < 10; i += 1) {
171             buffer[i] = (byte) ' ';
172         }
173         bp.setOffset(0);
174         assertEquals("LongBlank", 0L, bp.getLong(10));
175
176         long[] lng = new long[100];
177         for (int i = 0; i < lng.length; i += 1) {
178             lng[i] = (long) (Long.MAX_VALUE * (2 * (Math.random() - 0.5)));
179         }
180
181         lng[0] = Long.MAX_VALUE;
182         lng[1] = Long.MIN_VALUE;
183         lng[2] = 0;
184
185         bf.setTruncationThrow(false);
186         bp.setFillFields(true);
187         bf.setAlign(true);
188         offset = 0;
189         for (int i = 0; i < lng.length; i += 1) {
190             offset = bf.format(lng[i], buffer, offset, 20);
191             if ((i + 1) % 4 == 0) {
192                 offset = bf.format("\n", buffer, offset, 1);
193             }
194         }
195
196         bp.setOffset(0);
197
198         for (int i = 0; i < lng.length; i += 1) {
199             assertEquals("Long check", lng[i], bp.getLong(20));
200             if ((i + 1) % 4 == 0) {
201                 bp.skip(1);
202             }
203         }
204     }
205
206     @Test
207     public void testFloat() throws Exception {
208
209         for (int i = 0; i < 10; i += 1) {
210             buffer[i] = (byte) ' ';
211         }
212         bp.setOffset(0);
213         assertEquals("FloatBlank", 0.f, bp.getFloat(10), 0.);
214
215         float[] flt = new float[100];
216         for (int i = 6; i < flt.length; i += 1) {
217             flt[i] = (float) (2 * (Math.random() - 0.5) * Math.pow(10, 60 * (Math.random() - 0.5)));
218         }
219
220         flt[0] = Float.MAX_VALUE;
221         flt[1] = Float.MIN_VALUE;
222         flt[2] = 0;
223         flt[3] = Float.NaN;
224         flt[4] = Float.POSITIVE_INFINITY;
225         flt[5] = Float.NEGATIVE_INFINITY;
226
227
228         bf.setTruncationThrow(false);
229         bf.setAlign(true);
230
231         offset = 0;
232         cnt = 0;
233
234         while (cnt < flt.length) {
235             offset = bf.format(flt[cnt], buffer, offset, 24);
236             cnt += 1;
237             if (cnt % 4 == 0) {
238                 offset = bf.format("\n", buffer, offset, 1);
239             }
240         }
241
242
243         bp.setOffset(0);
244
245         for (int i = 0; i < flt.length; i += 1) {
246
247             float chk = bp.getFloat(24);
248
249             float dx = Math.abs(chk - flt[i]);
250             if (flt[i] != 0) {
251                 dx = dx / Math.abs(flt[i]);
252             }
253             if (Float.isNaN(flt[i])) {
254                 assertEquals("Float check:" + i, true, Float.isNaN(chk));
255             } else if (Float.isInfinite(flt[i])) {
256                 assertEquals("Float check:" + i, flt[i], chk, 0);
257             } else {
258                 assertEquals("Float check:" + i, 0., dx, 1.e-6);
259             }
260             if ((i + 1) % 4 == 0) {
261                 bp.skip(1);
262             }
263         }
264     }
265
266     @Test
267     public void testDouble() throws Exception {
268
269         for (int i = 0; i < 10; i += 1) {
270             buffer[i] = (byte) ' ';
271         }
272         bp.setOffset(0);
273         assertEquals("DoubBlank", 0., bp.getDouble(10), 0.);
274
275         double[] dbl = new double[100];
276         for (int i = 6; i < dbl.length; i += 1) {
277             dbl[i] = 2 * (Math.random() - 0.5) * Math.pow(10, 60 * (Math.random() - 0.5));
278         }
279
280         dbl[0] = Double.MAX_VALUE;
281         dbl[1] = Double.MIN_VALUE;
282         dbl[2] = 0;
283         dbl[3] = Double.NaN;
284         dbl[4] = Double.POSITIVE_INFINITY;
285         dbl[5] = Double.NEGATIVE_INFINITY;
286
287
288         bf.setTruncationThrow(false);
289         bf.setAlign(true);
290         offset = 0;
291         cnt = 0;
292         while (cnt < dbl.length) {
293             offset = bf.format(dbl[cnt], buffer, offset, 25);
294             cnt += 1;
295             if (cnt % 4 == 0) {
296                 offset = bf.format("\n", buffer, offset, 1);
297             }
298         }
299
300
301         bp.setOffset(0);
302         for (int i = 0; i < dbl.length; i += 1) {
303
304             double chk = bp.getDouble(25);
305
306             double dx = Math.abs(chk - dbl[i]);
307             if (dbl[i] != 0) {
308                 dx = dx / Math.abs(dbl[i]);
309             }
310             if (Double.isNaN(dbl[i])) {
311                 assertEquals("Double check:" + i, true, Double.isNaN(chk));
312             } else if (Double.isInfinite(dbl[i])) {
313                 assertEquals("Double check:" + i, dbl[i], chk, 0);
314             } else {
315                 assertEquals("Double check:" + i, 0., dx, 1.e-14);
316             }
317
318             if ((i + 1) % 4 == 0) {
319                 bp.skip(1);
320             }
321         }
322     }
323
324     @Test
325     public void testBoolean() throws Exception {
326
327         boolean[] btst = new boolean[100];
328         for (int i = 0; i < btst.length; i += 1) {
329             btst[i] = Math.random() > 0.5;
330         }
331         offset = 0;
332         for (int i = 0; i < btst.length; i += 1) {
333             offset = bf.format(btst[i], buffer, offset, 1);
334             offset = bf.format(" ", buffer, offset, 1);
335         }
336
337         bp.setOffset(0);
338         for (int i = 0; i < btst.length; i += 1) {
339             assertEquals("Boolean:" + i, btst[i], bp.getBoolean());
340         }
341     }
342
343     @Test
344     public void testString() throws Exception {
345
346         offset = 0;
347         String bigStr = "abcdefghijklmnopqrstuvwxyz";
348
349         for (int i = 0; i < 100; i += 1) {
350             offset = bf.format(bigStr.substring(i % 27), buffer, offset, 13);
351             offset = bf.format(" ", buffer, offset, 1);
352         }
353
354         bp.setOffset(0);
355         for (int i = 0; i < 100; i += 1) {
356             int ind = i % 27;
357             if (ind > 13) {
358                 ind = 13;
359             }
360             String want = bigStr.substring(i % 27);
361             if (want.length() > 13) {
362                 want = want.substring(0, 13);
363             }
364             String s = bp.getString(want.length());
365             assertEquals("String:" + i, want, s);
366             bp.skip(1);
367         }
368     }
369 }