001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.nio.tests.java.nio;
019:
020: import java.nio.BufferOverflowException;
021: import java.nio.BufferUnderflowException;
022: import java.nio.ByteBuffer;
023: import java.nio.ByteOrder;
024: import java.nio.DoubleBuffer;
025: import java.nio.InvalidMarkException;
026:
027: /**
028: * Tests java.nio.DoubleBuffer
029: */
030: public class DoubleBufferTest extends AbstractBufferTest {
031:
032: protected static final int SMALL_TEST_LENGTH = 5;
033:
034: protected static final int BUFFER_LENGTH = 20;
035:
036: protected DoubleBuffer buf;
037:
038: protected void setUp() throws Exception {
039: buf = DoubleBuffer.allocate(BUFFER_LENGTH);
040: loadTestData1(buf);
041: baseBuf = buf;
042: }
043:
044: protected void tearDown() throws Exception {
045: buf = null;
046: baseBuf = null;
047: }
048:
049: /*
050: * Test with bit sequences that represent the IEEE754 doubles Positive
051: * infinity, negative infinity, and NaN.
052: */
053: public void testNaNs() {
054: long[] nans = new long[] { 0x7ff0000000000000L,
055: 0xfff0000000000000L, 0x7ff8000000000000L };
056: for (int i = 0; i < nans.length; i++) {
057: long longBitsIn = nans[i];
058: double dbl = Double.longBitsToDouble(longBitsIn);
059: long longBitsOut = Double.doubleToRawLongBits(dbl);
060: // Sanity check
061: assertTrue(longBitsIn == longBitsOut);
062:
063: // Store the double and retrieve it
064: ByteBuffer buffer = ByteBuffer.allocate(8);
065: buffer.putDouble(dbl);
066: double bufDoubleOut = buffer.getDouble(0);
067:
068: // Check the bits sequence was not normalized
069: long bufLongOut = Double.doubleToRawLongBits(bufDoubleOut);
070: assertTrue(longBitsIn == bufLongOut);
071: }
072: }
073:
074: public void testArray() {
075: double array[] = buf.array();
076: assertContentEquals(buf, array, buf.arrayOffset(), buf
077: .capacity());
078:
079: loadTestData1(array, buf.arrayOffset(), buf.capacity());
080: assertContentEquals(buf, array, buf.arrayOffset(), buf
081: .capacity());
082:
083: loadTestData2(array, buf.arrayOffset(), buf.capacity());
084: assertContentEquals(buf, array, buf.arrayOffset(), buf
085: .capacity());
086:
087: loadTestData1(buf);
088: assertContentEquals(buf, array, buf.arrayOffset(), buf
089: .capacity());
090:
091: loadTestData2(buf);
092: assertContentEquals(buf, array, buf.arrayOffset(), buf
093: .capacity());
094: }
095:
096: public void testArrayOffset() {
097: double array[] = buf.array();
098: assertContentEquals(buf, array, buf.arrayOffset(), buf
099: .capacity());
100:
101: loadTestData1(array, buf.arrayOffset(), buf.capacity());
102: assertContentEquals(buf, array, buf.arrayOffset(), buf
103: .capacity());
104:
105: loadTestData2(array, buf.arrayOffset(), buf.capacity());
106: assertContentEquals(buf, array, buf.arrayOffset(), buf
107: .capacity());
108:
109: loadTestData1(buf);
110: assertContentEquals(buf, array, buf.arrayOffset(), buf
111: .capacity());
112:
113: loadTestData2(buf);
114: assertContentEquals(buf, array, buf.arrayOffset(), buf
115: .capacity());
116: }
117:
118: public void testAsReadOnlyBuffer() {
119: buf.clear();
120: buf.mark();
121: buf.position(buf.limit());
122:
123: // readonly's contents should be the same as buf
124: DoubleBuffer readonly = buf.asReadOnlyBuffer();
125: assertNotSame(buf, readonly);
126: assertTrue(readonly.isReadOnly());
127: assertEquals(buf.position(), readonly.position());
128: assertEquals(buf.limit(), readonly.limit());
129: assertEquals(buf.isDirect(), readonly.isDirect());
130: assertEquals(buf.order(), readonly.order());
131: assertContentEquals(buf, readonly);
132:
133: // readonly's position, mark, and limit should be independent to buf
134: readonly.reset();
135: assertEquals(readonly.position(), 0);
136: readonly.clear();
137: assertEquals(buf.position(), buf.limit());
138: buf.reset();
139: assertEquals(buf.position(), 0);
140: }
141:
142: public void testCompact() {
143: // case: buffer is full
144: buf.clear();
145: buf.mark();
146: loadTestData1(buf);
147: DoubleBuffer ret = buf.compact();
148: assertSame(ret, buf);
149: assertEquals(buf.position(), buf.capacity());
150: assertEquals(buf.limit(), buf.capacity());
151: assertContentLikeTestData1(buf, 0, 0.0, buf.capacity());
152: try {
153: buf.reset();
154: fail("Should throw Exception"); //$NON-NLS-1$
155: } catch (InvalidMarkException e) {
156: // expected
157: }
158:
159: // case: buffer is empty
160: buf.position(0);
161: buf.limit(0);
162: buf.mark();
163: ret = buf.compact();
164: assertSame(ret, buf);
165: assertEquals(buf.position(), 0);
166: assertEquals(buf.limit(), buf.capacity());
167: assertContentLikeTestData1(buf, 0, 0.0, buf.capacity());
168: try {
169: buf.reset();
170: fail("Should throw Exception"); //$NON-NLS-1$
171: } catch (InvalidMarkException e) {
172: // expected
173: }
174:
175: // case: normal
176: assertTrue(buf.capacity() > 5);
177: buf.position(1);
178: buf.limit(5);
179: buf.mark();
180: ret = buf.compact();
181: assertSame(ret, buf);
182: assertEquals(buf.position(), 4);
183: assertEquals(buf.limit(), buf.capacity());
184: assertContentLikeTestData1(buf, 0, 1.0, 4);
185: try {
186: buf.reset();
187: fail("Should throw Exception"); //$NON-NLS-1$
188: } catch (InvalidMarkException e) {
189: // expected
190: }
191: }
192:
193: public void testCompareTo() {
194: DoubleBuffer other = DoubleBuffer.allocate(buf.capacity());
195: loadTestData1(other);
196: assertEquals(0, buf.compareTo(other));
197: assertEquals(0, other.compareTo(buf));
198: buf.position(1);
199: assertTrue(buf.compareTo(other) > 0);
200: assertTrue(other.compareTo(buf) < 0);
201: other.position(2);
202: assertTrue(buf.compareTo(other) < 0);
203: assertTrue(other.compareTo(buf) > 0);
204: buf.position(2);
205: other.limit(5);
206: assertTrue(buf.compareTo(other) > 0);
207: assertTrue(other.compareTo(buf) < 0);
208:
209: DoubleBuffer dbuffer1 = DoubleBuffer
210: .wrap(new double[] { Double.NaN });
211: DoubleBuffer dbuffer2 = DoubleBuffer
212: .wrap(new double[] { Double.NaN });
213: DoubleBuffer dbuffer3 = DoubleBuffer.wrap(new double[] { 42d });
214:
215: assertEquals("Failed equal comparison with NaN entry", 0,
216: dbuffer1.compareTo(dbuffer2));
217: assertEquals("Failed greater than comparison with NaN entry",
218: 1, dbuffer3.compareTo(dbuffer1));
219: assertEquals("Failed greater than comparison with NaN entry",
220: 1, dbuffer1.compareTo(dbuffer3));
221: }
222:
223: public void testDuplicate() {
224: buf.clear();
225: buf.mark();
226: buf.position(buf.limit());
227:
228: // duplicate's contents should be the same as buf
229: DoubleBuffer duplicate = buf.duplicate();
230: assertNotSame(buf, duplicate);
231: assertEquals(buf.position(), duplicate.position());
232: assertEquals(buf.limit(), duplicate.limit());
233: assertEquals(buf.isReadOnly(), duplicate.isReadOnly());
234: assertEquals(buf.isDirect(), duplicate.isDirect());
235: assertEquals(buf.order(), duplicate.order());
236: assertContentEquals(buf, duplicate);
237:
238: // duplicate's position, mark, and limit should be independent to buf
239: duplicate.reset();
240: assertEquals(duplicate.position(), 0);
241: duplicate.clear();
242: assertEquals(buf.position(), buf.limit());
243: buf.reset();
244: assertEquals(buf.position(), 0);
245:
246: // duplicate share the same content with buf
247: // FIXME
248: if (!duplicate.isReadOnly()) {
249: loadTestData1(buf);
250: assertContentEquals(buf, duplicate);
251: loadTestData2(duplicate);
252: assertContentEquals(buf, duplicate);
253: }
254: }
255:
256: public void testEquals() {
257: // equal to self
258: assertTrue(buf.equals(buf));
259: DoubleBuffer readonly = buf.asReadOnlyBuffer();
260: assertTrue(buf.equals(readonly));
261: DoubleBuffer duplicate = buf.duplicate();
262: assertTrue(buf.equals(duplicate));
263:
264: // always false, if type mismatch
265: assertFalse(buf.equals(Boolean.TRUE));
266:
267: assertTrue(buf.capacity() > 5);
268:
269: buf.limit(buf.capacity()).position(0);
270: readonly.limit(readonly.capacity()).position(1);
271: assertFalse(buf.equals(readonly));
272:
273: buf.limit(buf.capacity() - 1).position(0);
274: duplicate.limit(duplicate.capacity()).position(0);
275: assertFalse(buf.equals(duplicate));
276: }
277:
278: /*
279: * Class under test for double get()
280: */
281: public void testGet() {
282: buf.clear();
283: for (int i = 0; i < buf.capacity(); i++) {
284: assertEquals(buf.position(), i);
285: assertEquals(buf.get(), buf.get(i), 0.01);
286: }
287: try {
288: buf.get();
289: fail("Should throw Exception"); //$NON-NLS-1$
290: } catch (BufferUnderflowException e) {
291: // expected
292: }
293: }
294:
295: /*
296: * Class under test for java.nio.DoubleBuffer get(double[])
297: */
298: public void testGetdoubleArray() {
299: double array[] = new double[1];
300: buf.clear();
301: for (int i = 0; i < buf.capacity(); i++) {
302: assertEquals(buf.position(), i);
303: DoubleBuffer ret = buf.get(array);
304: assertEquals(array[0], buf.get(i), 0.01);
305: assertSame(ret, buf);
306: }
307: try {
308: buf.get(array);
309: fail("Should throw Exception"); //$NON-NLS-1$
310: } catch (BufferUnderflowException e) {
311: // expected
312: }
313: }
314:
315: /*
316: * Class under test for java.nio.DoubleBuffer get(double[], int, int)
317: */
318: public void testGetdoubleArrayintint() {
319: buf.clear();
320: double array[] = new double[buf.capacity()];
321:
322: try {
323: buf.get(new double[buf.capacity() + 1], 0,
324: buf.capacity() + 1);
325: fail("Should throw Exception"); //$NON-NLS-1$
326: } catch (BufferUnderflowException e) {
327: // expected
328: }
329: assertEquals(buf.position(), 0);
330: try {
331: buf.get(array, -1, array.length);
332: fail("Should throw Exception"); //$NON-NLS-1$
333: } catch (IndexOutOfBoundsException e) {
334: // expected
335: }
336: buf.get(array, array.length, 0);
337: try {
338: buf.get(array, array.length + 1, 1);
339: fail("Should throw Exception"); //$NON-NLS-1$
340: } catch (IndexOutOfBoundsException e) {
341: // expected
342: }
343: assertEquals(buf.position(), 0);
344: try {
345: buf.get(array, 2, -1);
346: fail("Should throw Exception"); //$NON-NLS-1$
347: } catch (IndexOutOfBoundsException e) {
348: // expected
349: }
350: try {
351: buf.get((double[]) null, 0, -1);
352: fail("Should throw Exception"); //$NON-NLS-1$
353: } catch (NullPointerException e) {
354: // expected
355: }
356: try {
357: buf.get(array, 2, array.length);
358: fail("Should throw Exception"); //$NON-NLS-1$
359: } catch (IndexOutOfBoundsException e) {
360: // expected
361: }
362: try {
363: buf.get(array, 1, Integer.MAX_VALUE);
364: fail("Should throw Exception"); //$NON-NLS-1$
365: } catch (IndexOutOfBoundsException e) {
366: // expected
367: }
368: try {
369: buf.get(array, Integer.MAX_VALUE, 1);
370: fail("Should throw Exception"); //$NON-NLS-1$
371: } catch (IndexOutOfBoundsException e) {
372: // expected
373: }
374: assertEquals(buf.position(), 0);
375:
376: buf.clear();
377: DoubleBuffer ret = buf.get(array, 0, array.length);
378: assertEquals(buf.position(), buf.capacity());
379: assertContentEquals(buf, array, 0, array.length);
380: assertSame(ret, buf);
381: }
382:
383: /*
384: * Class under test for double get(int)
385: */
386: public void testGetint() {
387: buf.clear();
388: for (int i = 0; i < buf.capacity(); i++) {
389: assertEquals(buf.position(), i);
390: assertEquals(buf.get(), buf.get(i), 0.01);
391: }
392: try {
393: buf.get(-1);
394: fail("Should throw Exception"); //$NON-NLS-1$
395: } catch (IndexOutOfBoundsException e) {
396: // expected
397: }
398: try {
399: buf.get(buf.limit());
400: fail("Should throw Exception"); //$NON-NLS-1$
401: } catch (IndexOutOfBoundsException e) {
402: // expected
403: }
404: }
405:
406: public void testHasArray() {
407: assertTrue(buf.hasArray());
408: }
409:
410: public void testHashCode() {
411: buf.clear();
412: DoubleBuffer readonly = buf.asReadOnlyBuffer();
413: DoubleBuffer duplicate = buf.duplicate();
414: assertTrue(buf.hashCode() == readonly.hashCode());
415:
416: assertTrue(buf.capacity() > 5);
417: duplicate.position(buf.capacity() / 2);
418: assertTrue(buf.hashCode() != duplicate.hashCode());
419: }
420:
421: public void testIsDirect() {
422: assertFalse(buf.isDirect());
423: }
424:
425: public void testOrder() {
426: assertEquals(ByteOrder.nativeOrder(), buf.order());
427: }
428:
429: /*
430: * Class under test for java.nio.DoubleBuffer put(double)
431: */
432: public void testPutdouble() {
433:
434: buf.clear();
435: for (int i = 0; i < buf.capacity(); i++) {
436: assertEquals(buf.position(), i);
437: DoubleBuffer ret = buf.put((double) i);
438: assertEquals(buf.get(i), (double) i, 0.0);
439: assertSame(ret, buf);
440: }
441: try {
442: buf.put(0);
443: fail("Should throw Exception"); //$NON-NLS-1$
444: } catch (BufferOverflowException e) {
445: // expected
446: }
447: }
448:
449: /*
450: * Class under test for java.nio.DoubleBuffer put(double[])
451: */
452: public void testPutdoubleArray() {
453: double array[] = new double[1];
454:
455: buf.clear();
456: for (int i = 0; i < buf.capacity(); i++) {
457: assertEquals(buf.position(), i);
458: array[0] = (double) i;
459: DoubleBuffer ret = buf.put(array);
460: assertEquals(buf.get(i), (double) i, 0.0);
461: assertSame(ret, buf);
462: }
463: try {
464: buf.put(array);
465: fail("Should throw Exception"); //$NON-NLS-1$
466: } catch (BufferOverflowException e) {
467: // expected
468: }
469: }
470:
471: /*
472: * Class under test for java.nio.DoubleBuffer put(double[], int, int)
473: */
474: public void testPutdoubleArrayintint() {
475: buf.clear();
476: double array[] = new double[buf.capacity()];
477:
478: try {
479: buf.put(new double[buf.capacity() + 1], 0,
480: buf.capacity() + 1);
481: fail("Should throw Exception"); //$NON-NLS-1$
482: } catch (BufferOverflowException e) {
483: // expected
484: }
485: assertEquals(buf.position(), 0);
486: try {
487: buf.put(array, -1, array.length);
488: fail("Should throw Exception"); //$NON-NLS-1$
489: } catch (IndexOutOfBoundsException e) {
490: // expected
491: }
492: try {
493: buf.put(array, array.length + 1, 0);
494: fail("Should throw Exception"); //$NON-NLS-1$
495: } catch (IndexOutOfBoundsException e) {
496: // expected
497: }
498: buf.put(array, array.length, 0);
499: assertEquals(buf.position(), 0);
500: try {
501: buf.put(array, 0, -1);
502: fail("Should throw Exception"); //$NON-NLS-1$
503: } catch (IndexOutOfBoundsException e) {
504: // expected
505: }
506: try {
507: buf.put((double[]) null, 0, -1);
508: fail("Should throw Exception"); //$NON-NLS-1$
509: } catch (NullPointerException e) {
510: // expected
511: }
512: try {
513: buf.put(array, 2, array.length);
514: fail("Should throw Exception"); //$NON-NLS-1$
515: } catch (IndexOutOfBoundsException e) {
516: // expected
517: }
518: try {
519: buf.put(array, Integer.MAX_VALUE, 1);
520: fail("Should throw Exception"); //$NON-NLS-1$
521: } catch (IndexOutOfBoundsException e) {
522: // expected
523: }
524: try {
525: buf.put(array, 1, Integer.MAX_VALUE);
526: fail("Should throw Exception"); //$NON-NLS-1$
527: } catch (IndexOutOfBoundsException e) {
528: // expected
529: }
530: assertEquals(buf.position(), 0);
531:
532: loadTestData2(array, 0, array.length);
533: DoubleBuffer ret = buf.put(array, 0, array.length);
534: assertEquals(buf.position(), buf.capacity());
535: assertContentEquals(buf, array, 0, array.length);
536: assertSame(ret, buf);
537: }
538:
539: /*
540: * Class under test for java.nio.DoubleBuffer put(java.nio.DoubleBuffer)
541: */
542: public void testPutDoubleBuffer() {
543: DoubleBuffer other = DoubleBuffer.allocate(buf.capacity());
544:
545: try {
546: buf.put(buf);
547: fail("Should throw Exception"); //$NON-NLS-1$
548: } catch (IllegalArgumentException e) {
549: // expected
550: }
551: try {
552: buf.put(DoubleBuffer.allocate(buf.capacity() + 1));
553: fail("Should throw Exception"); //$NON-NLS-1$
554: } catch (BufferOverflowException e) {
555: // expected
556: }
557:
558: loadTestData2(other);
559: other.clear();
560: buf.clear();
561: DoubleBuffer ret = buf.put(other);
562: assertEquals(other.position(), other.capacity());
563: assertEquals(buf.position(), buf.capacity());
564: assertContentEquals(other, buf);
565: assertSame(ret, buf);
566: }
567:
568: /*
569: * Class under test for java.nio.DoubleBuffer put(int, double)
570: */
571: public void testPutintdouble() {
572: buf.clear();
573: for (int i = 0; i < buf.capacity(); i++) {
574: assertEquals(buf.position(), 0);
575: DoubleBuffer ret = buf.put(i, (double) i);
576: assertEquals(buf.get(i), (double) i, 0.0);
577: assertSame(ret, buf);
578: }
579: try {
580: buf.put(-1, 0);
581: fail("Should throw Exception"); //$NON-NLS-1$
582: } catch (IndexOutOfBoundsException e) {
583: // expected
584: }
585: try {
586: buf.put(buf.limit(), 0);
587: fail("Should throw Exception"); //$NON-NLS-1$
588: } catch (IndexOutOfBoundsException e) {
589: // expected
590: }
591: }
592:
593: public void testSlice() {
594: assertTrue(buf.capacity() > 5);
595: buf.position(1);
596: buf.limit(buf.capacity() - 1);
597:
598: DoubleBuffer slice = buf.slice();
599: assertEquals(buf.isReadOnly(), slice.isReadOnly());
600: assertEquals(buf.isDirect(), slice.isDirect());
601: assertEquals(buf.order(), slice.order());
602: assertEquals(slice.position(), 0);
603: assertEquals(slice.limit(), buf.remaining());
604: assertEquals(slice.capacity(), buf.remaining());
605: try {
606: slice.reset();
607: fail("Should throw Exception"); //$NON-NLS-1$
608: } catch (InvalidMarkException e) {
609: // expected
610: }
611:
612: // slice share the same content with buf
613: // FIXME:
614: if (!slice.isReadOnly()) {
615: loadTestData1(slice);
616: assertContentLikeTestData1(buf, 1, 0, slice.capacity());
617: buf.put(2, 500);
618: assertEquals(slice.get(1), 500, 0.0);
619: }
620: }
621:
622: public void testToString() {
623: String str = buf.toString();
624: assertTrue(str.indexOf("Double") >= 0
625: || str.indexOf("double") >= 0);
626: assertTrue(str.indexOf("" + buf.position()) >= 0);
627: assertTrue(str.indexOf("" + buf.limit()) >= 0);
628: assertTrue(str.indexOf("" + buf.capacity()) >= 0);
629: }
630:
631: void loadTestData1(double array[], int offset, int length) {
632: for (int i = 0; i < length; i++) {
633: array[offset + i] = (double) i;
634: }
635: }
636:
637: void loadTestData2(double array[], int offset, int length) {
638: for (int i = 0; i < length; i++) {
639: array[offset + i] = (double) length - i;
640: }
641: }
642:
643: void loadTestData1(DoubleBuffer buf) {
644: buf.clear();
645: for (int i = 0; i < buf.capacity(); i++) {
646: buf.put(i, (double) i);
647: }
648: }
649:
650: void loadTestData2(DoubleBuffer buf) {
651: buf.clear();
652: for (int i = 0; i < buf.capacity(); i++) {
653: buf.put(i, (double) buf.capacity() - i);
654: }
655: }
656:
657: private void assertContentEquals(DoubleBuffer buf, double array[],
658: int offset, int length) {
659: for (int i = 0; i < length; i++) {
660: assertEquals(buf.get(i), array[offset + i], 0.01);
661: }
662: }
663:
664: private void assertContentEquals(DoubleBuffer buf,
665: DoubleBuffer other) {
666: assertEquals(buf.capacity(), other.capacity());
667: for (int i = 0; i < buf.capacity(); i++) {
668: assertEquals(buf.get(i), other.get(i), 0.01);
669: }
670: }
671:
672: private void assertContentLikeTestData1(DoubleBuffer buf,
673: int startIndex, double startValue, int length) {
674: double value = startValue;
675: for (int i = 0; i < length; i++) {
676: assertEquals(buf.get(startIndex + i), value, 0.01);
677: value = value + 1.0;
678: }
679: }
680: }
|