1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.grinder.util;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29 import java.io.ObjectOutputStream;
30 import java.util.Random;
31
32 import junit.framework.TestCase;
33
34
35
36
37
38
39
40 public class TestSerialiser extends TestCase
41 {
42 private final Random m_random = new Random();
43
44 public void testUnsignedLongs() throws Exception
45 {
46 final ByteArrayOutputStream byteArrayOutputStream =
47 new ByteArrayOutputStream();
48
49 final ObjectOutputStream objectOutputStream =
50 new ObjectOutputStream(byteArrayOutputStream);
51
52 final long[] longs = new long[10000];
53
54 final Serialiser serialiser = new Serialiser();
55
56 for (int i=0; i<longs.length; i++) {
57 if (i < 1000) {
58 longs[i] = i;
59 }
60 else {
61 longs[i] = Math.abs(m_random.nextLong() & 0xFFF);
62 }
63
64 serialiser.writeUnsignedLong(objectOutputStream, longs[i]);
65 }
66
67 try {
68 serialiser.writeUnsignedLong(objectOutputStream, -1);
69 fail("Should not reach");
70 }
71 catch (IOException e) {
72 }
73
74 objectOutputStream.close();
75
76 final byte[] bytes = byteArrayOutputStream.toByteArray();
77
78 assertTrue("We should compress", bytes.length < 8 * longs.length);
79
80 final ObjectInputStream objectInputStream =
81 new ObjectInputStream(new ByteArrayInputStream(bytes));
82
83 for (int i=0; i<longs.length; i++) {
84 assertEquals(longs[i],
85 serialiser.readUnsignedLong(objectInputStream));
86 }
87 }
88
89 public void testLongs() throws Exception
90 {
91 final ByteArrayOutputStream byteArrayOutputStream =
92 new ByteArrayOutputStream();
93
94 final ObjectOutputStream objectOutputStream =
95 new ObjectOutputStream(byteArrayOutputStream);
96
97 final long[] longs = new long[3002];
98
99 final Serialiser serialiser = new Serialiser();
100
101 for (int i=0; i<longs.length; i++) {
102 if (i < 1000) {
103 longs[i] = i;
104 }
105 else if (i <2000) {
106 longs[i] = i - 2000;
107 }
108 else {
109 longs[i] = m_random.nextLong();
110 }
111
112 longs[3000] = Long.MIN_VALUE;
113 longs[3001] = Long.MAX_VALUE;
114
115 serialiser.writeLong(objectOutputStream, longs[i]);
116 }
117
118 objectOutputStream.close();
119
120 final byte[] bytes = byteArrayOutputStream.toByteArray();
121
122 assertTrue("We should compress", bytes.length < 8 * longs.length);
123
124 final ObjectInputStream objectInputStream =
125 new ObjectInputStream(new ByteArrayInputStream(bytes));
126
127 for (int i=0; i<longs.length; i++) {
128 assertEquals(longs[i], serialiser.readLong(objectInputStream));
129 }
130 }
131
132 public void testDoubles() throws Exception
133 {
134 final ByteArrayOutputStream byteArrayOutputStream =
135 new ByteArrayOutputStream();
136
137 final ObjectOutputStream objectOutputStream =
138 new ObjectOutputStream(byteArrayOutputStream);
139
140 final double[] doubles = new double[10000];
141
142 final Serialiser serialiser = new Serialiser();
143
144 for (int i=0; i<doubles.length; i++) {
145 if (i < 1000) {
146 doubles[i] = i;
147 }
148 else {
149 doubles[i] = m_random.nextDouble();
150 }
151
152 serialiser.writeDouble(objectOutputStream, doubles[i]);
153 }
154
155 objectOutputStream.close();
156
157 final byte[] bytes = byteArrayOutputStream.toByteArray();
158
159
160
161
162 final ObjectInputStream objectInputStream =
163 new ObjectInputStream(new ByteArrayInputStream(bytes));
164
165 for (int i=0; i<doubles.length; i++) {
166 assertEquals(doubles[i], serialiser.readDouble(objectInputStream),
167 0.00001);
168 }
169 }
170 }