-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathTimeNodeTest.java
More file actions
executable file
·141 lines (118 loc) · 5.89 KB
/
Copy pathTimeNodeTest.java
File metadata and controls
executable file
·141 lines (118 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2015 jmrozanec
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cronutils.model.time;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class TimeNodeTest {
private static final int LIST_START_VALUE = 2;
private static final int LIST_MEDIUM_VALUE = 4;
private static final int LIST_END_VALUE = 6;
private static final int LOW_INTERMEDIATE_VALUE = 1;
private static final int HIGH_INTERMEDIATE_VALUE = 5;
private List<Integer> values;
private TimeNode timeNode;
@BeforeEach
public void setUp() {
values = new ArrayList<>();
values.add(LIST_START_VALUE);
values.add(LIST_MEDIUM_VALUE);
values.add(LIST_END_VALUE);
timeNode = new TimeNode(values);
}
@Test
public void testGetNextValue() {
assertResult(LIST_START_VALUE, 0, timeNode.getNextValue(LIST_START_VALUE, 0));
assertResult(LIST_MEDIUM_VALUE, 0, timeNode.getNextValue(LIST_MEDIUM_VALUE, 0));
assertResult(LIST_END_VALUE, 0, timeNode.getNextValue(LIST_END_VALUE, 0));
assertResult(LIST_MEDIUM_VALUE, 0, timeNode.getNextValue(LIST_START_VALUE, 1));
assertResult(LIST_END_VALUE, 0, timeNode.getNextValue(LIST_MEDIUM_VALUE, 1));
assertResult(LIST_START_VALUE, 1, timeNode.getNextValue(LIST_END_VALUE, 1));
assertResult(LIST_START_VALUE, 1, timeNode.getNextValue(LIST_MEDIUM_VALUE, 2));
}
@Test
public void testGetValues() {
assertEquals(values, timeNode.getValues());
}
@Test
public void testGetPreviousValue() {
assertResult(LIST_START_VALUE, 0, timeNode.getPreviousValue(LIST_START_VALUE, 0));
assertResult(LIST_MEDIUM_VALUE, 0, timeNode.getPreviousValue(LIST_MEDIUM_VALUE, 0));
assertResult(LIST_END_VALUE, 0, timeNode.getPreviousValue(LIST_END_VALUE, 0));
assertResult(LIST_END_VALUE, 1, timeNode.getPreviousValue(LIST_START_VALUE, 1));
assertResult(LIST_START_VALUE, 0, timeNode.getPreviousValue(LIST_MEDIUM_VALUE, 1));
assertResult(LIST_MEDIUM_VALUE, 0, timeNode.getPreviousValue(LIST_END_VALUE, 1));
assertResult(LIST_END_VALUE, 1, timeNode.getPreviousValue(LIST_MEDIUM_VALUE, 2));
assertResult(LIST_MEDIUM_VALUE, 0, timeNode.getPreviousValue(HIGH_INTERMEDIATE_VALUE, 1));
assertResult(LIST_END_VALUE, 1, timeNode.getPreviousValue(LOW_INTERMEDIATE_VALUE, 0));
}
@Test
public void testGetValueFromListWhereIndexLessThanZero() {
final int index = -1;
final int expectedShifts = 1;
final AtomicInteger shift = new AtomicInteger(0);
final List<Integer> list = Arrays.asList(1, 2, 3, 4);
final int value = timeNode.getValueFromList(list, index, shift);
assertEquals(expectedShifts, shift.get(), String.format("Shift was: %s; expected: %s", shift.get(), expectedShifts));
assertEquals((int) list.get(list.size() + index), value);
}
@Test
public void testGetValueFromListWhereIndexMoreThanZero() {
final int index = 1;
final int expectedShifts = 0;
final AtomicInteger shift = new AtomicInteger(0);
final List<Integer> list = Arrays.asList(1, 2, 3, 4);
final int value = timeNode.getValueFromList(list, index, shift);
assertEquals(expectedShifts, shift.get(), String.format("Shift was: %s; expected: %s", shift.get(), expectedShifts));
assertEquals((int) list.get(index), value);
}
@Test
public void testGetValueFromListWithEmptyList() {
assertThrows(IllegalArgumentException.class, () -> timeNode.getValueFromList(new ArrayList<>(), 0, new AtomicInteger(0)));
}
@Test
public void testGetValueFromListWithLargeIndices() {
final List<Integer> values = Arrays.asList(2, 8, 14, 20);
final AtomicInteger shift = new AtomicInteger(0);
final int largePositiveIndex = 365 * 6;
int value = timeNode.getValueFromList(values, largePositiveIndex, shift);
assertEquals(14, value);
assertEquals(547, shift.get());
shift.set(0);
final int largeNegativeIndex = -1000;
value = timeNode.getValueFromList(values, largeNegativeIndex, shift);
assertEquals(2, value);
assertEquals(250, shift.get());
}
@Test
public void testGetValueFromListWithDuplicates() {
final List<Integer> tradingTimes = Arrays.asList(930, 930, 1130, 1130, 1300, 1500);
final AtomicInteger shift = new AtomicInteger(0);
int nextSession = timeNode.getValueFromList(tradingTimes, 10, shift);
assertEquals(1300, nextSession);
assertEquals(1, shift.get());
shift.set(0);
int prevSession = timeNode.getValueFromList(tradingTimes, -5, shift);
assertEquals(930, prevSession);
assertEquals(1, shift.get());
}
private void assertResult(final int value, final int shift, final NearestValue nearestValue) {
assertEquals(value, nearestValue.getValue(), String.format("Values do not match! Expected: %s Found: %s", value, nearestValue.getValue()));
assertEquals(shift, nearestValue.getShifts(), String.format("Shifts do not match! Expected: %s Found: %s", shift, nearestValue.getShifts()));
}
}