001: /*
002: ******************************************************************
003: Copyright (c) 200, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit.util;
038:
039: import junit.framework.TestCase;
040:
041: /**
042: * Tests for IntegerBuffer
043: */
044: public class test_IntegerBuffer extends TestCase {
045:
046: public void testToArrayEmpty() {
047: assertNotNull((new IntegerBuffer()).toIntArray());
048: assertEquals(0, (new IntegerBuffer()).toIntArray().length);
049: }
050:
051: public void testSingleIntAppend() {
052: IntegerBuffer b = new IntegerBuffer();
053: b.append(1);
054: assertNotNull(b.toIntArray());
055: assertEquals(1, b.toIntArray().length);
056: assertEquals(1, b.toIntArray()[0]);
057: }
058:
059: public void testArrayAppend() {
060: IntegerBuffer b = new IntegerBuffer();
061: b.append(new int[] { 1, 2 });
062: assertNotNull(b.toIntArray());
063: assertEquals(2, b.toIntArray().length);
064: for (int i = 0; i < 2; i++) {
065: assertEquals(i + 1, b.toIntArray()[i]);
066: }
067: }
068:
069: public void testSingleIntAppendWithGrowth() {
070: IntegerBuffer b = new IntegerBuffer(1);
071: for (int i = 0; i < 2; i++) {
072: b.append(i);
073: }
074: assertNotNull(b.toIntArray());
075: assertEquals(2, b.toIntArray().length);
076: for (int i = 0; i < 2; i++) {
077: assertEquals(i, b.toIntArray()[i]);
078: }
079: }
080:
081: public void testArrayAppendWithGrowth() {
082: IntegerBuffer b = new IntegerBuffer(1);
083: b.append(new int[] { 1, 2 });
084: assertNotNull(b.toIntArray());
085: assertEquals(2, b.toIntArray().length);
086: for (int i = 0; i < 2; i++) {
087: assertEquals(i + 1, b.toIntArray()[i]);
088: }
089: }
090:
091: public void testSize() {
092: IntegerBuffer b = new IntegerBuffer();
093: assertEquals(0, b.size());
094: b.append(0);
095: assertEquals(1, b.size());
096: b.append(new int[] { 1, 2 });
097: assertEquals(3, b.size());
098: }
099:
100: public void testCapacity() {
101: IntegerBuffer b = new IntegerBuffer(1);
102: assertEquals(1, b.capacity());
103: b.append(0);
104: assertEquals(1, b.capacity());
105: b.append(0);
106: assertTrue(b.capacity() > 1);
107: }
108:
109: public void testIndexOfSimple() {
110: IntegerBuffer b = new IntegerBuffer();
111: int[] test = new int[] { 1, 2, 3 };
112: assertEquals(-1, b.indexOf(test));
113: b.append(test);
114: assertEquals(0, b.indexOf(test));
115: b.append(test);
116: assertEquals(0, b.indexOf(test));
117: }
118:
119: public void testIndexOfWithOffset() {
120: IntegerBuffer b = new IntegerBuffer();
121: int[] test = new int[] { 1, 2, 3 };
122: b.append(0);
123: assertEquals(-1, b.indexOf(test));
124: b.append(test);
125: assertEquals(1, b.indexOf(test));
126: }
127:
128: public void testIndexOfWithRepeatedInts() {
129: IntegerBuffer b = new IntegerBuffer();
130: int[] test = new int[] { 1, 2, 3 };
131: b.append(1);
132: assertEquals(-1, b.indexOf(test));
133: b.append(test);
134: assertEquals(1, b.indexOf(test));
135: }
136:
137: public void testIndexOfSupSequenceIsThere() {
138: IntegerBuffer b = new IntegerBuffer();
139: int[] test = new int[] { 1, 2, 3 };
140: b.append(new int[] { 1, 2 });
141: b.append(4);
142: assertEquals(-1, b.indexOf(test));
143: }
144:
145: public void testAllBytes() {
146: IntegerBuffer buf = new IntegerBuffer();
147: for (byte b = Byte.MIN_VALUE; b < Byte.MAX_VALUE; b++) {
148: buf.append(b);
149: }
150: buf.append(Byte.MAX_VALUE);
151: int[] is = buf.toIntArray();
152: for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
153: assertEquals((byte) i, is[i + Math.abs(Byte.MIN_VALUE)]);
154: }
155: }
156:
157: public void testAllChars() {
158: IntegerBuffer buf = new IntegerBuffer();
159: for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) {
160: buf.append(c);
161: }
162: buf.append(Character.MAX_VALUE);
163: int[] is = buf.toIntArray();
164: for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {
165: assertEquals((char) i,
166: is[i + Math.abs(Character.MIN_VALUE)]);
167: }
168: }
169: }
|