aboutsummaryrefslogtreecommitdiff
path: root/test/ch
diff options
context:
space:
mode:
Diffstat (limited to 'test/ch')
-rw-r--r--test/ch/epfl/xblast/GameStateSerializerTest.java61
-rw-r--r--test/ch/epfl/xblast/RunLengthEncoderTest.java100
2 files changed, 161 insertions, 0 deletions
diff --git a/test/ch/epfl/xblast/GameStateSerializerTest.java b/test/ch/epfl/xblast/GameStateSerializerTest.java
new file mode 100644
index 0000000..881b1ec
--- /dev/null
+++ b/test/ch/epfl/xblast/GameStateSerializerTest.java
@@ -0,0 +1,61 @@
1package ch.epfl.xblast;
2
3import ch.epfl.xblast.server.*;
4import ch.epfl.xblast.server.Level;
5import org.junit.Assert;
6import org.junit.Test;
7
8import java.lang.reflect.Array;
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.List;
12import java.util.stream.Collectors;
13
14/**
15 * @author Timothée FLOURE (257420)
16 */
17public class GameStateSerializerTest {
18
19 @Test
20 public void IntialGameStateSerializationTest() {
21 List<Integer> sourceValues = Arrays.asList(
22 // Serialized Board
23 121, -50, 2, 1, -2, 0, 3, 1, 3, 1, -2, 0, 1, 1, 3, 1, 3,
24 1, 3, 1, 1, -2, 0, 1, 3, 1, 3, -2, 0, -1, 1, 3, 1, 3, 1,
25 3, 1, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
26 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2,
27 3, 1, 0, 0, 3, 1, 3, 1, 0, 0, 1, 1, 3, 1, 1, 0, 0, 1, 3,
28 1, 3, 0, 0, -1, 1, 3, 1, 1, -5, 2, 3, 2, 3, -5, 2, 3, 2,
29 3, 1, -2, 0, 3, -2, 0, 1, 3, 2, 1, 2,
30 // Explosions (blasts & bombs)
31 4, -128, 16, -63, 16,
32 // Players
33 3, 24, 24, 6,
34 3, -40, 24, 26,
35 3, -40, -72, 46,
36 3, 24, -72, 66,
37 // Ticks
38 60);
39
40 // Build a List of Bytes from the Expected Values
41 List<Byte> expectedValues = sourceValues.stream().map(i -> (byte) i.intValue()).collect(Collectors.toList());
42
43 // Get the actual values
44 List<Byte> actualValues = GameStateSerializer.serialize(
45 Level.DEFAULT_LEVEL.painter(),
46 Level.DEFAULT_LEVEL.initialState()
47 );
48
49 // Check the first element (number of elements)
50 Assert.assertEquals(
51 Byte.toUnsignedInt(expectedValues.get(0)),
52 Byte.toUnsignedInt(actualValues.get(0))
53 );
54
55 // Check the rest of the data chunk
56 Assert.assertEquals(
57 expectedValues.subList(1,expectedValues.size()),
58 actualValues.subList(1,expectedValues.size())
59 );
60 }
61}
diff --git a/test/ch/epfl/xblast/RunLengthEncoderTest.java b/test/ch/epfl/xblast/RunLengthEncoderTest.java
new file mode 100644
index 0000000..6880b99
--- /dev/null
+++ b/test/ch/epfl/xblast/RunLengthEncoderTest.java
@@ -0,0 +1,100 @@
1package ch.epfl.xblast;
2
3import org.junit.Test;
4
5import java.util.Arrays;
6import java.util.List;
7
8import static org.junit.Assert.assertEquals;
9
10/**
11 * @author Pacien TRAN-GIRARD (261948)
12 */
13public class RunLengthEncoderTest {
14
15 private static final Byte[] SAMPLE_PLAIN = {10, 10, 10, 20, 20, 30, 20, 30, 40, 40, 40, 40, 40, 40};
16 private static final Byte[] SAMPLE_ENCODED = {-1, 10, 20, 20, 30, 20, 30, -4, 40};
17 private static final RunLengthEncoder.RunLength[] SAMPLE_RUN_LENGTHS = {
18 new RunLengthEncoder.RunLength(3, (byte) 10),
19 new RunLengthEncoder.RunLength(2, (byte) 20),
20 new RunLengthEncoder.RunLength(1, (byte) 30),
21 new RunLengthEncoder.RunLength(1, (byte) 20),
22 new RunLengthEncoder.RunLength(1, (byte) 30),
23 new RunLengthEncoder.RunLength(6, (byte) 40)};
24
25 @Test
26 public void isSampleEncoded() {
27 List<Byte> encoded = RunLengthEncoder.encode(Arrays.asList(SAMPLE_PLAIN));
28 assertEquals(Arrays.asList(SAMPLE_ENCODED), encoded);
29 }
30
31 @Test
32 public void isSampleDecoded() {
33 List<Byte> decoded = RunLengthEncoder.decode(Arrays.asList(SAMPLE_ENCODED));
34 assertEquals(Arrays.asList(SAMPLE_PLAIN), decoded);
35 }
36
37 @Test
38 public void isSampleRunLengthListDecoded() {
39 List<RunLengthEncoder.RunLength> rll = RunLengthEncoder.decodeRunLengths(Arrays.asList(SAMPLE_ENCODED));
40 assertEquals(Arrays.asList(SAMPLE_RUN_LENGTHS), RunLengthEncoder.collapseRunLengths(rll));
41 }
42
43 @Test
44 public void isSampleRunLengthListCollapsed() {
45 final RunLengthEncoder.RunLength[] runLengths = {
46 new RunLengthEncoder.RunLength(1, (byte) 10),
47 new RunLengthEncoder.RunLength(2, (byte) 20),
48 new RunLengthEncoder.RunLength(3, (byte) 20)};
49 final RunLengthEncoder.RunLength[] expectedCollapsed = {
50 new RunLengthEncoder.RunLength(1, (byte) 10),
51 new RunLengthEncoder.RunLength(5, (byte) 20)};
52
53 List<RunLengthEncoder.RunLength> collapsed = RunLengthEncoder.collapseRunLengths(Arrays.asList(runLengths));
54 assertEquals(Arrays.asList(expectedCollapsed), collapsed);
55 }
56
57 @Test
58 public void isSampleRunLengthExpanded() {
59 final RunLengthEncoder.RunLength rl = new RunLengthEncoder.RunLength(5, (byte) 0);
60 final Byte[] expectedExpanded = {0, 0, 0, 0, 0};
61
62 assertEquals(Arrays.asList(expectedExpanded), rl.expand());
63 }
64
65 @Test
66 public void isSampleLongRunLengthSplit() {
67 final RunLengthEncoder.RunLength longRL = new RunLengthEncoder.RunLength(200, (byte) 0);
68 final RunLengthEncoder.RunLength[] expectedSplit = {
69 new RunLengthEncoder.RunLength(130, (byte) 0),
70 new RunLengthEncoder.RunLength(70, (byte) 0)};
71
72 assertEquals(Arrays.asList(expectedSplit), longRL.split());
73 }
74
75 @Test
76 public void isSampleRunLengthEncoded() {
77 final RunLengthEncoder.RunLength rl = new RunLengthEncoder.RunLength(5, (byte) 0);
78 final Byte[] expectedEncoded = {-3, 0};
79
80 assertEquals(Arrays.asList(expectedEncoded), rl.encode());
81 }
82
83 @Test
84 public void isSampleShortRunLengthEncoded() {
85 final RunLengthEncoder.RunLength rl = new RunLengthEncoder.RunLength(2, (byte) 0);
86 final Byte[] expectedEncoded = {0, 0};
87
88 assertEquals(Arrays.asList(expectedEncoded), rl.encode());
89 }
90
91 @Test
92 public void isSampleRunLengthDecoded() {
93 final Byte[] encoded = {-3, 0};
94 final RunLengthEncoder.RunLength expectedDecoded = new RunLengthEncoder.RunLength(5, (byte) 0);
95
96 RunLengthEncoder.RunLength decoded = new RunLengthEncoder.RunLength(encoded[0], encoded[1]);
97 assertEquals(expectedDecoded, decoded);
98 }
99
100}