- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestStringArrayList.java
More file actions
Latest commit
136 lines (121 loc) · 4.97 KB
/
Copy pathTestStringArrayList.java
File metadata and controls
136 lines (121 loc) · 4.97 KB
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
importstaticorg.junit.Assert.assertEquals;
importorg.junit.Test;
importjava.util.ArrayList;
importjava.util.Arrays;
publicclassTestStringArrayList {
/**
* Test edge cases for get.
*/
@Test(expected = IndexOutOfBoundsException.class)
publicvoidtestGetOutOfBounds1() {
StringArrayListtester = newStringArrayList();
tester.get(-3);
}
/**
* Test edge cases for get.
*/
@Test(expected = IndexOutOfBoundsException.class)
publicvoidtestGetOutOfBounds2() {
StringArrayListtester = newStringArrayList();
tester.get(3);
}
/**
* Test that get works for item 0 when set with single item constructor.
*/
@Test
publicvoidtestGetSingleItemConstructor() {
StringArrayListtester = newStringArrayList("Anna");
Stringitem0 = tester.get(0);
assertEquals("Testing get for 0th index and single item constructor.\n", "Anna", item0);
}
/**
* Test edge cases for set.
*/
@Test(expected = IndexOutOfBoundsException.class)
publicvoidtestSetOutOfBounds1() {
StringArrayListtester = newStringArrayList();
tester.set(-3, "Anna");
}
/**
* Test edge cases for set.
*/
@Test(expected = IndexOutOfBoundsException.class)
publicvoidtestSetOutOfBounds2() {
StringArrayListtester = newStringArrayList();
tester.set(3, "Anna");
}
/**
* Test that set (and get) work for item 0 when set with single item constructor.
*/
@Test
publicvoidtestSetSingleItemConstructor() {
StringArrayListtester = newStringArrayList("Anna");
StringoldItem = tester.set(0, "Eris");
assertEquals("Testing set for 0th index and single item constructor.\n", "Anna", oldItem);
assertEquals("Testing set and get for 0th index and single item constructor.\n", "Eris", tester.get(0));
}
/**
* Test that adding at the end works when no resizing of array is needed.
*/
@Test
publicvoidtestAddAtEndNoResize() {
StringArrayListtester = newStringArrayList();
java.util.List<String> stringList = newArrayList<String>();
//Add the numbers 0-6 to the list in string form
//and check array after each one.
for (inti = 0; i < 7; i++) {
tester.add(Integer.toString(i));
stringList.add(Integer.toString(i));
assertEquals("Checking add at end. (Look at how you change numItems if your output doesn't match the expected one in length.)\n", stringList.toString(), Arrays.toString(tester.toArray()));
}
}
/**
* Test that adding at the beginning works when no resizing of array is needed.
*/
@Test
publicvoidtestAddAtIndexNoResize() {
StringArrayListtester = newStringArrayList();
java.util.List<String> stringList = newArrayList<String>();
//Add the numbers 0-6 to the list in string form
//and check array after each one.
for (inti = 0; i < 7; i++) {
tester.add(0, Integer.toString(i));
stringList.add(0, Integer.toString(i));
assertEquals("Checking add at index. (Look at how you change numItems if your output doesn't match the expected one in length.)\n", stringList.toString(), Arrays.toString(tester.toArray()));
}
}
/**
* Test that adding at the end works when resizing of array is needed.
*/
@Test
publicvoidtestAddAtEndResize() {
StringArrayListtester = newStringArrayList();
java.util.List<String> stringList = newArrayList<String>();
//Add the numbers 0-24 to the list in string form
//and check array after each one.
for (inti = 0; i < 25; i++) {
tester.add(Integer.toString(i));
stringList.add(Integer.toString(i));
assertEquals("Checking add at end. (Look at how you change numItems if your output doesn't match the expected one in length.)\n", stringList.toString(), Arrays.toString(tester.toArray()));
}
}
/**
* Test that getting works for a variety of spots in the array, as well as
* adding at an index and resizing.
*/
@Test
publicvoidtestAddAtIndexResize() {
StringArrayListtester = newStringArrayList();
java.util.List<String> stringList = newArrayList<String>();
//Add the numbers 0-24 to the list in string form
//and check array after each one.
for (inti = 0; i < 25; i++) {
tester.add(0, Integer.toString(i));
stringList.add(0, Integer.toString(i));
assertEquals("Checking add at index. (Look at how you change numItems if your output doesn't match the expected one in length.)\n", stringList.toString(), Arrays.toString(tester.toArray()));
for(intj = 0; j < stringList.size(); j++) {
assertEquals("Checking get at many spots in array, after adding.", stringList.get(j), tester.get(j));
}
}
}
}