comparison src/test/java/org/msgpack/TestSimpleArrays.java @ 0:cb825acd883a

first commit
author sugi
date Sat, 18 Oct 2014 15:06:15 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cb825acd883a
1 package org.msgpack;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import org.junit.Ignore;
12 import org.junit.Test;
13 import org.msgpack.annotation.Message;
14 import org.msgpack.packer.BufferPacker;
15 import org.msgpack.packer.Unconverter;
16 import org.msgpack.type.Value;
17 import org.msgpack.unpacker.BufferUnpacker;
18 import org.msgpack.unpacker.Converter;
19
20 public class TestSimpleArrays {
21
22 @Message
23 public static class PrimitiveTest {
24 public boolean[] b = new boolean[0];
25 public short[] s = new short[0];
26 public int[] i = new int[0];
27 // public long[] l = new long[0]; // FIXME javassist?
28 public float[] f = new float[0];
29
30 // public double[] d = new double[0]; // FIXME javassist?
31
32 public PrimitiveTest() {
33 }
34 }
35
36 @Test
37 public void testPrimitive() throws Exception {
38 MessagePack msgpack = new MessagePack();
39
40 PrimitiveTest t = new PrimitiveTest();
41 t.b = new boolean[] { true, false };
42 t.s = new short[] { 0, 1 };
43 t.i = new int[] { 2, 3 };
44 // t.l = new long[] {4, 5};
45 t.f = new float[] { 2.0f, 4.0f };
46 // t.d = new double[] {8.0, 16.0};
47
48 BufferPacker packer = msgpack.createBufferPacker();
49 packer.write(t);
50 byte[] raw = packer.toByteArray();
51 BufferUnpacker unpacker = msgpack.createBufferUnpacker(raw);
52 PrimitiveTest u = unpacker.read(PrimitiveTest.class);
53 assertEquals(t.b.length, u.b.length);
54 for (int i = 0; i < t.b.length; i++) {
55 assertEquals(t.b[i], u.b[i]);
56 }
57 assertEquals(t.s.length, u.s.length);
58 for (int i = 0; i < t.s.length; i++) {
59 assertEquals(t.s[i], u.s[i]);
60 }
61 assertEquals(t.i.length, u.i.length);
62 for (int i = 0; i < t.i.length; i++) {
63 assertEquals(t.i[i], u.i[i]);
64 }
65 // assertEquals(t.l.length, u.l.length);
66 // for(int i=0; i < t.l.length; i++) { assertEquals(t.l[i], u.l[i]); }
67 assertEquals(t.f.length, u.f.length);
68 for (int i = 0; i < t.f.length; i++) {
69 assertEquals(t.f[i], u.f[i], 10e-10);
70 }
71 // assertEquals(t.d.length, u.d.length);
72 // for(int i=0; i < t.d.length; i++) { assertEquals(t.d[i], u.d[i]); }
73
74 Unconverter unconverter = new Unconverter(msgpack);
75 unconverter.write(t);
76 Value value = unconverter.getResult();
77 Converter converter = new Converter(msgpack, value);
78 PrimitiveTest c = converter.read(PrimitiveTest.class);
79 assertEquals(t.b.length, c.b.length);
80 for (int i = 0; i < t.b.length; i++) {
81 assertEquals(t.b[i], c.b[i]);
82 }
83 assertEquals(t.s.length, c.s.length);
84 for (int i = 0; i < t.s.length; i++) {
85 assertEquals(t.s[i], c.s[i]);
86 }
87 assertEquals(t.i.length, c.i.length);
88 for (int i = 0; i < t.i.length; i++) {
89 assertEquals(t.i[i], c.i[i]);
90 }
91 // assertEquals(t.l.length, c.l.length);
92 // for(int i=0; i < t.l.length; i++) { assertEquals(t.l[i], c.l[i]); }
93 assertEquals(t.f.length, c.f.length);
94 for (int i = 0; i < t.f.length; i++) {
95 assertEquals(t.f[i], c.f[i], 10e-10);
96 }
97 // assertEquals(t.d.length, c.d.length);
98 // for(int i=0; i < t.d.length; i++) { assertEquals(t.d[i], c.d[i]); }
99 }
100
101 @Message
102 public static class ReferenceTest {
103 public ReferenceTest() {
104 }
105
106 public Boolean[] b;
107 public Short[] s;
108 public Integer[] i;
109 public Long[] l;
110 public Float[] f;
111 public Double[] d;
112 public String[] str;
113 }
114
115 @Test
116 public void testReference() throws Exception {
117 MessagePack msgpack = new MessagePack();
118
119 ReferenceTest t = new ReferenceTest();
120 t.b = new Boolean[] { true, false };
121 t.s = new Short[] { 0, 1 };
122 t.i = new Integer[] { 2, 3 };
123 t.l = new Long[] { 4l, 5l };
124 t.f = new Float[] { 2.0f, 4.0f };
125 t.d = new Double[] { 8.0, 16.0 };
126 t.str = new String[] { "furuhashi", "java" };
127
128 BufferPacker packer = msgpack.createBufferPacker();
129 packer.write(t);
130 byte[] raw = packer.toByteArray();
131 BufferUnpacker unpacker = msgpack.createBufferUnpacker(raw);
132 ReferenceTest u = unpacker.read(ReferenceTest.class);
133 assertEquals(t.b.length, u.b.length);
134 for (int i = 0; i < t.b.length; i++) {
135 assertEquals(t.b[i], u.b[i]);
136 }
137 assertEquals(t.s.length, u.s.length);
138 for (int i = 0; i < t.s.length; i++) {
139 assertEquals(t.s[i], u.s[i]);
140 }
141 assertEquals(t.i.length, u.i.length);
142 for (int i = 0; i < t.i.length; i++) {
143 assertEquals(t.i[i], u.i[i]);
144 }
145 assertEquals(t.l.length, u.l.length);
146 for (int i = 0; i < t.l.length; i++) {
147 assertEquals(t.l[i], u.l[i]);
148 }
149 assertEquals(t.f.length, u.f.length);
150 for (int i = 0; i < t.f.length; i++) {
151 assertEquals(t.f[i], u.f[i]);
152 }
153 assertEquals(t.d.length, u.d.length);
154 for (int i = 0; i < t.d.length; i++) {
155 assertEquals(t.d[i], u.d[i]);
156 }
157 assertEquals(t.str.length, u.str.length);
158 for (int i = 0; i < t.str.length; i++) {
159 assertEquals(t.str[i], u.str[i]);
160 }
161
162 Unconverter unconverter = new Unconverter(msgpack);
163 unconverter.write(t);
164 Value value = unconverter.getResult();
165 Converter converter = new Converter(msgpack, value);
166 ReferenceTest c = converter.read(ReferenceTest.class);
167 assertEquals(t.b.length, c.b.length);
168 for (int i = 0; i < t.b.length; i++) {
169 assertEquals(t.b[i], c.b[i]);
170 }
171 assertEquals(t.s.length, c.s.length);
172 for (int i = 0; i < t.s.length; i++) {
173 assertEquals(t.s[i], c.s[i]);
174 }
175 assertEquals(t.i.length, c.i.length);
176 for (int i = 0; i < t.i.length; i++) {
177 assertEquals(t.i[i], c.i[i]);
178 }
179 assertEquals(t.l.length, c.l.length);
180 for (int i = 0; i < t.l.length; i++) {
181 assertEquals(t.l[i], c.l[i]);
182 }
183 assertEquals(t.f.length, c.f.length);
184 for (int i = 0; i < t.f.length; i++) {
185 assertEquals(t.f[i], c.f[i]);
186 }
187 assertEquals(t.d.length, c.d.length);
188 for (int i = 0; i < t.d.length; i++) {
189 assertEquals(t.d[i], c.d[i]);
190 }
191 assertEquals(t.str.length, c.str.length);
192 for (int i = 0; i < t.str.length; i++) {
193 assertEquals(t.str[i], c.str[i]);
194 }
195 }
196
197 @Message
198 public static class GenericsTest {
199 public List<String>[] slist;
200 public Map<String, Integer>[] imap;
201
202 public GenericsTest() {
203 }
204 }
205
206 @SuppressWarnings({ "unchecked", "rawtypes" })
207 @Ignore
208 @Test
209 public void testGenerics() throws Exception {
210 MessagePack msgpack = new MessagePack();
211
212 GenericsTest t = new GenericsTest();
213 t.slist = new List[2];
214 t.slist[0] = new ArrayList();
215 t.slist[0].add("aa");
216 t.slist[0].add("bb");
217 t.slist[1] = new ArrayList();
218 t.slist[1].add("cc");
219 t.imap = new Map[2];
220 t.imap[0] = new HashMap();
221 t.imap[0].put("aa", 1);
222 t.imap[0].put("bb", 2);
223 t.imap[1] = new HashMap();
224 t.imap[1].put("cc", 3);
225
226 BufferPacker packer = msgpack.createBufferPacker();
227 packer.write(t);
228 byte[] raw = packer.toByteArray();
229 BufferUnpacker unpacker = msgpack.createBufferUnpacker(raw);
230 GenericsTest u = unpacker.read(GenericsTest.class);
231 assertEquals(t.slist.length, u.slist.length);
232 for (int i = 0; i < t.slist.length; i++) {
233 assertEquals(t.slist[i].size(), u.slist[i].size());
234 for (int j = 0; j < t.slist[i].size(); j++) {
235 assertEquals(t.slist[i].get(j), u.slist[i].get(j));
236 }
237 }
238 for (int i = 0; i < t.imap.length; i++) {
239 assertEquals(t.imap[i].size(), u.imap[i].size());
240 for (String j : t.imap[i].keySet()) {
241 assertEquals(t.imap[i].get(j), u.imap[i].get(j));
242 }
243 }
244
245 Unconverter unconverter = new Unconverter(msgpack);
246 unconverter.write(t);
247 Value value = unconverter.getResult();
248 Converter converter = new Converter(msgpack, value);
249 GenericsTest c = converter.read(GenericsTest.class);
250 assertEquals(t.slist.length, c.slist.length);
251 for (int i = 0; i < t.slist.length; i++) {
252 assertEquals(t.slist[i].size(), c.slist[i].size());
253 for (int j = 0; j < t.slist[i].size(); j++) {
254 assertEquals(t.slist[i].get(j), c.slist[i].get(j));
255 }
256 }
257 for (int i = 0; i < t.imap.length; i++) {
258 assertEquals(t.imap[i].size(), c.imap[i].size());
259 for (String j : t.imap[i].keySet()) {
260 assertEquals(t.imap[i].get(j), c.imap[i].get(j));
261 }
262 }
263 }
264
265 @Message
266 public static class Dim2Test {
267 public int[][] i;
268 public byte[][] b;
269 public String[][] str;
270 //public List<String>[][] slist;
271
272 public Dim2Test() {
273 }
274 }
275
276 @SuppressWarnings({ "unchecked", "rawtypes" })
277 @Test
278 public void testDim2() throws Exception {
279 MessagePack msgpack = new MessagePack();
280 Dim2Test t = new Dim2Test();
281 t.i = new int[2][];
282 t.i[0] = new int[] { 0, 1 };
283 t.i[1] = new int[] { 2, 3, 4 };
284 t.b = new byte[2][];
285 t.b[0] = new byte[] { 5, 6 };
286 t.b[1] = new byte[] { 7, 8, 9 };
287 t.str = new String[2][];
288 t.str[0] = new String[] { "aa", "bb" };
289 t.str[1] = new String[] { "cc", "dd", "ee" };
290 /**
291 t.slist = new List[2][];
292 t.slist[0] = new List[1];
293 t.slist[0][0] = new ArrayList();
294 t.slist[0][0].add("ff");
295 t.slist[0][0].add("gg");
296 t.slist[1] = new List[2];
297 t.slist[1][0] = new ArrayList();
298 t.slist[1][0].add("hh");
299 t.slist[1][0].add("ii");
300 t.slist[1][1] = new ArrayList();
301 t.slist[1][1].add("jj");
302 t.slist[1][1].add("kk");
303 */
304
305 BufferPacker packer = msgpack.createBufferPacker();
306 packer.write(t);
307 byte[] raw = packer.toByteArray();
308 BufferUnpacker unpacker = msgpack.createBufferUnpacker(raw);
309 Dim2Test u = unpacker.read(Dim2Test.class);
310 assertEquals(t.i.length, u.i.length);
311 for (int i = 0; i < t.i.length; i++) {
312 assertEquals(t.i[i].length, u.i[i].length);
313 for (int j = 0; j < t.i[i].length; j++) {
314 assertEquals(t.i[i][j], u.i[i][j]);
315 }
316 }
317 assertEquals(t.b.length, u.b.length);
318 for (int i = 0; i < t.b.length; i++) {
319 assertEquals(t.b[i].length, u.b[i].length);
320 for (int j = 0; j < t.i[i].length; j++) {
321 assertEquals(t.b[i][j], u.b[i][j]);
322 }
323 }
324 assertEquals(t.str.length, u.str.length);
325 for (int i = 0; i < t.str.length; i++) {
326 assertEquals(t.str[i].length, u.str[i].length);
327 for (int j = 0; j < t.str[i].length; j++) {
328 assertEquals(t.str[i][j], u.str[i][j]);
329 }
330 }
331 /**
332 assertEquals(t.slist.length, u.slist.length);
333 for (int i = 0; i < t.slist.length; i++) {
334 assertEquals(t.slist[i].length, u.slist[i].length);
335 for (int j = 0; j < t.slist[i].length; j++) {
336 assertEquals(t.slist[i][j].size(), u.slist[i][j].size());
337 for (int k = 0; k < t.slist[i][j].size(); k++) {
338 assertEquals(t.slist[i][j].get(k), u.slist[i][j].get(k));
339 }
340 }
341 }
342 */
343 }
344
345 @Message
346 public static class Dim3Test {
347 public int[][][] i;
348 public String[][][] str;
349 public List<String>[][][] slist;
350
351 public Dim3Test() {
352 }
353 }
354
355 @SuppressWarnings({ "unchecked", "rawtypes" })
356 @Ignore
357 @Test
358 public void testDim3() throws Exception {
359 MessagePack msgpack = new MessagePack();
360
361 Dim3Test t = new Dim3Test();
362 t.i = new int[2][][];
363 t.i[0] = new int[2][];
364 t.i[0][0] = new int[] { 0, 1 };
365 t.i[0][1] = new int[] { 2, 3, 4 };
366 t.i[1] = new int[1][];
367 t.i[1][0] = new int[] { 5 };
368 t.str = new String[2][][];
369 t.str[0] = new String[1][];
370 t.str[0][0] = new String[] { "aa", "bb" };
371 t.str[1] = new String[2][];
372 t.str[1][0] = new String[] { "cc", "dd", "ee" };
373 t.str[1][1] = new String[] { "ff" };
374 t.slist = new List[2][][];
375 t.slist[0] = new List[2][];
376 t.slist[0][0] = new List[1];
377 t.slist[0][0][0] = new ArrayList();
378 t.slist[0][0][0].add("ff");
379 t.slist[0][0][0].add("gg");
380 t.slist[0][1] = new List[2];
381 t.slist[0][1][0] = new ArrayList();
382 t.slist[0][1][0].add("hh");
383 t.slist[0][1][0].add("ii");
384 t.slist[0][1][1] = new ArrayList();
385 t.slist[0][1][1].add("jj");
386 t.slist[0][1][1].add("kk");
387 t.slist[1] = new List[1][];
388 t.slist[1][0] = new List[0];
389
390 BufferPacker packer = msgpack.createBufferPacker();
391 packer.write(t);
392 byte[] raw = packer.toByteArray();
393 BufferUnpacker unpacker = msgpack.createBufferUnpacker(raw);
394 Dim3Test u = unpacker.read(Dim3Test.class);
395 assertEquals(t.i.length, t.i.length);
396 for (int i = 0; i < t.i.length; i++) {
397 assertEquals(t.i[i].length, u.i[i].length);
398 for (int j = 0; j < t.i[i].length; j++) {
399 for (int k = 0; k < t.i[i].length; k++) {
400 assertEquals(t.i[i][j][k], u.i[i][j][k]);
401 }
402 }
403 }
404 assertEquals(t.str.length, t.str.length);
405 for (int i = 0; i < t.str.length; i++) {
406 assertEquals(t.str[i].length, u.str[i].length);
407 for (int j = 0; j < t.str[i].length; j++) {
408 assertEquals(t.str[i][j].length, u.str[i][j].length);
409 for (int k = 0; k < t.str[i][j].length; k++) {
410 assertEquals(t.str[i][j][k], u.str[i][j][k]);
411 }
412 }
413 }
414 assertEquals(t.slist.length, t.slist.length);
415 for (int i = 0; i < t.slist.length; i++) {
416 assertEquals(t.slist[i].length, u.slist[i].length);
417 for (int j = 0; j < t.slist[i].length; j++) {
418 assertEquals(t.slist[i][j].length, u.slist[i][j].length);
419 for (int k = 0; k < t.slist[i][j].length; k++) {
420 assertEquals(t.slist[i][j][k].size(),
421 u.slist[i][j][k].size());
422 for (int l = 0; l < t.slist[i][j][k].size(); l++) {
423 assertEquals(t.slist[i][j][k].get(l),
424 u.slist[i][j][k].get(l));
425 }
426 }
427 }
428 }
429 }
430
431 @Test
432 public void testLocal() throws IOException {
433 MessagePack msgpack = new MessagePack();
434
435 int[][][] src = new int[10][20][30];
436 for (int i = 0; i < 10; ++i) {
437 for (int j = 0; j < 20; ++j) {
438 for (int k = 0; k < 30; ++k) {
439 src[i][j][k] = (int) (Math.random() * 100);
440 }
441 }
442 }
443
444 BufferPacker packer = msgpack.createBufferPacker();
445 packer.write(src);
446 byte[] raw = packer.toByteArray();
447 BufferUnpacker unpacker = msgpack.createBufferUnpacker(raw);
448 int[][][] u = unpacker.read(int[][][].class);
449 assertEquals(src.length, u.length);
450 for (int i = 0; i < src.length; ++i) {
451 assertEquals(src[i].length, u[i].length);
452 for (int j = 0; j < src[i].length; ++j) {
453 assertEquals(src[i][j].length, u[i][j].length);
454 for (int k = 0; k < src[i][j].length; ++k) {
455 assertEquals(src[i][j][k], u[i][j][k]);
456 }
457 }
458 }
459
460 Unconverter unconverter = new Unconverter(msgpack);
461 unconverter.write(src);
462 Value value = unconverter.getResult();
463 Converter converter = new Converter(msgpack, value);
464 int[][][] c = converter.read(int[][][].class);
465 assertEquals(src.length, c.length);
466 for (int i = 0; i < src.length; ++i) {
467 assertEquals(src[i].length, c[i].length);
468 for (int j = 0; j < src[i].length; ++j) {
469 assertEquals(src[i][j].length, c[i][j].length);
470 for (int k = 0; k < src[i][j].length; ++k) {
471 assertEquals(src[i][j][k], c[i][j][k]);
472 }
473 }
474 }
475 }
476 }