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