Skip to content

Commit 48448a7

Browse files
committed
Tests refactoring
1 parent 1a65765 commit 48448a7

File tree

2 files changed

+57873
-20
lines changed

2 files changed

+57873
-20
lines changed

portable/comp-transfer/src/test/java/org/bosik/merklesync/TestHashUtils.java

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@
2020
import org.junit.Ignore;
2121
import org.junit.Test;
2222

23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.net.URI;
26+
import java.net.URISyntaxException;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
2329
import java.util.ArrayList;
2430
import java.util.Arrays;
31+
import java.util.HashMap;
2532
import java.util.List;
2633
import java.util.Locale;
27-
import java.util.SortedMap;
28-
import java.util.TreeMap;
34+
import java.util.Map;
35+
import java.util.stream.Stream;
2936

3037
import static org.junit.Assert.assertEquals;
3138
import static org.junit.Assert.fail;
@@ -187,9 +194,6 @@ public void test_subHash()
187194
assertArraysEquals(new byte[] { 15, 14, 13 }, HashUtils.sub(new byte[] { 0, 0, 0 }, new byte[] { 1, 2, 3 }));
188195
assertArraysEquals(new byte[] { 15, 14, 13 }, HashUtils.sub(null, new byte[] { 1, 2, 3 }));
189196

190-
// "0f0e0d" - "000000" = "0f0e0d"
191-
assertArraysEquals(new byte[] { 15, 14, 13 }, HashUtils.sub(new byte[] { 15, 14, 13 }, new byte[] { 0, 0, 0 }));
192-
193197
// normal cases
194198
assertArraysEquals(new byte[] { 0, 0, 0 }, HashUtils.sub(new byte[] { 1, 2, 3 }, new byte[] { 1, 2, 3 }));
195199
// "7f" - "ff" = "80"
@@ -242,7 +246,7 @@ public void test_addHash()
242246
}
243247

244248
@Test
245-
public void test_sumIterable()
249+
public void test_sumHashes()
246250
{
247251
// given
248252
final List<String> hashes = Arrays.asList(
@@ -260,23 +264,49 @@ public void test_sumIterable()
260264

261265
@Test
262266
@Ignore("This test case is for manual performance check only")
263-
public void test_performance_buildHashTree()
267+
public void hashesLoadingTest()
264268
{
265-
long time = System.currentTimeMillis();
266-
SortedMap<String, String> data = new TreeMap<>();
267-
for (int i = 0; i < 500_000; i++)
269+
final double time = Profiler.measureInMsec(() ->
268270
{
269-
String id = HashUtils.generateGuid();
270-
String hash = HashUtils.generateGuid();
271-
data.put(id, hash);
272-
}
273-
time = System.currentTimeMillis() - time;
274-
System.out.println(String.format("%d items prepared in %d ms", data.size(), time));
271+
try
272+
{
273+
loadMap("merkletree/input_hashes.csv");
274+
}
275+
catch (Exception e)
276+
{
277+
throw new RuntimeException(e);
278+
}
279+
}, 100);
280+
281+
System.out.printf(Locale.US, "loadHashes(): %.3f ms%n", time);
282+
// unsorted: 73.64 ms
283+
// sorted: 41.668 ms (44% faster than unsorted)
284+
}
275285

276-
time = System.currentTimeMillis();
277-
SortedMap<String, String> tree = HashUtils.buildHashTree(data);
278-
time = System.currentTimeMillis() - time;
286+
private static Map<String, String> loadMap(String resourceName) throws IOException, URISyntaxException
287+
{
288+
final URI uri = TestHashUtils.class.getClassLoader()
289+
.getResource(resourceName)
290+
.toURI();
291+
return loadMap(new File(uri).toPath());
292+
}
293+
294+
private static Map<String, String> loadMap(Path fileName) throws IOException
295+
{
296+
final Map<String, String> data = new HashMap<>();
297+
298+
try (Stream<String> lines = Files.lines(fileName))
299+
{
300+
lines.skip(1) // header
301+
.forEach(line ->
302+
{
303+
final int k = line.indexOf('\t');
304+
final String id = line.substring(0, k);
305+
final String hash = line.substring(k + 1);
306+
data.put(id, hash);
307+
});
308+
}
279309

280-
System.out.println(String.format("Tree with %d items build in %d ms", tree.size(), time));
310+
return data;
281311
}
282312
}

0 commit comments

Comments
 (0)