1 | package geniusweb.progress;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Date;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.junit.Test;
|
---|
14 |
|
---|
15 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
16 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
17 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
18 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
19 |
|
---|
20 | import tudelft.utilities.junit.GeneralTests;
|
---|
21 |
|
---|
22 | public class ProgressTimeTest extends GeneralTests<Progress> {
|
---|
23 | // we set T0 quite high because Python has trouble handling small datetime
|
---|
24 | private static final long T0 = 100000l;
|
---|
25 | private static final Date TIMEZERO = new Date(T0);
|
---|
26 | private final static long TESTTIME = 10; // milliseconds!
|
---|
27 | private final ProgressTime progress1 = new ProgressTime(TESTTIME, TIMEZERO);
|
---|
28 | private final ProgressTime progress1a = new ProgressTime(TESTTIME,
|
---|
29 | TIMEZERO);
|
---|
30 | private final ProgressTime progress2 = new ProgressTime(200l, TIMEZERO);
|
---|
31 |
|
---|
32 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
33 | private final String progressstring = "{\"ProgressTime\":{\"duration\":10,\"start\":100000}}";
|
---|
34 |
|
---|
35 | private final long start = 1234;
|
---|
36 | private final ProgressTime pr = new ProgressTime(TESTTIME, new Date(start));
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public List<List<Progress>> getGeneralTestData() {
|
---|
40 | List<List<Progress>> list = new LinkedList<>();
|
---|
41 | list.add(Arrays.asList(progress1, progress1a));
|
---|
42 | list.add(Arrays.asList(progress2));
|
---|
43 | return list;
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public List<String> getGeneralTestStrings() {
|
---|
48 | return Arrays.asList("ProgressTime\\[100000.*10ms\\]",
|
---|
49 | "ProgressTime\\[100000.*200ms\\]");
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Test
|
---|
53 | public void testInit() {
|
---|
54 | assertEquals((Double) 0d, progress1.get(0l));
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Test
|
---|
58 | public void testAdvancingTime() throws InterruptedException {
|
---|
59 | for (long n = 0; n <= TESTTIME; n++) {
|
---|
60 | assertEquals((Double) ((double) n / TESTTIME),
|
---|
61 | progress1.get(T0 + n));
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Test
|
---|
66 | public void testAdvanceWhenDeadlineReached() throws InterruptedException {
|
---|
67 | ProgressTime progress = new ProgressTime(TESTTIME, TIMEZERO);
|
---|
68 | assertEquals((Double) 1d, progress1.get(T0 + TESTTIME));
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Test
|
---|
72 | public void testSerialize() throws JsonProcessingException {
|
---|
73 | String actual = jackson.writeValueAsString(progress1);
|
---|
74 | System.out.println(actual);
|
---|
75 | assertEquals(progressstring, actual);
|
---|
76 |
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Test
|
---|
80 | public void testDeserialize()
|
---|
81 | throws JsonParseException, JsonMappingException, IOException {
|
---|
82 | ProgressTime newprog = jackson.readValue(progressstring,
|
---|
83 | ProgressTime.class);
|
---|
84 | // we can't directly compare with progress since that's a hacked
|
---|
85 | // object...
|
---|
86 | assertEquals(TESTTIME, newprog.getDuration().intValue());
|
---|
87 | assertEquals(T0, newprog.getStart().getTime());
|
---|
88 |
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Test
|
---|
92 | public void pastDeadline() {
|
---|
93 | assertTrue(pr.isPastDeadline(start + TESTTIME + 1));
|
---|
94 | assertFalse(pr.isPastDeadline(start + TESTTIME - 1));
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Test
|
---|
99 | public void terminationTime() {
|
---|
100 | assertEquals(start + TESTTIME, pr.getTerminationTime().getTime());
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Test
|
---|
104 | public void get() {
|
---|
105 | assertEquals((Double) 0d, pr.get(start));
|
---|
106 | assertEquals((Double) 1d, pr.get(start + TESTTIME));
|
---|
107 | assertEquals((Double) 0.5d, pr.get(start + TESTTIME / 2));
|
---|
108 | assertEquals((Double) 0d, pr.get(start - 10));
|
---|
109 | assertEquals((Double) 1d, pr.get(start + TESTTIME + 10));
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | }
|
---|