OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / nio / tests / java / nio / DoubleBufferTest.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package org.apache.harmony.nio.tests.java.nio;
19
20 import dalvik.annotation.TestLevel;
21 import dalvik.annotation.TestTargetNew;
22 import dalvik.annotation.TestTargetClass;
23 import dalvik.annotation.AndroidOnly;
24
25 import java.nio.BufferOverflowException;
26 import java.nio.BufferUnderflowException;
27 import java.nio.ByteBuffer;
28 import java.nio.ByteOrder;
29 import java.nio.DoubleBuffer;
30 import java.nio.InvalidMarkException;
31
32 /**
33  * Tests java.nio.DoubleBuffer
34  */
35 @TestTargetClass(java.nio.DoubleBuffer.class)
36 public abstract class DoubleBufferTest extends AbstractBufferTest {
37
38     protected static final int SMALL_TEST_LENGTH = 5;
39
40     protected static final int BUFFER_LENGTH = 20;
41
42     protected DoubleBuffer buf;
43
44     protected void setUp() throws Exception {
45         capacity = BUFFER_LENGTH;
46         buf = DoubleBuffer.allocate(BUFFER_LENGTH);
47         loadTestData1(buf);
48         baseBuf = buf;
49     }
50
51     protected void tearDown() throws Exception {
52         buf = null;
53         baseBuf = null;
54     }
55
56     /*
57      * test for method static DoubleBuffer allocate(int capacity) test covers
58      * following usecases: 1. case for check DoubleBuffer testBuf properties 2.
59      * case expected IllegalArgumentException
60      */
61     @TestTargetNew(
62         level = TestLevel.PARTIAL_COMPLETE,
63         notes = "",
64         method = "allocate",
65         args = {int.class}
66     )
67     public void test_AllocateI() {
68         // case: DoubleBuffer testBuf properties is satisfy the conditions
69         // specification
70         DoubleBuffer testBuf = DoubleBuffer.allocate(20);
71         assertEquals(0, testBuf.position());
72         assertNotNull(testBuf.array());
73         assertEquals(0, testBuf.arrayOffset());
74         assertEquals(20, testBuf.limit());
75         assertEquals(20, testBuf.capacity());
76
77         testBuf = DoubleBuffer.allocate(0);
78         assertEquals(0, testBuf.position());
79         assertNotNull(testBuf.array());
80         assertEquals(0, testBuf.arrayOffset());
81         assertEquals(0, testBuf.limit());
82         assertEquals(0, testBuf.capacity());
83
84         // case: expected IllegalArgumentException
85         try {
86             testBuf = DoubleBuffer.allocate(-20);
87             fail("allocate method does not throws expected exception");
88         } catch (IllegalArgumentException e) {
89             // expected
90         }
91     }
92
93     /*
94      * Test with bit sequences that represent the IEEE754 doubles Positive
95      * infinity, negative infinity, and NaN.
96      */
97     @TestTargetNew(
98         level = TestLevel.PARTIAL_COMPLETE,
99         notes = "Verifies boundary values.",
100         method = "put",
101         args = {double.class}
102     )
103     public void testNaNs() {
104         long[] nans = new long[] { 0x7ff0000000000000L, 0xfff0000000000000L,
105                 0x7ff8000000000000L };
106         for (int i = 0; i < nans.length; i++) {
107             long longBitsIn = nans[i];
108             double dbl = Double.longBitsToDouble(longBitsIn);
109             long longBitsOut = Double.doubleToRawLongBits(dbl);
110             // Sanity check
111             assertTrue(longBitsIn == longBitsOut);
112
113             // Store the double and retrieve it
114             ByteBuffer buffer = ByteBuffer.allocate(8);
115             buffer.putDouble(dbl);
116             double bufDoubleOut = buffer.getDouble(0);
117
118             // Check the bits sequence was not normalized
119             long bufLongOut = Double.doubleToRawLongBits(bufDoubleOut);
120             assertTrue(longBitsIn == bufLongOut);
121         }
122     }
123
124     @TestTargetNew(
125         level = TestLevel.PARTIAL_COMPLETE,
126         notes = "",
127         method = "array",
128         args = {}
129     )
130     public void testArray() {
131         double array[] = buf.array();
132         assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
133
134         loadTestData1(array, buf.arrayOffset(), buf.capacity());
135         assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
136
137         loadTestData2(array, buf.arrayOffset(), buf.capacity());
138         assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
139
140         loadTestData1(buf);
141         assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
142
143         loadTestData2(buf);
144         assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
145     }
146
147     @TestTargetNew(
148         level = TestLevel.PARTIAL_COMPLETE,
149         notes = "",
150         method = "arrayOffset",
151         args = {}
152     )
153     public void testArrayOffset() {
154         double array[] = buf.array();
155         for(int i = 0; i < buf.capacity(); i++) {
156             array[i] = i;
157         }
158         int offset = buf.arrayOffset();
159         assertContentEquals(buf, array, offset, buf.capacity());
160
161         DoubleBuffer wrapped = DoubleBuffer.wrap(array, 3, array.length - 3);
162
163         loadTestData1(array, wrapped.arrayOffset(), wrapped.capacity());
164         assertContentEquals(buf, array, offset, buf.capacity());
165
166         loadTestData2(array, wrapped.arrayOffset(), wrapped.capacity());
167         assertContentEquals(buf, array, offset, buf.capacity());
168     }
169
170     @TestTargetNew(
171         level = TestLevel.PARTIAL_COMPLETE,
172         notes = "",
173         method = "asReadOnlyBuffer",
174         args = {}
175     )
176     public void testAsReadOnlyBuffer() {
177         buf.clear();
178         buf.mark();
179         buf.position(buf.limit());
180
181         // readonly's contents should be the same as buf
182         DoubleBuffer readonly = buf.asReadOnlyBuffer();
183         assertNotSame(buf, readonly);
184         assertTrue(readonly.isReadOnly());
185         assertEquals(buf.position(), readonly.position());
186         assertEquals(buf.limit(), readonly.limit());
187         assertEquals(buf.isDirect(), readonly.isDirect());
188         assertEquals(buf.order(), readonly.order());
189         assertContentEquals(buf, readonly);
190
191         // readonly's position, mark, and limit should be independent to buf
192         readonly.reset();
193         assertEquals(readonly.position(), 0);
194         readonly.clear();
195         assertEquals(buf.position(), buf.limit());
196         buf.reset();
197         assertEquals(buf.position(), 0);
198
199         // BEGIN android-added
200         // copied from a newer version of Harmony
201         DoubleBuffer dbuffer1 = DoubleBuffer.wrap(new double[] { Double.NaN });
202         DoubleBuffer dbuffer2 = DoubleBuffer.wrap(new double[] { Double.NaN });
203         DoubleBuffer dbuffer3 = DoubleBuffer.wrap(new double[] { 42d });
204
205         assertEquals("Failed equal comparison with NaN entry", 0, dbuffer1
206                 .compareTo(dbuffer2));
207         assertEquals("Failed greater than comparison with NaN entry", 1, dbuffer3
208                 .compareTo(dbuffer1));
209         assertEquals("Failed greater than comparison with NaN entry", 1, dbuffer1
210                 .compareTo(dbuffer3));
211         // END android-added
212     }
213
214     @TestTargetNew(
215         level = TestLevel.PARTIAL_COMPLETE,
216         notes = "",
217         method = "compact",
218         args = {}
219     )
220     @AndroidOnly("Fails on RI. See comment below")
221     public void testCompact() {
222         // case: buffer is full
223         buf.clear();
224         buf.mark();
225         loadTestData1(buf);
226         DoubleBuffer ret = buf.compact();
227         assertSame(ret, buf);
228         assertEquals(buf.position(), buf.capacity());
229         assertEquals(buf.limit(), buf.capacity());
230         assertContentLikeTestData1(buf, 0, 0.0, buf.capacity());
231         try {
232             buf.reset();
233             fail("Should throw Exception");
234         } catch (InvalidMarkException e) {
235             // expected
236         }
237
238         // case: buffer is empty
239         buf.position(0);
240         buf.limit(0);
241         buf.mark();
242         ret = buf.compact();
243         assertSame(ret, buf);
244         assertEquals(buf.position(), 0);
245         assertEquals(buf.limit(), buf.capacity());
246         assertContentLikeTestData1(buf, 0, 0.0, buf.capacity());
247         try {
248             // Fails on RI. Spec doesn't specify the behavior if
249             // actually nothing to be done by compact(). So RI doesn't reset
250             // mark position
251             buf.reset();
252             fail("Should throw Exception");
253         } catch (InvalidMarkException e) {
254             // expected
255         }
256
257         // case: normal
258         assertTrue(buf.capacity() > 5);
259         buf.position(1);
260         buf.limit(5);
261         buf.mark();
262         ret = buf.compact();
263         assertSame(ret, buf);
264         assertEquals(buf.position(), 4);
265         assertEquals(buf.limit(), buf.capacity());
266         assertContentLikeTestData1(buf, 0, 1.0, 4);
267         try {
268             buf.reset();
269             fail("Should throw Exception");
270         } catch (InvalidMarkException e) {
271             // expected
272         }
273     }
274
275     @TestTargetNew(
276         level = TestLevel.PARTIAL_COMPLETE,
277         notes = "",
278         method = "compareTo",
279         args = {java.nio.DoubleBuffer.class}
280     )
281     public void testCompareTo() {
282         DoubleBuffer other = DoubleBuffer.allocate(buf.capacity());
283         loadTestData1(other);
284         assertEquals(0, buf.compareTo(other));
285         assertEquals(0, other.compareTo(buf));
286         buf.position(1);
287         assertTrue(buf.compareTo(other) > 0);
288         assertTrue(other.compareTo(buf) < 0);
289         other.position(2);
290         assertTrue(buf.compareTo(other) < 0);
291         assertTrue(other.compareTo(buf) > 0);
292         buf.position(2);
293         other.limit(5);
294         assertTrue(buf.compareTo(other) > 0);
295         assertTrue(other.compareTo(buf) < 0);
296
297         DoubleBuffer dbuffer1 = DoubleBuffer.wrap(new double[] { Double.NaN });
298         DoubleBuffer dbuffer2 = DoubleBuffer.wrap(new double[] { Double.NaN });
299         DoubleBuffer dbuffer3 = DoubleBuffer.wrap(new double[] { 42d });
300
301         assertEquals("Failed equal comparison with NaN entry", 0, dbuffer1
302                 .compareTo(dbuffer2));
303         assertEquals("Failed greater than comparison with NaN entry", 1, dbuffer3
304                 .compareTo(dbuffer1));
305         assertEquals("Failed greater than comparison with NaN entry", 1, dbuffer1
306                 .compareTo(dbuffer3));
307     }
308
309     @TestTargetNew(
310         level = TestLevel.PARTIAL_COMPLETE,
311         notes = "",
312         method = "duplicate",
313         args = {}
314     )
315     public void testDuplicate() {
316         buf.clear();
317         buf.mark();
318         buf.position(buf.limit());
319
320         // duplicate's contents should be the same as buf
321         DoubleBuffer duplicate = buf.duplicate();
322         assertNotSame(buf, duplicate);
323         assertEquals(buf.position(), duplicate.position());
324         assertEquals(buf.limit(), duplicate.limit());
325         assertEquals(buf.isReadOnly(), duplicate.isReadOnly());
326         assertEquals(buf.isDirect(), duplicate.isDirect());
327         assertEquals(buf.order(), duplicate.order());
328         assertContentEquals(buf, duplicate);
329
330         // duplicate's position, mark, and limit should be independent to buf
331         duplicate.reset();
332         assertEquals(duplicate.position(), 0);
333         duplicate.clear();
334         assertEquals(buf.position(), buf.limit());
335         buf.reset();
336         assertEquals(buf.position(), 0);
337
338         // duplicate share the same content with buf
339         // FIXME
340         if (!duplicate.isReadOnly()) {
341             loadTestData1(buf);
342             assertContentEquals(buf, duplicate);
343             loadTestData2(duplicate);
344             assertContentEquals(buf, duplicate);
345         }
346     }
347
348     @TestTargetNew(
349         level = TestLevel.PARTIAL_COMPLETE,
350         notes = "",
351         method = "equals",
352         args = {java.lang.Object.class}
353     )
354     public void testEquals() {
355         // equal to self
356         assertTrue(buf.equals(buf));
357         DoubleBuffer readonly = buf.asReadOnlyBuffer();
358         assertTrue(buf.equals(readonly));
359         DoubleBuffer duplicate = buf.duplicate();
360         assertTrue(buf.equals(duplicate));
361
362         // always false, if type mismatch
363         assertFalse(buf.equals(Boolean.TRUE));
364
365         assertTrue(buf.capacity() > 5);
366
367         buf.limit(buf.capacity()).position(0);
368         readonly.limit(readonly.capacity()).position(1);
369         assertFalse(buf.equals(readonly));
370
371         buf.limit(buf.capacity() - 1).position(0);
372         duplicate.limit(duplicate.capacity()).position(0);
373         assertFalse(buf.equals(duplicate));
374     }
375
376     /*
377      * Class under test for double get()
378      */
379     @TestTargetNew(
380         level = TestLevel.PARTIAL_COMPLETE,
381         notes = "",
382         method = "get",
383         args = {}
384     )
385     public void testGet() {
386         buf.clear();
387         for (int i = 0; i < buf.capacity(); i++) {
388             assertEquals(buf.position(), i);
389             assertEquals(buf.get(), buf.get(i), 0.01);
390         }
391         try {
392             buf.get();
393             fail("Should throw Exception");
394         } catch (BufferUnderflowException e) {
395             // expected
396         }
397     }
398
399     /*
400      * Class under test for java.nio.DoubleBuffer get(double[])
401      */
402     @TestTargetNew(
403         level = TestLevel.PARTIAL_COMPLETE,
404         notes = "",
405         method = "get",
406         args = {double[].class}
407     )
408     public void testGetdoubleArray() {
409         double array[] = new double[1];
410         buf.clear();
411         for (int i = 0; i < buf.capacity(); i++) {
412             assertEquals(buf.position(), i);
413             DoubleBuffer ret = buf.get(array);
414             assertEquals(array[0], buf.get(i), 0.01);
415             assertSame(ret, buf);
416         }
417
418         buf.get(new double[0]);
419
420         try {
421             buf.get(array);
422             fail("Should throw Exception");
423         } catch (BufferUnderflowException e) {
424             // expected
425         }
426
427         try {
428             buf.get((double[])null);
429             fail("Should throw Exception");
430         } catch (NullPointerException e) {
431             // expected
432         }
433     }
434
435     /*
436      * Class under test for java.nio.DoubleBuffer get(double[], int, int)
437      */
438     @TestTargetNew(
439         level = TestLevel.PARTIAL_COMPLETE,
440         notes = "",
441         method = "get",
442         args = {double[].class, int.class, int.class}
443     )
444     public void testGetdoubleArrayintint() {
445         buf.clear();
446         double array[] = new double[buf.capacity()];
447
448         try {
449             buf.get(new double[buf.capacity() + 1], 0, buf.capacity() + 1);
450             fail("Should throw Exception");
451         } catch (BufferUnderflowException e) {
452             // expected
453         }
454         assertEquals(buf.position(), 0);
455         try {
456             buf.get(array, -1, array.length);
457             fail("Should throw Exception");
458         } catch (IndexOutOfBoundsException e) {
459             // expected
460         }
461         buf.get(array, array.length, 0);
462         try {
463             buf.get(array, array.length + 1, 1);
464             fail("Should throw Exception");
465         } catch (IndexOutOfBoundsException e) {
466             // expected
467         }
468         assertEquals(buf.position(), 0);
469         try {
470             buf.get(array, 2, -1);
471             fail("Should throw Exception");
472         } catch (IndexOutOfBoundsException e) {
473             // expected
474         }
475         try {
476             buf.get((double[])null, 0, -1);
477             fail("Should throw Exception");
478         } catch (NullPointerException e) {
479             // expected
480         }
481         try {
482             buf.get(array, 2, array.length);
483             fail("Should throw Exception");
484         } catch (IndexOutOfBoundsException e) {
485             // expected
486         }
487         try {
488             buf.get(array, 1, Integer.MAX_VALUE);
489             fail("Should throw Exception");
490         } catch (IndexOutOfBoundsException e) {
491             // expected
492         }
493         try {
494             buf.get(array, Integer.MAX_VALUE, 1);
495             fail("Should throw Exception");
496         } catch (IndexOutOfBoundsException e) {
497             // expected
498         }
499         assertEquals(buf.position(), 0);
500
501         buf.clear();
502         DoubleBuffer ret = buf.get(array, 0, array.length);
503         assertEquals(buf.position(), buf.capacity());
504         assertContentEquals(buf, array, 0, array.length);
505         assertSame(ret, buf);
506     }
507
508     /*
509      * Class under test for double get(int)
510      */
511     @TestTargetNew(
512         level = TestLevel.PARTIAL_COMPLETE,
513         notes = "",
514         method = "get",
515         args = {int.class}
516     )
517     public void testGetint() {
518         buf.clear();
519         for (int i = 0; i < buf.capacity(); i++) {
520             assertEquals(buf.position(), i);
521             assertEquals(buf.get(), buf.get(i), 0.01);
522         }
523         try {
524             buf.get(-1);
525             fail("Should throw Exception");
526         } catch (IndexOutOfBoundsException e) {
527             // expected
528         }
529         try {
530             buf.get(buf.limit());
531             fail("Should throw Exception");
532         } catch (IndexOutOfBoundsException e) {
533             // expected
534         }
535     }
536
537     @TestTargetNew(
538         level = TestLevel.PARTIAL_COMPLETE,
539         notes = "",
540         method = "hasArray",
541         args = {}
542     )
543     public void testHasArray() {
544         if (buf.hasArray()) {
545             assertNotNull(buf.array());
546         } else {
547             try {
548                 buf.array();
549                 fail("Should throw Exception");
550             } catch (UnsupportedOperationException e) {
551                 // expected
552                 // Note:can not tell when to catch
553                 // UnsupportedOperationException or
554                 // ReadOnlyBufferException, so catch all.
555             }
556         }
557     }
558
559     @TestTargetNew(
560         level = TestLevel.PARTIAL_COMPLETE,
561         notes = "",
562         method = "hashCode",
563         args = {}
564     )
565     public void testHashCode() {
566         buf.clear();
567         DoubleBuffer readonly = buf.asReadOnlyBuffer();
568         DoubleBuffer duplicate = buf.duplicate();
569         assertTrue(buf.hashCode() == readonly.hashCode());
570
571         assertTrue(buf.capacity() > 5);
572         duplicate.position(buf.capacity() / 2);
573         assertTrue(buf.hashCode() != duplicate.hashCode());
574     }
575
576     @TestTargetNew(
577         level = TestLevel.PARTIAL_COMPLETE,
578         notes = "Doesn't verify direct buffer.",
579         method = "isDirect",
580         args = {}
581     )
582     public void testIsDirect() {
583         assertFalse(buf.isDirect());
584     }
585
586     @TestTargetNew(
587         level = TestLevel.PARTIAL_COMPLETE,
588         notes = "Abstract method.",
589         method = "isReadOnly",
590         args = {}
591     )
592     public void testIsReadOnly() {
593         assertFalse(buf.isReadOnly());
594     }
595
596     @TestTargetNew(
597         level = TestLevel.PARTIAL_COMPLETE,
598         notes = "",
599         method = "order",
600         args = {}
601     )
602     public void testOrder() {
603         assertEquals(ByteOrder.nativeOrder(), buf.order());
604     }
605
606     /*
607      * Class under test for java.nio.DoubleBuffer put(double)
608      */
609     @TestTargetNew(
610         level = TestLevel.PARTIAL_COMPLETE,
611         notes = "Doesn't verify boundary values, and ReadOnlyBufferException.",
612         method = "put",
613         args = {double.class}
614     )
615     public void testPutdouble() {
616
617         buf.clear();
618         for (int i = 0; i < buf.capacity(); i++) {
619             assertEquals(buf.position(), i);
620             DoubleBuffer ret = buf.put((double) i);
621             assertEquals(buf.get(i), (double) i, 0.0);
622             assertSame(ret, buf);
623         }
624         try {
625             buf.put(0);
626             fail("Should throw Exception");
627         } catch (BufferOverflowException e) {
628             // expected
629         }
630     }
631
632     /*
633      * Class under test for java.nio.DoubleBuffer put(double[])
634      */
635     @TestTargetNew(
636         level = TestLevel.PARTIAL_COMPLETE,
637         notes = "Doesn't verify ReadOnlyBufferException.",
638         method = "put",
639         args = {double[].class}
640     )
641     public void testPutdoubleArray() {
642         double array[] = new double[1];
643
644         buf.clear();
645         for (int i = 0; i < buf.capacity(); i++) {
646             assertEquals(buf.position(), i);
647             array[0] = (double) i;
648             DoubleBuffer ret = buf.put(array);
649             assertEquals(buf.get(i), (double) i, 0.0);
650             assertSame(ret, buf);
651         }
652         try {
653             buf.put(array);
654             fail("Should throw Exception");
655         } catch (BufferOverflowException e) {
656             // expected
657         }
658     }
659
660     /*
661      * Class under test for java.nio.DoubleBuffer put(double[], int, int)
662      */
663     @TestTargetNew(
664         level = TestLevel.PARTIAL_COMPLETE,
665         notes = "Doesn't verify ReadOnlyBufferException.",
666         method = "put",
667         args = {double[].class, int.class, int.class}
668     )
669     public void testPutdoubleArrayintint() {
670         buf.clear();
671         double array[] = new double[buf.capacity()];
672
673         try {
674             buf.put(new double[buf.capacity() + 1], 0, buf.capacity() + 1);
675             fail("Should throw Exception");
676         } catch (BufferOverflowException e) {
677             // expected
678         }
679         assertEquals(buf.position(), 0);
680         try {
681             buf.put(array, -1, array.length);
682             fail("Should throw Exception");
683         } catch (IndexOutOfBoundsException e) {
684             // expected
685         }
686         try {
687             buf.put(array, array.length + 1, 0);
688             fail("Should throw Exception");
689         } catch (IndexOutOfBoundsException e) {
690             // expected
691         }
692         buf.put(array, array.length, 0);
693         assertEquals(buf.position(), 0);
694         try {
695             buf.put(array, 0, -1);
696             fail("Should throw Exception");
697         } catch (IndexOutOfBoundsException e) {
698             // expected
699         }
700         try {
701             buf.put((double[])null, 0, -1);
702             fail("Should throw Exception");
703         } catch (NullPointerException e) {
704             // expected
705         }
706         try {
707             buf.put(array, 2, array.length);
708             fail("Should throw Exception");
709         } catch (IndexOutOfBoundsException e) {
710             // expected
711         }
712         try {
713             buf.put(array, Integer.MAX_VALUE, 1);
714             fail("Should throw Exception");
715         } catch (IndexOutOfBoundsException e) {
716             // expected
717         }
718         try {
719             buf.put(array, 1, Integer.MAX_VALUE);
720             fail("Should throw Exception");
721         } catch (IndexOutOfBoundsException e) {
722             // expected
723         }
724         assertEquals(buf.position(), 0);
725
726         loadTestData2(array, 0, array.length);
727         DoubleBuffer ret = buf.put(array, 0, array.length);
728         assertEquals(buf.position(), buf.capacity());
729         assertContentEquals(buf, array, 0, array.length);
730         assertSame(ret, buf);
731     }
732
733     /*
734      * Class under test for java.nio.DoubleBuffer put(java.nio.DoubleBuffer)
735      */
736     @TestTargetNew(
737         level = TestLevel.PARTIAL_COMPLETE,
738         notes = "Doesn't verify ReadOnlyBufferException.",
739         method = "put",
740         args = {java.nio.DoubleBuffer.class}
741     )
742     public void testPutDoubleBuffer() {
743         DoubleBuffer other = DoubleBuffer.allocate(buf.capacity());
744
745         try {
746             buf.put(buf);
747             fail("Should throw Exception");
748         } catch (IllegalArgumentException e) {
749             // expected
750         }
751         try {
752             buf.put(DoubleBuffer.allocate(buf.capacity() + 1));
753             fail("Should throw Exception");
754         } catch (BufferOverflowException e) {
755             // expected
756         }
757
758         loadTestData2(other);
759         other.clear();
760         buf.clear();
761         DoubleBuffer ret = buf.put(other);
762         assertEquals(other.position(), other.capacity());
763         assertEquals(buf.position(), buf.capacity());
764         assertContentEquals(other, buf);
765         assertSame(ret, buf);
766     }
767
768     /*
769      * Class under test for java.nio.DoubleBuffer put(int, double)
770      */
771     @TestTargetNew(
772         level = TestLevel.PARTIAL_COMPLETE,
773         notes = "Doesn't verify ReadOnlyBufferException.",
774         method = "put",
775         args = {int.class, double.class}
776     )
777     public void testPutintdouble() {
778         buf.clear();
779         for (int i = 0; i < buf.capacity(); i++) {
780             assertEquals(buf.position(), 0);
781             DoubleBuffer ret = buf.put(i, (double) i);
782             assertEquals(buf.get(i), (double) i, 0.0);
783             assertSame(ret, buf);
784         }
785         try {
786             buf.put(-1, 0);
787             fail("Should throw Exception");
788         } catch (IndexOutOfBoundsException e) {
789             // expected
790         }
791         try {
792             buf.put(buf.limit(), 0);
793             fail("Should throw Exception");
794         } catch (IndexOutOfBoundsException e) {
795             // expected
796         }
797     }
798
799     @TestTargetNew(
800         level = TestLevel.PARTIAL_COMPLETE,
801         notes = "",
802         method = "slice",
803         args = {}
804     )
805     public void testSlice() {
806         assertTrue(buf.capacity() > 5);
807         buf.position(1);
808         buf.limit(buf.capacity() - 1);
809
810         DoubleBuffer slice = buf.slice();
811         assertEquals(buf.isReadOnly(), slice.isReadOnly());
812         assertEquals(buf.isDirect(), slice.isDirect());
813         assertEquals(buf.order(), slice.order());
814         assertEquals(slice.position(), 0);
815         assertEquals(slice.limit(), buf.remaining());
816         assertEquals(slice.capacity(), buf.remaining());
817         try {
818             slice.reset();
819             fail("Should throw Exception");
820         } catch (InvalidMarkException e) {
821             // expected
822         }
823
824         // slice share the same content with buf
825         // FIXME:
826         if (!slice.isReadOnly()) {
827             loadTestData1(slice);
828             assertContentLikeTestData1(buf, 1, 0, slice.capacity());
829             buf.put(2, 500);
830             assertEquals(slice.get(1), 500, 0.0);
831         }
832     }
833
834     @TestTargetNew(
835         level = TestLevel.PARTIAL_COMPLETE,
836         notes = "",
837         method = "toString",
838         args = {}
839     )
840     public void testToString() {
841         String str = buf.toString();
842         assertTrue(str.indexOf("Double") >= 0 || str.indexOf("double") >= 0);
843         assertTrue(str.indexOf("" + buf.position()) >= 0);
844         assertTrue(str.indexOf("" + buf.limit()) >= 0);
845         assertTrue(str.indexOf("" + buf.capacity()) >= 0);
846     }
847
848     /*
849      * test for method static DoubleBuffer wrap(double[] array) test covers
850      * following usecases: 1. case for check DoubleBuffer buf2 properties 2.
851      * case for check equal between buf2 and double array[] 3. case for check a
852      * buf2 dependens to array[]
853      */
854     @TestTargetNew(
855         level = TestLevel.PARTIAL_COMPLETE,
856         notes = "",
857         method = "wrap",
858         args = {double[].class}
859     )
860     public void test_Wrap$D() {
861         double array[] = new double[BUFFER_LENGTH];
862         loadTestData1(array, 0, BUFFER_LENGTH);
863         DoubleBuffer buf2 = DoubleBuffer.wrap(array);
864
865         // case: DoubleBuffer buf2 properties is satisfy the conditions
866         // specification
867         assertEquals(buf2.capacity(), array.length);
868         assertEquals(buf2.limit(), array.length);
869         assertEquals(buf2.position(), 0);
870
871         // case: DoubleBuffer buf2 is equal to double array[]
872         assertContentEquals(buf2, array, 0, array.length);
873
874         // case: DoubleBuffer buf2 is depended to double array[]
875         loadTestData2(array, 0, buf.capacity());
876         assertContentEquals(buf2, array, 0, array.length);
877     }
878
879     /*
880      * test for method static DoubleBuffer wrap(double[] array, int offset, int
881      * length) test covers following usecases: 1. case for check DoubleBuffer
882      * buf2 properties 2. case for check equal between buf2 and double array[]
883      * 3. case for check a buf2 dependens to array[] 4. case expected
884      * IndexOutOfBoundsException
885      */
886     @TestTargetNew(
887         level = TestLevel.PARTIAL_COMPLETE,
888         notes = "",
889         method = "wrap",
890         args = {double[].class, int.class, int.class}
891     )
892     public void test_Wrap$DII() {
893         double array[] = new double[BUFFER_LENGTH];
894         int offset = 5;
895         int length = BUFFER_LENGTH - offset;
896         loadTestData1(array, 0, BUFFER_LENGTH);
897         DoubleBuffer buf2 = DoubleBuffer.wrap(array, offset, length);
898
899         // case: DoubleBuffer buf2 properties is satisfy the conditions
900         // specification
901         assertEquals(buf2.capacity(), array.length);
902         assertEquals(buf2.position(), offset);
903         assertEquals(buf2.limit(), offset + length);
904         assertEquals(buf2.arrayOffset(), 0);
905
906         // case: DoubleBuffer buf2 is equal to double array[]
907         assertContentEquals(buf2, array, 0, array.length);
908
909         // case: DoubleBuffer buf2 is depended to double array[]
910         loadTestData2(array, 0, buf.capacity());
911         assertContentEquals(buf2, array, 0, array.length);
912
913         // case: expected IndexOutOfBoundsException
914         try {
915             offset = 7;
916             buf2 = DoubleBuffer.wrap(array, offset, length);
917             fail("wrap method does not throws expected exception");
918         } catch (IndexOutOfBoundsException e) {
919             // expected
920         }
921     }
922
923     void loadTestData1(double array[], int offset, int length) {
924         for (int i = 0; i < length; i++) {
925             array[offset + i] = (double) i;
926         }
927     }
928
929     void loadTestData2(double array[], int offset, int length) {
930         for (int i = 0; i < length; i++) {
931             array[offset + i] = (double) length - i;
932         }
933     }
934
935     void loadTestData1(DoubleBuffer buf) {
936         buf.clear();
937         for (int i = 0; i < buf.capacity(); i++) {
938             buf.put(i, (double) i);
939         }
940     }
941
942     void loadTestData2(DoubleBuffer buf) {
943         buf.clear();
944         for (int i = 0; i < buf.capacity(); i++) {
945             buf.put(i, (double) buf.capacity() - i);
946         }
947     }
948
949     private void assertContentEquals(DoubleBuffer buf, double array[],
950             int offset, int length) {
951         for (int i = 0; i < length; i++) {
952             assertEquals(buf.get(i), array[offset + i], 0.01);
953         }
954     }
955
956     private void assertContentEquals(DoubleBuffer buf, DoubleBuffer other) {
957         assertEquals(buf.capacity(), other.capacity());
958         for (int i = 0; i < buf.capacity(); i++) {
959             assertEquals(buf.get(i), other.get(i), 0.01);
960         }
961     }
962
963     private void assertContentLikeTestData1(DoubleBuffer buf, int startIndex,
964             double startValue, int length) {
965         double value = startValue;
966         for (int i = 0; i < length; i++) {
967             assertEquals(buf.get(startIndex + i), value, 0.01);
968             value = value + 1.0;
969         }
970     }
971 }