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