[47] | 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 | import static org.mockito.Mockito.mock;
|
---|
| 7 | import static org.mockito.Mockito.when;
|
---|
| 8 |
|
---|
| 9 | import java.io.IOException;
|
---|
| 10 | import java.util.Arrays;
|
---|
| 11 | import java.util.Date;
|
---|
| 12 | import java.util.LinkedList;
|
---|
| 13 | import java.util.List;
|
---|
| 14 |
|
---|
| 15 | import org.junit.Before;
|
---|
| 16 | import org.junit.Test;
|
---|
| 17 |
|
---|
| 18 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
| 19 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 20 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
| 21 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 22 |
|
---|
| 23 | import geniusweb.deadline.DeadlineRounds;
|
---|
| 24 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 25 |
|
---|
| 26 | public 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(999l);
|
---|
| 32 | private final Date date0 = new Date(0l);
|
---|
| 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\":999}}";
|
---|
| 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\\[0.*20ms\\]",
|
---|
| 68 | "ProgressTime\\[999.*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 | }
|
---|