2323import java .util .List ;
2424import 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
2928class 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