1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 module capnproto.tests.SerializePackedSuite;
23 
24 import java.nio.ByteBuffer;
25 
26 import capnproto;
27 
28 void expectPacksTo(ubyte[] unpacked, ubyte[] packed)
29 {
30 	// ----
31 	// write
32 	{
33 		auto bytes = new ubyte[](packed.length);
34 		auto writer = new ArrayOutputStream(bytes);
35 		auto packedOutputStream = new PackedOutputStream(writer);
36 		auto wrapped = ByteBuffer(unpacked);
37 		packedOutputStream.write(wrapped);
38 		
39 		assert(bytes == packed);
40 	}
41 	
42 	// ------
43 	// read
44 	{
45 		auto reader = new ArrayInputStream(ByteBuffer(packed));
46 		auto packedInputStream = new PackedInputStream(reader);
47 		auto bytes = new ubyte[](unpacked.length);
48 		auto wrapped = ByteBuffer(bytes);
49 		auto n = packedInputStream.read(wrapped);
50 		
51 		assert(n == unpacked.length);
52 		assert(bytes == unpacked);
53 	}
54 }
55 
56 //SimplePacking
57 unittest
58 {
59 	expectPacksTo([], []);
60 	expectPacksTo([0,0,0,0,0,0,0,0], [0,0]);
61 	expectPacksTo([0,0,12,0,0,34,0,0], [0x24,12,34]);
62 	expectPacksTo([1,3,2,4,5,7,6,8], [0xff,1,3,2,4,5,7,6,8,0]);
63 	expectPacksTo([0,0,0,0,0,0,0,0, 1,3,2,4,5,7,6,8], [0,0,0xff,1,3,2,4,5,7,6,8,0]);
64 	expectPacksTo([0,0,12,0,0,34,0,0, 1,3,2,4,5,7,6,8], [0x24, 12, 34, 0xff,1,3,2,4,5,7,6,8,0]);
65 	expectPacksTo([1,3,2,4,5,7,6,8, 8,6,7,4,5,2,3,1], [0xff,1,3,2,4,5,7,6,8,1,8,6,7,4,5,2,3,1]);
66 	
67 	expectPacksTo([1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 0,2,4,0,9,0,5,1],
68 	              [0xff,1,2,3,4,5,6,7,8, 3, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 0xd6,2,4,9,5,1]);
69 	
70 	expectPacksTo([1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 6,2,4,3,9,0,5,1, 1,2,3,4,5,6,7,8, 0,2,4,0,9,0,5,1],
71 	              [0xff,1,2,3,4,5,6,7,8, 3, 1,2,3,4,5,6,7,8, 6,2,4,3,9,0,5,1, 1,2,3,4,5,6,7,8, 0xd6,2,4,9,5,1]);
72 	
73 	expectPacksTo([8,0,100,6,0,1,1,2, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,1,0,2,0,3,1],
74 	              [0xed,8,100,6,1,1,2, 0,2, 0xd4,1,2,3,1]);
75 	
76 	expectPacksTo([0,0,0,0,2,0,0,0, 0,0,0,0,0,0,1,0, 0,0,0,0,0,0,0,0], [0x10,2, 0x40,1, 0,0]);
77 	
78 	import std.array : replicate;
79 	
80 	expectPacksTo([cast(ubyte)0].replicate(8 * 200), [cast(ubyte)0, 199]);
81 	
82 	expectPacksTo([cast(ubyte)1].replicate(8 * 200), cast(ubyte[])[0xff, 1,1,1,1,1,1,1,1, 199] ~ [cast(ubyte)1].replicate(8 * 199));
83 }