View Javadoc
1   /*
2    * Copyright (C) 2008, 2015 Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 testDecodeInt32() {
94  		assertEquals(0, NB.decodeInt32(b(0, 0, 0, 0), 0));
95  		assertEquals(0, NB.decodeInt32(padb(3, 0, 0, 0, 0), 3));
96  
97  		assertEquals(3, NB.decodeInt32(b(0, 0, 0, 3), 0));
98  		assertEquals(3, NB.decodeInt32(padb(3, 0, 0, 0, 3), 3));
99  
100 		assertEquals(0xdeadbeef, NB.decodeInt32(b(0xde, 0xad, 0xbe, 0xef), 0));
101 		assertEquals(0xdeadbeef, NB.decodeInt32(
102 				padb(3, 0xde, 0xad, 0xbe, 0xef), 3));
103 
104 		assertEquals(0x0310adef, NB.decodeInt32(b(0x03, 0x10, 0xad, 0xef), 0));
105 		assertEquals(0x0310adef, NB.decodeInt32(
106 				padb(3, 0x03, 0x10, 0xad, 0xef), 3));
107 
108 		assertEquals(0xffffffff, NB.decodeInt32(b(0xff, 0xff, 0xff, 0xff), 0));
109 		assertEquals(0xffffffff, NB.decodeInt32(
110 				padb(3, 0xff, 0xff, 0xff, 0xff), 3));
111 	}
112 
113 	@Test
114 	public void testDecodeUInt32() {
115 		assertEquals(0L, NB.decodeUInt32(b(0, 0, 0, 0), 0));
116 		assertEquals(0L, NB.decodeUInt32(padb(3, 0, 0, 0, 0), 3));
117 
118 		assertEquals(3L, NB.decodeUInt32(b(0, 0, 0, 3), 0));
119 		assertEquals(3L, NB.decodeUInt32(padb(3, 0, 0, 0, 3), 3));
120 
121 		assertEquals(0xdeadbeefL, NB.decodeUInt32(b(0xde, 0xad, 0xbe, 0xef), 0));
122 		assertEquals(0xdeadbeefL, NB.decodeUInt32(padb(3, 0xde, 0xad, 0xbe,
123 				0xef), 3));
124 
125 		assertEquals(0x0310adefL, NB.decodeUInt32(b(0x03, 0x10, 0xad, 0xef), 0));
126 		assertEquals(0x0310adefL, NB.decodeUInt32(padb(3, 0x03, 0x10, 0xad,
127 				0xef), 3));
128 
129 		assertEquals(0xffffffffL, NB.decodeUInt32(b(0xff, 0xff, 0xff, 0xff), 0));
130 		assertEquals(0xffffffffL, NB.decodeUInt32(padb(3, 0xff, 0xff, 0xff,
131 				0xff), 3));
132 	}
133 
134 	@Test
135 	public void testDecodeUInt64() {
136 		assertEquals(0L, NB.decodeUInt64(b(0, 0, 0, 0, 0, 0, 0, 0), 0));
137 		assertEquals(0L, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0, 0, 0, 0), 3));
138 
139 		assertEquals(3L, NB.decodeUInt64(b(0, 0, 0, 0, 0, 0, 0, 3), 0));
140 		assertEquals(3L, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0, 0, 0, 3), 3));
141 
142 		assertEquals(0xdeadbeefL, NB.decodeUInt64(b(0, 0, 0, 0, 0xde, 0xad,
143 				0xbe, 0xef), 0));
144 		assertEquals(0xdeadbeefL, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0xde,
145 				0xad, 0xbe, 0xef), 3));
146 
147 		assertEquals(0x0310adefL, NB.decodeUInt64(b(0, 0, 0, 0, 0x03, 0x10,
148 				0xad, 0xef), 0));
149 		assertEquals(0x0310adefL, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0x03,
150 				0x10, 0xad, 0xef), 3));
151 
152 		assertEquals(0xc0ffee78deadbeefL, NB.decodeUInt64(b(0xc0, 0xff, 0xee,
153 				0x78, 0xde, 0xad, 0xbe, 0xef), 0));
154 		assertEquals(0xc0ffee78deadbeefL, NB.decodeUInt64(padb(3, 0xc0, 0xff,
155 				0xee, 0x78, 0xde, 0xad, 0xbe, 0xef), 3));
156 
157 		assertEquals(0x00000000ffffffffL, NB.decodeUInt64(b(0, 0, 0, 0, 0xff,
158 				0xff, 0xff, 0xff), 0));
159 		assertEquals(0x00000000ffffffffL, NB.decodeUInt64(padb(3, 0, 0, 0, 0,
160 				0xff, 0xff, 0xff, 0xff), 3));
161 		assertEquals(0xffffffffffffffffL, NB.decodeUInt64(b(0xff, 0xff, 0xff,
162 				0xff, 0xff, 0xff, 0xff, 0xff), 0));
163 		assertEquals(0xffffffffffffffffL, NB.decodeUInt64(padb(3, 0xff, 0xff,
164 				0xff, 0xff, 0xff, 0xff, 0xff, 0xff), 3));
165 	}
166 
167 	@Test
168 	public void testEncodeInt16() {
169 		final byte[] out = new byte[16];
170 
171 		prepareOutput(out);
172 		NB.encodeInt16(out, 0, 0);
173 		assertOutput(b(0, 0), out, 0);
174 
175 		prepareOutput(out);
176 		NB.encodeInt16(out, 3, 0);
177 		assertOutput(b(0, 0), out, 3);
178 
179 		prepareOutput(out);
180 		NB.encodeInt16(out, 0, 3);
181 		assertOutput(b(0, 3), out, 0);
182 
183 		prepareOutput(out);
184 		NB.encodeInt16(out, 3, 3);
185 		assertOutput(b(0, 3), out, 3);
186 
187 		prepareOutput(out);
188 		NB.encodeInt16(out, 0, 0xdeac);
189 		assertOutput(b(0xde, 0xac), out, 0);
190 
191 		prepareOutput(out);
192 		NB.encodeInt16(out, 3, 0xdeac);
193 		assertOutput(b(0xde, 0xac), out, 3);
194 
195 		prepareOutput(out);
196 		NB.encodeInt16(out, 3, -1);
197 		assertOutput(b(0xff, 0xff), out, 3);
198 	}
199 
200 	@Test
201 	public void testEncodeInt32() {
202 		final byte[] out = new byte[16];
203 
204 		prepareOutput(out);
205 		NB.encodeInt32(out, 0, 0);
206 		assertOutput(b(0, 0, 0, 0), out, 0);
207 
208 		prepareOutput(out);
209 		NB.encodeInt32(out, 3, 0);
210 		assertOutput(b(0, 0, 0, 0), out, 3);
211 
212 		prepareOutput(out);
213 		NB.encodeInt32(out, 0, 3);
214 		assertOutput(b(0, 0, 0, 3), out, 0);
215 
216 		prepareOutput(out);
217 		NB.encodeInt32(out, 3, 3);
218 		assertOutput(b(0, 0, 0, 3), out, 3);
219 
220 		prepareOutput(out);
221 		NB.encodeInt32(out, 0, 0xdeac);
222 		assertOutput(b(0, 0, 0xde, 0xac), out, 0);
223 
224 		prepareOutput(out);
225 		NB.encodeInt32(out, 3, 0xdeac);
226 		assertOutput(b(0, 0, 0xde, 0xac), out, 3);
227 
228 		prepareOutput(out);
229 		NB.encodeInt32(out, 0, 0xdeac9853);
230 		assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 0);
231 
232 		prepareOutput(out);
233 		NB.encodeInt32(out, 3, 0xdeac9853);
234 		assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 3);
235 
236 		prepareOutput(out);
237 		NB.encodeInt32(out, 3, -1);
238 		assertOutput(b(0xff, 0xff, 0xff, 0xff), out, 3);
239 	}
240 
241 	@Test
242 	public void testEncodeInt64() {
243 		final byte[] out = new byte[16];
244 
245 		prepareOutput(out);
246 		NB.encodeInt64(out, 0, 0L);
247 		assertOutput(b(0, 0, 0, 0, 0, 0, 0, 0), out, 0);
248 
249 		prepareOutput(out);
250 		NB.encodeInt64(out, 3, 0L);
251 		assertOutput(b(0, 0, 0, 0, 0, 0, 0, 0), out, 3);
252 
253 		prepareOutput(out);
254 		NB.encodeInt64(out, 0, 3L);
255 		assertOutput(b(0, 0, 0, 0, 0, 0, 0, 3), out, 0);
256 
257 		prepareOutput(out);
258 		NB.encodeInt64(out, 3, 3L);
259 		assertOutput(b(0, 0, 0, 0, 0, 0, 0, 3), out, 3);
260 
261 		prepareOutput(out);
262 		NB.encodeInt64(out, 0, 0xdeacL);
263 		assertOutput(b(0, 0, 0, 0, 0, 0, 0xde, 0xac), out, 0);
264 
265 		prepareOutput(out);
266 		NB.encodeInt64(out, 3, 0xdeacL);
267 		assertOutput(b(0, 0, 0, 0, 0, 0, 0xde, 0xac), out, 3);
268 
269 		prepareOutput(out);
270 		NB.encodeInt64(out, 0, 0xdeac9853L);
271 		assertOutput(b(0, 0, 0, 0, 0xde, 0xac, 0x98, 0x53), out, 0);
272 
273 		prepareOutput(out);
274 		NB.encodeInt64(out, 3, 0xdeac9853L);
275 		assertOutput(b(0, 0, 0, 0, 0xde, 0xac, 0x98, 0x53), out, 3);
276 
277 		prepareOutput(out);
278 		NB.encodeInt64(out, 0, 0xac431242deac9853L);
279 		assertOutput(b(0xac, 0x43, 0x12, 0x42, 0xde, 0xac, 0x98, 0x53), out, 0);
280 
281 		prepareOutput(out);
282 		NB.encodeInt64(out, 3, 0xac431242deac9853L);
283 		assertOutput(b(0xac, 0x43, 0x12, 0x42, 0xde, 0xac, 0x98, 0x53), out, 3);
284 
285 		prepareOutput(out);
286 		NB.encodeInt64(out, 3, -1L);
287 		assertOutput(b(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), out, 3);
288 	}
289 
290 	private static void prepareOutput(final byte[] buf) {
291 		for (int i = 0; i < buf.length; i++)
292 			buf[i] = (byte) (0x77 + i);
293 	}
294 
295 	private static void assertOutput(final byte[] expect, final byte[] buf,
296 			final int offset) {
297 		for (int i = 0; i < offset; i++)
298 			assertEquals((byte) (0x77 + i), buf[i]);
299 		for (int i = 0; i < expect.length; i++)
300 			assertEquals(expect[i], buf[offset + i]);
301 		for (int i = offset + expect.length; i < buf.length; i++)
302 			assertEquals((byte) (0x77 + i), buf[i]);
303 	}
304 
305 	private static byte[] b(final int a, final int b) {
306 		return new byte[] { (byte) a, (byte) b };
307 	}
308 
309 	private static byte[] padb(final int len, final int a, final int b) {
310 		final byte[] r = new byte[len + 2];
311 		for (int i = 0; i < len; i++)
312 			r[i] = (byte) 0xaf;
313 		r[len] = (byte) a;
314 		r[len + 1] = (byte) b;
315 		return r;
316 	}
317 
318 	private static byte[] b(final int a, final int b, final int c, final int d) {
319 		return new byte[] { (byte) a, (byte) b, (byte) c, (byte) d };
320 	}
321 
322 	private static byte[] padb(final int len, final int a, final int b,
323 			final int c, final int d) {
324 		final byte[] r = new byte[len + 4];
325 		for (int i = 0; i < len; i++)
326 			r[i] = (byte) 0xaf;
327 		r[len] = (byte) a;
328 		r[len + 1] = (byte) b;
329 		r[len + 2] = (byte) c;
330 		r[len + 3] = (byte) d;
331 		return r;
332 	}
333 
334 	private static byte[] b(final int a, final int b, final int c, final int d,
335 			final int e, final int f, final int g, final int h) {
336 		return new byte[] { (byte) a, (byte) b, (byte) c, (byte) d, (byte) e,
337 				(byte) f, (byte) g, (byte) h };
338 	}
339 
340 	private static byte[] padb(final int len, final int a, final int b,
341 			final int c, final int d, final int e, final int f, final int g,
342 			final int h) {
343 		final byte[] r = new byte[len + 8];
344 		for (int i = 0; i < len; i++)
345 			r[i] = (byte) 0xaf;
346 		r[len] = (byte) a;
347 		r[len + 1] = (byte) b;
348 		r[len + 2] = (byte) c;
349 		r[len + 3] = (byte) d;
350 		r[len + 4] = (byte) e;
351 		r[len + 5] = (byte) f;
352 		r[len + 6] = (byte) g;
353 		r[len + 7] = (byte) h;
354 		return r;
355 	}
356 }