Skip to content

Commit fafd4da

Browse files
committed
[test] Wrote an abstract class for common test cases of int list
1 parent 3e34cf7 commit fafd4da

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (C) 2018 Muhammad Tayyab Akram
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mta.tehreer.collections;
18+
19+
import org.junit.Test;
20+
21+
import java.util.Arrays;
22+
23+
import static org.junit.Assert.assertArrayEquals;
24+
import static org.junit.Assert.assertEquals;
25+
26+
public abstract class IntListTestSuite {
27+
protected IntList actual;
28+
protected int[] expected;
29+
30+
protected IntListTestSuite() {
31+
}
32+
33+
@Test
34+
public void testSize() {
35+
assertEquals(actual.size(), expected.length);
36+
}
37+
38+
@Test
39+
public void testElements() {
40+
for (int i = 0; i < expected.length; i++) {
41+
assertEquals(actual.get(i), expected[i]);
42+
}
43+
}
44+
45+
@Test
46+
public void testCopyFull() {
47+
int[] array = new int[expected.length];
48+
actual.copyTo(array, 0);
49+
50+
assertArrayEquals(array, expected);
51+
}
52+
53+
@Test
54+
public void testCopyAtStart() {
55+
int actualLength = expected.length;
56+
int extraLength = actualLength / 2;
57+
58+
int[] extra = new int[extraLength];
59+
int[] array = new int[actualLength + extraLength];
60+
actual.copyTo(array, 0);
61+
62+
int[] copiedChunk = Arrays.copyOfRange(array, 0, actualLength);
63+
int[] lastChunk = Arrays.copyOfRange(array, actualLength, array.length);
64+
65+
assertArrayEquals(copiedChunk, expected);
66+
assertArrayEquals(lastChunk, extra);
67+
}
68+
69+
@Test
70+
public void testCopyAtEnd() {
71+
int actualLength = expected.length;
72+
int extraLength = actualLength / 2;
73+
74+
int[] extra = new int[extraLength];
75+
int[] array = new int[actualLength + extraLength];
76+
actual.copyTo(array, extraLength);
77+
78+
int[] firstChunk = Arrays.copyOfRange(array, 0, extraLength);
79+
int[] copiedChunk = Arrays.copyOfRange(array, extraLength, array.length);
80+
81+
assertArrayEquals(firstChunk, extra);
82+
assertArrayEquals(copiedChunk, expected);
83+
}
84+
85+
@Test
86+
public void testToArray() {
87+
assertArrayEquals(actual.toArray(), expected);
88+
}
89+
90+
@Test
91+
public void testEquals() {
92+
assertEquals(actual, IntList.of(expected));
93+
}
94+
}

0 commit comments

Comments
 (0)