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