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.InvalidMarkException;
024: import java.nio.LongBuffer;
025:
026: /**
027: * Tests java.nio.LongBuffer
028: *
029: */
030: public class LongBufferTest extends AbstractBufferTest {
031:
032: protected static final int SMALL_TEST_LENGTH = 5;
033:
034: protected static final int BUFFER_LENGTH = 20;
035:
036: protected LongBuffer buf;
037:
038: protected void setUp() throws Exception {
039: buf = LongBuffer.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: long 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: long 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: LongBuffer 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: LongBuffer 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: LongBuffer other = LongBuffer.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: LongBuffer 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: LongBuffer readonly = buf.asReadOnlyBuffer();
227: assertTrue(buf.equals(readonly));
228: LongBuffer 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 long 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.LongBuffer get(long[])
264: */
265: public void testGetlongArray() {
266: long array[] = new long[1];
267: buf.clear();
268: for (int i = 0; i < buf.capacity(); i++) {
269: assertEquals(buf.position(), i);
270: LongBuffer 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.position(buf.limit());
282: buf.get((long[]) null);
283: fail("Should throw Exception"); //$NON-NLS-1$
284: } catch (NullPointerException e) {
285: // expected
286: }
287: }
288:
289: /*
290: * Class under test for java.nio.LongBuffer get(long[], int, int)
291: */
292: public void testGetlongArrayintint() {
293: buf.clear();
294: long array[] = new long[buf.capacity()];
295:
296: try {
297: buf
298: .get(new long[buf.capacity() + 1], 0, buf
299: .capacity() + 1);
300: fail("Should throw Exception"); //$NON-NLS-1$
301: } catch (BufferUnderflowException e) {
302: // expected
303: }
304: assertEquals(buf.position(), 0);
305: try {
306: buf.get(array, -1, array.length);
307: fail("Should throw Exception"); //$NON-NLS-1$
308: } catch (IndexOutOfBoundsException e) {
309: // expected
310: }
311: buf.get(array, array.length, 0);
312: try {
313: buf.get(array, array.length + 1, 1);
314: fail("Should throw Exception"); //$NON-NLS-1$
315: } catch (IndexOutOfBoundsException e) {
316: // expected
317: }
318: assertEquals(buf.position(), 0);
319: try {
320: buf.get(array, 2, -1);
321: fail("Should throw Exception"); //$NON-NLS-1$
322: } catch (IndexOutOfBoundsException e) {
323: // expected
324: }
325: try {
326: buf.get((long[]) null, 2, -1);
327: fail("Should throw Exception"); //$NON-NLS-1$
328: } catch (NullPointerException e) {
329: // expected
330: }
331: try {
332: buf.get(array, 2, array.length);
333: fail("Should throw Exception"); //$NON-NLS-1$
334: } catch (IndexOutOfBoundsException e) {
335: // expected
336: }
337: try {
338: buf.get(array, 1, Integer.MAX_VALUE);
339: fail("Should throw Exception"); //$NON-NLS-1$
340: } catch (IndexOutOfBoundsException e) {
341: // expected
342: }
343: try {
344: buf.get(array, Integer.MAX_VALUE, 1);
345: fail("Should throw Exception"); //$NON-NLS-1$
346: } catch (IndexOutOfBoundsException e) {
347: // expected
348: }
349: assertEquals(buf.position(), 0);
350:
351: buf.clear();
352: LongBuffer ret = buf.get(array, 0, array.length);
353: assertEquals(buf.position(), buf.capacity());
354: assertContentEquals(buf, array, 0, array.length);
355: assertSame(ret, buf);
356: }
357:
358: /*
359: * Class under test for long get(int)
360: */
361: public void testGetint() {
362: buf.clear();
363: for (int i = 0; i < buf.capacity(); i++) {
364: assertEquals(buf.position(), i);
365: assertEquals(buf.get(), buf.get(i));
366: }
367: try {
368: buf.get(-1);
369: fail("Should throw Exception"); //$NON-NLS-1$
370: } catch (IndexOutOfBoundsException e) {
371: // expected
372: }
373: try {
374: buf.get(buf.limit());
375: fail("Should throw Exception"); //$NON-NLS-1$
376: } catch (IndexOutOfBoundsException e) {
377: // expected
378: }
379: }
380:
381: public void testHasArray() {
382: assertNotNull(buf.array());
383: }
384:
385: public void testHashCode() {
386: buf.clear();
387: LongBuffer readonly = buf.asReadOnlyBuffer();
388: LongBuffer duplicate = buf.duplicate();
389: assertTrue(buf.hashCode() == readonly.hashCode());
390:
391: assertTrue(buf.capacity() > 5);
392: duplicate.position(buf.capacity() / 2);
393: assertTrue(buf.hashCode() != duplicate.hashCode());
394: }
395:
396: public void testIsDirect() {
397: assertFalse(buf.isDirect());
398: }
399:
400: public void testOrder() {
401: buf.order();
402: assertEquals(ByteOrder.nativeOrder(), buf.order());
403: }
404:
405: /*
406: * Class under test for java.nio.LongBuffer put(long)
407: */
408: public void testPutlong() {
409: buf.clear();
410: for (int i = 0; i < buf.capacity(); i++) {
411: assertEquals(buf.position(), i);
412: LongBuffer ret = buf.put((long) i);
413: assertEquals(buf.get(i), (long) i);
414: assertSame(ret, buf);
415: }
416: try {
417: buf.put(0);
418: fail("Should throw Exception"); //$NON-NLS-1$
419: } catch (BufferOverflowException e) {
420: // expected
421: }
422: }
423:
424: /*
425: * Class under test for java.nio.LongBuffer put(long[])
426: */
427: public void testPutlongArray() {
428: long array[] = new long[1];
429: buf.clear();
430: for (int i = 0; i < buf.capacity(); i++) {
431: assertEquals(buf.position(), i);
432: array[0] = (long) i;
433: LongBuffer ret = buf.put(array);
434: assertEquals(buf.get(i), (long) i);
435: assertSame(ret, buf);
436: }
437: try {
438: buf.put(array);
439: fail("Should throw Exception"); //$NON-NLS-1$
440: } catch (BufferOverflowException e) {
441: // expected
442: }
443: try {
444: buf.position(buf.limit());
445: buf.put((long[]) null);
446: fail("Should throw Exception"); //$NON-NLS-1$
447: } catch (NullPointerException e) {
448: // expected
449: }
450: }
451:
452: /*
453: * Class under test for java.nio.LongBuffer put(long[], int, int)
454: */
455: public void testPutlongArrayintint() {
456: buf.clear();
457: long array[] = new long[buf.capacity()];
458: try {
459: buf
460: .put(new long[buf.capacity() + 1], 0, buf
461: .capacity() + 1);
462: fail("Should throw Exception"); //$NON-NLS-1$
463: } catch (BufferOverflowException e) {
464: // expected
465: }
466: assertEquals(buf.position(), 0);
467: try {
468: buf.put(array, -1, array.length);
469: fail("Should throw Exception"); //$NON-NLS-1$
470: } catch (IndexOutOfBoundsException e) {
471: // expected
472: }
473: try {
474: buf.put(array, array.length + 1, 0);
475: fail("Should throw Exception"); //$NON-NLS-1$
476: } catch (IndexOutOfBoundsException e) {
477: // expected
478: }
479: buf.put(array, array.length, 0);
480: assertEquals(buf.position(), 0);
481: try {
482: buf.put(array, 0, -1);
483: fail("Should throw Exception"); //$NON-NLS-1$
484: } catch (IndexOutOfBoundsException e) {
485: // expected
486: }
487: try {
488: buf.put(array, 2, array.length);
489: fail("Should throw Exception"); //$NON-NLS-1$
490: } catch (IndexOutOfBoundsException e) {
491: // expected
492: }
493: try {
494: buf.put((long[]) null, 0, -1);
495: fail("Should throw Exception"); //$NON-NLS-1$
496: } catch (NullPointerException e) {
497: // expected
498: }
499: try {
500: buf.put(array, 2, array.length);
501: fail("Should throw Exception"); //$NON-NLS-1$
502: } catch (IndexOutOfBoundsException e) {
503: // expected
504: }
505: try {
506: buf.put(array, Integer.MAX_VALUE, 1);
507: fail("Should throw Exception"); //$NON-NLS-1$
508: } catch (IndexOutOfBoundsException e) {
509: // expected
510: }
511: try {
512: buf.put(array, 1, Integer.MAX_VALUE);
513: fail("Should throw Exception"); //$NON-NLS-1$
514: } catch (IndexOutOfBoundsException e) {
515: // expected
516: }
517: assertEquals(buf.position(), 0);
518:
519: loadTestData2(array, 0, array.length);
520: LongBuffer ret = buf.put(array, 0, array.length);
521: assertEquals(buf.position(), buf.capacity());
522: assertContentEquals(buf, array, 0, array.length);
523: assertSame(ret, buf);
524: }
525:
526: /*
527: * Class under test for java.nio.LongBuffer put(java.nio.LongBuffer)
528: */
529: public void testPutLongBuffer() {
530: LongBuffer other = LongBuffer.allocate(buf.capacity());
531: try {
532: buf.put(buf);
533: fail("Should throw Exception"); //$NON-NLS-1$
534: } catch (IllegalArgumentException e) {
535: // expected
536: }
537: try {
538: buf.put(LongBuffer.allocate(buf.capacity() + 1));
539: fail("Should throw Exception"); //$NON-NLS-1$
540: } catch (BufferOverflowException e) {
541: // expected
542: }
543: try {
544: buf.flip();
545: buf.put((LongBuffer) null);
546: fail("Should throw Exception"); //$NON-NLS-1$
547: } catch (NullPointerException e) {
548: // expected
549: }
550:
551: loadTestData2(other);
552: other.clear();
553: buf.clear();
554: LongBuffer ret = buf.put(other);
555: assertEquals(other.position(), other.capacity());
556: assertEquals(buf.position(), buf.capacity());
557: assertContentEquals(other, buf);
558: assertSame(ret, buf);
559: }
560:
561: /*
562: * Class under test for java.nio.LongBuffer put(int, long)
563: */
564: public void testPutintlong() {
565: buf.clear();
566: for (int i = 0; i < buf.capacity(); i++) {
567: assertEquals(buf.position(), 0);
568: LongBuffer ret = buf.put(i, (long) i);
569: assertEquals(buf.get(i), (long) i);
570: assertSame(ret, buf);
571: }
572: try {
573: buf.put(-1, 0);
574: fail("Should throw Exception"); //$NON-NLS-1$
575: } catch (IndexOutOfBoundsException e) {
576: // expected
577: }
578: try {
579: buf.put(buf.limit(), 0);
580: fail("Should throw Exception"); //$NON-NLS-1$
581: } catch (IndexOutOfBoundsException e) {
582: // expected
583: }
584: }
585:
586: public void testSlice() {
587: assertTrue(buf.capacity() > 5);
588: buf.position(1);
589: buf.limit(buf.capacity() - 1);
590:
591: LongBuffer slice = buf.slice();
592: assertEquals(buf.isReadOnly(), slice.isReadOnly());
593: assertEquals(buf.isDirect(), slice.isDirect());
594: assertEquals(buf.order(), slice.order());
595: assertEquals(slice.position(), 0);
596: assertEquals(slice.limit(), buf.remaining());
597: assertEquals(slice.capacity(), buf.remaining());
598: try {
599: slice.reset();
600: fail("Should throw Exception"); //$NON-NLS-1$
601: } catch (InvalidMarkException e) {
602: // expected
603: }
604:
605: // slice share the same content with buf
606: if (!slice.isReadOnly()) {
607: loadTestData1(slice);
608: assertContentLikeTestData1(buf, 1, 0, slice.capacity());
609: buf.put(2, 500);
610: assertEquals(slice.get(1), 500);
611: }
612: }
613:
614: public void testToString() {
615: String str = buf.toString();
616: assertTrue(str.indexOf("Long") >= 0 || str.indexOf("long") >= 0);
617: assertTrue(str.indexOf("" + buf.position()) >= 0);
618: assertTrue(str.indexOf("" + buf.limit()) >= 0);
619: assertTrue(str.indexOf("" + buf.capacity()) >= 0);
620: }
621:
622: void loadTestData1(long array[], int offset, int length) {
623: for (int i = 0; i < length; i++) {
624: array[offset + i] = (long) i;
625: }
626: }
627:
628: void loadTestData2(long array[], int offset, int length) {
629: for (int i = 0; i < length; i++) {
630: array[offset + i] = (long) length - i;
631: }
632: }
633:
634: void loadTestData1(LongBuffer buf) {
635: buf.clear();
636: for (int i = 0; i < buf.capacity(); i++) {
637: buf.put(i, (long) i);
638: }
639: }
640:
641: void loadTestData2(LongBuffer buf) {
642: buf.clear();
643: for (int i = 0; i < buf.capacity(); i++) {
644: buf.put(i, (long) buf.capacity() - i);
645: }
646: }
647:
648: void assertContentEquals(LongBuffer buf, long array[], int offset,
649: int length) {
650: for (int i = 0; i < length; i++) {
651: assertEquals(buf.get(i), array[offset + i]);
652: }
653: }
654:
655: void assertContentEquals(LongBuffer buf, LongBuffer other) {
656: assertEquals(buf.capacity(), other.capacity());
657: for (int i = 0; i < buf.capacity(); i++) {
658: assertEquals(buf.get(i), other.get(i));
659: }
660: }
661:
662: void assertContentLikeTestData1(LongBuffer buf, int startIndex,
663: long startValue, int length) {
664: long value = startValue;
665: for (int i = 0; i < length; i++) {
666: assertEquals(buf.get(startIndex + i), value);
667: value = value + 1;
668: }
669: }
670: }
|