1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.util;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertTrue;
48
49 import org.junit.Test;
50
51 public class NBTest {
52 @Test
53 public void testCompareUInt32() {
54 assertTrue(NB.compareUInt32(0, 0) == 0);
55 assertTrue(NB.compareUInt32(1, 0) > 0);
56 assertTrue(NB.compareUInt32(0, 1) < 0);
57 assertTrue(NB.compareUInt32(-1, 0) > 0);
58 assertTrue(NB.compareUInt32(0, -1) < 0);
59 assertTrue(NB.compareUInt32(-1, 1) > 0);
60 assertTrue(NB.compareUInt32(1, -1) < 0);
61 }
62
63 @Test
64 public void testCompareUInt64() {
65 assertTrue(NB.compareUInt64(0, 0) == 0);
66 assertTrue(NB.compareUInt64(1, 0) > 0);
67 assertTrue(NB.compareUInt64(0, 1) < 0);
68 assertTrue(NB.compareUInt64(-1, 0) > 0);
69 assertTrue(NB.compareUInt64(0, -1) < 0);
70 assertTrue(NB.compareUInt64(-1, 1) > 0);
71 assertTrue(NB.compareUInt64(1, -1) < 0);
72 }
73
74 @Test
75 public void testDecodeUInt16() {
76 assertEquals(0, NB.decodeUInt16(b(0, 0), 0));
77 assertEquals(0, NB.decodeUInt16(padb(3, 0, 0), 3));
78
79 assertEquals(3, NB.decodeUInt16(b(0, 3), 0));
80 assertEquals(3, NB.decodeUInt16(padb(3, 0, 3), 3));
81
82 assertEquals(0xde03, NB.decodeUInt16(b(0xde, 3), 0));
83 assertEquals(0xde03, NB.decodeUInt16(padb(3, 0xde, 3), 3));
84
85 assertEquals(0x03de, NB.decodeUInt16(b(3, 0xde), 0));
86 assertEquals(0x03de, NB.decodeUInt16(padb(3, 3, 0xde), 3));
87
88 assertEquals(0xffff, NB.decodeUInt16(b(0xff, 0xff), 0));
89 assertEquals(0xffff, NB.decodeUInt16(padb(3, 0xff, 0xff), 3));
90 }
91
92 @Test
93 public void testDecodeUInt24() {
94 assertEquals(0, NB.decodeUInt24(b(0, 0, 0), 0));
95 assertEquals(0, NB.decodeUInt24(padb(3, 0, 0, 0), 3));
96
97 assertEquals(3, NB.decodeUInt24(b(0, 0, 3), 0));
98 assertEquals(3, NB.decodeUInt24(padb(3, 0, 0, 3), 3));
99
100 assertEquals(0xcede03, NB.decodeUInt24(b(0xce, 0xde, 3), 0));
101 assertEquals(0xbade03, NB.decodeUInt24(padb(3, 0xba, 0xde, 3), 3));
102
103 assertEquals(0x03bade, NB.decodeUInt24(b(3, 0xba, 0xde), 0));
104 assertEquals(0x03bade, NB.decodeUInt24(padb(3, 3, 0xba, 0xde), 3));
105
106 assertEquals(0xffffff, NB.decodeUInt24(b(0xff, 0xff, 0xff), 0));
107 assertEquals(0xffffff, NB.decodeUInt24(padb(3, 0xff, 0xff, 0xff), 3));
108 }
109
110 @Test
111 public void testDecodeInt32() {
112 assertEquals(0, NB.decodeInt32(b(0, 0, 0, 0), 0));
113 assertEquals(0, NB.decodeInt32(padb(3, 0, 0, 0, 0), 3));
114
115 assertEquals(3, NB.decodeInt32(b(0, 0, 0, 3), 0));
116 assertEquals(3, NB.decodeInt32(padb(3, 0, 0, 0, 3), 3));
117
118 assertEquals(0xdeadbeef, NB.decodeInt32(b(0xde, 0xad, 0xbe, 0xef), 0));
119 assertEquals(0xdeadbeef, NB.decodeInt32(
120 padb(3, 0xde, 0xad, 0xbe, 0xef), 3));
121
122 assertEquals(0x0310adef, NB.decodeInt32(b(0x03, 0x10, 0xad, 0xef), 0));
123 assertEquals(0x0310adef, NB.decodeInt32(
124 padb(3, 0x03, 0x10, 0xad, 0xef), 3));
125
126 assertEquals(0xffffffff, NB.decodeInt32(b(0xff, 0xff, 0xff, 0xff), 0));
127 assertEquals(0xffffffff, NB.decodeInt32(
128 padb(3, 0xff, 0xff, 0xff, 0xff), 3));
129 }
130
131 @Test
132 public void testDecodeUInt32() {
133 assertEquals(0L, NB.decodeUInt32(b(0, 0, 0, 0), 0));
134 assertEquals(0L, NB.decodeUInt32(padb(3, 0, 0, 0, 0), 3));
135
136 assertEquals(3L, NB.decodeUInt32(b(0, 0, 0, 3), 0));
137 assertEquals(3L, NB.decodeUInt32(padb(3, 0, 0, 0, 3), 3));
138
139 assertEquals(0xdeadbeefL, NB.decodeUInt32(b(0xde, 0xad, 0xbe, 0xef), 0));
140 assertEquals(0xdeadbeefL, NB.decodeUInt32(padb(3, 0xde, 0xad, 0xbe,
141 0xef), 3));
142
143 assertEquals(0x0310adefL, NB.decodeUInt32(b(0x03, 0x10, 0xad, 0xef), 0));
144 assertEquals(0x0310adefL, NB.decodeUInt32(padb(3, 0x03, 0x10, 0xad,
145 0xef), 3));
146
147 assertEquals(0xffffffffL, NB.decodeUInt32(b(0xff, 0xff, 0xff, 0xff), 0));
148 assertEquals(0xffffffffL, NB.decodeUInt32(padb(3, 0xff, 0xff, 0xff,
149 0xff), 3));
150 }
151
152 @Test
153 public void testDecodeUInt64() {
154 assertEquals(0L, NB.decodeUInt64(b(0, 0, 0, 0, 0, 0, 0, 0), 0));
155 assertEquals(0L, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0, 0, 0, 0), 3));
156
157 assertEquals(3L, NB.decodeUInt64(b(0, 0, 0, 0, 0, 0, 0, 3), 0));
158 assertEquals(3L, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0, 0, 0, 3), 3));
159
160 assertEquals(0xdeadbeefL, NB.decodeUInt64(b(0, 0, 0, 0, 0xde, 0xad,
161 0xbe, 0xef), 0));
162 assertEquals(0xdeadbeefL, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0xde,
163 0xad, 0xbe, 0xef), 3));
164
165 assertEquals(0x0310adefL, NB.decodeUInt64(b(0, 0, 0, 0, 0x03, 0x10,
166 0xad, 0xef), 0));
167 assertEquals(0x0310adefL, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0x03,
168 0x10, 0xad, 0xef), 3));
169
170 assertEquals(0xc0ffee78deadbeefL, NB.decodeUInt64(b(0xc0, 0xff, 0xee,
171 0x78, 0xde, 0xad, 0xbe, 0xef), 0));
172 assertEquals(0xc0ffee78deadbeefL, NB.decodeUInt64(padb(3, 0xc0, 0xff,
173 0xee, 0x78, 0xde, 0xad, 0xbe, 0xef), 3));
174
175 assertEquals(0x00000000ffffffffL, NB.decodeUInt64(b(0, 0, 0, 0, 0xff,
176 0xff, 0xff, 0xff), 0));
177 assertEquals(0x00000000ffffffffL, NB.decodeUInt64(padb(3, 0, 0, 0, 0,
178 0xff, 0xff, 0xff, 0xff), 3));
179 assertEquals(0xffffffffffffffffL, NB.decodeUInt64(b(0xff, 0xff, 0xff,
180 0xff, 0xff, 0xff, 0xff, 0xff), 0));
181 assertEquals(0xffffffffffffffffL, NB.decodeUInt64(padb(3, 0xff, 0xff,
182 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), 3));
183 }
184
185 @Test
186 public void testEncodeInt16() {
187 final byte[] out = new byte[16];
188
189 prepareOutput(out);
190 NB.encodeInt16(out, 0, 0);
191 assertOutput(b(0, 0), out, 0);
192
193 prepareOutput(out);
194 NB.encodeInt16(out, 3, 0);
195 assertOutput(b(0, 0), out, 3);
196
197 prepareOutput(out);
198 NB.encodeInt16(out, 0, 3);
199 assertOutput(b(0, 3), out, 0);
200
201 prepareOutput(out);
202 NB.encodeInt16(out, 3, 3);
203 assertOutput(b(0, 3), out, 3);
204
205 prepareOutput(out);
206 NB.encodeInt16(out, 0, 0xdeac);
207 assertOutput(b(0xde, 0xac), out, 0);
208
209 prepareOutput(out);
210 NB.encodeInt16(out, 3, 0xdeac);
211 assertOutput(b(0xde, 0xac), out, 3);
212
213 prepareOutput(out);
214 NB.encodeInt16(out, 3, -1);
215 assertOutput(b(0xff, 0xff), out, 3);
216 }
217
218 @Test
219 public void testEncodeInt24() {
220 byte[] out = new byte[16];
221
222 prepareOutput(out);
223 NB.encodeInt24(out, 0, 0);
224 assertOutput(b(0, 0, 0), out, 0);
225
226 prepareOutput(out);
227 NB.encodeInt24(out, 3, 0);
228 assertOutput(b(0, 0, 0), out, 3);
229
230 prepareOutput(out);
231 NB.encodeInt24(out, 0, 3);
232 assertOutput(b(0, 0, 3), out, 0);
233
234 prepareOutput(out);
235 NB.encodeInt24(out, 3, 3);
236 assertOutput(b(0, 0, 3), out, 3);
237
238 prepareOutput(out);
239 NB.encodeInt24(out, 0, 0xc0deac);
240 assertOutput(b(0xc0, 0xde, 0xac), out, 0);
241
242 prepareOutput(out);
243 NB.encodeInt24(out, 3, 0xbadeac);
244 assertOutput(b(0xba, 0xde, 0xac), out, 3);
245
246 prepareOutput(out);
247 NB.encodeInt24(out, 3, -1);
248 assertOutput(b(0xff, 0xff, 0xff), out, 3);
249 }
250
251 @Test
252 public void testEncodeInt32() {
253 final byte[] out = new byte[16];
254
255 prepareOutput(out);
256 NB.encodeInt32(out, 0, 0);
257 assertOutput(b(0, 0, 0, 0), out, 0);
258
259 prepareOutput(out);
260 NB.encodeInt32(out, 3, 0);
261 assertOutput(b(0, 0, 0, 0), out, 3);
262
263 prepareOutput(out);
264 NB.encodeInt32(out, 0, 3);
265 assertOutput(b(0, 0, 0, 3), out, 0);
266
267 prepareOutput(out);
268 NB.encodeInt32(out, 3, 3);
269 assertOutput(b(0, 0, 0, 3), out, 3);
270
271 prepareOutput(out);
272 NB.encodeInt32(out, 0, 0xdeac);
273 assertOutput(b(0, 0, 0xde, 0xac), out, 0);
274
275 prepareOutput(out);
276 NB.encodeInt32(out, 3, 0xdeac);
277 assertOutput(b(0, 0, 0xde, 0xac), out, 3);
278
279 prepareOutput(out);
280 NB.encodeInt32(out, 0, 0xdeac9853);
281 assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 0);
282
283 prepareOutput(out);
284 NB.encodeInt32(out, 3, 0xdeac9853);
285 assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 3);
286
287 prepareOutput(out);
288 NB.encodeInt32(out, 3, -1);
289 assertOutput(b(0xff, 0xff, 0xff, 0xff), out, 3);
290 }
291
292 @Test
293 public void testEncodeInt64() {
294 final byte[] out = new byte[16];
295
296 prepareOutput(out);
297 NB.encodeInt64(out, 0, 0L);
298 assertOutput(b(0, 0, 0, 0, 0, 0, 0, 0), out, 0);
299
300 prepareOutput(out);
301 NB.encodeInt64(out, 3, 0L);
302 assertOutput(b(0, 0, 0, 0, 0, 0, 0, 0), out, 3);
303
304 prepareOutput(out);
305 NB.encodeInt64(out, 0, 3L);
306 assertOutput(b(0, 0, 0, 0, 0, 0, 0, 3), out, 0);
307
308 prepareOutput(out);
309 NB.encodeInt64(out, 3, 3L);
310 assertOutput(b(0, 0, 0, 0, 0, 0, 0, 3), out, 3);
311
312 prepareOutput(out);
313 NB.encodeInt64(out, 0, 0xdeacL);
314 assertOutput(b(0, 0, 0, 0, 0, 0, 0xde, 0xac), out, 0);
315
316 prepareOutput(out);
317 NB.encodeInt64(out, 3, 0xdeacL);
318 assertOutput(b(0, 0, 0, 0, 0, 0, 0xde, 0xac), out, 3);
319
320 prepareOutput(out);
321 NB.encodeInt64(out, 0, 0xdeac9853L);
322 assertOutput(b(0, 0, 0, 0, 0xde, 0xac, 0x98, 0x53), out, 0);
323
324 prepareOutput(out);
325 NB.encodeInt64(out, 3, 0xdeac9853L);
326 assertOutput(b(0, 0, 0, 0, 0xde, 0xac, 0x98, 0x53), out, 3);
327
328 prepareOutput(out);
329 NB.encodeInt64(out, 0, 0xac431242deac9853L);
330 assertOutput(b(0xac, 0x43, 0x12, 0x42, 0xde, 0xac, 0x98, 0x53), out, 0);
331
332 prepareOutput(out);
333 NB.encodeInt64(out, 3, 0xac431242deac9853L);
334 assertOutput(b(0xac, 0x43, 0x12, 0x42, 0xde, 0xac, 0x98, 0x53), out, 3);
335
336 prepareOutput(out);
337 NB.encodeInt64(out, 3, -1L);
338 assertOutput(b(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), out, 3);
339 }
340
341 private static void prepareOutput(byte[] buf) {
342 for (int i = 0; i < buf.length; i++)
343 buf[i] = (byte) (0x77 + i);
344 }
345
346 private static void assertOutput(final byte[] expect, final byte[] buf,
347 final int offset) {
348 for (int i = 0; i < offset; i++)
349 assertEquals((byte) (0x77 + i), buf[i]);
350 for (int i = 0; i < expect.length; i++)
351 assertEquals(expect[i], buf[offset + i]);
352 for (int i = offset + expect.length; i < buf.length; i++)
353 assertEquals((byte) (0x77 + i), buf[i]);
354 }
355
356 private static byte[] b(int a, int b) {
357 return new byte[] { (byte) a, (byte) b };
358 }
359
360 private static byte[] padb(int len, int a, int b) {
361 final byte[] r = new byte[len + 2];
362 for (int i = 0; i < len; i++)
363 r[i] = (byte) 0xaf;
364 r[len] = (byte) a;
365 r[len + 1] = (byte) b;
366 return r;
367 }
368
369 private static byte[] b(int a, int b, int c) {
370 return new byte[] { (byte) a, (byte) b, (byte) c };
371 }
372
373 private static byte[] b(int a, int b, int c, int d) {
374 return new byte[] { (byte) a, (byte) b, (byte) c, (byte) d };
375 }
376
377 private static byte[] padb(int len, int a, int b, int c) {
378 final byte[] r = new byte[len + 4];
379 for (int i = 0; i < len; i++)
380 r[i] = (byte) 0xaf;
381 r[len] = (byte) a;
382 r[len + 1] = (byte) b;
383 r[len + 2] = (byte) c;
384 return r;
385 }
386
387 private static byte[] padb(final int len, final int a, final int b,
388 final int c, final int d) {
389 final byte[] r = new byte[len + 4];
390 for (int i = 0; i < len; i++)
391 r[i] = (byte) 0xaf;
392 r[len] = (byte) a;
393 r[len + 1] = (byte) b;
394 r[len + 2] = (byte) c;
395 r[len + 3] = (byte) d;
396 return r;
397 }
398
399 private static byte[] b(final int a, final int b, final int c, final int d,
400 final int e, final int f, final int g, final int h) {
401 return new byte[] { (byte) a, (byte) b, (byte) c, (byte) d, (byte) e,
402 (byte) f, (byte) g, (byte) h };
403 }
404
405 private static byte[] padb(final int len, final int a, final int b,
406 final int c, final int d, final int e, final int f, final int g,
407 final int h) {
408 final byte[] r = new byte[len + 8];
409 for (int i = 0; i < len; i++)
410 r[i] = (byte) 0xaf;
411 r[len] = (byte) a;
412 r[len + 1] = (byte) b;
413 r[len + 2] = (byte) c;
414 r[len + 3] = (byte) d;
415 r[len + 4] = (byte) e;
416 r[len + 5] = (byte) f;
417 r[len + 6] = (byte) g;
418 r[len + 7] = (byte) h;
419 return r;
420 }
421 }