diff options
Diffstat (limited to 'Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests')
-rw-r--r-- | Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/interval/SumTest.xtend | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/interval/SumTest.xtend b/Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/interval/SumTest.xtend new file mode 100644 index 00000000..cbd7e71f --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/interval/SumTest.xtend | |||
@@ -0,0 +1,140 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests.interval | ||
2 | |||
3 | import com.google.common.collect.HashMultiset | ||
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.Interval | ||
5 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.IntervalAggregationMode | ||
6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.IntervalAggregationOperator | ||
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.IntervalRedBlackNode | ||
8 | import java.math.BigDecimal | ||
9 | import java.util.Random | ||
10 | import org.junit.Assert | ||
11 | import org.junit.Before | ||
12 | import org.junit.Test | ||
13 | |||
14 | import static hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.interval.Interval.* | ||
15 | |||
16 | class SumTest { | ||
17 | val aggregator = new IntervalAggregationOperator(IntervalAggregationMode.SUM) | ||
18 | var IntervalRedBlackNode value = null | ||
19 | |||
20 | @Before | ||
21 | def void reset() { | ||
22 | value = aggregator.createNeutral | ||
23 | } | ||
24 | |||
25 | @Test | ||
26 | def void emptyTest() { | ||
27 | assertEquals(null) | ||
28 | } | ||
29 | |||
30 | @Test | ||
31 | def void addSingleTest() { | ||
32 | add(between(-1, 1)) | ||
33 | assertEquals(between(-1, 1)) | ||
34 | } | ||
35 | |||
36 | @Test | ||
37 | def void addRemoveTest() { | ||
38 | add(between(-1, 1)) | ||
39 | remove(between(-1, 1)) | ||
40 | assertEquals(null) | ||
41 | } | ||
42 | |||
43 | @Test | ||
44 | def void addTwoTest() { | ||
45 | add(between(-1, 1)) | ||
46 | add(above(2)) | ||
47 | assertEquals(above(1)) | ||
48 | } | ||
49 | |||
50 | @Test | ||
51 | def void addTwoRemoveFirstTest() { | ||
52 | add(between(-1, 1)) | ||
53 | add(above(2)) | ||
54 | remove(between(-1, 1)) | ||
55 | assertEquals(above(2)) | ||
56 | } | ||
57 | |||
58 | @Test | ||
59 | def void addTwoRemoveSecondTest() { | ||
60 | add(between(-1, 1)) | ||
61 | add(above(2)) | ||
62 | remove(above(2)) | ||
63 | assertEquals(between(-1, 1)) | ||
64 | } | ||
65 | |||
66 | @Test | ||
67 | def void addMultiplicityTest() { | ||
68 | add(between(-1, 1)) | ||
69 | add(between(-1, 1)) | ||
70 | add(between(-1, 1)) | ||
71 | assertEquals(between(-3, 3)) | ||
72 | } | ||
73 | |||
74 | @Test | ||
75 | def void removeAllMultiplicityTest() { | ||
76 | add(between(-1, 1)) | ||
77 | add(between(-1, 1)) | ||
78 | add(between(-1, 1)) | ||
79 | remove(between(-1, 1)) | ||
80 | remove(between(-1, 1)) | ||
81 | remove(between(-1, 1)) | ||
82 | assertEquals(null) | ||
83 | } | ||
84 | |||
85 | @Test | ||
86 | def void removeSomeMultiplicityTest() { | ||
87 | add(between(-1, 1)) | ||
88 | add(between(-1, 1)) | ||
89 | add(between(-1, 1)) | ||
90 | remove(between(-1, 1)) | ||
91 | remove(between(-1, 1)) | ||
92 | assertEquals(between(-1, 1)) | ||
93 | } | ||
94 | |||
95 | @Test | ||
96 | def void largeTest() { | ||
97 | val starts = #[null, new BigDecimal(-3), new BigDecimal(-2), new BigDecimal(-1)] | ||
98 | val ends = #[new BigDecimal(1), new BigDecimal(2), new BigDecimal(3), null] | ||
99 | val current = HashMultiset.create | ||
100 | val random = new Random(1) | ||
101 | for (var int i = 0; i < 1000; i++) { | ||
102 | val start = starts.get(random.nextInt(starts.size)) | ||
103 | val end = ends.get(random.nextInt(ends.size)) | ||
104 | val interval = Interval.of(start, end) | ||
105 | val isInsert = !current.contains(interval) || random.nextInt(3) == 0 | ||
106 | if (isInsert) { | ||
107 | current.add(interval) | ||
108 | } else { | ||
109 | current.remove(interval) | ||
110 | } | ||
111 | val expected = current.stream.reduce(aggregator.mode).orElse(null) | ||
112 | update(interval, isInsert) | ||
113 | assertEquals(expected) | ||
114 | } | ||
115 | } | ||
116 | |||
117 | private def update(Interval interval, boolean isInsert) { | ||
118 | value = aggregator.update(value, interval, isInsert) | ||
119 | val nodes = newArrayList | ||
120 | var node = value.min | ||
121 | while (node !== null) { | ||
122 | nodes += node | ||
123 | node = node.successor | ||
124 | } | ||
125 | value.assertSubtreeIsValid | ||
126 | } | ||
127 | |||
128 | private def add(Interval interval) { | ||
129 | update(interval, true) | ||
130 | } | ||
131 | |||
132 | private def remove(Interval interval) { | ||
133 | update(interval, false) | ||
134 | } | ||
135 | |||
136 | private def assertEquals(Interval interval) { | ||
137 | val actual = aggregator.getAggregate(value) | ||
138 | Assert.assertEquals(interval, actual) | ||
139 | } | ||
140 | } | ||