diff --git a/Core/src/test/java/org/tribuo/util/MurmurHash3Test.java b/Core/src/test/java/org/tribuo/util/MurmurHash3Test.java new file mode 100644 index 000000000..8e2f08f90 --- /dev/null +++ b/Core/src/test/java/org/tribuo/util/MurmurHash3Test.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.tribuo.util; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Unit tests for {@link MurmurHash3}. + */ +class MurmurHash3Test { + + @Test + void testFmix32() { + assertEquals(1364076727, MurmurHash3.fmix32(1)); + assertEquals(-2114883783, MurmurHash3.fmix32(-1)); + assertEquals(-383449968, MurmurHash3.fmix32(10)); + assertEquals(-36807446, MurmurHash3.fmix32(100)); + assertEquals(-1434176238, MurmurHash3.fmix32(21323)); + } + + @Test + void testFmix64() { + assertEquals(-5451962507482445012L, MurmurHash3.fmix64(1)); + assertEquals(7256831767414464289L, MurmurHash3.fmix64(-1)); + assertEquals(7233188113542599437L, MurmurHash3.fmix64(10)); + assertEquals(-1819968182471218078L, MurmurHash3.fmix64(100)); + assertEquals(3402563454534931576L, MurmurHash3.fmix64(21323)); + } + +} \ No newline at end of file diff --git a/Core/src/test/java/org/tribuo/util/UtilTest.java b/Core/src/test/java/org/tribuo/util/UtilTest.java index 97dbeea95..cda9db7ce 100644 --- a/Core/src/test/java/org/tribuo/util/UtilTest.java +++ b/Core/src/test/java/org/tribuo/util/UtilTest.java @@ -30,7 +30,7 @@ import static org.junit.jupiter.api.Assertions.fail; /** - * + * Unit Test for class {@link Util}. */ public class UtilTest { private static final double DELTA = 1e-12; @@ -50,6 +50,21 @@ public void testArgmax() { assertEquals(3, argmax.getB()); } + @Test + public void testArgmin() { + assertThrows(IllegalArgumentException.class, () -> Util.argmin(new ArrayList())); + + List lst = Collections.singletonList(1); + Pair argmin = Util.argmin(lst); + assertEquals(0, argmin.getA()); + assertEquals(1, argmin.getB()); + + lst = Arrays.asList(3, 2, 1); + argmin = Util.argmin(lst); + assertEquals(2, argmin.getA()); + assertEquals(1, argmin.getB()); + } + @Test public void testAUC() { double output; @@ -80,4 +95,26 @@ public void testAUC() { assertEquals(0.5,output,DELTA); } + @Test + public void testBinarySearch(){ + + List stringList = Arrays.asList("apple", "banana", "cherry", "date", "fig", "grape"); + List intList = List.of(1, 3, 5, 7, 9); + + assertEquals( -1, Util.binarySearch(stringList, "Cherry")); + assertEquals( 2, Util.binarySearch(stringList, "cherry")); + + assertEquals(2, Util.binarySearch(intList, 5, x -> x)); + assertEquals(-4, Util.binarySearch(intList, 6, x -> x)); + + } + + @Test + public void testProduct(){ + assertEquals(0, Util.product(new int[]{0, 0})); + assertEquals(0, Util.product(new int[]{1, 0})); + assertEquals(0, Util.product(new int[]{0, 1, 2, 3, 4, 5})); + assertEquals(120, Util.product(new int[]{1, 1, 2, 3, 4, 5})); + } + }