Skip to content

Commit e7de2e5

Browse files
committed
Initial Test cases for Vector
1 parent 3319a45 commit e7de2e5

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/com/deepak/data/structures/Vector/CustomVector.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package com.deepak.data.structures.Vector;
66

7+
import java.util.Arrays;
78
import java.util.Enumeration;
89
import java.util.NoSuchElementException;
910

@@ -267,12 +268,12 @@ public synchronized T get(int index) {
267268
* @param newSize
268269
*/
269270
private void ensureCapacity(int newSize) {
270-
if (newSize >= size) {
271+
if (newSize >= capacity()) {
271272
/* If capacity increment is given, use that else double the size */
272273
if (capacityIncrement > 0) {
273-
System.arraycopy(elements, 0, elements, size, size + capacityIncrement);
274+
elements = Arrays.copyOf(elements, capacity() + capacityIncrement);
274275
} else {
275-
System.arraycopy(elements, 0, elements, size, size * 2);
276+
elements = Arrays.copyOf(elements, size * 2);
276277
}
277278
}
278279
}

test/com/deepak/data/structures/Vector/CustomVectorTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,55 @@
44
*/
55
package com.deepak.data.structures.Vector;
66

7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
710
/**
811
* Test cases for custom vector
912
*
1013
* @author Deepak
1114
*/
1215
public class CustomVectorTest {
1316

14-
17+
/**
18+
* Test case for vector creation when no capacity is defined
19+
*/
20+
@Test
21+
public void testVecorCreationWhenNoCapacityIsDefined() {
22+
CustomVector<String> vector = new CustomVector<>();
23+
Assert.assertEquals(vector.capacity(), 10);
24+
}
25+
26+
/**
27+
* Test case for vector creation when capacity is defined
28+
*/
29+
@Test
30+
public void testVectorCreationWhenCapacityIsDefined() {
31+
CustomVector<String> vector = new CustomVector<>(3);
32+
Assert.assertEquals(vector.capacity(), 3);
33+
vector.addElement("A");
34+
vector.addElement("B");
35+
vector.addElement("C");
36+
vector.addElement("D");
37+
/* When 4th element is added, capacity should double
38+
* since we have not specified increment capacity */
39+
Assert.assertEquals(vector.capacity(), 6);
40+
}
41+
42+
/**
43+
* Test case for vector creation when capacity increases
44+
* and increment capacity is defined
45+
*/
46+
@Test
47+
public void testVectorCreationWhenCapacityIncreases() {
48+
CustomVector<String> vector = new CustomVector<>(3, 10);
49+
Assert.assertEquals(vector.capacity(), 3);
50+
vector.addElement("A");
51+
vector.addElement("B");
52+
vector.addElement("C");
53+
vector.addElement("D");
54+
/* When 4th element is added, capacity should increase
55+
* taking into consideration, the increment capacity */
56+
Assert.assertEquals(vector.capacity(), 13);
57+
}
1558
}

0 commit comments

Comments
 (0)