Skip to content

Commit b30f75f

Browse files
author
George Bouroutzoglou
committed
Changes to tests and benchmarks
- Added parameters to tests and cleaned them up a little - Removed Benchmark test (replaced with benchmark repo @ https://github.com/Abductcows/bit-array-benchmarks)
1 parent d3313ea commit b30f75f

File tree

5 files changed

+78
-279
lines changed

5 files changed

+78
-279
lines changed

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<groupId>gr.geompokon</groupId>
99
<artifactId>bit-array</artifactId>
10-
<version>1.0.1</version>
10+
<version>1.0.2</version>
1111

1212
<name>BitArray</name>
1313
<description>Java List of Booleans class with reduced memory usage and higher performance</description>
@@ -25,6 +25,14 @@
2525
<groupId>org.junit.jupiter</groupId>
2626
<artifactId>junit-jupiter-engine</artifactId>
2727
<version>5.1.0</version>
28+
<scope>test</scope>
29+
<optional>true</optional>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter-params</artifactId>
34+
<version>5.1.0</version>
35+
<scope>test</scope>
2836
<optional>true</optional>
2937
</dependency>
3038
</dependencies>

src/main/java/gr/geompokon/bitarray/BitArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* not follow the one bit per entry principle.
5151
* </p>
5252
*
53-
* @version 1.0.1
53+
* @version 1.0.2
5454
* @see java.util.List
5555
* @see java.util.AbstractList
5656
* @see java.util.ArrayList

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

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package gr.geompokon.bitarray;
1818

1919
import org.junit.jupiter.api.BeforeEach;
20-
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.DisplayName;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.ValueSource;
2123

2224
import java.util.ArrayList;
2325
import java.util.List;
@@ -27,8 +29,6 @@
2729

2830
class BitArrayInterfaceTest {
2931

30-
final static int MAX_TEST_SIZE = 200;
31-
3232
static List<Boolean> bitArray;
3333
static List<Boolean> boolArray;
3434
static Random random;
@@ -71,9 +71,11 @@ void myAssertSameArrays() {
7171
/**
7272
* Random insertions at random indices
7373
*/
74-
@Test
75-
void addAtIndex() {
76-
for (int i = 0; i < MAX_TEST_SIZE; i++) {
74+
@ParameterizedTest
75+
@ValueSource(ints = {0, 1, 5, 10, 50, 100})
76+
@DisplayName("Lists should be the same when doing the same insertions")
77+
void addAtIndex(int elementsToAdd) {
78+
for (int i = 0; i < elementsToAdd; i++) {
7779
int index = random.nextInt(bitArray.size() + 1); // bound is exclusive
7880
boolean element = random.nextBoolean();
7981
bitArray.add(index, element);
@@ -85,15 +87,17 @@ void addAtIndex() {
8587
/**
8688
* Modification of elements at random indices
8789
*/
88-
@Test
89-
void set() {
90+
@ParameterizedTest
91+
@ValueSource(ints = {0, 1, 5, 10, 50, 100})
92+
@DisplayName("Lists should be the same when doing the same set operations")
93+
void set(int elementsToAdd) {
9094
// test empty array behaviour
9195
assertThrows(IndexOutOfBoundsException.class, () -> bitArray.set(0, true));
9296

9397
// test with elements
94-
initArrays(MAX_TEST_SIZE);
98+
initArrays(elementsToAdd);
9599

96-
for (int i = 0; i < MAX_TEST_SIZE; i++) {
100+
for (int i = 0; i < elementsToAdd; i++) {
97101
int index = random.nextInt(bitArray.size());
98102
Boolean negatedElement = !bitArray.get(index); // to ensure contents change
99103
bitArray.set(index, negatedElement);
@@ -106,49 +110,35 @@ void set() {
106110
/**
107111
* Remove of elements at random indices
108112
*/
109-
@Test
110-
void remove() {
113+
@ParameterizedTest
114+
@ValueSource(ints = {0, 1, 5, 10, 50, 100})
115+
@DisplayName("Lists should be the same at any point while doing the same removes")
116+
void remove(int elementsToAdd) {
111117
// test empty array behaviour
112118
assertThrows(IndexOutOfBoundsException.class, () -> bitArray.remove(0));
113119

114120
// test with elements
115-
initArrays(MAX_TEST_SIZE);
116-
for (int i = 0; i < MAX_TEST_SIZE; i++) {
121+
initArrays(elementsToAdd);
122+
for (int i = 0; i < elementsToAdd; i++) {
117123
int index = random.nextInt(boolArray.size());
118124
assertEquals(boolArray.remove(index), bitArray.remove(index));
119125
}
120126
}
121127

122-
@Test
123-
void clear() {
124-
initArrays(10);
128+
@ParameterizedTest
129+
@ValueSource(ints = {0, 1, 5, 10, 50, 100})
130+
@DisplayName("Cleared array should be empty")
131+
void clear(int elementsToAdd) {
132+
initArrays(elementsToAdd);
125133
bitArray.clear();
126134
assertTrue(bitArray.isEmpty());
127135
}
128136

129-
@Test
130-
void size() {
131-
// test newly created array size
132-
assertEquals(0, bitArray.size());
133-
134-
// add some elements
135-
initArrays(MAX_TEST_SIZE);
136-
int expectedSize = MAX_TEST_SIZE;
137-
assertEquals(expectedSize, bitArray.size());
138-
139-
// remove some
140-
int noToRemove = MAX_TEST_SIZE / 2;
141-
bitArray.subList(0, noToRemove).clear();
142-
143-
expectedSize -= noToRemove;
144-
assertEquals(expectedSize, bitArray.size());
145-
146-
// add back some
147-
int noToAdd = MAX_TEST_SIZE / 2;
148-
for (int i = 0; i < noToAdd; i++) {
149-
bitArray.add(random.nextBoolean());
150-
}
151-
expectedSize += noToAdd;
152-
assertEquals(expectedSize, bitArray.size());
137+
@ParameterizedTest
138+
@ValueSource(ints = {0, 1, 5, 10, 50, 100})
139+
@DisplayName("Number of elements should be the same as the number of insertions on new arrays")
140+
void size(int elementsToAdd) {
141+
initArrays(elementsToAdd);
142+
assertEquals(elementsToAdd, bitArray.size());
153143
}
154144
}

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

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@
1717
package gr.geompokon.bitarray;
1818

1919
import org.junit.jupiter.api.BeforeEach;
20-
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.DisplayName;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.ValueSource;
2123

2224
import java.util.ArrayList;
2325
import java.util.Random;
26+
import java.util.UnknownFormatConversionException;
2427

2528
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.fail;
2630

2731
class BitArrayTest {
2832

29-
final static int MAX_TEST_SIZE = 200;
30-
3133
static BitArray bitArray;
3234
static ArrayList<Boolean> boolArray;
3335
static Random random;
@@ -70,37 +72,50 @@ void myAssertSameArrays() {
7072
/**
7173
* Make the bit array from the boolean array
7274
*/
73-
@Test
74-
void testCopyConstructor() {
75-
initArrays(MAX_TEST_SIZE);
76-
bitArray.clear();
77-
75+
@ParameterizedTest
76+
@ValueSource(ints = {0, 1, 5, 10, 50, 100})
77+
@DisplayName("Copy constructor result should have same size and elements")
78+
void testCopyConstructor(int elementsToAdd) {
79+
initArrays(elementsToAdd);
7880
bitArray = new BitArray(boolArray);
81+
7982
myAssertSameArrays();
8083
}
8184

82-
@Test
83-
void testClone() {
84-
initArrays(MAX_TEST_SIZE);
85+
@ParameterizedTest
86+
@ValueSource(ints = {0, 1, 5, 10, 50, 100})
87+
@DisplayName("Result of clone() should have the same elements")
88+
void testClone(int elementsToAdd) {
89+
initArrays(elementsToAdd);
8590
BitArray clone = bitArray.clone();
8691

8792
assertEquals(bitArray, clone);
8893
}
8994

90-
@Test
91-
void testToFromString() {
95+
96+
@ParameterizedTest
97+
@ValueSource(ints = {0, 1, 5, 10, 50, 100})
98+
@DisplayName("Serialized and immediately deserialized array should be the same as original")
99+
void testToFromString(int elementsToAdd) {
92100
// test array with elements
93-
initArrays(MAX_TEST_SIZE);
94-
String bitArrayStr = bitArray.toString();
95-
BitArray copy = BitArray.fromString(bitArrayStr);
101+
initArrays(elementsToAdd);
102+
BitArray copy = BitArray.fromString(bitArray.toString());
96103

97104
assertEquals(bitArray, copy);
105+
}
98106

99-
100-
// test empty array
101-
bitArrayStr = new BitArray().toString();
102-
copy = BitArray.fromString(bitArrayStr);
103-
104-
assertEquals(new BitArray(), copy);
107+
@ParameterizedTest
108+
@ValueSource(strings = {"[0 1]", "Size = 2, [true true]", "Size =z, [0 1]", "Size = 3, [0 1]"})
109+
@DisplayName("Bad strings should throw specific exceptions")
110+
void testBadFromString(String faultyString) {
111+
try {
112+
BitArray.fromString(faultyString);
113+
} catch (Exception e) {
114+
if (!(e instanceof UnknownFormatConversionException
115+
|| e instanceof IndexOutOfBoundsException
116+
|| e instanceof NumberFormatException)) {
117+
fail(e);
118+
}
119+
}
105120
}
106121
}

0 commit comments

Comments
 (0)