Skip to content

Commit

Permalink
Serialization test. (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ts-wong authored Sep 6, 2023
1 parent 065ed56 commit 72a0f0e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
<configuration>
<source>1.5</source>
<target>1.5</target>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>
</configuration>
</plugin>
<plugin>
Expand Down
82 changes: 82 additions & 0 deletions src/test/java/com/zaxxer/sparsebits/SerializationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.zaxxer.sparsebits;

import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

/**
* Tests serialization of SparseBitSet of different number of set bits,
* from 1 to MAX_BITS.
*/
public class SerializationTest {

private static final Path DST = Paths.get(System.getProperty("java.io.tmpdir"), "sbs.ser");
private static final int MAX_BITS = Integer.parseInt(System.getProperty("test.max_bits", "100"));

private int numOfBits;
private int[] data;
private SparseBitSet sparseBitSet;

@Test
public void testFromOneToNBits() {
IntStream.range(1, MAX_BITS + 1).forEach(this::execute);
}

private void execute(int numOfBits) {
try {
this.numOfBits = numOfBits;
this.sparseBitSet = new SparseBitSet();

createData();
fillSparseBitSet();
write();

this.sparseBitSet = null;
sortData();
read();

verify();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private void verify() {
for (int i = sparseBitSet.nextSetBit(0), j = 0; i >= 0; i = sparseBitSet.nextSetBit(i + 1), j++) {
assertEquals(data[j], i);
}
}

private void createData() {
Set<Integer> s = new HashSet<>();
Random r = new Random();
while (s.size() < numOfBits) {
s.add(r.nextInt(Integer.MAX_VALUE));
}
data = s.stream().mapToInt(i -> i).toArray();
}

private void sortData() {
Arrays.sort(data);
}

private void fillSparseBitSet() {
IntStream.of(data).forEach(sparseBitSet::set);
}

private void write() throws Exception {
try (ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream(DST.toFile()))) {
s.writeObject(sparseBitSet);
}
}

private void read() throws Exception {
try (ObjectInputStream s = new ObjectInputStream(new FileInputStream(DST.toFile()))) {
sparseBitSet = (SparseBitSet) s.readObject();
}
}
}

0 comments on commit 72a0f0e

Please sign in to comment.