Skip to content

Commit 18bdb2a

Browse files
author
George Bouroutzoglou
committed
added a few comments and assertions to existing tests
1 parent b11846e commit 18bdb2a

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/test/java/gr/geompokon/bitarray/BitArrayInterfaceTest.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@
2323
import java.util.List;
2424
import java.util.Random;
2525

26-
import static org.junit.jupiter.api.Assertions.assertEquals;
27-
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
import static org.junit.jupiter.api.Assertions.*;
2827

2928
class BitArrayInterfaceTest {
3029

31-
final static int MAX_TEST_SIZE = 500;
30+
final static int MAX_TEST_SIZE = 200;
3231

3332
static List<Boolean> bitArray;
3433
static List<Boolean> boolArray;
3534
static Random random;
3635

36+
/**
37+
* Start each test with a fresh array
38+
*/
3739
@BeforeEach
3840
void setUp() {
3941
bitArray = new BitArray();
@@ -64,33 +66,50 @@ void myAssertSameArrays() {
6466
assertEquals(boolArray, bitArray);
6567
}
6668

69+
/**
70+
* Random insertions at random indices
71+
*/
6772
@Test
6873
void addAtIndex() {
6974
for (int i = 0; i < MAX_TEST_SIZE; i++) {
70-
int index = random.nextInt(bitArray.size() + 1);
75+
int index = random.nextInt(bitArray.size() + 1); // bound is exclusive
7176
boolean element = random.nextBoolean();
7277
bitArray.add(index, element);
7378
boolArray.add(index, element);
7479
}
7580
myAssertSameArrays();
7681
}
7782

83+
/**
84+
* Modification of elements at random indices
85+
*/
7886
@Test
7987
void set() {
88+
// test empty array behaviour
89+
assertThrows(Exception.class, () -> bitArray.set(0, true));
90+
91+
// test with elements
8092
initArrays(MAX_TEST_SIZE);
8193

8294
for (int i = 0; i < MAX_TEST_SIZE; i++) {
8395
int index = random.nextInt(bitArray.size());
84-
Boolean negatedElement = !bitArray.get(index);
96+
Boolean negatedElement = !bitArray.get(index); // to ensure contents change
8597
bitArray.set(index, negatedElement);
8698
boolArray.set(index, negatedElement);
8799
}
88100

89101
myAssertSameArrays();
90102
}
91103

104+
/**
105+
* Remove of elements at random indices
106+
*/
92107
@Test
93108
void remove() {
109+
// test empty array behaviour
110+
assertThrows(Exception.class, () -> bitArray.remove(0));
111+
112+
// test with elements
94113
initArrays(MAX_TEST_SIZE);
95114

96115
for (int i = 0; i < MAX_TEST_SIZE && !bitArray.isEmpty(); i++) {
@@ -116,17 +135,23 @@ void clear() {
116135

117136
@Test
118137
void size() {
138+
// test newly created array size
139+
assertEquals(0, bitArray.size());
140+
141+
// add some elements
119142
initArrays(MAX_TEST_SIZE);
120143
int expectedSize = MAX_TEST_SIZE;
121144
assertEquals(expectedSize, bitArray.size());
122145

146+
// remove some
123147
int noToRemove = MAX_TEST_SIZE / 2;
124148
bitArray.subList(0, noToRemove).clear();
125149

126150
expectedSize -= noToRemove;
127151
assertEquals(expectedSize, bitArray.size());
128152

129-
int noToAdd = 100;
153+
// add back some
154+
int noToAdd = MAX_TEST_SIZE / 2;
130155
for (int i = 0; i < noToAdd; i++) {
131156
bitArray.add(random.nextBoolean());
132157
}

0 commit comments

Comments
 (0)