source: timeline/src/test/java/geniusweb/progress/ProgressRoundsTest.java@ 53

Last change on this file since 53 was 52, checked in by ruud, 22 months ago

Fixed small issues in domaineditor.

File size: 4.7 KB
Line 
1package geniusweb.progress;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.when;
8
9import java.io.IOException;
10import java.util.Arrays;
11import java.util.Date;
12import java.util.LinkedList;
13import java.util.List;
14
15import org.junit.Before;
16import org.junit.Test;
17
18import com.fasterxml.jackson.core.JsonParseException;
19import com.fasterxml.jackson.core.JsonProcessingException;
20import com.fasterxml.jackson.databind.JsonMappingException;
21import com.fasterxml.jackson.databind.ObjectMapper;
22
23import geniusweb.deadline.DeadlineRounds;
24import tudelft.utilities.junit.GeneralTests;
25
26public class ProgressRoundsTest extends GeneralTests<Progress> {
27 private DeadlineRounds deadline = mock(DeadlineRounds.class);
28 private final static Integer TESTROUNDS = 10;
29 private final static long TESTTIME = 10; // milliseconds!
30
31 private final Date date = new Date(98765l);
32 private final Date date0 = new Date(100000l);
33
34 private final Progress deadline1 = new ProgressRounds(20, 0, date);
35 private final Progress deadline2 = new ProgressRounds(20, 0, date);
36 private final Progress deadline3 = new ProgressRounds(30, 0, date);
37 private final Progress deadline4 = new ProgressTime(20l, date0);
38 private final Progress deadline5 = new ProgressTime(20l, date);
39 private ProgressRounds progress;
40 private final ObjectMapper jackson = new ObjectMapper();
41 private final String progressstring = "{\"ProgressRounds\":{\"duration\":10,\"currentRound\":0,\"endtime\":98765}}";
42
43 private final long start = 1234;
44 private final Date end = new Date(start + TESTTIME);
45 private final ProgressRounds pr = new ProgressRounds(TESTROUNDS, 4, end);
46
47 @Before
48 public void before() {
49 when(deadline.getRounds()).thenReturn(TESTROUNDS);
50 progress = new ProgressRounds(deadline.getRounds(), 0, date);
51
52 }
53
54 @Override
55 public List<List<Progress>> getGeneralTestData() {
56 List<List<Progress>> list = new LinkedList<>();
57 list.add(Arrays.asList(deadline1, deadline2));
58 list.add(Arrays.asList(deadline3));
59 list.add(Arrays.asList(deadline4));
60 list.add(Arrays.asList(deadline5));
61 return list;
62 }
63
64 @Override
65 public List<String> getGeneralTestStrings() {
66 return Arrays.asList("ProgressRounds\\[0.*20\\]",
67 "ProgressRounds\\[0.*30\\]", "ProgressTime\\[100000.*20ms\\]",
68 "ProgressTime\\[98765.*20ms\\]");
69 }
70
71 @Test
72 public void testInit() {
73 assertEquals((Double) 0d, progress.get(1l));
74 }
75
76 @Test
77 public void testCurrentRound() {
78 assertEquals((Integer) 0, progress.getCurrentRound());
79 }
80
81 @Test
82 public void testAdvance() {
83 ProgressRounds p = progress;
84 int n = 0;
85 while (n <= TESTROUNDS) {
86 assertEquals((Double) ((double) n / TESTROUNDS), p.get(1l));
87 n++;
88 p = p.advance();
89 }
90 }
91
92 @Test
93 public void testAdvanceWhenDeadlineReached() {
94 ProgressRounds p = new ProgressRounds(TESTROUNDS, 9, date);
95 p = p.advance();
96 assertEquals((Double) 1d, p.get(1l));
97 }
98
99 @Test
100 public void testSerialize() throws JsonProcessingException {
101 System.out.println(jackson.writeValueAsString(progress));
102 assertEquals(progressstring, jackson.writeValueAsString(progress));
103
104 }
105
106 @Test
107 public void testDeserialize()
108 throws JsonParseException, JsonMappingException, IOException {
109 assertEquals(progress,
110 jackson.readValue(progressstring, Progress.class));
111
112 }
113
114 @SuppressWarnings("unused")
115 @Test(expected = IllegalArgumentException.class)
116 public void testIllegalArg1() {
117 new ProgressRounds(10, 11, date);
118 }
119
120 @SuppressWarnings("unused")
121 @Test(expected = IllegalArgumentException.class)
122 public void testIllegalArg2() {
123 new ProgressRounds(10, -1, date);
124 }
125
126 @Test
127 public void pastDeadline() {
128 assertFalse(pr.isPastDeadline(start));
129 assertTrue(pr.isPastDeadline(start + TESTTIME + 1));
130 assertFalse(pr.isPastDeadline(start + TESTTIME - 1));
131 assertTrue(new ProgressRounds(TESTROUNDS, TESTROUNDS, end)
132 .isPastDeadline(start + TESTTIME + 1));
133 }
134
135 @Test
136 public void terminationTime() {
137 assertEquals(start + TESTTIME, pr.getTerminationTime().getTime());
138 }
139
140 @Test
141 public void get() {
142 // time does not matter for rounds-based deadline
143 assertEquals((Double) 0.4d, pr.get(start));
144 assertEquals((Double) 0.4d, pr.get(start + TESTTIME + 1));
145 assertEquals((Double) 0.4d, pr.get(start + TESTTIME));
146 assertEquals((Double) 0.4d, pr.get(start + TESTTIME / 2));
147 assertEquals((Double) 0.4d, pr.get(start - 10));
148 assertEquals((Double) 0.4d, pr.get(start + TESTTIME + 10));
149
150 assertEquals((Double) 0.1d,
151 new ProgressRounds(TESTROUNDS, 1, end).get(start));
152 assertEquals((Double) 1d,
153 new ProgressRounds(TESTROUNDS, TESTROUNDS, end).get(start));
154 }
155
156 @Test
157 public void totalRounds() {
158 assertEquals((Integer) 10, pr.getTotalRounds());
159 }
160}
Note: See TracBrowser for help on using the repository browser.