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.poi.util;
019:
020: import junit.framework.*;
021:
022: /**
023: * Unit test for ArrayUtil
024: *
025: * @author Nick Burch
026: */
027: public class TestArrayUtil extends TestCase {
028: /**
029: * Test to ensure that our own arraycopy behaves as it should do
030: */
031: public void testarraycopy() {
032: byte[] bytes = new byte[] { 0x01, 0x02, 0x03, 0x04 };
033:
034: // Test copy whole thing
035: byte[] dest = new byte[4];
036: ArrayUtil.arraycopy(bytes, 0, dest, 0, 4);
037:
038: assertEquals(dest.length, bytes.length);
039: for (int i = 0; i < dest.length; i++) {
040: assertEquals(bytes[i], dest[i]);
041: }
042:
043: // ToDo - test exceptions are as expected
044: }
045:
046: /**
047: * Helper for testArrayMoveWithin
048: */
049: private Integer[] getIntsList() {
050: return new Integer[] { new Integer(0), new Integer(1),
051: new Integer(2), new Integer(3), new Integer(4),
052: new Integer(5), new Integer(6), new Integer(7),
053: new Integer(8), new Integer(9) };
054: }
055:
056: /**
057: * Test to ensure that arrayMoveWithin works as expected
058: */
059: public void testArrayMoveWithin() {
060: Integer[] ints = getIntsList();
061:
062: assertEquals(new Integer(0), ints[0]);
063: assertEquals(new Integer(1), ints[1]);
064: assertEquals(new Integer(2), ints[2]);
065: assertEquals(new Integer(3), ints[3]);
066: assertEquals(new Integer(4), ints[4]);
067: assertEquals(new Integer(5), ints[5]);
068: assertEquals(new Integer(6), ints[6]);
069: assertEquals(new Integer(7), ints[7]);
070: assertEquals(new Integer(8), ints[8]);
071: assertEquals(new Integer(9), ints[9]);
072:
073: //
074: // Moving to a later point in the array
075: //
076:
077: // Shift 1 back
078: ints = getIntsList();
079: ArrayUtil.arrayMoveWithin(ints, 4, 8, 1);
080: assertEquals(new Integer(0), ints[0]);
081: assertEquals(new Integer(1), ints[1]);
082: assertEquals(new Integer(2), ints[2]);
083: assertEquals(new Integer(3), ints[3]);
084: assertEquals(new Integer(5), ints[4]);
085: assertEquals(new Integer(6), ints[5]);
086: assertEquals(new Integer(7), ints[6]);
087: assertEquals(new Integer(8), ints[7]);
088: assertEquals(new Integer(4), ints[8]);
089: assertEquals(new Integer(9), ints[9]);
090:
091: // Shift front 1 back
092: ints = getIntsList();
093: ArrayUtil.arrayMoveWithin(ints, 0, 7, 1);
094: assertEquals(new Integer(1), ints[0]);
095: assertEquals(new Integer(2), ints[1]);
096: assertEquals(new Integer(3), ints[2]);
097: assertEquals(new Integer(4), ints[3]);
098: assertEquals(new Integer(5), ints[4]);
099: assertEquals(new Integer(6), ints[5]);
100: assertEquals(new Integer(7), ints[6]);
101: assertEquals(new Integer(0), ints[7]);
102: assertEquals(new Integer(8), ints[8]);
103: assertEquals(new Integer(9), ints[9]);
104:
105: // Shift 1 to end
106: ints = getIntsList();
107: ArrayUtil.arrayMoveWithin(ints, 4, 9, 1);
108: assertEquals(new Integer(0), ints[0]);
109: assertEquals(new Integer(1), ints[1]);
110: assertEquals(new Integer(2), ints[2]);
111: assertEquals(new Integer(3), ints[3]);
112: assertEquals(new Integer(5), ints[4]);
113: assertEquals(new Integer(6), ints[5]);
114: assertEquals(new Integer(7), ints[6]);
115: assertEquals(new Integer(8), ints[7]);
116: assertEquals(new Integer(9), ints[8]);
117: assertEquals(new Integer(4), ints[9]);
118:
119: //
120: // Moving to an earlier point in the array
121: //
122:
123: // Shift 1 forward
124: ints = getIntsList();
125: ArrayUtil.arrayMoveWithin(ints, 8, 3, 1);
126: assertEquals(new Integer(0), ints[0]);
127: assertEquals(new Integer(1), ints[1]);
128: assertEquals(new Integer(2), ints[2]);
129: assertEquals(new Integer(8), ints[3]);
130: assertEquals(new Integer(3), ints[4]);
131: assertEquals(new Integer(4), ints[5]);
132: assertEquals(new Integer(5), ints[6]);
133: assertEquals(new Integer(6), ints[7]);
134: assertEquals(new Integer(7), ints[8]);
135: assertEquals(new Integer(9), ints[9]);
136:
137: // Shift end 1 forward
138: ints = getIntsList();
139: ArrayUtil.arrayMoveWithin(ints, 9, 2, 1);
140: assertEquals(new Integer(0), ints[0]);
141: assertEquals(new Integer(1), ints[1]);
142: assertEquals(new Integer(9), ints[2]);
143: assertEquals(new Integer(2), ints[3]);
144: assertEquals(new Integer(3), ints[4]);
145: assertEquals(new Integer(4), ints[5]);
146: assertEquals(new Integer(5), ints[6]);
147: assertEquals(new Integer(6), ints[7]);
148: assertEquals(new Integer(7), ints[8]);
149: assertEquals(new Integer(8), ints[9]);
150:
151: // Shift 1 to front
152: ints = getIntsList();
153: ArrayUtil.arrayMoveWithin(ints, 5, 0, 1);
154: assertEquals(new Integer(5), ints[0]);
155: assertEquals(new Integer(0), ints[1]);
156: assertEquals(new Integer(1), ints[2]);
157: assertEquals(new Integer(2), ints[3]);
158: assertEquals(new Integer(3), ints[4]);
159: assertEquals(new Integer(4), ints[5]);
160: assertEquals(new Integer(6), ints[6]);
161: assertEquals(new Integer(7), ints[7]);
162: assertEquals(new Integer(8), ints[8]);
163: assertEquals(new Integer(9), ints[9]);
164:
165: //
166: // Moving many to a later point in the array
167: //
168:
169: // Shift 3 back
170: ints = getIntsList();
171: ArrayUtil.arrayMoveWithin(ints, 2, 6, 3);
172: assertEquals(new Integer(0), ints[0]);
173: assertEquals(new Integer(1), ints[1]);
174: assertEquals(new Integer(5), ints[2]);
175: assertEquals(new Integer(6), ints[3]);
176: assertEquals(new Integer(7), ints[4]);
177: assertEquals(new Integer(8), ints[5]);
178: assertEquals(new Integer(2), ints[6]);
179: assertEquals(new Integer(3), ints[7]);
180: assertEquals(new Integer(4), ints[8]);
181: assertEquals(new Integer(9), ints[9]);
182:
183: // Shift 3 to back
184: ints = getIntsList();
185: ArrayUtil.arrayMoveWithin(ints, 2, 7, 3);
186: assertEquals(new Integer(0), ints[0]);
187: assertEquals(new Integer(1), ints[1]);
188: assertEquals(new Integer(5), ints[2]);
189: assertEquals(new Integer(6), ints[3]);
190: assertEquals(new Integer(7), ints[4]);
191: assertEquals(new Integer(8), ints[5]);
192: assertEquals(new Integer(9), ints[6]);
193: assertEquals(new Integer(2), ints[7]);
194: assertEquals(new Integer(3), ints[8]);
195: assertEquals(new Integer(4), ints[9]);
196:
197: // Shift from 3 front
198: ints = getIntsList();
199: ArrayUtil.arrayMoveWithin(ints, 0, 5, 3);
200: assertEquals(new Integer(3), ints[0]);
201: assertEquals(new Integer(4), ints[1]);
202: assertEquals(new Integer(5), ints[2]);
203: assertEquals(new Integer(6), ints[3]);
204: assertEquals(new Integer(7), ints[4]);
205: assertEquals(new Integer(0), ints[5]);
206: assertEquals(new Integer(1), ints[6]);
207: assertEquals(new Integer(2), ints[7]);
208: assertEquals(new Integer(8), ints[8]);
209: assertEquals(new Integer(9), ints[9]);
210:
211: //
212: // Moving many to an earlier point in the array
213: //
214:
215: // Shift 3 forward
216: ints = getIntsList();
217: ArrayUtil.arrayMoveWithin(ints, 6, 2, 3);
218: assertEquals(new Integer(0), ints[0]);
219: assertEquals(new Integer(1), ints[1]);
220: assertEquals(new Integer(6), ints[2]);
221: assertEquals(new Integer(7), ints[3]);
222: assertEquals(new Integer(8), ints[4]);
223: assertEquals(new Integer(2), ints[5]);
224: assertEquals(new Integer(3), ints[6]);
225: assertEquals(new Integer(4), ints[7]);
226: assertEquals(new Integer(5), ints[8]);
227: assertEquals(new Integer(9), ints[9]);
228:
229: // Shift 3 to front
230: ints = getIntsList();
231: ArrayUtil.arrayMoveWithin(ints, 6, 0, 3);
232: assertEquals(new Integer(6), ints[0]);
233: assertEquals(new Integer(7), ints[1]);
234: assertEquals(new Integer(8), ints[2]);
235: assertEquals(new Integer(0), ints[3]);
236: assertEquals(new Integer(1), ints[4]);
237: assertEquals(new Integer(2), ints[5]);
238: assertEquals(new Integer(3), ints[6]);
239: assertEquals(new Integer(4), ints[7]);
240: assertEquals(new Integer(5), ints[8]);
241: assertEquals(new Integer(9), ints[9]);
242:
243: // Shift from 3 back
244: ints = getIntsList();
245: ArrayUtil.arrayMoveWithin(ints, 7, 3, 3);
246: assertEquals(new Integer(0), ints[0]);
247: assertEquals(new Integer(1), ints[1]);
248: assertEquals(new Integer(2), ints[2]);
249: assertEquals(new Integer(7), ints[3]);
250: assertEquals(new Integer(8), ints[4]);
251: assertEquals(new Integer(9), ints[5]);
252: assertEquals(new Integer(3), ints[6]);
253: assertEquals(new Integer(4), ints[7]);
254: assertEquals(new Integer(5), ints[8]);
255: assertEquals(new Integer(6), ints[9]);
256:
257: // Check can't shift more than we have
258: try {
259: ints = getIntsList();
260: ArrayUtil.arrayMoveWithin(ints, 7, 3, 5);
261: fail();
262: } catch (IllegalArgumentException e) {
263: // Good, we don't have 5 from 7 onwards
264: }
265:
266: // Check can't shift where would overshoot
267: try {
268: ints = getIntsList();
269: ArrayUtil.arrayMoveWithin(ints, 2, 7, 5);
270: fail();
271: } catch (IllegalArgumentException e) {
272: // Good, we can't fit 5 in starting at 7
273: }
274: }
275: }
|