001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: TupleFormatTest.java,v 1.25.2.2 2008/01/07 15:14:23 cwl Exp $
007: */
008:
009: package com.sleepycat.bind.tuple.test;
010:
011: import java.util.Arrays;
012:
013: import junit.framework.Test;
014: import junit.framework.TestCase;
015: import junit.framework.TestSuite;
016:
017: import com.sleepycat.bind.tuple.TupleBinding;
018: import com.sleepycat.bind.tuple.TupleInput;
019: import com.sleepycat.bind.tuple.TupleOutput;
020: import com.sleepycat.collections.test.DbTestUtil;
021: import com.sleepycat.je.DatabaseEntry;
022:
023: /**
024: * @author Mark Hayes
025: */
026: public class TupleFormatTest extends TestCase {
027:
028: private TupleInput in;
029: private TupleOutput out;
030: private DatabaseEntry buffer;
031:
032: public static void main(String[] args) throws Exception {
033:
034: junit.framework.TestResult tr = junit.textui.TestRunner
035: .run(suite());
036: if (tr.errorCount() > 0 || tr.failureCount() > 0) {
037: System.exit(1);
038: } else {
039: System.exit(0);
040: }
041: }
042:
043: public static Test suite() throws Exception {
044:
045: TestSuite suite = new TestSuite(TupleFormatTest.class);
046: return suite;
047: }
048:
049: public TupleFormatTest(String name) {
050:
051: super (name);
052: }
053:
054: public void setUp() {
055:
056: DbTestUtil.printTestName("TupleFormatTest." + getName());
057: buffer = new DatabaseEntry();
058: out = new TupleOutput();
059: }
060:
061: public void tearDown() {
062:
063: /* Ensure that GC can cleanup. */
064: in = null;
065: out = null;
066: buffer = null;
067: }
068:
069: private void copyOutputToInput() {
070:
071: TupleBinding.outputToEntry(out, buffer);
072: assertEquals(out.size(), buffer.getSize());
073: in = TupleBinding.entryToInput(buffer);
074: assertEquals(in.available(), buffer.getSize());
075: assertEquals(in.getBufferLength(), buffer.getSize());
076: }
077:
078: private void stringTest(String val) {
079:
080: out.reset();
081: out.writeString(val);
082: assertEquals(val.length() + 1, out.size()); // assume 1-byte chars
083: copyOutputToInput();
084: assertEquals(val, in.readString());
085: assertEquals(0, in.available());
086: }
087:
088: public void testString() {
089:
090: stringTest("");
091: stringTest("a");
092: stringTest("abc");
093:
094: out.reset();
095: out.writeString("abc");
096: out.writeString("defg");
097: assertEquals(9, out.size());
098: copyOutputToInput();
099: assertEquals("abc", in.readString());
100: assertEquals("defg", in.readString());
101: assertEquals(0, in.available());
102:
103: out.reset();
104: out.writeString("abc");
105: out.writeString("defg");
106: out.writeString("hijkl");
107: assertEquals(15, out.size());
108: copyOutputToInput();
109: assertEquals("abc", in.readString());
110: assertEquals("defg", in.readString());
111: assertEquals("hijkl", in.readString());
112: assertEquals(0, in.available());
113: }
114:
115: private void fixedStringTest(char[] val) {
116:
117: out.reset();
118: out.writeString(val);
119: assertEquals(val.length, out.size()); // assume 1 byte chars
120: copyOutputToInput();
121: char[] val2 = new char[val.length];
122: in.readString(val2);
123: assertTrue(Arrays.equals(val, val2));
124: assertEquals(0, in.available());
125: in.reset();
126: String val3 = in.readString(val.length);
127: assertTrue(Arrays.equals(val, val3.toCharArray()));
128: assertEquals(0, in.available());
129: }
130:
131: public void testFixedString() {
132:
133: fixedStringTest(new char[0]);
134: fixedStringTest(new char[] { 'a' });
135: fixedStringTest(new char[] { 'a', 'b', 'c' });
136:
137: out.reset();
138: out.writeString(new char[] { 'a', 'b', 'c' });
139: out.writeString(new char[] { 'd', 'e', 'f', 'g' });
140: assertEquals(7, out.size());
141: copyOutputToInput();
142: assertEquals("abc", in.readString(3));
143: assertEquals("defg", in.readString(4));
144: assertEquals(0, in.available());
145:
146: out.reset();
147: out.writeString(new char[] { 'a', 'b', 'c' });
148: out.writeString(new char[] { 'd', 'e', 'f', 'g' });
149: out.writeString(new char[] { 'h', 'i', 'j', 'k', 'l' });
150: assertEquals(12, out.size());
151: copyOutputToInput();
152: assertEquals("abc", in.readString(3));
153: assertEquals("defg", in.readString(4));
154: assertEquals("hijkl", in.readString(5));
155: assertEquals(0, in.available());
156: }
157:
158: public void testNullString() {
159:
160: out.reset();
161: out.writeString((String) null);
162: assertEquals(2, out.size());
163: copyOutputToInput();
164: assertEquals(null, in.readString());
165: assertEquals(0, in.available());
166:
167: out.reset();
168: out.writeString((String) null);
169: out.writeString("x");
170: assertEquals(4, out.size());
171: copyOutputToInput();
172: assertEquals(null, in.readString());
173: assertEquals(2, in.available());
174: assertEquals("x", in.readString());
175: assertEquals(0, in.available());
176:
177: out.reset();
178: out.writeString("x");
179: out.writeString((String) null);
180: assertEquals(4, out.size());
181: copyOutputToInput();
182: assertEquals("x", in.readString());
183: assertEquals(2, in.available());
184: assertEquals(null, in.readString());
185: assertEquals(0, in.available());
186:
187: out.reset();
188: out.writeString((String) null);
189: out.writeInt(123);
190: assertEquals(6, out.size());
191: copyOutputToInput();
192: assertEquals(null, in.readString());
193: assertEquals(4, in.available());
194: assertEquals(123, in.readInt());
195: assertEquals(0, in.available());
196:
197: out.reset();
198: out.writeInt(123);
199: out.writeString((String) null);
200: assertEquals(6, out.size());
201: copyOutputToInput();
202: assertEquals(123, in.readInt());
203: assertEquals(2, in.available());
204: assertEquals(null, in.readString());
205: assertEquals(0, in.available());
206: }
207:
208: private void charsTest(char[] val) {
209:
210: for (int mode = 0; mode < 2; mode += 1) {
211: out.reset();
212: switch (mode) {
213: case 0:
214: out.writeChars(val);
215: break;
216: case 1:
217: out.writeChars(new String(val));
218: break;
219: default:
220: throw new IllegalStateException();
221: }
222: assertEquals(val.length * 2, out.size());
223: copyOutputToInput();
224: char[] val2 = new char[val.length];
225: in.readChars(val2);
226: assertTrue(Arrays.equals(val, val2));
227: assertEquals(0, in.available());
228: in.reset();
229: String val3 = in.readChars(val.length);
230: assertTrue(Arrays.equals(val, val3.toCharArray()));
231: assertEquals(0, in.available());
232: }
233: }
234:
235: public void testChars() {
236:
237: charsTest(new char[0]);
238: charsTest(new char[] { 'a' });
239: charsTest(new char[] { 'a', 'b', 'c' });
240:
241: out.reset();
242: out.writeChars("abc");
243: out.writeChars("defg");
244: assertEquals(7 * 2, out.size());
245: copyOutputToInput();
246: assertEquals("abc", in.readChars(3));
247: assertEquals("defg", in.readChars(4));
248: assertEquals(0, in.available());
249:
250: out.reset();
251: out.writeChars("abc");
252: out.writeChars("defg");
253: out.writeChars("hijkl");
254: assertEquals(12 * 2, out.size());
255: copyOutputToInput();
256: assertEquals("abc", in.readChars(3));
257: assertEquals("defg", in.readChars(4));
258: assertEquals("hijkl", in.readChars(5));
259: assertEquals(0, in.available());
260: }
261:
262: private void bytesTest(char[] val) {
263:
264: char[] valBytes = new char[val.length];
265: for (int i = 0; i < val.length; i += 1)
266: valBytes[i] = (char) (val[i] & 0xFF);
267:
268: for (int mode = 0; mode < 2; mode += 1) {
269: out.reset();
270: switch (mode) {
271: case 0:
272: out.writeBytes(val);
273: break;
274: case 1:
275: out.writeBytes(new String(val));
276: break;
277: default:
278: throw new IllegalStateException();
279: }
280: assertEquals(val.length, out.size());
281: copyOutputToInput();
282: char[] val2 = new char[val.length];
283: in.readBytes(val2);
284: assertTrue(Arrays.equals(valBytes, val2));
285: assertEquals(0, in.available());
286: in.reset();
287: String val3 = in.readBytes(val.length);
288: assertTrue(Arrays.equals(valBytes, val3.toCharArray()));
289: assertEquals(0, in.available());
290: }
291: }
292:
293: public void testBytes() {
294:
295: bytesTest(new char[0]);
296: bytesTest(new char[] { 'a' });
297: bytesTest(new char[] { 'a', 'b', 'c' });
298: bytesTest(new char[] { 0x7F00, 0x7FFF, 0xFF00, 0xFFFF });
299:
300: out.reset();
301: out.writeBytes("abc");
302: out.writeBytes("defg");
303: assertEquals(7, out.size());
304: copyOutputToInput();
305: assertEquals("abc", in.readBytes(3));
306: assertEquals("defg", in.readBytes(4));
307: assertEquals(0, in.available());
308:
309: out.reset();
310: out.writeBytes("abc");
311: out.writeBytes("defg");
312: out.writeBytes("hijkl");
313: assertEquals(12, out.size());
314: copyOutputToInput();
315: assertEquals("abc", in.readBytes(3));
316: assertEquals("defg", in.readBytes(4));
317: assertEquals("hijkl", in.readBytes(5));
318: assertEquals(0, in.available());
319: }
320:
321: private void booleanTest(boolean val) {
322:
323: out.reset();
324: out.writeBoolean(val);
325: assertEquals(1, out.size());
326: copyOutputToInput();
327: assertEquals(val, in.readBoolean());
328: assertEquals(0, in.available());
329: }
330:
331: public void testBoolean() {
332:
333: booleanTest(true);
334: booleanTest(false);
335:
336: out.reset();
337: out.writeBoolean(true);
338: out.writeBoolean(false);
339: assertEquals(2, out.size());
340: copyOutputToInput();
341: assertEquals(true, in.readBoolean());
342: assertEquals(false, in.readBoolean());
343: assertEquals(0, in.available());
344:
345: out.reset();
346: out.writeBoolean(true);
347: out.writeBoolean(false);
348: out.writeBoolean(true);
349: assertEquals(3, out.size());
350: copyOutputToInput();
351: assertEquals(true, in.readBoolean());
352: assertEquals(false, in.readBoolean());
353: assertEquals(true, in.readBoolean());
354: assertEquals(0, in.available());
355: }
356:
357: private void unsignedByteTest(int val) {
358:
359: unsignedByteTest(val, val);
360: }
361:
362: private void unsignedByteTest(int val, int expected) {
363:
364: out.reset();
365: out.writeUnsignedByte(val);
366: assertEquals(1, out.size());
367: copyOutputToInput();
368: assertEquals(expected, in.readUnsignedByte());
369: }
370:
371: public void testUnsignedByte() {
372:
373: unsignedByteTest(0);
374: unsignedByteTest(1);
375: unsignedByteTest(254);
376: unsignedByteTest(255);
377: unsignedByteTest(256, 0);
378: unsignedByteTest(-1, 255);
379: unsignedByteTest(-2, 254);
380: unsignedByteTest(-255, 1);
381:
382: out.reset();
383: out.writeUnsignedByte(0);
384: out.writeUnsignedByte(1);
385: out.writeUnsignedByte(255);
386: assertEquals(3, out.size());
387: copyOutputToInput();
388: assertEquals(0, in.readUnsignedByte());
389: assertEquals(1, in.readUnsignedByte());
390: assertEquals(255, in.readUnsignedByte());
391: assertEquals(0, in.available());
392: }
393:
394: private void unsignedShortTest(int val) {
395:
396: unsignedShortTest(val, val);
397: }
398:
399: private void unsignedShortTest(int val, int expected) {
400:
401: out.reset();
402: out.writeUnsignedShort(val);
403: assertEquals(2, out.size());
404: copyOutputToInput();
405: assertEquals(expected, in.readUnsignedShort());
406: }
407:
408: public void testUnsignedShort() {
409:
410: unsignedShortTest(0);
411: unsignedShortTest(1);
412: unsignedShortTest(255);
413: unsignedShortTest(256);
414: unsignedShortTest(257);
415: unsignedShortTest(Short.MAX_VALUE - 1);
416: unsignedShortTest(Short.MAX_VALUE);
417: unsignedShortTest(Short.MAX_VALUE + 1);
418: unsignedShortTest(0xFFFF - 1);
419: unsignedShortTest(0xFFFF);
420: unsignedShortTest(0xFFFF + 1, 0);
421: unsignedShortTest(0x7FFF0000, 0);
422: unsignedShortTest(0xFFFF0000, 0);
423: unsignedShortTest(-1, 0xFFFF);
424: unsignedShortTest(-2, 0xFFFF - 1);
425: unsignedShortTest(-0xFFFF, 1);
426:
427: out.reset();
428: out.writeUnsignedShort(0);
429: out.writeUnsignedShort(1);
430: out.writeUnsignedShort(0xFFFF);
431: assertEquals(6, out.size());
432: copyOutputToInput();
433: assertEquals(0, in.readUnsignedShort());
434: assertEquals(1, in.readUnsignedShort());
435: assertEquals(0xFFFF, in.readUnsignedShort());
436: assertEquals(0, in.available());
437: }
438:
439: private void unsignedIntTest(long val) {
440:
441: unsignedIntTest(val, val);
442: }
443:
444: private void unsignedIntTest(long val, long expected) {
445:
446: out.reset();
447: out.writeUnsignedInt(val);
448: assertEquals(4, out.size());
449: copyOutputToInput();
450: assertEquals(expected, in.readUnsignedInt());
451: }
452:
453: public void testUnsignedInt() {
454:
455: unsignedIntTest(0L);
456: unsignedIntTest(1L);
457: unsignedIntTest(255L);
458: unsignedIntTest(256L);
459: unsignedIntTest(257L);
460: unsignedIntTest(Short.MAX_VALUE - 1L);
461: unsignedIntTest(Short.MAX_VALUE);
462: unsignedIntTest(Short.MAX_VALUE + 1L);
463: unsignedIntTest(Integer.MAX_VALUE - 1L);
464: unsignedIntTest(Integer.MAX_VALUE);
465: unsignedIntTest(Integer.MAX_VALUE + 1L);
466: unsignedIntTest(0xFFFFFFFFL - 1L);
467: unsignedIntTest(0xFFFFFFFFL);
468: unsignedIntTest(0xFFFFFFFFL + 1L, 0L);
469: unsignedIntTest(0x7FFFFFFF00000000L, 0L);
470: unsignedIntTest(0xFFFFFFFF00000000L, 0L);
471: unsignedIntTest(-1, 0xFFFFFFFFL);
472: unsignedIntTest(-2, 0xFFFFFFFFL - 1L);
473: unsignedIntTest(-0xFFFFFFFFL, 1L);
474:
475: out.reset();
476: out.writeUnsignedInt(0L);
477: out.writeUnsignedInt(1L);
478: out.writeUnsignedInt(0xFFFFFFFFL);
479: assertEquals(12, out.size());
480: copyOutputToInput();
481: assertEquals(0L, in.readUnsignedInt());
482: assertEquals(1L, in.readUnsignedInt());
483: assertEquals(0xFFFFFFFFL, in.readUnsignedInt());
484: assertEquals(0L, in.available());
485: }
486:
487: private void byteTest(int val) {
488:
489: out.reset();
490: out.writeByte(val);
491: assertEquals(1, out.size());
492: copyOutputToInput();
493: assertEquals((byte) val, in.readByte());
494: }
495:
496: public void testByte() {
497:
498: byteTest(0);
499: byteTest(1);
500: byteTest(-1);
501: byteTest(Byte.MAX_VALUE - 1);
502: byteTest(Byte.MAX_VALUE);
503: byteTest(Byte.MAX_VALUE + 1);
504: byteTest(Byte.MIN_VALUE + 1);
505: byteTest(Byte.MIN_VALUE);
506: byteTest(Byte.MIN_VALUE - 1);
507: byteTest(0x7F);
508: byteTest(0xFF);
509: byteTest(0x7FFF);
510: byteTest(0xFFFF);
511: byteTest(0x7FFFFFFF);
512: byteTest(0xFFFFFFFF);
513:
514: out.reset();
515: out.writeByte(0);
516: out.writeByte(1);
517: out.writeByte(-1);
518: assertEquals(3, out.size());
519: copyOutputToInput();
520: assertEquals(0, in.readByte());
521: assertEquals(1, in.readByte());
522: assertEquals(-1, in.readByte());
523: assertEquals(0, in.available());
524: }
525:
526: private void shortTest(int val) {
527:
528: out.reset();
529: out.writeShort(val);
530: assertEquals(2, out.size());
531: copyOutputToInput();
532: assertEquals((short) val, in.readShort());
533: }
534:
535: public void testShort() {
536:
537: shortTest(0);
538: shortTest(1);
539: shortTest(-1);
540: shortTest(Short.MAX_VALUE - 1);
541: shortTest(Short.MAX_VALUE);
542: shortTest(Short.MAX_VALUE + 1);
543: shortTest(Short.MIN_VALUE + 1);
544: shortTest(Short.MIN_VALUE);
545: shortTest(Short.MIN_VALUE - 1);
546: shortTest(0x7F);
547: shortTest(0xFF);
548: shortTest(0x7FFF);
549: shortTest(0xFFFF);
550: shortTest(0x7FFFFFFF);
551: shortTest(0xFFFFFFFF);
552:
553: out.reset();
554: out.writeShort(0);
555: out.writeShort(1);
556: out.writeShort(-1);
557: assertEquals(3 * 2, out.size());
558: copyOutputToInput();
559: assertEquals(0, in.readShort());
560: assertEquals(1, in.readShort());
561: assertEquals(-1, in.readShort());
562: assertEquals(0, in.available());
563: }
564:
565: private void intTest(int val) {
566:
567: out.reset();
568: out.writeInt(val);
569: assertEquals(4, out.size());
570: copyOutputToInput();
571: assertEquals((int) val, in.readInt());
572: }
573:
574: public void testInt() {
575:
576: intTest(0);
577: intTest(1);
578: intTest(-1);
579: intTest(Integer.MAX_VALUE - 1);
580: intTest(Integer.MAX_VALUE);
581: intTest(Integer.MAX_VALUE + 1);
582: intTest(Integer.MIN_VALUE + 1);
583: intTest(Integer.MIN_VALUE);
584: intTest(Integer.MIN_VALUE - 1);
585: intTest(0x7F);
586: intTest(0xFF);
587: intTest(0x7FFF);
588: intTest(0xFFFF);
589: intTest(0x7FFFFFFF);
590: intTest(0xFFFFFFFF);
591:
592: out.reset();
593: out.writeInt(0);
594: out.writeInt(1);
595: out.writeInt(-1);
596: assertEquals(3 * 4, out.size());
597: copyOutputToInput();
598: assertEquals(0, in.readInt());
599: assertEquals(1, in.readInt());
600: assertEquals(-1, in.readInt());
601: assertEquals(0, in.available());
602: }
603:
604: private void longTest(long val) {
605:
606: out.reset();
607: out.writeLong(val);
608: assertEquals(8, out.size());
609: copyOutputToInput();
610: assertEquals((long) val, in.readLong());
611: }
612:
613: public void testLong() {
614:
615: longTest(0);
616: longTest(1);
617: longTest(-1);
618: longTest(Long.MAX_VALUE - 1);
619: longTest(Long.MAX_VALUE);
620: longTest(Long.MAX_VALUE + 1);
621: longTest(Long.MIN_VALUE + 1);
622: longTest(Long.MIN_VALUE);
623: longTest(Long.MIN_VALUE - 1);
624: longTest(0x7F);
625: longTest(0xFF);
626: longTest(0x7FFF);
627: longTest(0xFFFF);
628: longTest(0x7FFFFFFF);
629: longTest(0xFFFFFFFF);
630: longTest(0x7FFFFFFFFFFFFFFFL);
631: longTest(0xFFFFFFFFFFFFFFFFL);
632:
633: out.reset();
634: out.writeLong(0);
635: out.writeLong(1);
636: out.writeLong(-1);
637: assertEquals(3 * 8, out.size());
638: copyOutputToInput();
639: assertEquals(0, in.readLong());
640: assertEquals(1, in.readLong());
641: assertEquals(-1, in.readLong());
642: assertEquals(0, in.available());
643: }
644:
645: private void floatTest(double val) {
646:
647: out.reset();
648: out.writeFloat((float) val);
649: assertEquals(4, out.size());
650: copyOutputToInput();
651: if (Double.isNaN(val)) {
652: assertTrue(Float.isNaN(in.readFloat()));
653: } else {
654: assertEquals((float) val, in.readFloat(), 0);
655: }
656: }
657:
658: public void testFloat() {
659:
660: floatTest(0);
661: floatTest(1);
662: floatTest(-1);
663: floatTest(1.0);
664: floatTest(0.1);
665: floatTest(-1.0);
666: floatTest(-0.1);
667: floatTest(Float.NaN);
668: floatTest(Float.NEGATIVE_INFINITY);
669: floatTest(Float.POSITIVE_INFINITY);
670: floatTest(Short.MAX_VALUE);
671: floatTest(Short.MIN_VALUE);
672: floatTest(Integer.MAX_VALUE);
673: floatTest(Integer.MIN_VALUE);
674: floatTest(Long.MAX_VALUE);
675: floatTest(Long.MIN_VALUE);
676: floatTest(Float.MAX_VALUE);
677: floatTest(Float.MAX_VALUE + 1);
678: floatTest(Float.MIN_VALUE + 1);
679: floatTest(Float.MIN_VALUE);
680: floatTest(Float.MIN_VALUE - 1);
681: floatTest(0x7F);
682: floatTest(0xFF);
683: floatTest(0x7FFF);
684: floatTest(0xFFFF);
685: floatTest(0x7FFFFFFF);
686: floatTest(0xFFFFFFFF);
687: floatTest(0x7FFFFFFFFFFFFFFFL);
688: floatTest(0xFFFFFFFFFFFFFFFFL);
689:
690: out.reset();
691: out.writeFloat(0);
692: out.writeFloat(1);
693: out.writeFloat(-1);
694: assertEquals(3 * 4, out.size());
695: copyOutputToInput();
696: assertEquals(0, in.readFloat(), 0);
697: assertEquals(1, in.readFloat(), 0);
698: assertEquals(-1, in.readFloat(), 0);
699: assertEquals(0, in.available(), 0);
700: }
701:
702: private void doubleTest(double val) {
703:
704: out.reset();
705: out.writeDouble((double) val);
706: assertEquals(8, out.size());
707: copyOutputToInput();
708: if (Double.isNaN(val)) {
709: assertTrue(Double.isNaN(in.readDouble()));
710: } else {
711: assertEquals((double) val, in.readDouble(), 0);
712: }
713: }
714:
715: public void testDouble() {
716:
717: doubleTest(0);
718: doubleTest(1);
719: doubleTest(-1);
720: doubleTest(1.0);
721: doubleTest(0.1);
722: doubleTest(-1.0);
723: doubleTest(-0.1);
724: doubleTest(Double.NaN);
725: doubleTest(Double.NEGATIVE_INFINITY);
726: doubleTest(Double.POSITIVE_INFINITY);
727: doubleTest(Short.MAX_VALUE);
728: doubleTest(Short.MIN_VALUE);
729: doubleTest(Integer.MAX_VALUE);
730: doubleTest(Integer.MIN_VALUE);
731: doubleTest(Long.MAX_VALUE);
732: doubleTest(Long.MIN_VALUE);
733: doubleTest(Float.MAX_VALUE);
734: doubleTest(Float.MIN_VALUE);
735: doubleTest(Double.MAX_VALUE - 1);
736: doubleTest(Double.MAX_VALUE);
737: doubleTest(Double.MAX_VALUE + 1);
738: doubleTest(Double.MIN_VALUE + 1);
739: doubleTest(Double.MIN_VALUE);
740: doubleTest(Double.MIN_VALUE - 1);
741: doubleTest(0x7F);
742: doubleTest(0xFF);
743: doubleTest(0x7FFF);
744: doubleTest(0xFFFF);
745: doubleTest(0x7FFFFFFF);
746: doubleTest(0xFFFFFFFF);
747: doubleTest(0x7FFFFFFFFFFFFFFFL);
748: doubleTest(0xFFFFFFFFFFFFFFFFL);
749:
750: out.reset();
751: out.writeDouble(0);
752: out.writeDouble(1);
753: out.writeDouble(-1);
754: assertEquals(3 * 8, out.size());
755: copyOutputToInput();
756: assertEquals(0, in.readDouble(), 0);
757: assertEquals(1, in.readDouble(), 0);
758: assertEquals(-1, in.readDouble(), 0);
759: assertEquals(0, in.available(), 0);
760: }
761:
762: private void sortedFloatTest(double val) {
763:
764: out.reset();
765: out.writeSortedFloat((float) val);
766: assertEquals(4, out.size());
767: copyOutputToInput();
768: if (Double.isNaN(val)) {
769: assertTrue(Float.isNaN(in.readSortedFloat()));
770: } else {
771: assertEquals((float) val, in.readSortedFloat(), 0);
772: }
773: }
774:
775: public void testSortedFloat() {
776:
777: sortedFloatTest(0);
778: sortedFloatTest(1);
779: sortedFloatTest(-1);
780: sortedFloatTest(1.0);
781: sortedFloatTest(0.1);
782: sortedFloatTest(-1.0);
783: sortedFloatTest(-0.1);
784: sortedFloatTest(Float.NaN);
785: sortedFloatTest(Float.NEGATIVE_INFINITY);
786: sortedFloatTest(Float.POSITIVE_INFINITY);
787: sortedFloatTest(Short.MAX_VALUE);
788: sortedFloatTest(Short.MIN_VALUE);
789: sortedFloatTest(Integer.MAX_VALUE);
790: sortedFloatTest(Integer.MIN_VALUE);
791: sortedFloatTest(Long.MAX_VALUE);
792: sortedFloatTest(Long.MIN_VALUE);
793: sortedFloatTest(Float.MAX_VALUE);
794: sortedFloatTest(Float.MAX_VALUE + 1);
795: sortedFloatTest(Float.MIN_VALUE + 1);
796: sortedFloatTest(Float.MIN_VALUE);
797: sortedFloatTest(Float.MIN_VALUE - 1);
798: sortedFloatTest(0x7F);
799: sortedFloatTest(0xFF);
800: sortedFloatTest(0x7FFF);
801: sortedFloatTest(0xFFFF);
802: sortedFloatTest(0x7FFFFFFF);
803: sortedFloatTest(0xFFFFFFFF);
804: sortedFloatTest(0x7FFFFFFFFFFFFFFFL);
805: sortedFloatTest(0xFFFFFFFFFFFFFFFFL);
806:
807: out.reset();
808: out.writeSortedFloat(0);
809: out.writeSortedFloat(1);
810: out.writeSortedFloat(-1);
811: assertEquals(3 * 4, out.size());
812: copyOutputToInput();
813: assertEquals(0, in.readSortedFloat(), 0);
814: assertEquals(1, in.readSortedFloat(), 0);
815: assertEquals(-1, in.readSortedFloat(), 0);
816: assertEquals(0, in.available(), 0);
817: }
818:
819: private void sortedDoubleTest(double val) {
820:
821: out.reset();
822: out.writeSortedDouble((double) val);
823: assertEquals(8, out.size());
824: copyOutputToInput();
825: if (Double.isNaN(val)) {
826: assertTrue(Double.isNaN(in.readSortedDouble()));
827: } else {
828: assertEquals((double) val, in.readSortedDouble(), 0);
829: }
830: }
831:
832: public void testSortedDouble() {
833:
834: sortedDoubleTest(0);
835: sortedDoubleTest(1);
836: sortedDoubleTest(-1);
837: sortedDoubleTest(1.0);
838: sortedDoubleTest(0.1);
839: sortedDoubleTest(-1.0);
840: sortedDoubleTest(-0.1);
841: sortedDoubleTest(Double.NaN);
842: sortedDoubleTest(Double.NEGATIVE_INFINITY);
843: sortedDoubleTest(Double.POSITIVE_INFINITY);
844: sortedDoubleTest(Short.MAX_VALUE);
845: sortedDoubleTest(Short.MIN_VALUE);
846: sortedDoubleTest(Integer.MAX_VALUE);
847: sortedDoubleTest(Integer.MIN_VALUE);
848: sortedDoubleTest(Long.MAX_VALUE);
849: sortedDoubleTest(Long.MIN_VALUE);
850: sortedDoubleTest(Float.MAX_VALUE);
851: sortedDoubleTest(Float.MIN_VALUE);
852: sortedDoubleTest(Double.MAX_VALUE - 1);
853: sortedDoubleTest(Double.MAX_VALUE);
854: sortedDoubleTest(Double.MAX_VALUE + 1);
855: sortedDoubleTest(Double.MIN_VALUE + 1);
856: sortedDoubleTest(Double.MIN_VALUE);
857: sortedDoubleTest(Double.MIN_VALUE - 1);
858: sortedDoubleTest(0x7F);
859: sortedDoubleTest(0xFF);
860: sortedDoubleTest(0x7FFF);
861: sortedDoubleTest(0xFFFF);
862: sortedDoubleTest(0x7FFFFFFF);
863: sortedDoubleTest(0xFFFFFFFF);
864: sortedDoubleTest(0x7FFFFFFFFFFFFFFFL);
865: sortedDoubleTest(0xFFFFFFFFFFFFFFFFL);
866:
867: out.reset();
868: out.writeSortedDouble(0);
869: out.writeSortedDouble(1);
870: out.writeSortedDouble(-1);
871: assertEquals(3 * 8, out.size());
872: copyOutputToInput();
873: assertEquals(0, in.readSortedDouble(), 0);
874: assertEquals(1, in.readSortedDouble(), 0);
875: assertEquals(-1, in.readSortedDouble(), 0);
876: assertEquals(0, in.available(), 0);
877: }
878: }
|