1 /*******************************************************************************
2 * Copyright (c) 2017 École Polytechnique de Montréal
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 ******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.datastore
.core
.condition
;
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
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.Collections
;
21 import java
.util
.List
;
23 import org
.eclipse
.tracecompass
.internal
.provisional
.datastore
.core
.condition
.IntegerRangeCondition
;
24 import org
.junit
.Test
;
27 * Test the discrete integer range condition.
29 * @author Loïc Prieur-Drevon
31 public class DiscreteIntegerRangeConditionTest
{
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
);
39 * Ensure that we cannot build a condition with an empty collection.
41 @Test(expected
= IllegalArgumentException
.class)
42 public void testConstructor() {
43 new ArrayIntegerRangeCondition(Collections
.emptyList());
47 * Ensure that the minimum and maximum functions return the correct values.
50 public void testBounds() {
51 assertEquals(LOW
, CONDITION
.min());
52 assertEquals(HIGH
, CONDITION
.max());
56 * Test that the right elements are contained in the condition.
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));
65 assertFalse(CONDITION
.test(15));
69 * Test that modifying the list used to populate the condition does not
70 * affect the condition
73 public void testPredicateAndAdd() {
74 List
<Integer
> values
= new ArrayList
<>();
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));
83 assertFalse(condition
.test(15));
84 // Add the values to the initial set and make sure it is not part of the
87 assertFalse(condition
.test(15));
91 * Test that the right intervals intersect the condition.
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
));
105 * Test that the returned subcondition has the correct bounds.
108 public void testSubCondition() {
109 IntegerRangeCondition sub
= CONDITION
.subCondition(-5, 8);
111 assertEquals(ArrayIntegerRangeCondition
.class, sub
.getClass());
113 int high
= sub
.max();
114 assertEquals(LOW
, low
);
115 assertEquals(HIGH
/ 2, high
);
117 // For a range where no value is include, it should return null
118 sub
= CONDITION
.subCondition(LOW
+ 1, HIGH
/ 2 - 1);
121 // Test conditions for border values, sub conditions are inclusive
122 sub
= CONDITION
.subCondition(LOW
, HIGH
/ 2);
126 assertEquals(LOW
, low
);
127 assertEquals(HIGH
/ 2, high
);