datastore: Test the discrete and continuous range conditions
authorLoïc Prieur-Drevon <loic.prieurdrevon@gmail.com>
Mon, 19 Dec 2016 11:36:36 +0000 (12:36 +0100)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Thu, 2 Feb 2017 13:56:40 +0000 (08:56 -0500)
Change-Id: I6d56fd97c5d315d0b4173b006ec15565aa3053ae
Signed-off-by: Loïc Prieur-Drevon <loic.prieurdrevon@gmail.com>
Reviewed-on: https://git.eclipse.org/r/87393
Reviewed-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Reviewed-by: Hudson CI
Tested-by: Genevieve Bastien <gbastien+lttng@versatic.net>
statesystem/org.eclipse.tracecompass.datastore.core.tests/META-INF/MANIFEST.MF
statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/datastore/core/condition/ContinuousRangeConditionTest.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/datastore/core/condition/DiscreteRangeConditionTest.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/provisional/datastore/core/condition/RangeConditionTest.java [new file with mode: 0644]

index 8fcea04745f51735f683f62015e94d87bbdefee2..bb8b848a4f767644a8194ab16ec1745b5cb3300b 100644 (file)
@@ -8,4 +8,7 @@ Bundle-SymbolicName: org.eclipse.tracecompass.datastore.core.tests;singleton:=tr
 Fragment-Host: org.eclipse.tracecompass.datastore.core
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Require-Bundle: org.junit;bundle-version="4.0.0"
-Export-Package: org.eclipse.tracecompass.internal.datastore.core.serialization
+Export-Package: org.eclipse.tracecompass.internal.datastore.core.condition;x-internal:=true,
+ org.eclipse.tracecompass.internal.datastore.core.serialization;x-internal:=true,
+ org.eclipse.tracecompass.internal.provisional.datastore.core.condition;x-internal:=true
+Import-Package: com.google.common.collect
diff --git a/statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/datastore/core/condition/ContinuousRangeConditionTest.java b/statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/datastore/core/condition/ContinuousRangeConditionTest.java
new file mode 100644 (file)
index 0000000..5678636
--- /dev/null
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * Copyright (c) 2017 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ ******************************************************************************/
+
+package org.eclipse.tracecompass.internal.datastore.core.condition;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import org.eclipse.tracecompass.internal.provisional.datastore.core.condition.RangeCondition;
+import org.junit.Test;
+
+/**
+ * Test the continuous range condition with Integers.
+ *
+ * @author Loïc Prieur-Drevon
+ */
+public class ContinuousRangeConditionTest {
+
+    private static final int LOW = 0;
+    private static final int HIGH = 10;
+    private static final ContinuousRangeCondition<Integer> CONDITION = new ContinuousRangeCondition<>(LOW, HIGH);
+
+    /**
+     * Ensure that we cannot build a condition with a bigger low than high bound.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testConstructor() {
+        new ContinuousRangeCondition<>(HIGH, LOW);
+    }
+
+    /**
+     * Ensure that the minimum and maximum functions return the correct values.
+     */
+    @Test
+    public void testBounds() {
+        int low = CONDITION.min();
+        assertEquals(LOW, low);
+        int high = CONDITION.max();
+        assertEquals(HIGH, high);
+    }
+
+    /**
+     * Test that the right elements are contained in the condition.
+     */
+    @Test
+    public void testContains() {
+        assertFalse(CONDITION.contains(-5));
+        assertTrue(CONDITION.contains(LOW));
+        assertTrue(CONDITION.contains(5));
+        assertTrue(CONDITION.contains(HIGH));
+        assertFalse(CONDITION.contains(15));
+    }
+
+    /**
+     * Test that the right intervals intersect the condition.
+     */
+    @Test
+    public void testIntersects() {
+        assertFalse(CONDITION.intersects(Integer.MIN_VALUE, LOW - 1));
+        assertTrue(CONDITION.intersects(-5, 5));
+        assertTrue(CONDITION.intersects(2, 8));
+        assertTrue(CONDITION.intersects(5, 15));
+        assertFalse(CONDITION.intersects(HIGH + 1, Integer.MAX_VALUE));
+    }
+
+    /**
+     * Test that the returned subcondition has the correct bounds.
+     */
+    @Test
+    public void testSubCondition() {
+        RangeCondition<Integer> sub = CONDITION.subCondition(-5, 8);
+        assertNotNull(sub);
+        assertEquals(ContinuousRangeCondition.class, sub.getClass());
+        int low = sub.min();
+        int high = sub.max();
+        assertEquals(LOW, low);
+        assertEquals(8, high);
+    }
+
+}
diff --git a/statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/datastore/core/condition/DiscreteRangeConditionTest.java b/statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/datastore/core/condition/DiscreteRangeConditionTest.java
new file mode 100644 (file)
index 0000000..6867062
--- /dev/null
@@ -0,0 +1,122 @@
+/*******************************************************************************
+ * Copyright (c) 2017 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ ******************************************************************************/
+
+package org.eclipse.tracecompass.internal.datastore.core.condition;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.tracecompass.internal.provisional.datastore.core.condition.RangeCondition;
+import org.junit.Test;
+
+/**
+ * Test the discrete range condition with Integers.
+ *
+ * @author Loïc Prieur-Drevon
+ */
+public class DiscreteRangeConditionTest {
+
+    private static final int LOW = 0;
+    private static final int HIGH = 10;
+    private static final List<Integer> VALUES = Arrays.asList(LOW, HIGH / 2, HIGH);
+    private static final DiscreteRangeCondition<Integer> CONDITION = new DiscreteRangeCondition<>(VALUES);
+
+    /**
+     * Ensure that we cannot build a condition with an empty collection.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testConstructor() {
+        new DiscreteRangeCondition<>(Collections.emptyList());
+    }
+
+    /**
+     * Ensure that the minimum and maximum functions return the correct values.
+     */
+    @Test
+    public void testBounds() {
+        assertEquals(LOW, (int) CONDITION.min());
+        assertEquals(HIGH, (int) CONDITION.max());
+    }
+
+    /**
+     * Test that the right elements are contained in the condition.
+     */
+    @Test
+    public void testContains() {
+        assertFalse(CONDITION.contains(-5));
+        for (Integer v : VALUES) {
+            assertTrue(CONDITION.contains(v));
+            assertFalse(CONDITION.contains(v + 1));
+        }
+        assertFalse(CONDITION.contains(15));
+    }
+
+    /**
+     * Test that modifying the list used to populate the condition does not
+     * affect the condition
+     */
+    @Test
+    public void testContainsAndAdd() {
+        List<Integer> values = new ArrayList<>();
+        values.add(1);
+        values.add(5);
+        DiscreteRangeCondition<Integer> condition = new DiscreteRangeCondition<>(values);
+        assertFalse(condition.contains(-5));
+        for (Integer v : values) {
+            assertTrue(condition.contains(v));
+            assertFalse(condition.contains(v + 1));
+        }
+        assertFalse(condition.contains(15));
+        // Add the values to the initial set and make sure it is not part of the
+        // condition
+        values.add(15);
+        assertFalse(condition.contains(15));
+    }
+
+    /**
+     * Test that the right intervals intersect the condition.
+     */
+    @Test
+    public void testIntersects() {
+        assertFalse(CONDITION.intersects(Integer.MIN_VALUE, LOW - 1));
+        assertTrue(CONDITION.intersects(0, 4));
+        assertFalse(CONDITION.intersects(1, 4));
+        assertTrue(CONDITION.intersects(2, 8));
+        assertFalse(CONDITION.intersects(6, 9));
+        assertTrue(CONDITION.intersects(5, 15));
+        assertFalse(CONDITION.intersects(HIGH + 1, Integer.MAX_VALUE));
+    }
+
+    /**
+     * Test that the returned subcondition has the correct bounds.
+     */
+    @Test
+    public void testSubCondition() {
+        RangeCondition<Integer> sub = CONDITION.subCondition(-5, 8);
+        assertNotNull(sub);
+        assertEquals(DiscreteRangeCondition.class, sub.getClass());
+        int low = sub.min();
+        int high = sub.max();
+        assertEquals(LOW, low);
+        assertEquals(HIGH / 2, high);
+
+        // For a range where no value is include, it should return null
+        sub = CONDITION.subCondition(LOW + 1, HIGH / 2 - 1);
+        assertNull(sub);
+    }
+
+}
diff --git a/statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/provisional/datastore/core/condition/RangeConditionTest.java b/statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/provisional/datastore/core/condition/RangeConditionTest.java
new file mode 100644 (file)
index 0000000..1a19a61
--- /dev/null
@@ -0,0 +1,155 @@
+/*******************************************************************************
+ * Copyright (c) 2017 École Polytechnique de Montréal
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.internal.provisional.datastore.core.condition;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.eclipse.tracecompass.internal.datastore.core.condition.ContinuousRangeCondition;
+import org.eclipse.tracecompass.internal.datastore.core.condition.DiscreteRangeCondition;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Test the {@link RangeCondition} static methods
+ *
+ * @author Geneviève Bastien
+ */
+public class RangeConditionTest {
+
+    /**
+     * Test the {@link RangeCondition#singleton(Comparable)} method
+     */
+    @Test
+    public void testSingleton() {
+        /* A test value */
+        Long value = 3L;
+        RangeCondition<Long> cnd = RangeCondition.singleton(3L);
+        /* Make sure the return value is a discrete range of the right value */
+        assertTrue(cnd instanceof DiscreteRangeCondition);
+        assertEquals(value, cnd.max());
+        assertEquals(value, cnd.min());
+    }
+
+    /**
+     * Test the
+     * {@link RangeCondition#forContinuousRange(Comparable, Comparable)} method
+     * for value with bound1 < bound2
+     */
+    @Test
+    public void testForContinuousRangeValid() {
+        Long min = 1L;
+        Long max = 10L;
+        RangeCondition<Long> cnd = RangeCondition.forContinuousRange(min, max);
+        assertTrue(cnd instanceof ContinuousRangeCondition);
+        assertEquals(min, cnd.min());
+        assertEquals(max, cnd.max());
+    }
+
+    /**
+     * Test the
+     * {@link RangeCondition#forContinuousRange(Comparable, Comparable)} method
+     * for value with bound1 > bound2
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testForContinuousRangeInvalid() {
+        Long min = 1L;
+        Long max = 10L;
+        RangeCondition.forContinuousRange(max, min);
+    }
+
+    /**
+     * Test the {@link RangeCondition#fromCollection(java.util.Collection)}
+     * method
+     */
+    @Test
+    public void testFromCollection() {
+        // Add a list of unsorted elements
+        ImmutableList<Long> elements = ImmutableList.of(1L, 4L, 2L, 10L, -3L);
+        RangeCondition<Long> cnd = RangeCondition.fromCollection(elements);
+        assertTrue(cnd instanceof DiscreteRangeCondition);
+        assertEquals(-3L, (long) cnd.min());
+        assertEquals(10L, (long) cnd.max());
+    }
+
+    /**
+     * Test the {@link RangeCondition#forDiscreteRange(long, long, long)} method
+     * for value with bound1 < bound2
+     */
+    @Test
+    public void testForDiscreteRangeValid() {
+
+        // Test with valid values and a step of 1: all values should be in the
+        // range
+        Long min = 1L;
+        Long max = 10L;
+        Long step = 1L;
+        RangeCondition<Long> cnd = RangeCondition.forDiscreteRange(min, max, step);
+        assertTrue(cnd instanceof DiscreteRangeCondition);
+        assertEquals(min, cnd.min());
+        assertEquals(max, cnd.max());
+        for (Long i = min; i <= max; i++) {
+            assertTrue(cnd.contains(i));
+        }
+
+        // Test with a step of 0, it should take 1 as a default value
+        step = 0L;
+        cnd = RangeCondition.forDiscreteRange(min, max, step);
+        assertTrue(cnd instanceof DiscreteRangeCondition);
+        assertEquals(min, cnd.min());
+        assertEquals(max, cnd.max());
+        for (Long i = min; i <= max; i++) {
+            assertTrue(cnd.contains(i));
+        }
+
+        // Test with a step of 2. Make sure that values have the right steps and
+        // the max element is part of the condition
+        step = 2L;
+        cnd = RangeCondition.forDiscreteRange(min, max, step);
+        assertTrue(cnd instanceof DiscreteRangeCondition);
+        assertEquals(min, cnd.min());
+        assertEquals(max, cnd.max());
+        for (Long i = min; i < max; i++) {
+            if ((i - min) % step == 0) {
+                assertTrue(cnd.contains(i));
+            } else {
+                assertFalse(cnd.contains(i));
+            }
+        }
+        assertTrue(cnd.contains(max));
+    }
+
+    /**
+     * Test the {@link RangeCondition#forDiscreteRange(long, long, long)} method
+     * for value with bound1 > bound2
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testForDiscreteRangeInvalid() {
+        Long min = 1L;
+        Long max = 10L;
+        Long step = 1L;
+        RangeCondition.forDiscreteRange(max, min, step);
+    }
+
+    /**
+     * Test the {@link RangeCondition#forDiscreteRange(long, long, long)} method
+     * with a step < 0
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testForDiscreteRangeInvalidStep() {
+        Long min = 1L;
+        Long max = 10L;
+        Long step = -2L;
+        RangeCondition.forDiscreteRange(max, min, step);
+    }
+
+}
This page took 0.048614 seconds and 5 git commands to generate.