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.SerializeSuite;
23
24 import java.nio.ByteBuffer;
25
26 import capnproto;
27
28 //SegmentReading
29 unittest
30 {
31 //When transmitting over a stream, the following should be sent. All integers are unsigned and little-endian.
32 //- (4 bytes) The number of segments, minus one (since there is always at least one segment).
33 //- (N * 4 bytes) The size of each segment, in words.
34 //- (0 or 4 bytes) Padding up to the next word boundary.
35 //- The content of each segment, in order.
36
37 expectSerializesTo(1, [
38 0, 0, 0, 0, //1 segment.
39 0, 0, 0, 0 //Segment 0 contains 0 bytes.
40 //No padding.
41 //Segment 0 (empty).
42 ]);
43
44 expectSerializesTo(2, [
45 1, 0, 0, 0, //2 segments.
46 0, 0, 0, 0, //Segment 0 contains 0 words.
47 1, 0, 0, 0, //Segment 1 contains 1 words.
48 //Padding.
49 0, 0, 0, 0,
50 //Segment 0 (empty).
51 //Segment 1.
52 1, 0, 0, 0, 0, 0, 0, 0
53 ]);
54
55 expectSerializesTo(3, [
56 2, 0, 0, 0, //3 segments.
57 0, 0, 0, 0, //Segment 0 contains 0 words.
58 1, 0, 0, 0, //Segment 1 contains 1 words.
59 2, 0, 0, 0, //Segment 2 contains 2 words.
60 //No padding.
61 //Segment 0 (empty).
62 //Segment 1.
63 1, 0, 0, 0, 0, 0, 0, 0,
64 //Segment 2.
65 2, 0, 0, 0, 0, 0, 0, 0,
66 2, 0, 0, 0, 0, 0, 0, 0
67 ]);
68
69 expectSerializesTo(4, [
70 3, 0, 0, 0, //4 segments.
71 0, 0, 0, 0, //Segment 0 contains 0 words.
72 1, 0, 0, 0, //Segment 1 contains 1 words.
73 2, 0, 0, 0, //Segment 2 contains 2 words.
74 3, 0, 0, 0, //Segment 3 contains 3 words.
75 //Padding.
76 0, 0, 0, 0,
77 //Segment 0 (empty).
78 //Segment 1.
79 1, 0, 0, 0, 0, 0, 0, 0,
80 //Segment 2.
81 2, 0, 0, 0, 0, 0, 0, 0,
82 2, 0, 0, 0, 0, 0, 0, 0,
83 //Segment 3.
84 3, 0, 0, 0, 0, 0, 0, 0,
85 3, 0, 0, 0, 0, 0, 0, 0,
86 3, 0, 0, 0, 0, 0, 0, 0
87 ]);
88 }
89
90 /**
91 * \param exampleSegmentCount number of segments.
92 * \param exampleBytes byte array containing `segmentCount` segments; segment `i` contains `i` words each set to `i`.
93 */
94 void expectSerializesTo(int exampleSegmentCount, ubyte[] exampleBytes)
95 {
96 void checkSegmentContents(ReaderArena arena)
97 {
98 assert(arena.segments.data.length == exampleSegmentCount);
99 foreach(i; 0..exampleSegmentCount)
100 {
101 auto segment = arena.segments.data[i];
102 auto segmentWords = ByteBuffer(segment.buffer.buffer[segment.buffer.position..segment.buffer.limit], segment.buffer.limit/8, 0);
103
104 assert(segmentWords.capacity/8 == i);
105 segmentWords.rewind();
106 while(!segmentWords.empty())
107 assert(segmentWords.get!long() == i);
108 }
109 }
110
111
112 // ----
113 // read via ReadableByteChannel
114 {
115 auto messageReader = Serialize.read(new ArrayInputStream(exampleBytes));
116 checkSegmentContents(messageReader.arena);
117 }
118
119 // ------
120 // read via ByteBuffer
121 {
122 auto wrapped = ByteBuffer(exampleBytes);
123 auto messageReader = Serialize.read(wrapped);
124 checkSegmentContents(messageReader.arena);
125 }
126 }