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