comparison src/test/java/org/msgpack/template/TestIntegerArrayTemplate.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.template;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.io.ByteArrayInputStream;
6 import java.io.ByteArrayOutputStream;
7
8 import org.junit.Test;
9 import org.msgpack.MessagePack;
10 import org.msgpack.TestSet;
11 import org.msgpack.packer.BufferPacker;
12 import org.msgpack.packer.Packer;
13 import org.msgpack.unpacker.BufferUnpacker;
14 import org.msgpack.unpacker.Unpacker;
15
16
17 public class TestIntegerArrayTemplate {
18
19 @Test
20 public void testPackUnpack00() throws Exception {
21 new TestPackUnpack(0).testIntegerArray();
22 }
23
24 @Test
25 public void testPackUnpack01() throws Exception {
26 new TestPackUnpack(1).testIntegerArray();
27 }
28
29 @Test
30 public void testPackUnpack02() throws Exception {
31 new TestPackUnpack(2).testIntegerArray();
32 }
33
34 @Test
35 public void testPackBufferUnpack00() throws Exception {
36 new TestPackBufferUnpack(0).testIntegerArray();
37 }
38
39 @Test
40 public void testPackBufferUnpack01() throws Exception {
41 new TestPackBufferUnpack(1).testIntegerArray();
42 }
43
44 @Test
45 public void testPackBufferUnpack02() throws Exception {
46 new TestPackBufferUnpack(2).testIntegerArray();
47 }
48
49 @Test
50 public void testBufferPackBufferUnpack00() throws Exception {
51 new TestBufferPackBufferUnpack(0).testIntegerArray();
52 }
53
54 @Test
55 public void testBufferPackBufferUnpack01() throws Exception {
56 new TestBufferPackBufferUnpack(1).testIntegerArray();
57 }
58
59 @Test
60 public void testBufferPackBufferUnpack02() throws Exception {
61 new TestBufferPackBufferUnpack(2).testIntegerArray();
62 }
63
64 @Test
65 public void testBufferPackUnpack00() throws Exception {
66 new TestBufferPackUnpack(0).testIntegerArray();
67 }
68
69 @Test
70 public void testBufferPackUnpack01() throws Exception {
71 new TestBufferPackUnpack(1).testIntegerArray();
72 }
73
74 @Test
75 public void testBufferPackUnpack02() throws Exception {
76 new TestBufferPackUnpack(2).testIntegerArray();
77 }
78
79 private static class TestPackUnpack extends TestSet {
80 private int index;
81
82 TestPackUnpack(int i) {
83 index = i;
84 }
85
86 @Test @Override
87 public void testIntegerArray() throws Exception {
88 super.testIntegerArray();
89 }
90
91 @Override
92 public void testIntegerArray(int[] v) throws Exception {
93 MessagePack msgpack = new MessagePack();
94 Template<int[]> tmpl = IntegerArrayTemplate.instance;
95 ByteArrayOutputStream out = new ByteArrayOutputStream();
96 Packer packer = msgpack.createPacker(out);
97 tmpl.write(packer, v);
98 byte[] bytes = out.toByteArray();
99 Unpacker unpacker = msgpack.createUnpacker(new ByteArrayInputStream(bytes));
100 unpacker.resetReadByteCount();
101 int[] ret0;
102 switch (index) {
103 case 0:
104 ret0 = null;
105 break;
106 case 1:
107 if (v == null) {
108 ret0 = new int[0];
109 } else {
110 ret0 = new int[v.length];
111 }
112 break;
113 case 2:
114 if (v == null) {
115 ret0 = new int[0];
116 } else {
117 ret0 = new int[(int) v.length / 2];
118 }
119 break;
120 default:
121 throw new IllegalArgumentException();
122 }
123 int[] ret = tmpl.read(unpacker, ret0);
124 assertIntegerArrayEquals(v, ret);
125 assertEquals(bytes.length, unpacker.getReadByteCount());
126 }
127 }
128
129 private static class TestPackBufferUnpack extends TestSet {
130 private int index;
131
132 TestPackBufferUnpack(int i) {
133 index = i;
134 }
135
136 @Test @Override
137 public void testIntegerArray() throws Exception {
138 super.testIntegerArray();
139 }
140
141 @Override
142 public void testIntegerArray(int[] v) throws Exception {
143 MessagePack msgpack = new MessagePack();
144 Template<int[]> tmpl = IntegerArrayTemplate.instance;
145 ByteArrayOutputStream out = new ByteArrayOutputStream();
146 Packer packer = msgpack.createPacker(out);
147 tmpl.write(packer, v);
148 byte[] bytes = out.toByteArray();
149 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes);
150 unpacker.resetReadByteCount();
151 int[] ret0;
152 switch (index) {
153 case 0:
154 ret0 = null;
155 break;
156 case 1:
157 if (v == null) {
158 ret0 = new int[0];
159 } else {
160 ret0 = new int[v.length];
161 }
162 break;
163 case 2:
164 if (v == null) {
165 ret0 = new int[0];
166 } else {
167 ret0 = new int[(int) v.length / 2];
168 }
169 break;
170 default:
171 throw new IllegalArgumentException();
172 }
173 int[] ret = tmpl.read(unpacker, ret0);
174 assertIntegerArrayEquals(v, ret);
175 assertEquals(bytes.length, unpacker.getReadByteCount());
176 }
177 }
178
179 private static class TestBufferPackBufferUnpack extends TestSet {
180 private int index;
181
182 TestBufferPackBufferUnpack(int i) {
183 index = i;
184 }
185
186 @Test @Override
187 public void testIntegerArray() throws Exception {
188 super.testIntegerArray();
189 }
190
191 @Override
192 public void testIntegerArray(int[] v) throws Exception {
193 MessagePack msgpack = new MessagePack();
194 Template<int[]> tmpl = IntegerArrayTemplate.instance;
195 BufferPacker packer = msgpack.createBufferPacker();
196 tmpl.write(packer, v);
197 byte[] bytes = packer.toByteArray();
198 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes);
199 unpacker.resetReadByteCount();
200 int[] ret0;
201 switch (index) {
202 case 0:
203 ret0 = null;
204 break;
205 case 1:
206 if (v == null) {
207 ret0 = new int[0];
208 } else {
209 ret0 = new int[v.length];
210 }
211 break;
212 case 2:
213 if (v == null) {
214 ret0 = new int[0];
215 } else {
216 ret0 = new int[(int) v.length / 2];
217 }
218 break;
219 default:
220 throw new IllegalArgumentException();
221 }
222 int[] ret = tmpl.read(unpacker, ret0);
223 assertIntegerArrayEquals(v, ret);
224 assertEquals(bytes.length, unpacker.getReadByteCount());
225 }
226 }
227
228 private static class TestBufferPackUnpack extends TestSet {
229 private int index;
230
231 TestBufferPackUnpack(int i) {
232 index = i;
233 }
234
235 @Test @Override
236 public void testIntegerArray() throws Exception {
237 super.testIntegerArray();
238 }
239
240 @Override
241 public void testIntegerArray(int[] v) throws Exception {
242 MessagePack msgpack = new MessagePack();
243 Template<int[]> tmpl = IntegerArrayTemplate.instance;
244 BufferPacker packer = msgpack.createBufferPacker();
245 tmpl.write(packer, v);
246 byte[] bytes = packer.toByteArray();
247 Unpacker unpacker = msgpack.createUnpacker(new ByteArrayInputStream(bytes));
248 unpacker.resetReadByteCount();
249 int[] ret0;
250 switch (index) {
251 case 0:
252 ret0 = null;
253 break;
254 case 1:
255 if (v == null) {
256 ret0 = new int[0];
257 } else {
258 ret0 = new int[v.length];
259 }
260 break;
261 case 2:
262 if (v == null) {
263 ret0 = new int[0];
264 } else {
265 ret0 = new int[(int) v.length / 2];
266 }
267 break;
268 default:
269 throw new IllegalArgumentException();
270 }
271 int[] ret = tmpl.read(unpacker, ret0);
272 assertIntegerArrayEquals(v, ret);
273 assertEquals(bytes.length, unpacker.getReadByteCount());
274 }
275 }
276
277 public static void assertIntegerArrayEquals(int[] v, int[] ret) {
278 if (v == null) {
279 assertEquals(null, ret);
280 return;
281 }
282 assertEquals(v.length, ret.length);
283 for (int i = 0; i < v.length; ++i) {
284 assertEquals(v[i], ret[i]);
285 }
286 }
287 }