ss: add 2D iterator queries to the SS API
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.datastore.core.tests / src / org / eclipse / tracecompass / internal / datastore / core / condition / DiscreteIntegerRangeConditionTest.java
1 /*******************************************************************************
2 * Copyright (c) 2017 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 ******************************************************************************/
9
10 package org.eclipse.tracecompass.internal.datastore.core.condition;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertTrue;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.eclipse.tracecompass.internal.provisional.datastore.core.condition.IntegerRangeCondition;
24 import org.junit.Test;
25
26 /**
27 * Test the discrete integer range condition.
28 *
29 * @author Loïc Prieur-Drevon
30 */
31 public class DiscreteIntegerRangeConditionTest {
32
33 private static final int LOW = 0;
34 private static final int HIGH = 10;
35 private static final List<Integer> VALUES = Arrays.asList(LOW, HIGH / 2, HIGH);
36 private static final IntegerRangeCondition CONDITION = new ArrayIntegerRangeCondition(VALUES);
37
38 /**
39 * Ensure that we cannot build a condition with an empty collection.
40 */
41 @Test(expected = IllegalArgumentException.class)
42 public void testConstructor() {
43 new ArrayIntegerRangeCondition(Collections.emptyList());
44 }
45
46 /**
47 * Ensure that the minimum and maximum functions return the correct values.
48 */
49 @Test
50 public void testBounds() {
51 assertEquals(LOW, CONDITION.min());
52 assertEquals(HIGH, CONDITION.max());
53 }
54
55 /**
56 * Test that the right elements are contained in the condition.
57 */
58 @Test
59 public void testPredicate() {
60 assertFalse(CONDITION.test(-5));
61 for (Integer v : VALUES) {
62 assertTrue(CONDITION.test(v));
63 assertFalse(CONDITION.test(v + 1));
64 }
65 assertFalse(CONDITION.test(15));
66 }
67
68 /**
69 * Test that modifying the list used to populate the condition does not
70 * affect the condition
71 */
72 @Test
73 public void testPredicateAndAdd() {
74 List<Integer> values = new ArrayList<>();
75 values.add(1);
76 values.add(5);
77 IntegerRangeCondition condition = new ArrayIntegerRangeCondition(values);
78 assertFalse(condition.test(-5));
79 for (Integer v : values) {
80 assertTrue(condition.test(v));
81 assertFalse(condition.test(v + 1));
82 }
83 assertFalse(condition.test(15));
84 // Add the values to the initial set and make sure it is not part of the
85 // condition
86 values.add(15);
87 assertFalse(condition.test(15));
88 }
89
90 /**
91 * Test that the right intervals intersect the condition.
92 */
93 @Test
94 public void testIntersects() {
95 assertFalse(CONDITION.intersects(Integer.MIN_VALUE, LOW - 1));
96 assertTrue(CONDITION.intersects(0, 4));
97 assertFalse(CONDITION.intersects(1, 4));
98 assertTrue(CONDITION.intersects(2, 8));
99 assertFalse(CONDITION.intersects(6, 9));
100 assertTrue(CONDITION.intersects(5, 15));
101 assertFalse(CONDITION.intersects(HIGH + 1, Integer.MAX_VALUE));
102 }
103
104 /**
105 * Test that the returned subcondition has the correct bounds.
106 */
107 @Test
108 public void testSubCondition() {
109 IntegerRangeCondition sub = CONDITION.subCondition(-5, 8);
110 assertNotNull(sub);
111 assertEquals(ArrayIntegerRangeCondition.class, sub.getClass());
112 int low = sub.min();
113 int high = sub.max();
114 assertEquals(LOW, low);
115 assertEquals(HIGH / 2, high);
116
117 // For a range where no value is include, it should return null
118 sub = CONDITION.subCondition(LOW + 1, HIGH / 2 - 1);
119 assertNull(sub);
120
121 // Test conditions for border values, sub conditions are inclusive
122 sub = CONDITION.subCondition(LOW, HIGH / 2);
123 assertNotNull(sub);
124 low = sub.min();
125 high = sub.max();
126 assertEquals(LOW, low);
127 assertEquals(HIGH / 2, high);
128 }
129
130 }
This page took 0.03359 seconds and 5 git commands to generate.