1 package nom.tam.util.test;
4 import static org.junit.Assert.assertEquals;
5 import junit.framework.JUnit4TestAdapter;
7 /** This class tests the ByteFormatter and ByteParser classes.
10 import java.util.Arrays;
12 public class ByteFormatParseTest {
14 byte[] buffer = new byte[100000];
15 ByteFormatter bf = new ByteFormatter();
16 ByteParser bp = new ByteParser(buffer);
21 public void testInt() throws Exception {
23 for (int i = 0; i < 10; i += 1) {
24 buffer[i] = (byte) ' ';
27 assertEquals("IntBlank", 0, bp.getInt(10));
30 bf.setTruncationThrow(false);
32 int[] tint = new int[100];
34 tint[0] = Integer.MIN_VALUE;
35 tint[1] = Integer.MAX_VALUE;
38 for (int i = 0; i < tint.length; i += 1) {
39 tint[i] = (int) (Integer.MAX_VALUE * (2 * (Math.random() - .5)));
45 while (cnt < tint.length) {
46 offset = bf.format(tint[cnt], buffer, offset, colSize);
49 offset = bf.format("\n", buffer, offset, 1);
53 // Now see if we can get them back
55 for (int i = 0; i < tint.length; i += 1) {
57 int chk = bp.getInt(colSize);
59 assertEquals("IntegersRA", chk, tint[i]);
60 if ((i + 1) % 8 == 0) {
65 // Now do it with left-aligned numbers.
67 bp.setFillFields(true);
72 while (cnt < tint.length) {
73 int oldOffset = offset;
74 offset = bf.format(tint[cnt], buffer, offset, colSize);
75 int nb = colSize - (offset - oldOffset);
77 offset = bf.alignFill(buffer, offset, nb);
81 offset = bf.format("\n", buffer, offset, 1);
85 // Now see if we can get them back
87 for (int i = 0; i < tint.length; i += 1) {
89 int chk = bp.getInt(colSize);
91 assertEquals("IntegersLA", chk, tint[i]);
92 if ((i + 1) % 8 == 0) {
101 while (cnt < tint.length) {
102 offset = bf.format(tint[cnt], buffer, offset, colSize);
105 offset = bf.format("\n", buffer, offset, 1);
109 String myStr = new String(buffer, 0, offset);
110 assertEquals("No spaces", -1, myStr.indexOf(" "));
118 while (cnt < tint.length) {
119 offset = bf.format(tint[cnt], buffer, offset, colSize);
120 offset = bf.format(" ", buffer, offset, 1);
123 myStr = new String(buffer, 0, offset);
124 String[] array = myStr.split(" ");
126 assertEquals("Split size", 100, array.length);
128 for (int i = 0; i < array.length; i += 1) {
129 assertEquals("Parse token", tint[i], Integer.parseInt(array[i]));
133 bf.setTruncationThrow(false);
136 Arrays.fill(buffer, (byte) ' ');
138 for (int i = 0; i < 10; i += 1) {
139 offset = bf.format(val, buffer, 0, 6);
140 String test = (val + " ").substring(0, 6);
142 assertEquals("TestTrunc" + i, test, new String(buffer, 0, 6));
144 assertEquals("TestTrunc" + i, "******", new String(buffer, 0, 6));
149 bf.setTruncationThrow(true);
151 for (int i = 0; i < 10; i += 1) {
152 boolean thrown = false;
154 offset = bf.format(val, buffer, 0, 6);
155 } catch (TruncationException e) {
159 assertEquals("TestTruncThrow" + i, false, thrown);
161 assertEquals("TestTruncThrow" + i, true, thrown);
168 public void testLong() throws Exception {
170 for (int i = 0; i < 10; i += 1) {
171 buffer[i] = (byte) ' ';
174 assertEquals("LongBlank", 0L, bp.getLong(10));
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)));
181 lng[0] = Long.MAX_VALUE;
182 lng[1] = Long.MIN_VALUE;
185 bf.setTruncationThrow(false);
186 bp.setFillFields(true);
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);
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) {
207 public void testFloat() throws Exception {
209 for (int i = 0; i < 10; i += 1) {
210 buffer[i] = (byte) ' ';
213 assertEquals("FloatBlank", 0.f, bp.getFloat(10), 0.);
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)));
220 flt[0] = Float.MAX_VALUE;
221 flt[1] = Float.MIN_VALUE;
224 flt[4] = Float.POSITIVE_INFINITY;
225 flt[5] = Float.NEGATIVE_INFINITY;
228 bf.setTruncationThrow(false);
234 while (cnt < flt.length) {
235 offset = bf.format(flt[cnt], buffer, offset, 24);
238 offset = bf.format("\n", buffer, offset, 1);
245 for (int i = 0; i < flt.length; i += 1) {
247 float chk = bp.getFloat(24);
249 float dx = Math.abs(chk - flt[i]);
251 dx = dx / Math.abs(flt[i]);
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);
258 assertEquals("Float check:" + i, 0., dx, 1.e-6);
260 if ((i + 1) % 4 == 0) {
267 public void testDouble() throws Exception {
269 for (int i = 0; i < 10; i += 1) {
270 buffer[i] = (byte) ' ';
273 assertEquals("DoubBlank", 0., bp.getDouble(10), 0.);
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));
280 dbl[0] = Double.MAX_VALUE;
281 dbl[1] = Double.MIN_VALUE;
284 dbl[4] = Double.POSITIVE_INFINITY;
285 dbl[5] = Double.NEGATIVE_INFINITY;
288 bf.setTruncationThrow(false);
292 while (cnt < dbl.length) {
293 offset = bf.format(dbl[cnt], buffer, offset, 25);
296 offset = bf.format("\n", buffer, offset, 1);
302 for (int i = 0; i < dbl.length; i += 1) {
304 double chk = bp.getDouble(25);
306 double dx = Math.abs(chk - dbl[i]);
308 dx = dx / Math.abs(dbl[i]);
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);
315 assertEquals("Double check:" + i, 0., dx, 1.e-14);
318 if ((i + 1) % 4 == 0) {
325 public void testBoolean() throws Exception {
327 boolean[] btst = new boolean[100];
328 for (int i = 0; i < btst.length; i += 1) {
329 btst[i] = Math.random() > 0.5;
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);
338 for (int i = 0; i < btst.length; i += 1) {
339 assertEquals("Boolean:" + i, btst[i], bp.getBoolean());
344 public void testString() throws Exception {
347 String bigStr = "abcdefghijklmnopqrstuvwxyz";
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);
355 for (int i = 0; i < 100; i += 1) {
360 String want = bigStr.substring(i % 27);
361 if (want.length() > 13) {
362 want = want.substring(0, 13);
364 String s = bp.getString(want.length());
365 assertEquals("String:" + i, want, s);