aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/test/java/me/lucko/spark/common/util
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2024-07-29 18:33:08 +0100
committerLuck <git@lucko.me>2024-07-29 18:33:08 +0100
commit60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7 (patch)
tree2bf8fcf914ac57466549d35dcd89ef96d3a2d65f /spark-common/src/test/java/me/lucko/spark/common/util
parent4c0149b6a15fa887328bbd88c8055c2138cc4d72 (diff)
downloadspark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.tar.gz
spark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.tar.bz2
spark-60d54cc4df05e3328f8b8d64ea3b44d5d22c9ed7.zip
Add some unit tests
Diffstat (limited to 'spark-common/src/test/java/me/lucko/spark/common/util')
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/FormatUtilTest.java67
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/IndexedListBuilderTest.java42
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/JavaVersionTest.java41
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/MethodDisambiguatorTest.java66
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/RollingAverageTest.java81
-rw-r--r--spark-common/src/test/java/me/lucko/spark/common/util/ThreadFinderTest.java50
6 files changed, 347 insertions, 0 deletions
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/FormatUtilTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/FormatUtilTest.java
new file mode 100644
index 0000000..5b77fb5
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/FormatUtilTest.java
@@ -0,0 +1,67 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class FormatUtilTest {
+
+ @Test
+ public void testPercent() {
+ assertEquals("50%", FormatUtil.percent(0.5, 1));
+ assertEquals("100%", FormatUtil.percent(1, 1));
+ assertEquals("0%", FormatUtil.percent(0, 1));
+
+ assertEquals("50%", FormatUtil.percent(50, 100));
+ assertEquals("100%", FormatUtil.percent(100, 100));
+ assertEquals("0%", FormatUtil.percent(0, 100));
+ }
+
+ @Test
+ public void testBytes() {
+ assertEquals("0 bytes", FormatUtil.formatBytes(0));
+ assertEquals("1.0 bytes", FormatUtil.formatBytes(1));
+ assertEquals("1.0 KB", FormatUtil.formatBytes(1024));
+ assertEquals("1.0 MB", FormatUtil.formatBytes(1024 * 1024));
+ assertEquals("1.0 GB", FormatUtil.formatBytes(1024 * 1024 * 1024));
+ assertEquals("1.0 TB", FormatUtil.formatBytes(1024L * 1024 * 1024 * 1024));
+
+ assertEquals("2.5 KB", FormatUtil.formatBytes((long) (1024 * 2.5d)));
+ assertEquals("2.5 MB", FormatUtil.formatBytes((long) (1024 * 1024 * 2.5d)));
+ }
+
+ @Test
+ public void testSeconds() {
+ assertEquals("0s", FormatUtil.formatSeconds(0));
+ assertEquals("1s", FormatUtil.formatSeconds(1));
+ assertEquals("59s", FormatUtil.formatSeconds(59));
+ assertEquals("1m", FormatUtil.formatSeconds(60));
+ assertEquals("1m 1s", FormatUtil.formatSeconds(61));
+ assertEquals("1m 59s", FormatUtil.formatSeconds(119));
+ assertEquals("2m", FormatUtil.formatSeconds(120));
+ assertEquals("2m 1s", FormatUtil.formatSeconds(121));
+ assertEquals("2m 59s", FormatUtil.formatSeconds(179));
+ assertEquals("3m", FormatUtil.formatSeconds(180));
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/IndexedListBuilderTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/IndexedListBuilderTest.java
new file mode 100644
index 0000000..bb3820e
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/IndexedListBuilderTest.java
@@ -0,0 +1,42 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class IndexedListBuilderTest {
+
+ @Test
+ public void testIndexes() {
+ IndexedListBuilder<String> builder = new IndexedListBuilder<>();
+
+ assertEquals(0, builder.add("a"));
+ assertEquals(1, builder.add("b"));
+ assertEquals(2, builder.add("c"));
+
+ assertEquals(Arrays.asList("a", "b", "c"), builder.build());
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/JavaVersionTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/JavaVersionTest.java
new file mode 100644
index 0000000..470f4d0
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/JavaVersionTest.java
@@ -0,0 +1,41 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class JavaVersionTest {
+
+ @Test
+ public void testJavaVersion() {
+ assertEquals(7, JavaVersion.parseJavaVersion("1.7"));
+ assertEquals(8, JavaVersion.parseJavaVersion("1.8"));
+ assertEquals(9, JavaVersion.parseJavaVersion("9"));
+ assertEquals(11, JavaVersion.parseJavaVersion("11"));
+ assertEquals(17, JavaVersion.parseJavaVersion("17"));
+ assertEquals(9, JavaVersion.parseJavaVersion("9.0.1"));
+ assertEquals(11, JavaVersion.parseJavaVersion("11.0.1"));
+ assertEquals(17, JavaVersion.parseJavaVersion("17.0.1"));
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/MethodDisambiguatorTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/MethodDisambiguatorTest.java
new file mode 100644
index 0000000..f0bea8f
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/MethodDisambiguatorTest.java
@@ -0,0 +1,66 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import me.lucko.spark.test.TestClass;
+import me.lucko.spark.common.util.MethodDisambiguator.MethodDescription;
+import me.lucko.spark.common.util.classfinder.FallbackClassFinder;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class MethodDisambiguatorTest {
+
+ private static final MethodDisambiguator DISAMBIGUATOR = new MethodDisambiguator(FallbackClassFinder.INSTANCE);
+
+ @ParameterizedTest
+ @CsvSource({
+ "25, test(Ljava/lang/String;)V",
+ "26, test(Ljava/lang/String;)V",
+ "27, test(Ljava/lang/String;)V",
+ "28, test(Ljava/lang/String;)V",
+ "31, test(I)V",
+ "32, test(I)V",
+ "33, test(I)V",
+ "34, test(I)V",
+ "37, test(Z)V",
+ "38, test(Z)V",
+ "39, test(Z)V",
+ "40, test(Z)V",
+ })
+ public void testSuccessfulDisambiguate(int line, String expectedDesc) {
+ MethodDescription method = DISAMBIGUATOR.disambiguate(TestClass.class.getName(), "test", line).orElse(null);
+ assertNotNull(method);
+ assertEquals(expectedDesc, method.toString());
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = {24, 29, 100})
+ public void testUnsuccessfulDisambiguate(int line) {
+ MethodDescription method = DISAMBIGUATOR.disambiguate(TestClass.class.getName(), "test", line).orElse(null);
+ assertNull(method);
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/RollingAverageTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/RollingAverageTest.java
new file mode 100644
index 0000000..a5b4a00
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/RollingAverageTest.java
@@ -0,0 +1,81 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class RollingAverageTest {
+
+ @Test
+ public void testMean() {
+ RollingAverage ra = new RollingAverage(3);
+ ra.add(BigDecimal.valueOf(1));
+ ra.add(BigDecimal.valueOf(2));
+ ra.add(BigDecimal.valueOf(3));
+
+ assertEquals(2, ra.mean());
+ ra.add(BigDecimal.valueOf(4));
+ assertEquals(3, ra.mean());
+ ra.add(BigDecimal.valueOf(5));
+ assertEquals(4, ra.mean());
+ ra.add(BigDecimal.valueOf(6));
+ assertEquals(5, ra.mean());
+ }
+
+ @Test
+ public void testMax() {
+ RollingAverage ra = new RollingAverage(3);
+ ra.add(BigDecimal.valueOf(1));
+ ra.add(BigDecimal.valueOf(2));
+ ra.add(BigDecimal.valueOf(3));
+
+ assertEquals(3, ra.max());
+ }
+
+ @Test
+ public void testMin() {
+ RollingAverage ra = new RollingAverage(3);
+ ra.add(BigDecimal.valueOf(1));
+ ra.add(BigDecimal.valueOf(2));
+ ra.add(BigDecimal.valueOf(3));
+
+ assertEquals(1, ra.min());
+ }
+
+ @Test
+ public void testPercentile() {
+ RollingAverage ra = new RollingAverage(3);
+ ra.add(BigDecimal.valueOf(1));
+ ra.add(BigDecimal.valueOf(2));
+ ra.add(BigDecimal.valueOf(3));
+
+ assertEquals(1, ra.percentile(0));
+ assertEquals(2, ra.percentile(0.25));
+ assertEquals(2, ra.percentile(0.5));
+ assertEquals(3, ra.percentile(0.75));
+ assertEquals(3, ra.percentile(1));
+ }
+
+}
diff --git a/spark-common/src/test/java/me/lucko/spark/common/util/ThreadFinderTest.java b/spark-common/src/test/java/me/lucko/spark/common/util/ThreadFinderTest.java
new file mode 100644
index 0000000..bffbf27
--- /dev/null
+++ b/spark-common/src/test/java/me/lucko/spark/common/util/ThreadFinderTest.java
@@ -0,0 +1,50 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ThreadFinderTest {
+
+ @Test
+ public void testFindThread() {
+ Thread thread = new Thread(() -> {
+ try {
+ Thread.sleep(100_000);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ }, "test-thread-1");
+ thread.start();
+
+ ThreadFinder threadFinder = new ThreadFinder();
+ List<Thread> threads = threadFinder.getThreads().collect(Collectors.toList());
+ assertTrue(threads.contains(thread));
+
+ thread.interrupt();
+ }
+
+}