aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/inputmethod/latin/common/ResizableIntArrayTests.java
blob: bd1629faf5659d8443dfe9dc224bb9b0798016cf (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.latin.common;

import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.Arrays;

@SmallTest
public class ResizableIntArrayTests extends AndroidTestCase {
    private static final int DEFAULT_CAPACITY = 48;

    public void testNewInstance() {
        final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
        final int[] array = src.getPrimitiveArray();
        assertEquals("new instance length", 0, src.getLength());
        assertNotNull("new instance array", array);
        assertEquals("new instance array length", DEFAULT_CAPACITY, array.length);
    }

    public void testAdd() {
        final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
        final int[] array = src.getPrimitiveArray();
        int[] array2 = null, array3 = null;
        final int limit = DEFAULT_CAPACITY * 2 + 10;
        for (int i = 0; i < limit; i++) {
            final int value = i;
            src.add(value);
            assertEquals("length after add " + i, i + 1, src.getLength());
            if (i == DEFAULT_CAPACITY) {
                array2 = src.getPrimitiveArray();
            }
            if (i == DEFAULT_CAPACITY * 2) {
                array3 = src.getPrimitiveArray();
            }
            if (i < DEFAULT_CAPACITY) {
                assertSame("array after add " + i, array, src.getPrimitiveArray());
            } else if (i < DEFAULT_CAPACITY * 2) {
                assertSame("array after add " + i, array2, src.getPrimitiveArray());
            } else if (i < DEFAULT_CAPACITY * 3) {
                assertSame("array after add " + i, array3, src.getPrimitiveArray());
            }
        }
        for (int i = 0; i < limit; i++) {
            final int value = i;
            assertEquals("value at " + i, value, src.get(i));
        }
    }

    public void testAddAt() {
        final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
        final int limit = DEFAULT_CAPACITY * 10, step = DEFAULT_CAPACITY * 2;
        for (int i = 0; i < limit; i += step) {
            final int value = i;
            src.addAt(i, value);
            assertEquals("length after add at " + i, i + 1, src.getLength());
        }
        for (int i = 0; i < limit; i += step) {
            final int value = i;
            assertEquals("value at " + i, value, src.get(i));
        }
    }

    public void testGet() {
        final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
        try {
            src.get(0);
            fail("get(0) shouldn't succeed");
        } catch (ArrayIndexOutOfBoundsException e) {
            // success
        }
        try {
            src.get(DEFAULT_CAPACITY);
            fail("get(DEFAULT_CAPACITY) shouldn't succeed");
        } catch (ArrayIndexOutOfBoundsException e) {
            // success
        }

        final int index = DEFAULT_CAPACITY / 2;
        final int valueAddAt = 100;
        src.addAt(index, valueAddAt);
        assertEquals("legth after add at " + index, index + 1, src.getLength());
        assertEquals("value after add at " + index, valueAddAt, src.get(index));
        assertEquals("value after add at 0", 0, src.get(0));
        try {
            src.get(src.getLength());
            fail("get(length) shouldn't succeed");
        } catch (ArrayIndexOutOfBoundsException e) {
            // success
        }
    }

    public void testReset() {
        final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
        final int[] array = src.getPrimitiveArray();
        for (int i = 0; i < DEFAULT_CAPACITY; i++) {
            final int value = i;
            src.add(value);
            assertEquals("length after add " + i, i + 1, src.getLength());
        }

        final int smallerLength = DEFAULT_CAPACITY / 2;
        src.reset(smallerLength);
        final int[] array2 = src.getPrimitiveArray();
        assertEquals("length after reset", 0, src.getLength());
        assertNotSame("array after reset", array, array2);

        int[] array3 = null;
        for (int i = 0; i < DEFAULT_CAPACITY; i++) {
            final int value = i;
            src.add(value);
            assertEquals("length after add " + i, i + 1, src.getLength());
            if (i == smallerLength) {
                array3 = src.getPrimitiveArray();
            }
            if (i < smallerLength) {
                assertSame("array after add " + i, array2, src.getPrimitiveArray());
            } else if (i < smallerLength * 2) {
                assertSame("array after add " + i, array3, src.getPrimitiveArray());
            }
        }
    }

    public void testSetLength() {
        final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
        final int[] array = src.getPrimitiveArray();
        for (int i = 0; i < DEFAULT_CAPACITY; i++) {
            final int value = i;
            src.add(value);
            assertEquals("length after add " + i, i + 1, src.getLength());
        }

        final int largerLength = DEFAULT_CAPACITY * 2;
        src.setLength(largerLength);
        final int[] array2 = src.getPrimitiveArray();
        assertEquals("length after larger setLength", largerLength, src.getLength());
        assertNotSame("array after larger setLength", array, array2);
        assertEquals("array length after larger setLength", largerLength, array2.length);
        for (int i = 0; i < largerLength; i++) {
            final int value = i;
            if (i < DEFAULT_CAPACITY) {
                assertEquals("value at " + i, value, src.get(i));
            } else {
                assertEquals("value at " + i, 0, src.get(i));
            }
        }

        final int smallerLength = DEFAULT_CAPACITY / 2;
        src.setLength(smallerLength);
        final int[] array3 = src.getPrimitiveArray();
        assertEquals("length after smaller setLength", smallerLength, src.getLength());
        assertSame("array after smaller setLength", array2, array3);
        assertEquals("array length after smaller setLength", largerLength, array3.length);
        for (int i = 0; i < smallerLength; i++) {
            final int value = i;
            assertEquals("value at " + i, value, src.get(i));
        }
    }

    public void testSet() {
        final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
        final int limit = DEFAULT_CAPACITY * 2 + 10;
        for (int i = 0; i < limit; i++) {
            final int value = i;
            src.add(value);
        }

        final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
        dst.set(src);
        assertEquals("length after set", dst.getLength(), src.getLength());
        assertSame("array after set", dst.getPrimitiveArray(), src.getPrimitiveArray());
    }

    public void testCopy() {
        final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
        for (int i = 0; i < DEFAULT_CAPACITY; i++) {
            final int value =  i;
            src.add(value);
        }

        final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
        final int[] array = dst.getPrimitiveArray();
        dst.copy(src);
        assertEquals("length after copy", dst.getLength(), src.getLength());
        assertSame("array after copy", array, dst.getPrimitiveArray());
        assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray());
        assertIntArrayEquals("values after copy",
                dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());

        final int smallerLength = DEFAULT_CAPACITY / 2;
        dst.reset(smallerLength);
        final int[] array2 = dst.getPrimitiveArray();
        dst.copy(src);
        final int[] array3 = dst.getPrimitiveArray();
        assertEquals("length after copy to smaller", dst.getLength(), src.getLength());
        assertNotSame("array after copy to smaller", array2, array3);
        assertNotSame("array after copy to smaller", array3, src.getPrimitiveArray());
        assertIntArrayEquals("values after copy to smaller",
                dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());
    }

    public void testAppend() {
        final int srcLength = DEFAULT_CAPACITY;
        final ResizableIntArray src = new ResizableIntArray(srcLength);
        for (int i = 0; i < srcLength; i++) {
            final int value = i;
            src.add(value);
        }
        final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2);
        final int[] array = dst.getPrimitiveArray();
        final int dstLength = DEFAULT_CAPACITY / 2;
        for (int i = 0; i < dstLength; i++) {
            final int value = -i - 1;
            dst.add(value);
        }
        final ResizableIntArray dstCopy = new ResizableIntArray(dst.getLength());
        dstCopy.copy(dst);

        final int startPos = 0;
        dst.append(src, startPos, 0 /* length */);
        assertEquals("length after append zero", dstLength, dst.getLength());
        assertSame("array after append zero", array, dst.getPrimitiveArray());
        assertIntArrayEquals("values after append zero", dstCopy.getPrimitiveArray(), startPos,
                dst.getPrimitiveArray(), startPos, dstLength);

        dst.append(src, startPos, srcLength);
        assertEquals("length after append", dstLength + srcLength, dst.getLength());
        assertSame("array after append", array, dst.getPrimitiveArray());
        assertTrue("primitive length after append",
                dst.getPrimitiveArray().length >= dstLength + srcLength);
        assertIntArrayEquals("original values after append", dstCopy.getPrimitiveArray(), startPos,
                dst.getPrimitiveArray(), startPos, dstLength);
        assertIntArrayEquals("appended values after append", src.getPrimitiveArray(), startPos,
                dst.getPrimitiveArray(), dstLength, srcLength);

        dst.append(src, startPos, srcLength);
        assertEquals("length after 2nd append", dstLength + srcLength * 2, dst.getLength());
        assertNotSame("array after 2nd append", array, dst.getPrimitiveArray());
        assertTrue("primitive length after 2nd append",
                dst.getPrimitiveArray().length >= dstLength + srcLength * 2);
        assertIntArrayEquals("original values after 2nd append",
                dstCopy.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), startPos,
                dstLength);
        assertIntArrayEquals("appended values after 2nd append",
                src.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), dstLength,
                srcLength);
        assertIntArrayEquals("appended values after 2nd append",
                src.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), dstLength + srcLength,
                srcLength);
    }

    public void testFill() {
        final int srcLength = DEFAULT_CAPACITY;
        final ResizableIntArray src = new ResizableIntArray(srcLength);
        for (int i = 0; i < srcLength; i++) {
            final int value = i;
            src.add(value);
        }
        final int[] array = src.getPrimitiveArray();

        final int startPos = srcLength / 3;
        final int length = srcLength / 3;
        final int endPos = startPos + length;
        assertTrue(startPos >= 1);
        final int fillValue = 123;
        try {
            src.fill(fillValue, -1 /* startPos */, length);
            fail("fill from -1 shouldn't succeed");
        } catch (IllegalArgumentException e) {
            // success
        }
        try {
            src.fill(fillValue, startPos, -1 /* length */);
            fail("fill negative length shouldn't succeed");
        } catch (IllegalArgumentException e) {
            // success
        }

        src.fill(fillValue, startPos, length);
        assertEquals("length after fill", srcLength, src.getLength());
        assertSame("array after fill", array, src.getPrimitiveArray());
        for (int i = 0; i < srcLength; i++) {
            final int value = i;
            if (i >= startPos && i < endPos) {
                assertEquals("new values after fill at " + i, fillValue, src.get(i));
            } else {
                assertEquals("unmodified values after fill at " + i, value, src.get(i));
            }
        }

        final int length2 = srcLength * 2 - startPos;
        final int largeEnd = startPos + length2;
        assertTrue(largeEnd > srcLength);
        final int fillValue2 = 456;
        src.fill(fillValue2, startPos, length2);
        assertEquals("length after large fill", largeEnd, src.getLength());
        assertNotSame("array after large fill", array, src.getPrimitiveArray());
        for (int i = 0; i < largeEnd; i++) {
            final int value = i;
            if (i >= startPos && i < largeEnd) {
                assertEquals("new values after large fill at " + i, fillValue2, src.get(i));
            } else {
                assertEquals("unmodified values after large fill at " + i, value, src.get(i));
            }
        }

        final int startPos2 = largeEnd + length2;
        final int endPos2 = startPos2 + length2;
        final int fillValue3 = 789;
        src.fill(fillValue3, startPos2, length2);
        assertEquals("length after disjoint fill", endPos2, src.getLength());
        for (int i = 0; i < endPos2; i++) {
            final int value = i;
            if (i >= startPos2 && i < endPos2) {
                assertEquals("new values after disjoint fill at " + i, fillValue3, src.get(i));
            } else if (i >= startPos && i < largeEnd) {
                assertEquals("unmodified values after disjoint fill at " + i,
                        fillValue2, src.get(i));
            } else if (i < startPos) {
                assertEquals("unmodified values after disjoint fill at " + i, value, src.get(i));
            } else {
                assertEquals("gap values after disjoint fill at " + i, 0, src.get(i));
            }
        }
    }

    private static void assertIntArrayEquals(final String message, final int[] expecteds,
            final int expectedPos, final int[] actuals, final int actualPos, final int length) {
        if (expecteds == actuals) {
            return;
        }
        if (expecteds == null || actuals == null) {
            assertEquals(message, Arrays.toString(expecteds), Arrays.toString(actuals));
            return;
        }
        if (expecteds.length < expectedPos + length || actuals.length < actualPos + length) {
            fail(message + ": insufficient length: expecteds=" + Arrays.toString(expecteds)
                    + " actuals=" + Arrays.toString(actuals));
            return;
        }
        for (int i = 0; i < length; i++) {
            assertEquals(message + " [" + i + "]",
                    expecteds[i + expectedPos], actuals[i + actualPos]);
        }
    }

    public void testShift() {
        final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
        final int limit = DEFAULT_CAPACITY * 10;
        final int shiftAmount = 20;
        for (int i = 0; i < limit; ++i) {
            final int value = i;
            src.addAt(i, value);
            assertEquals("length after add at " + i, i + 1, src.getLength());
        }
        src.shift(shiftAmount);
        for (int i = 0; i < limit - shiftAmount; ++i) {
            final int oldValue = i + shiftAmount;
            assertEquals("value at " + i, oldValue, src.get(i));
        }
    }
}