Add test cases for NB.encode and NB.decode family of routines
[egit.git] / org.spearce.jgit.test / tst / org / spearce / jgit / util / NBTest.java
blob217db7ff6c2bdd4f6f126aa334b642d70095f363
1 /*
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.util;
40 import junit.framework.TestCase;
42 public class NBTest extends TestCase {
43 public void testCompareUInt32() {
44 assertTrue(NB.compareUInt32(0, 0) == 0);
45 assertTrue(NB.compareUInt32(1, 0) > 0);
46 assertTrue(NB.compareUInt32(0, 1) < 0);
47 assertTrue(NB.compareUInt32(-1, 0) > 0);
48 assertTrue(NB.compareUInt32(0, -1) < 0);
49 assertTrue(NB.compareUInt32(-1, 1) > 0);
50 assertTrue(NB.compareUInt32(1, -1) < 0);
53 public void testDecodeUInt16() {
54 assertEquals(0, NB.decodeUInt16(b(0, 0), 0));
55 assertEquals(0, NB.decodeUInt16(padb(3, 0, 0), 3));
57 assertEquals(3, NB.decodeUInt16(b(0, 3), 0));
58 assertEquals(3, NB.decodeUInt16(padb(3, 0, 3), 3));
60 assertEquals(0xde03, NB.decodeUInt16(b(0xde, 3), 0));
61 assertEquals(0xde03, NB.decodeUInt16(padb(3, 0xde, 3), 3));
63 assertEquals(0x03de, NB.decodeUInt16(b(3, 0xde), 0));
64 assertEquals(0x03de, NB.decodeUInt16(padb(3, 3, 0xde), 3));
66 assertEquals(0xffff, NB.decodeUInt16(b(0xff, 0xff), 0));
67 assertEquals(0xffff, NB.decodeUInt16(padb(3, 0xff, 0xff), 3));
70 public void testDecodeInt32() {
71 assertEquals(0, NB.decodeInt32(b(0, 0, 0, 0), 0));
72 assertEquals(0, NB.decodeInt32(padb(3, 0, 0, 0, 0), 3));
74 assertEquals(3, NB.decodeInt32(b(0, 0, 0, 3), 0));
75 assertEquals(3, NB.decodeInt32(padb(3, 0, 0, 0, 3), 3));
77 assertEquals(0xdeadbeef, NB.decodeInt32(b(0xde, 0xad, 0xbe, 0xef), 0));
78 assertEquals(0xdeadbeef, NB.decodeInt32(
79 padb(3, 0xde, 0xad, 0xbe, 0xef), 3));
81 assertEquals(0x0310adef, NB.decodeInt32(b(0x03, 0x10, 0xad, 0xef), 0));
82 assertEquals(0x0310adef, NB.decodeInt32(
83 padb(3, 0x03, 0x10, 0xad, 0xef), 3));
85 assertEquals(0xffffffff, NB.decodeInt32(b(0xff, 0xff, 0xff, 0xff), 0));
86 assertEquals(0xffffffff, NB.decodeInt32(
87 padb(3, 0xff, 0xff, 0xff, 0xff), 3));
90 public void testDecodeUInt32() {
91 assertEquals(0L, NB.decodeUInt32(b(0, 0, 0, 0), 0));
92 assertEquals(0L, NB.decodeUInt32(padb(3, 0, 0, 0, 0), 3));
94 assertEquals(3L, NB.decodeUInt32(b(0, 0, 0, 3), 0));
95 assertEquals(3L, NB.decodeUInt32(padb(3, 0, 0, 0, 3), 3));
97 assertEquals(0xdeadbeefL, NB.decodeUInt32(b(0xde, 0xad, 0xbe, 0xef), 0));
98 assertEquals(0xdeadbeefL, NB.decodeUInt32(padb(3, 0xde, 0xad, 0xbe,
99 0xef), 3));
101 assertEquals(0x0310adefL, NB.decodeUInt32(b(0x03, 0x10, 0xad, 0xef), 0));
102 assertEquals(0x0310adefL, NB.decodeUInt32(padb(3, 0x03, 0x10, 0xad,
103 0xef), 3));
105 assertEquals(0xffffffffL, NB.decodeUInt32(b(0xff, 0xff, 0xff, 0xff), 0));
106 assertEquals(0xffffffffL, NB.decodeUInt32(padb(3, 0xff, 0xff, 0xff,
107 0xff), 3));
110 public void testDecodeUInt64() {
111 assertEquals(0L, NB.decodeUInt64(b(0, 0, 0, 0, 0, 0, 0, 0), 0));
112 assertEquals(0L, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0, 0, 0, 0), 3));
114 assertEquals(3L, NB.decodeUInt64(b(0, 0, 0, 0, 0, 0, 0, 3), 0));
115 assertEquals(3L, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0, 0, 0, 3), 3));
117 assertEquals(0xdeadbeefL, NB.decodeUInt64(b(0, 0, 0, 0, 0xde, 0xad,
118 0xbe, 0xef), 0));
119 assertEquals(0xdeadbeefL, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0xde,
120 0xad, 0xbe, 0xef), 3));
122 assertEquals(0x0310adefL, NB.decodeUInt64(b(0, 0, 0, 0, 0x03, 0x10,
123 0xad, 0xef), 0));
124 assertEquals(0x0310adefL, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0x03,
125 0x10, 0xad, 0xef), 3));
127 assertEquals(0xc0ffee78deadbeefL, NB.decodeUInt64(b(0xc0, 0xff, 0xee,
128 0x78, 0xde, 0xad, 0xbe, 0xef), 0));
129 assertEquals(0xc0ffee78deadbeefL, NB.decodeUInt64(padb(3, 0xc0, 0xff,
130 0xee, 0x78, 0xde, 0xad, 0xbe, 0xef), 3));
132 assertEquals(0x00000000ffffffffL, NB.decodeUInt64(b(0, 0, 0, 0, 0xff,
133 0xff, 0xff, 0xff), 0));
134 assertEquals(0x00000000ffffffffL, NB.decodeUInt64(padb(3, 0, 0, 0, 0,
135 0xff, 0xff, 0xff, 0xff), 3));
136 assertEquals(0xffffffffffffffffL, NB.decodeUInt64(b(0xff, 0xff, 0xff,
137 0xff, 0xff, 0xff, 0xff, 0xff), 0));
138 assertEquals(0xffffffffffffffffL, NB.decodeUInt64(padb(3, 0xff, 0xff,
139 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), 3));
142 public void testEncodeInt16() {
143 final byte[] out = new byte[16];
145 prepareOutput(out);
146 NB.encodeInt16(out, 0, 0);
147 assertOutput(b(0, 0), out, 0);
149 prepareOutput(out);
150 NB.encodeInt16(out, 3, 0);
151 assertOutput(b(0, 0), out, 3);
153 prepareOutput(out);
154 NB.encodeInt16(out, 0, 3);
155 assertOutput(b(0, 3), out, 0);
157 prepareOutput(out);
158 NB.encodeInt16(out, 3, 3);
159 assertOutput(b(0, 3), out, 3);
161 prepareOutput(out);
162 NB.encodeInt16(out, 0, 0xdeac);
163 assertOutput(b(0xde, 0xac), out, 0);
165 prepareOutput(out);
166 NB.encodeInt16(out, 3, 0xdeac);
167 assertOutput(b(0xde, 0xac), out, 3);
169 prepareOutput(out);
170 NB.encodeInt16(out, 3, -1);
171 assertOutput(b(0xff, 0xff), out, 3);
174 public void testEncodeInt32() {
175 final byte[] out = new byte[16];
177 prepareOutput(out);
178 NB.encodeInt32(out, 0, 0);
179 assertOutput(b(0, 0, 0, 0), out, 0);
181 prepareOutput(out);
182 NB.encodeInt32(out, 3, 0);
183 assertOutput(b(0, 0, 0, 0), out, 3);
185 prepareOutput(out);
186 NB.encodeInt32(out, 0, 3);
187 assertOutput(b(0, 0, 0, 3), out, 0);
189 prepareOutput(out);
190 NB.encodeInt32(out, 3, 3);
191 assertOutput(b(0, 0, 0, 3), out, 3);
193 prepareOutput(out);
194 NB.encodeInt32(out, 0, 0xdeac);
195 assertOutput(b(0, 0, 0xde, 0xac), out, 0);
197 prepareOutput(out);
198 NB.encodeInt32(out, 3, 0xdeac);
199 assertOutput(b(0, 0, 0xde, 0xac), out, 3);
201 prepareOutput(out);
202 NB.encodeInt32(out, 0, 0xdeac9853);
203 assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 0);
205 prepareOutput(out);
206 NB.encodeInt32(out, 3, 0xdeac9853);
207 assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 3);
209 prepareOutput(out);
210 NB.encodeInt32(out, 3, -1);
211 assertOutput(b(0xff, 0xff, 0xff, 0xff), out, 3);
214 public void testEncodeInt64() {
215 final byte[] out = new byte[16];
217 prepareOutput(out);
218 NB.encodeInt64(out, 0, 0L);
219 assertOutput(b(0, 0, 0, 0, 0, 0, 0, 0), out, 0);
221 prepareOutput(out);
222 NB.encodeInt64(out, 3, 0L);
223 assertOutput(b(0, 0, 0, 0, 0, 0, 0, 0), out, 3);
225 prepareOutput(out);
226 NB.encodeInt64(out, 0, 3L);
227 assertOutput(b(0, 0, 0, 0, 0, 0, 0, 3), out, 0);
229 prepareOutput(out);
230 NB.encodeInt64(out, 3, 3L);
231 assertOutput(b(0, 0, 0, 0, 0, 0, 0, 3), out, 3);
233 prepareOutput(out);
234 NB.encodeInt64(out, 0, 0xdeacL);
235 assertOutput(b(0, 0, 0, 0, 0, 0, 0xde, 0xac), out, 0);
237 prepareOutput(out);
238 NB.encodeInt64(out, 3, 0xdeacL);
239 assertOutput(b(0, 0, 0, 0, 0, 0, 0xde, 0xac), out, 3);
241 prepareOutput(out);
242 NB.encodeInt64(out, 0, 0xdeac9853L);
243 assertOutput(b(0, 0, 0, 0, 0xde, 0xac, 0x98, 0x53), out, 0);
245 prepareOutput(out);
246 NB.encodeInt64(out, 3, 0xdeac9853L);
247 assertOutput(b(0, 0, 0, 0, 0xde, 0xac, 0x98, 0x53), out, 3);
249 prepareOutput(out);
250 NB.encodeInt64(out, 0, 0xac431242deac9853L);
251 assertOutput(b(0xac, 0x43, 0x12, 0x42, 0xde, 0xac, 0x98, 0x53), out, 0);
253 prepareOutput(out);
254 NB.encodeInt64(out, 3, 0xac431242deac9853L);
255 assertOutput(b(0xac, 0x43, 0x12, 0x42, 0xde, 0xac, 0x98, 0x53), out, 3);
257 prepareOutput(out);
258 NB.encodeInt64(out, 3, -1L);
259 assertOutput(b(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), out, 3);
262 private static void prepareOutput(final byte[] buf) {
263 for (int i = 0; i < buf.length; i++)
264 buf[i] = (byte) (0x77 + i);
267 private static void assertOutput(final byte[] expect, final byte[] buf,
268 final int offset) {
269 for (int i = 0; i < offset; i++)
270 assertEquals((byte) (0x77 + i), buf[i]);
271 for (int i = 0; i < expect.length; i++)
272 assertEquals(expect[i], buf[offset + i]);
273 for (int i = offset + expect.length; i < buf.length; i++)
274 assertEquals((byte) (0x77 + i), buf[i]);
277 private static byte[] b(final int a, final int b) {
278 return new byte[] { (byte) a, (byte) b };
281 private static byte[] padb(final int len, final int a, final int b) {
282 final byte[] r = new byte[len + 2];
283 for (int i = 0; i < len; i++)
284 r[i] = (byte) 0xaf;
285 r[len] = (byte) a;
286 r[len + 1] = (byte) b;
287 return r;
290 private static byte[] b(final int a, final int b, final int c, final int d) {
291 return new byte[] { (byte) a, (byte) b, (byte) c, (byte) d };
294 private static byte[] padb(final int len, final int a, final int b,
295 final int c, final int d) {
296 final byte[] r = new byte[len + 4];
297 for (int i = 0; i < len; i++)
298 r[i] = (byte) 0xaf;
299 r[len] = (byte) a;
300 r[len + 1] = (byte) b;
301 r[len + 2] = (byte) c;
302 r[len + 3] = (byte) d;
303 return r;
306 private static byte[] b(final int a, final int b, final int c, final int d,
307 final int e, final int f, final int g, final int h) {
308 return new byte[] { (byte) a, (byte) b, (byte) c, (byte) d, (byte) e,
309 (byte) f, (byte) g, (byte) h };
312 private static byte[] padb(final int len, final int a, final int b,
313 final int c, final int d, final int e, final int f, final int g,
314 final int h) {
315 final byte[] r = new byte[len + 8];
316 for (int i = 0; i < len; i++)
317 r[i] = (byte) 0xaf;
318 r[len] = (byte) a;
319 r[len + 1] = (byte) b;
320 r[len + 2] = (byte) c;
321 r[len + 3] = (byte) d;
322 r[len + 4] = (byte) e;
323 r[len + 5] = (byte) f;
324 r[len + 6] = (byte) g;
325 r[len + 7] = (byte) h;
326 return r;