1616
1717package gr .geompokon .bitarray ;
1818
19- import org .junit .jupiter .api .*;
19+ import org .junit .jupiter .api .BeforeEach ;
20+ import org .junit .jupiter .api .DisplayName ;
21+ import org .junit .jupiter .api .Nested ;
22+ import org .junit .jupiter .api .TestInstance ;
2023import org .junit .jupiter .params .ParameterizedTest ;
2124import org .junit .jupiter .params .provider .MethodSource ;
2225import org .junit .jupiter .params .provider .ValueSource ;
2326
27+ import java .util .ArrayList ;
2428import java .util .List ;
25- import java .util .Random ;
2629import java .util .UnknownFormatConversionException ;
27- import java .util .stream .Collectors ;
28- import java .util .stream .Stream ;
2930
3031import static org .assertj .core .api .Assertions .assertThat ;
3132import static org .assertj .core .api .Assertions .assertThatThrownBy ;
@@ -35,23 +36,107 @@ class BitArrayTest {
3536
3637 static BitArray bitArray ;
3738
38- static final int SEED = 101 ;
39- static final Random consistentRandom = new Random (SEED );
40-
4139 @ BeforeEach
4240 void setUp () {
4341 bitArray = new BitArray ();
4442 }
4543
4644
4745 @ Nested
48- @ DisplayName ("Copy tests" )
46+ @ DisplayName ("CRUD Tests" )
47+ @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
48+ class CRUDTest {
49+
50+ @ ParameterizedTest (name = "{0} elements" )
51+ @ DisplayName ("Add at index should behave like ArrayList" )
52+ @ MethodSource ("gr.geompokon.bitarray.TestUtils#testCaseBooleans" )
53+ void add_at_index_test (List <Boolean > elementsToAdd ) {
54+
55+ // given
56+ List <Boolean > authority = new ArrayList <>(elementsToAdd .size ());
57+ List <Integer > addIndices = TestUtils .getAddIndices (elementsToAdd .size ());
58+
59+ // when
60+ for (int i = 0 ; i < elementsToAdd .size (); i ++) {
61+ bitArray .add (addIndices .get (i ), elementsToAdd .get (i ));
62+ authority .add (addIndices .get (i ), elementsToAdd .get (i ));
63+ }
64+
65+ // then
66+ assertThat (bitArray ).isEqualTo (authority );
67+ }
68+
69+ @ ParameterizedTest (name = "{0} elements" )
70+ @ DisplayName ("Add at the end should behave like ArrayList" )
71+ @ MethodSource ("gr.geompokon.bitarray.TestUtils#testCaseBooleans" )
72+ void add_test (List <Boolean > elementsToAdd ) {
73+
74+ // given
75+ List <Boolean > authority = new ArrayList <>(elementsToAdd .size ());
76+
77+ // when
78+ for (Boolean aBoolean : elementsToAdd ) {
79+ bitArray .add (aBoolean );
80+ authority .add (aBoolean );
81+ }
82+
83+ // then
84+ assertThat (bitArray ).isEqualTo (authority );
85+ }
86+
87+ @ ParameterizedTest (name = "{0} elements" )
88+ @ DisplayName ("Set at index should behave like ArrayList" )
89+ @ MethodSource ("gr.geompokon.bitarray.TestUtils#testCaseBooleans" )
90+ void set_test (List <Boolean > elementsToAdd ) {
91+
92+ // given
93+ List <Boolean > authority = new ArrayList <>(elementsToAdd .size ());
94+ List <Integer > setIndices = TestUtils .getSetIndices (elementsToAdd .size ());
95+ bitArray .addAll (elementsToAdd );
96+ authority .addAll (elementsToAdd );
97+
98+ // when/then
99+ for (int i : setIndices ) {
100+ boolean nextSetValue = !authority .get (i );
101+
102+ authority .set (i , nextSetValue );
103+ bitArray .set (i , nextSetValue );
104+
105+ assertThat (bitArray .get (i )).isEqualTo (authority .get (i ));
106+ }
107+ }
108+
109+ @ ParameterizedTest (name = "{0} elements" )
110+ @ DisplayName ("Remove at index should behave like ArrayList" )
111+ @ MethodSource ("gr.geompokon.bitarray.TestUtils#testCaseBooleans" )
112+ void remove_test (List <Boolean > elementsToRemove ) {
113+
114+ // given
115+ List <Boolean > authority = new ArrayList <>(elementsToRemove .size ());
116+ List <Integer > removeIndices = TestUtils .getRemoveIndices (elementsToRemove .size ());
117+ System .out .println (removeIndices );
118+ bitArray .addAll (elementsToRemove );
119+ authority .addAll (elementsToRemove );
120+
121+ // when/then
122+ for (int i = 0 ; i < elementsToRemove .size (); i ++) {
123+ int nextRemoveIndex = removeIndices .get (i );
124+ assertThat (bitArray .remove (nextRemoveIndex ))
125+ .isEqualTo (authority .remove (nextRemoveIndex ));
126+ }
127+
128+ }
129+ }
130+
131+
132+ @ Nested
133+ @ DisplayName ("Copy Tests" )
49134 @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
50135 class CopyTests {
51136
52137 @ ParameterizedTest (name = "{0} elements" )
53138 @ DisplayName ("Result of copy constructor should have the same elements" )
54- @ MethodSource ("gr.geompokon.bitarray.BitArrayTest #testCaseBooleans" )
139+ @ MethodSource ("gr.geompokon.bitarray.TestUtils #testCaseBooleans" )
55140 void copy_constructor_returns_identical_list (List <Boolean > elementsToAdd ) {
56141 // given
57142 bitArray .addAll (elementsToAdd );
@@ -65,7 +150,7 @@ void copy_constructor_returns_identical_list(List<Boolean> elementsToAdd) {
65150
66151 @ ParameterizedTest (name = "{0} elements" )
67152 @ DisplayName ("Result of copy constructor should be a separate object" )
68- @ MethodSource ("gr.geompokon.bitarray.BitArrayTest #testCaseBooleans" )
153+ @ MethodSource ("gr.geompokon.bitarray.TestUtils #testCaseBooleans" )
69154 void copy_constructor_returns_new_Object (List <Boolean > elementsToAdd ) {
70155 // given
71156 bitArray .addAll (elementsToAdd );
@@ -80,7 +165,7 @@ void copy_constructor_returns_new_Object(List<Boolean> elementsToAdd) {
80165
81166 @ ParameterizedTest (name = "{0} elements" )
82167 @ DisplayName ("Result of clone() should have the same elements" )
83- @ MethodSource ("gr.geompokon.bitarray.BitArrayTest #testCaseBooleans" )
168+ @ MethodSource ("gr.geompokon.bitarray.TestUtils #testCaseBooleans" )
84169 void clone_returns_identical_list (List <Boolean > elementsToAdd ) {
85170 // given
86171 bitArray .addAll (elementsToAdd );
@@ -95,7 +180,7 @@ void clone_returns_identical_list(List<Boolean> elementsToAdd) {
95180
96181 @ ParameterizedTest (name = "{0} elements" )
97182 @ DisplayName ("Result of clone should be a separate object" )
98- @ MethodSource ("gr.geompokon.bitarray.BitArrayTest #testCaseBooleans" )
183+ @ MethodSource ("gr.geompokon.bitarray.TestUtils #testCaseBooleans" )
99184 void clone_returns_new_Object (List <Boolean > elementsToAdd ) {
100185 // given
101186 bitArray .addAll (elementsToAdd );
@@ -116,7 +201,7 @@ class SerializationTests {
116201
117202 @ ParameterizedTest (name = "{0} elements" )
118203 @ DisplayName ("Serialized and immediately deserialized array should be the same as original" )
119- @ MethodSource ("gr.geompokon.bitarray.BitArrayTest #testCaseBooleans" )
204+ @ MethodSource ("gr.geompokon.bitarray.TestUtils #testCaseBooleans" )
120205 void toString_and_fromString_do_not_alter_content (List <Boolean > elementsToAdd ) {
121206 // given
122207 bitArray .addAll (elementsToAdd );
@@ -143,16 +228,18 @@ void fromString_throws_on_bad_string(String faultyString) {
143228 }
144229
145230
146- static Stream <Named <List <Boolean >>> testCaseBooleans () {
147- synchronized (consistentRandom ) {
148- consistentRandom .setSeed (SEED );
149- return Stream .of (
150- Named .of ("0" , List .of ()),
151- Named .of ("1" , List .of (Boolean .FALSE )),
152- Named .of ("2" , List .of (Boolean .TRUE , Boolean .FALSE )),
153- Named .of ("5" , List .of (Boolean .TRUE , Boolean .FALSE , Boolean .FALSE , Boolean .TRUE , Boolean .TRUE )),
154- Named .of ("100" , Stream .generate (consistentRandom ::nextBoolean ).limit (100 ).collect (Collectors .toList ()))
155- );
156- }
231+ @ ParameterizedTest (name = "{0} elements before clear" )
232+ @ MethodSource ("gr.geompokon.bitarray.TestUtils#testCaseBooleans" )
233+ @ DisplayName ("Cleared array should be empty" )
234+ void clear_leaves_empty_list (List <Boolean > elementsToAdd ) {
235+ // given
236+ bitArray .addAll (elementsToAdd );
237+
238+ // when
239+ bitArray .clear ();
240+
241+ // then
242+ assertThat (bitArray ).isEmpty ();
157243 }
244+
158245}
0 commit comments