/** * Asserts that two {@code float} values are equal or very close -- that the absolute value of their * difference is at most {@link #FLOAT_EPSILON}. * * @param expected test's expected value * @param actual actual value */ public static void assertEquals(float expected, float actual) { assertEquals(expected, actual, FLOAT_EPSILON); }
public static void assertArrayEquals(float[] expecteds, float[] actuals) { assertArrayEquals(expecteds, actuals, FLOAT_EPSILON); }
public static void assertRange(double a, double min, double max) { assertGreaterOrEqual(a, min); assertLessOrEqual(a, max); }
public static <T> void assertContainsSame(Collection<T> expected, Collection<T> actual) { if (expected == null) { assertNull(actual); } else { assertNotNull(actual); assertEquals(expected.size(), actual.size()); if (!expected.containsAll(actual)) { fail("Found unexpected values " + minus(actual, expected)); } if (!actual.containsAll(expected)) { fail("Missing values " + minus(expected, actual)); } } }
public static <T> void assertContains(Collection<T> collection, T value) { assertTrue(abbreviatedToString(collection) + " should contain " + value, collection.contains(value)); }
public static <T> void assertNotContains(Collection<T> collection, T value) { assertFalse(abbreviatedToString(collection) + " should not contain " + value, collection.contains(value)); }
static void testTopCount(int expectedSize, List<IDCount> top) { Assert.assertEquals(expectedSize, top.size()); for (int i = 0; i < top.size(); i++) { int thisCount = top.get(i).getCount(); OryxTest.assertGreaterOrEqual(thisCount, 1); if (i > 0) { OryxTest.assertGreaterOrEqual(top.get(i - 1).getCount(), thisCount); } } }
public static void assertContains(String value, String target) { assertTrue("'" + value + " should contain '" + target + "'", value.contains(target)); }
@Test public void testKnownItems() { List<String> items = target("/knownItems/U1").request() .accept(MediaType.APPLICATION_JSON_TYPE).get(LIST_STRING_TYPE); Assert.assertEquals(5, items.size()); for (int i : new int[] {1, 4, 5, 7, 8}) { OryxTest.assertContains(items, "I" + i); } }
static void testTopByValue(int expectedSize, List<IDValue> values, boolean reverse) { Assert.assertEquals(expectedSize, values.size()); for (int i = 0; i < values.size(); i++) { IDValue value = values.get(i); double thisScore = value.getValue(); Assert.assertFalse(Double.isNaN(thisScore)); Assert.assertFalse(Double.isInfinite(thisScore)); if (i > 0) { double lastScore = values.get(i-1).getValue(); if (reverse) { OryxTest.assertLessOrEqual(lastScore, thisScore); } else { OryxTest.assertGreaterOrEqual(lastScore, thisScore); } } } }
/** * Asserts that two {@code double} values are equal or very close -- that the absolute value of their * difference is at most {@link #DOUBLE_EPSILON}. * * @param expected test's expected value * @param actual actual value */ @SuppressWarnings("deprecation") public static void assertEquals(double expected, double actual) { assertEquals(expected, actual, DOUBLE_EPSILON); }
public static void assertNonEmpty(Path p) throws IOException { assertTrue("File should exist: " + p, Files.exists(p)); assertTrue("File should not be empty: " + p, Files.size(p) > 0); }
@Test public void testAllItemIDs() { List<String> users = target("/user/allIDs").request() .accept(MediaType.APPLICATION_JSON_TYPE).get(LIST_STRING_TYPE); Assert.assertEquals(7, users.size()); for (int user = 0; user < 7; user++) { OryxTest.assertContains(users, "U" + user); } }
public static void assertArrayEquals(double[] expecteds, double[] actuals) { assertArrayEquals(expecteds, actuals, DOUBLE_EPSILON); }
@Test public void testSumWithNaN() throws Exception { OryxTest.assertEquals(1.0, MLFunctions.SUM_WITH_NAN.call(Arrays.asList(1.0)).doubleValue()); OryxTest.assertEquals(6.0, MLFunctions.SUM_WITH_NAN.call(Arrays.asList(1.0, 2.0, 3.0)).doubleValue()); OryxTest.assertEquals(3.0, MLFunctions.SUM_WITH_NAN.call(Arrays.asList(1.0, Double.NaN, 3.0)).doubleValue()); assertNaN(MLFunctions.SUM_WITH_NAN.call(Arrays.asList(1.0, 2.0, Double.NaN))); assertNaN(MLFunctions.SUM_WITH_NAN.call(Arrays.asList(Double.NaN))); }
public static <T> void assertInstanceOf(T value, Class<? extends T> clazz) { assertTrue(value + " of type " + value.getClass() + " should be a " + clazz, clazz.isAssignableFrom(value.getClass())); }
@Test public void testAllItemIDs() { List<String> items = target("/item/allIDs").request() .accept(MediaType.APPLICATION_JSON_TYPE).get(LIST_STRING_TYPE); Assert.assertEquals(9, items.size()); for (int item = 0; item < 9; item++) { OryxTest.assertContains(items, "I" + item); } }
public static void sleepSeconds(int seconds) { assertTrue(seconds >= 0); if (seconds > 0) { try { Thread.sleep(TimeUnit.MILLISECONDS.convert(seconds, TimeUnit.SECONDS)); } catch (InterruptedException e) { log.warn("Interrupted while sleeping; continuing"); } } }
@Test public void testPopularRepresentativeItems() { List<String> items = target("/popularRepresentativeItems").request() .accept(MediaType.APPLICATION_JSON_TYPE).get(LIST_STRING_TYPE); Assert.assertEquals(2, items.size()); // These are tied in the current model: OryxTest.assertContains(Arrays.asList("I0", "I3"), items.get(0)); Assert.assertEquals("I4", items.get(1)); }
/** * Asserts that the probability of sampling a value as or more extreme than the given value, * from the given discrete distribution, is at least 0.001. * * @param value sample value * @param dist discrete distribution */ public static void checkDiscreteProbability(int value, IntegerDistribution dist) { double probAsExtreme = value <= dist.getNumericalMean() ? dist.cumulativeProbability(value) : (1.0 - dist.cumulativeProbability(value - 1)); assertTrue(value + " is not likely (" + probAsExtreme + " ) to differ from expected value " + dist.getNumericalMean() + " by chance", probAsExtreme >= 0.001); }