From 448231f18ba53edf5c0e7ab4b6732ef69328281c Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 30 Nov 2022 10:04:50 +0100 Subject: Reduce the simple usage of guava where java has caught up --- .../com/yahoo/stream/CustomCollectorsTest.java | 5 +- .../com/yahoo/tensor/MappedTensorTestCase.java | 7 +- .../java/com/yahoo/tensor/MixedTensorTestCase.java | 17 ++-- .../test/java/com/yahoo/tensor/TensorTestCase.java | 7 +- .../com/yahoo/tensor/functions/MatmulTestCase.java | 7 +- .../src/test/java/com/yahoo/text/Benchmark.java | 108 --------------------- .../src/test/java/com/yahoo/text/Utf8TestCase.java | 76 ++------------- 7 files changed, 31 insertions(+), 196 deletions(-) delete mode 100644 vespajlib/src/test/java/com/yahoo/text/Benchmark.java (limited to 'vespajlib/src/test') diff --git a/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java b/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java index a5b2accec48..b8a13525159 100644 --- a/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java +++ b/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.stream; -import com.google.common.collect.Lists; import com.yahoo.stream.CustomCollectors.DuplicateKeyException; import org.junit.Test; @@ -42,7 +41,7 @@ public class CustomCollectorsTest { @Test public void custom_map_collector_throws_exception_upon_duplicate_keys() { - List duplicates = Lists.newArrayList("same", "same"); + List duplicates = List.of("same", "same"); try { duplicates.stream().collect(toCustomMap(Function.identity(), Function.identity(), HashMap::new)); @@ -53,7 +52,7 @@ public class CustomCollectorsTest { } private static List numberList() { - return Lists.newArrayList("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"); + return List.of("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"); } private static class CustomHashMap extends HashMap { diff --git a/vespajlib/src/test/java/com/yahoo/tensor/MappedTensorTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/MappedTensorTestCase.java index ba814f7ad54..09b989e4380 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/MappedTensorTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/MappedTensorTestCase.java @@ -1,9 +1,10 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor; -import com.google.common.collect.Sets; import org.junit.Test; +import java.util.Set; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -34,7 +35,7 @@ public class MappedTensorTestCase { Tensor tensor = Tensor.Builder.of(type). cell().label("x", "0").value(1). cell().label("x", "1").value(2).build(); - assertEquals(Sets.newHashSet("x"), tensor.type().dimensionNames()); + assertEquals(Set.of("x"), tensor.type().dimensionNames()); assertEquals("tensor(x{}):{0:1.0, 1:2.0}", tensor.toString()); } @@ -44,7 +45,7 @@ public class MappedTensorTestCase { Tensor tensor = Tensor.Builder.of(type). cell().label("x", "0").label("y", "0").value(1). cell().label("x", "1").label("y", "0").value(2).build(); - assertEquals(Sets.newHashSet("x", "y"), tensor.type().dimensionNames()); + assertEquals(Set.of("x", "y"), tensor.type().dimensionNames()); assertEquals("tensor(x{},y{}):{{x:0,y:0}:1.0, {x:1,y:0}:2.0}", tensor.toString()); } diff --git a/vespajlib/src/test/java/com/yahoo/tensor/MixedTensorTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/MixedTensorTestCase.java index a26e56c4468..0b5b761224b 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/MixedTensorTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/MixedTensorTestCase.java @@ -2,9 +2,10 @@ package com.yahoo.tensor; -import com.google.common.collect.Sets; import org.junit.Test; +import java.util.Set; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -40,7 +41,7 @@ public class MixedTensorTestCase { cell().label("y", 1).value(2). // {y:2} should be 0.0 and non NaN since we specify indexed size build(); - assertEquals(Sets.newHashSet("y"), tensor.type().dimensionNames()); + assertEquals(Set.of("y"), tensor.type().dimensionNames()); assertEquals("tensor(y[3]):[1.0, 2.0, 0.0]", tensor.toString()); } @@ -56,7 +57,7 @@ public class MixedTensorTestCase { cell().label("x", 1).label("y", 1).value(5). cell().label("x", 1).label("y", 2).value(6). build(); - assertEquals(Sets.newHashSet("x", "y"), tensor.type().dimensionNames()); + assertEquals(Set.of("x", "y"), tensor.type().dimensionNames()); assertEquals("tensor(x[2],y[3]):[[1.0, 2.0, 0.0], [4.0, 5.0, 6.0]]", tensor.toString()); } @@ -68,7 +69,7 @@ public class MixedTensorTestCase { cell().label("x", "0").value(1). cell().label("x", "1").value(2). build(); - assertEquals(Sets.newHashSet("x"), tensor.type().dimensionNames()); + assertEquals(Set.of("x"), tensor.type().dimensionNames()); assertEquals("tensor(x{}):{0:1.0, 1:2.0}", tensor.toString()); } @@ -83,7 +84,7 @@ public class MixedTensorTestCase { cell().label("x", "1").label("y", "1").value(5). cell().label("x", "1").label("y", "2").value(6). build(); - assertEquals(Sets.newHashSet("x", "y"), tensor.type().dimensionNames()); + assertEquals(Set.of("x", "y"), tensor.type().dimensionNames()); assertEquals("tensor(x{},y{}):{{x:0,y:0}:1.0, {x:0,y:1}:2.0, {x:1,y:0}:4.0, {x:1,y:1}:5.0, {x:1,y:2}:6.0}", tensor.toString()); } @@ -99,7 +100,7 @@ public class MixedTensorTestCase { cell().label("x", "2").label("y", 1).value(5). cell().label("x", "2").label("y", 2).value(6). build(); - assertEquals(Sets.newHashSet("x", "y"), tensor.type().dimensionNames()); + assertEquals(Set.of("x", "y"), tensor.type().dimensionNames()); assertEquals("tensor(x{},y[3]):{1:[1.0, 2.0, 0.0], 2:[4.0, 5.0, 6.0]}", tensor.toString()); } @@ -121,7 +122,7 @@ public class MixedTensorTestCase { cell().label("x", "x2").label("y", 2).label("z","z1").value(15). cell().label("x", "x2").label("y", 2).label("z","z2").value(16). build(); - assertEquals(Sets.newHashSet("x", "y", "z"), tensor.type().dimensionNames()); + assertEquals(Set.of("x", "y", "z"), tensor.type().dimensionNames()); assertEquals("tensor(x{},y[3],z{}):{{x:x1,y:0,z:z1}:1.0, {x:x1,y:0,z:z2}:2.0, {x:x1,y:1,z:z1}:3.0, " + "{x:x1,y:1,z:z2}:4.0, {x:x1,y:2,z:z1}:5.0, {x:x1,y:2,z:z2}:6.0, {x:x2,y:0,z:z1}:11.0, " + "{x:x2,y:0,z:z2}:12.0, {x:x2,y:1,z:z1}:13.0, {x:x2,y:1,z:z2}:14.0, {x:x2,y:2,z:z1}:15.0, {x:x2,y:2,z:z2}:16.0}", @@ -149,7 +150,7 @@ public class MixedTensorTestCase { cell().label("i", "b").label("k","d").label("j",1).label("l",0).value(15). cell().label("i", "b").label("k","d").label("j",1).label("l",1).value(16). build(); - assertEquals(Sets.newHashSet("i", "j", "k", "l"), tensor.type().dimensionNames()); + assertEquals(Set.of("i", "j", "k", "l"), tensor.type().dimensionNames()); assertEquals("tensor(i{},j[2],k{},l[2]):{{i:a,j:0,k:c,l:0}:1.0, {i:a,j:0,k:c,l:1}:2.0, " + "{i:a,j:0,k:d,l:0}:5.0, {i:a,j:0,k:d,l:1}:6.0, {i:a,j:1,k:c,l:0}:3.0, {i:a,j:1,k:c,l:1}:4.0, " + "{i:a,j:1,k:d,l:0}:7.0, {i:a,j:1,k:d,l:1}:8.0, {i:b,j:0,k:c,l:0}:9.0, {i:b,j:0,k:c,l:1}:10.0, " + diff --git a/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java index 428c8a83b47..5e392a74bf3 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.evaluation.MapEvaluationContext; import com.yahoo.tensor.evaluation.Name; import com.yahoo.tensor.evaluation.VariableTensor; @@ -154,7 +153,7 @@ public class TensorTestCase { Tensor xy = Tensor.from("{{x:0,y:1}:11, {x:1,y:1}:13}"); double nest1 = y.multiply(x.multiply(xy).sum("x")).sum("y").asDouble(); double nest2 = x.multiply(xy).sum("x").multiply(y).sum("y").asDouble(); - double flat = y.multiply(x).multiply(xy).sum(ImmutableList.of("x","y")).asDouble(); + double flat = y.multiply(x).multiply(xy).sum(List.of("x","y")).asDouble(); assertEquals(nest1, flat, 0.000000001); assertEquals(nest2, flat, 0.000000001); } @@ -176,8 +175,8 @@ public class TensorTestCase { assertEquals(Tensor.from("{ {x:1,y:1}:0, {x:2,y:1}:1 }"), tensor1.larger(tensor2)); assertEquals(Tensor.from("{ {y:1}:50.0 }"), tensor1.matmul(tensor2, "x")); assertEquals(Tensor.from("{ {z:1}:3, {z:2}:7 }"), tensor1.rename("x", "z")); - assertEquals(Tensor.from("{ {y:1,x:1}:8, {x:1,y:2}:12 }"), tensor1.add(tensor2).rename(ImmutableList.of("x", "y"), - ImmutableList.of("y", "x"))); + assertEquals(Tensor.from("{ {y:1,x:1}:8, {x:1,y:2}:12 }"), tensor1.add(tensor2).rename(List.of("x", "y"), + List.of("y", "x"))); assertEquals(Tensor.from("{ {x:0,y:0}:0, {x:0,y:1}:0, {x:1,y:0}:0, {x:1,y:1}:1, {x:2,y:0}:0, {x:2,y:1}:2, }"), Tensor.generate(new TensorType.Builder().indexed("x", 3).indexed("y", 2).build(), (List indexes) -> (double)indexes.get(0)*indexes.get(1))); diff --git a/vespajlib/src/test/java/com/yahoo/tensor/functions/MatmulTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/functions/MatmulTestCase.java index fb4cb659624..1ac796ae9c5 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/functions/MatmulTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/functions/MatmulTestCase.java @@ -1,11 +1,12 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorType; import org.junit.Test; +import java.util.List; + import static org.junit.Assert.assertEquals; /** @@ -41,7 +42,7 @@ public class MatmulTestCase { rb.cell(154,1, 1); Tensor r = rb.build(); - Tensor result = a.matmul(b.rename(ImmutableList.of("d0","d1"), ImmutableList.of("d1","d2")), "d1") + Tensor result = a.matmul(b.rename(List.of("d0","d1"), List.of("d1","d2")), "d1") .rename("d2","d1"); assertEquals(r, result); } @@ -90,7 +91,7 @@ public class MatmulTestCase { rb.cell(730,1, 1, 1); Tensor r = rb.build(); - Tensor result = a.matmul(b.rename(ImmutableList.of("d1","d2"), ImmutableList.of("d2","d3")), "d2") + Tensor result = a.matmul(b.rename(List.of("d1","d2"), List.of("d2","d3")), "d2") .rename("d3","d2"); assertEquals(r, result); } diff --git a/vespajlib/src/test/java/com/yahoo/text/Benchmark.java b/vespajlib/src/test/java/com/yahoo/text/Benchmark.java deleted file mode 100644 index 292503f462a..00000000000 --- a/vespajlib/src/test/java/com/yahoo/text/Benchmark.java +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.text; - -// import com.google.common.base.Preconditions; -// import com.google.inject.Provider; - -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.concurrent.Callable; -import java.util.concurrent.CyclicBarrier; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; - -/** - * @author Simon Thoresen Hult - */ -class Benchmark { - - public static interface Task { - public long run(CyclicBarrier barrier, int numIterations) throws Exception; - } - - - public static class TaskProvider { - final Class taskClass; - public Task get() { - try { - return taskClass.getDeclaredConstructor().newInstance(); - } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { - throw new RuntimeException(e); - } - } - public TaskProvider(final Class taskClass) { - this.taskClass = taskClass; - } - } - - private final TaskProvider taskProvider; - private final int numIterationsPerThread; - private final int numThreads; - - private Benchmark(Builder builder) { - Objects.requireNonNull(builder.taskProvider, "taskProvider"); -/* - Preconditions.checkArgument(builder.numIterationsPerThread > 0, "numIterationsPerThread; %s", - builder.numIterationsPerThread); - Preconditions.checkArgument(builder.numThreads > 0, "numThreads; %s", - builder.numThreads); -*/ - taskProvider = builder.taskProvider; - numIterationsPerThread = builder.numIterationsPerThread; - numThreads = builder.numThreads; - } - - public long run() throws Exception { - final CyclicBarrier barrier = new CyclicBarrier(numThreads); - List> clients = new ArrayList<>(numThreads); - for (int i = 0; i < numThreads; ++i) { - final Task task = taskProvider.get(); - clients.add(new Callable() { - - @Override - public Long call() throws Exception { - return task.run(barrier, numIterationsPerThread); - } - }); - } - long maxNanosPerClient = 0; - for (Future result : Executors.newFixedThreadPool(numThreads).invokeAll(clients)) { - maxNanosPerClient = Math.max(maxNanosPerClient, result.get()); - } - return TimeUnit.SECONDS.toNanos(1) * numThreads * numIterationsPerThread / maxNanosPerClient; - } - - public static class Builder { - - private TaskProvider taskProvider; - private int numIterationsPerThread = 1000; - private int numThreads = 1; - - public Builder setNumThreads(int numThreads) { - this.numThreads = numThreads; - return this; - } - - public Builder setNumIterationsPerThread(int numIterationsPerThread) { - this.numIterationsPerThread = numIterationsPerThread; - return this; - } - - public Builder setTaskClass(final Class taskClass) { - return setTaskProvider(new TaskProvider(taskClass)); - } - - public Builder setTaskProvider(TaskProvider taskProvider) { - this.taskProvider = taskProvider; - return this; - } - - public Benchmark build() { - return new Benchmark(this); - } - } - -} diff --git a/vespajlib/src/test/java/com/yahoo/text/Utf8TestCase.java b/vespajlib/src/test/java/com/yahoo/text/Utf8TestCase.java index 926d19f433f..098eaddacac 100644 --- a/vespajlib/src/test/java/com/yahoo/text/Utf8TestCase.java +++ b/vespajlib/src/test/java/com/yahoo/text/Utf8TestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.text; -import com.google.common.collect.ImmutableMap; import org.junit.Ignore; import org.junit.Test; @@ -11,9 +10,9 @@ import java.nio.ByteBuffer; import java.nio.charset.CharsetEncoder; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.Map; import java.util.function.Function; -import static com.yahoo.text.Lowercase.toLowerCase; import static com.yahoo.text.Utf8.calculateBytePositions; import static com.yahoo.text.Utf8.calculateStringPositions; import static org.hamcrest.CoreMatchers.equalTo; @@ -21,7 +20,6 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; /** * @author Bjorn Borud @@ -34,62 +32,6 @@ public class Utf8TestCase { 0x34, 0x355, 0x2567, 0xfff, 0xe987, 0x100abc }; - public void dumpSome() throws java.io.IOException { - int i = 32; - int j = 3; - int cnt = 0; - while (i < 0x110000) { - if (i < 0xD800 || i >= 0xE000) ++cnt; - i += j; - ++j; - } - System.out.println("allocate "+cnt+" array entries"); - int codes[] = new int[cnt]; - i = 32; - j = 3; - cnt = 0; - while (i < 0x110000) { - if (i < 0xD800 || i >= 0xE000) codes[cnt++] = i; - i += j; - ++j; - } - assertEquals(cnt, codes.length); - System.out.println("fill "+cnt+" array entries"); - String str = new String(codes, 0, cnt); - byte[] arr = Utf8.toBytes(str); - java.io.FileOutputStream fos = new java.io.FileOutputStream("random-long-utf8.dat"); - fos.write(arr); - fos.close(); - } - - public void dumpMore() throws java.io.IOException { - java.text.Normalizer.Form form = java.text.Normalizer.Form.NFKC; - - java.io.FileOutputStream fos = new java.io.FileOutputStream("lowercase-table.dat"); - for (int i = 0; i < 0x110000; i++) { - StringBuilder b = new StringBuilder(); - b.appendCodePoint(i); - String n1 = b.toString(); - String n2 = java.text.Normalizer.normalize(b, form); - if (n1.equals(n2)) { - String l = toLowerCase(n1); - int chars = l.length(); - int codes = l.codePointCount(0, chars); - if (codes != 1) { - System.out.println("codepoint "+i+" transformed into "+codes+" codepoints: "+n1+" -> "+l); - } else { - int lc = l.codePointAt(0); - if (lc != i) { - String o = "lowercase( "+i+" )= "+lc+"\n"; - byte[] arr = Utf8.toBytes(o); - fos.write(arr); - } - } - } - } - fos.close(); - } - @Test public void testSimple() { String s1 = "test"; @@ -322,7 +264,7 @@ public class Utf8TestCase { for (char c=0; c < i; c++) { sb.append(c); } - assertTrue(Arrays.equals(Utf8.toBytesStd(sb.toString()), Utf8.toBytes(sb.toString()))); + assertArrayEquals(Utf8.toBytesStd(sb.toString()), Utf8.toBytes(sb.toString())); } } @@ -340,7 +282,7 @@ public class Utf8TestCase { byte [] a = Utf8.toBytes(String.valueOf(l)); byte [] b = Utf8.toAsciiBytes(l); if (!Arrays.equals(a, b)) { - assertTrue(Arrays.equals(a, b)); + assertArrayEquals(a, b); } } @@ -348,8 +290,8 @@ public class Utf8TestCase { public void testBoolean() { assertEquals("true", String.valueOf(true)); assertEquals("false", String.valueOf(false)); - assertTrue(Arrays.equals(Utf8.toAsciiBytes(true), new Utf8String(String.valueOf(true)).getBytes())); - assertTrue(Arrays.equals(Utf8.toAsciiBytes(false), new Utf8String(String.valueOf(false)).getBytes())); + assertArrayEquals(Utf8.toAsciiBytes(true), new Utf8String(String.valueOf(true)).getBytes()); + assertArrayEquals(Utf8.toAsciiBytes(false), new Utf8String(String.valueOf(false)).getBytes()); } @Test public void testInt() @@ -358,7 +300,7 @@ public class Utf8TestCase { byte [] a = Utf8.toBytes(String.valueOf(l)); byte [] b = Utf8.toAsciiBytes(l); if (!Arrays.equals(a, b)) { - assertTrue(Arrays.equals(a, b)); + assertArrayEquals(a, b); } } } @@ -369,7 +311,7 @@ public class Utf8TestCase { byte [] a = Utf8.toBytes(String.valueOf(l)); byte [] b = Utf8.toAsciiBytes(l); if (!Arrays.equals(a, b)) { - assertTrue(Arrays.equals(a, b)); + assertArrayEquals(a, b); } } } @@ -561,7 +503,7 @@ public class Utf8TestCase { byte[] unicode = "This is just sort of random mix. \u5370\u57df\u60c5\u5831\u53EF\u4EE5\u6709x\u00e9\u00e8".getBytes(StandardCharsets.UTF_8); int iterations = 100_000; // Use 100_000+ for benchmarking - ImmutableMap.of("ascii", ascii, "unicode", unicode).forEach((type, b) -> { + Map.of("ascii", ascii, "unicode", unicode).forEach((type, b) -> { long time1 = benchmark(() -> decode(Utf8::toString, b, iterations)); System.out.printf("Utf8::toString of %s string took %d ms\n", type, time1); long time2 = benchmark(() -> decode((b1) -> new String(b1, StandardCharsets.UTF_8), b, iterations)); @@ -578,7 +520,7 @@ public class Utf8TestCase { String unicode = "This is just sort of random mix. \u5370\u57df\u60c5\u5831\u53EF\u4EE5\u6709x\u00e9\u00e8"; int iterations = 1_000_000; // Use 1_000_000+ for benchmarking - ImmutableMap.of("ascii", ascii, "unicode", unicode).forEach((type, s) -> { + Map.of("ascii", ascii, "unicode", unicode).forEach((type, s) -> { long time1 = benchmark(() -> encode(Utf8::toBytes, s, iterations)); System.out.printf("Utf8::toBytes of %s string took %d ms\n", type, time1); long time2 = benchmark(() -> encode((s1) -> s1.getBytes(StandardCharsets.UTF_8), s, iterations)); -- cgit v1.2.3