You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NBTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C) 2008, 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. package org.eclipse.jgit.util;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import org.junit.Test;
  47. public class NBTest {
  48. @Test
  49. public void testCompareUInt32() {
  50. assertTrue(NB.compareUInt32(0, 0) == 0);
  51. assertTrue(NB.compareUInt32(1, 0) > 0);
  52. assertTrue(NB.compareUInt32(0, 1) < 0);
  53. assertTrue(NB.compareUInt32(-1, 0) > 0);
  54. assertTrue(NB.compareUInt32(0, -1) < 0);
  55. assertTrue(NB.compareUInt32(-1, 1) > 0);
  56. assertTrue(NB.compareUInt32(1, -1) < 0);
  57. }
  58. @Test
  59. public void testDecodeUInt16() {
  60. assertEquals(0, NB.decodeUInt16(b(0, 0), 0));
  61. assertEquals(0, NB.decodeUInt16(padb(3, 0, 0), 3));
  62. assertEquals(3, NB.decodeUInt16(b(0, 3), 0));
  63. assertEquals(3, NB.decodeUInt16(padb(3, 0, 3), 3));
  64. assertEquals(0xde03, NB.decodeUInt16(b(0xde, 3), 0));
  65. assertEquals(0xde03, NB.decodeUInt16(padb(3, 0xde, 3), 3));
  66. assertEquals(0x03de, NB.decodeUInt16(b(3, 0xde), 0));
  67. assertEquals(0x03de, NB.decodeUInt16(padb(3, 3, 0xde), 3));
  68. assertEquals(0xffff, NB.decodeUInt16(b(0xff, 0xff), 0));
  69. assertEquals(0xffff, NB.decodeUInt16(padb(3, 0xff, 0xff), 3));
  70. }
  71. @Test
  72. public void testDecodeInt32() {
  73. assertEquals(0, NB.decodeInt32(b(0, 0, 0, 0), 0));
  74. assertEquals(0, NB.decodeInt32(padb(3, 0, 0, 0, 0), 3));
  75. assertEquals(3, NB.decodeInt32(b(0, 0, 0, 3), 0));
  76. 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));
  80. assertEquals(0x0310adef, NB.decodeInt32(b(0x03, 0x10, 0xad, 0xef), 0));
  81. assertEquals(0x0310adef, NB.decodeInt32(
  82. padb(3, 0x03, 0x10, 0xad, 0xef), 3));
  83. assertEquals(0xffffffff, NB.decodeInt32(b(0xff, 0xff, 0xff, 0xff), 0));
  84. assertEquals(0xffffffff, NB.decodeInt32(
  85. padb(3, 0xff, 0xff, 0xff, 0xff), 3));
  86. }
  87. @Test
  88. public void testDecodeUInt32() {
  89. assertEquals(0L, NB.decodeUInt32(b(0, 0, 0, 0), 0));
  90. assertEquals(0L, NB.decodeUInt32(padb(3, 0, 0, 0, 0), 3));
  91. assertEquals(3L, NB.decodeUInt32(b(0, 0, 0, 3), 0));
  92. assertEquals(3L, NB.decodeUInt32(padb(3, 0, 0, 0, 3), 3));
  93. assertEquals(0xdeadbeefL, NB.decodeUInt32(b(0xde, 0xad, 0xbe, 0xef), 0));
  94. assertEquals(0xdeadbeefL, NB.decodeUInt32(padb(3, 0xde, 0xad, 0xbe,
  95. 0xef), 3));
  96. assertEquals(0x0310adefL, NB.decodeUInt32(b(0x03, 0x10, 0xad, 0xef), 0));
  97. assertEquals(0x0310adefL, NB.decodeUInt32(padb(3, 0x03, 0x10, 0xad,
  98. 0xef), 3));
  99. assertEquals(0xffffffffL, NB.decodeUInt32(b(0xff, 0xff, 0xff, 0xff), 0));
  100. assertEquals(0xffffffffL, NB.decodeUInt32(padb(3, 0xff, 0xff, 0xff,
  101. 0xff), 3));
  102. }
  103. @Test
  104. public void testDecodeUInt64() {
  105. assertEquals(0L, NB.decodeUInt64(b(0, 0, 0, 0, 0, 0, 0, 0), 0));
  106. assertEquals(0L, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0, 0, 0, 0), 3));
  107. assertEquals(3L, NB.decodeUInt64(b(0, 0, 0, 0, 0, 0, 0, 3), 0));
  108. assertEquals(3L, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0, 0, 0, 3), 3));
  109. assertEquals(0xdeadbeefL, NB.decodeUInt64(b(0, 0, 0, 0, 0xde, 0xad,
  110. 0xbe, 0xef), 0));
  111. assertEquals(0xdeadbeefL, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0xde,
  112. 0xad, 0xbe, 0xef), 3));
  113. assertEquals(0x0310adefL, NB.decodeUInt64(b(0, 0, 0, 0, 0x03, 0x10,
  114. 0xad, 0xef), 0));
  115. assertEquals(0x0310adefL, NB.decodeUInt64(padb(3, 0, 0, 0, 0, 0x03,
  116. 0x10, 0xad, 0xef), 3));
  117. assertEquals(0xc0ffee78deadbeefL, NB.decodeUInt64(b(0xc0, 0xff, 0xee,
  118. 0x78, 0xde, 0xad, 0xbe, 0xef), 0));
  119. assertEquals(0xc0ffee78deadbeefL, NB.decodeUInt64(padb(3, 0xc0, 0xff,
  120. 0xee, 0x78, 0xde, 0xad, 0xbe, 0xef), 3));
  121. assertEquals(0x00000000ffffffffL, NB.decodeUInt64(b(0, 0, 0, 0, 0xff,
  122. 0xff, 0xff, 0xff), 0));
  123. assertEquals(0x00000000ffffffffL, NB.decodeUInt64(padb(3, 0, 0, 0, 0,
  124. 0xff, 0xff, 0xff, 0xff), 3));
  125. assertEquals(0xffffffffffffffffL, NB.decodeUInt64(b(0xff, 0xff, 0xff,
  126. 0xff, 0xff, 0xff, 0xff, 0xff), 0));
  127. assertEquals(0xffffffffffffffffL, NB.decodeUInt64(padb(3, 0xff, 0xff,
  128. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), 3));
  129. }
  130. @Test
  131. public void testEncodeInt16() {
  132. final byte[] out = new byte[16];
  133. prepareOutput(out);
  134. NB.encodeInt16(out, 0, 0);
  135. assertOutput(b(0, 0), out, 0);
  136. prepareOutput(out);
  137. NB.encodeInt16(out, 3, 0);
  138. assertOutput(b(0, 0), out, 3);
  139. prepareOutput(out);
  140. NB.encodeInt16(out, 0, 3);
  141. assertOutput(b(0, 3), out, 0);
  142. prepareOutput(out);
  143. NB.encodeInt16(out, 3, 3);
  144. assertOutput(b(0, 3), out, 3);
  145. prepareOutput(out);
  146. NB.encodeInt16(out, 0, 0xdeac);
  147. assertOutput(b(0xde, 0xac), out, 0);
  148. prepareOutput(out);
  149. NB.encodeInt16(out, 3, 0xdeac);
  150. assertOutput(b(0xde, 0xac), out, 3);
  151. prepareOutput(out);
  152. NB.encodeInt16(out, 3, -1);
  153. assertOutput(b(0xff, 0xff), out, 3);
  154. }
  155. @Test
  156. public void testEncodeInt32() {
  157. final byte[] out = new byte[16];
  158. prepareOutput(out);
  159. NB.encodeInt32(out, 0, 0);
  160. assertOutput(b(0, 0, 0, 0), out, 0);
  161. prepareOutput(out);
  162. NB.encodeInt32(out, 3, 0);
  163. assertOutput(b(0, 0, 0, 0), out, 3);
  164. prepareOutput(out);
  165. NB.encodeInt32(out, 0, 3);
  166. assertOutput(b(0, 0, 0, 3), out, 0);
  167. prepareOutput(out);
  168. NB.encodeInt32(out, 3, 3);
  169. assertOutput(b(0, 0, 0, 3), out, 3);
  170. prepareOutput(out);
  171. NB.encodeInt32(out, 0, 0xdeac);
  172. assertOutput(b(0, 0, 0xde, 0xac), out, 0);
  173. prepareOutput(out);
  174. NB.encodeInt32(out, 3, 0xdeac);
  175. assertOutput(b(0, 0, 0xde, 0xac), out, 3);
  176. prepareOutput(out);
  177. NB.encodeInt32(out, 0, 0xdeac9853);
  178. assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 0);
  179. prepareOutput(out);
  180. NB.encodeInt32(out, 3, 0xdeac9853);
  181. assertOutput(b(0xde, 0xac, 0x98, 0x53), out, 3);
  182. prepareOutput(out);
  183. NB.encodeInt32(out, 3, -1);
  184. assertOutput(b(0xff, 0xff, 0xff, 0xff), out, 3);
  185. }
  186. @Test
  187. public void testEncodeInt64() {
  188. final byte[] out = new byte[16];
  189. prepareOutput(out);
  190. NB.encodeInt64(out, 0, 0L);
  191. assertOutput(b(0, 0, 0, 0, 0, 0, 0, 0), out, 0);
  192. prepareOutput(out);
  193. NB.encodeInt64(out, 3, 0L);
  194. assertOutput(b(0, 0, 0, 0, 0, 0, 0, 0), out, 3);
  195. prepareOutput(out);
  196. NB.encodeInt64(out, 0, 3L);
  197. assertOutput(b(0, 0, 0, 0, 0, 0, 0, 3), out, 0);
  198. prepareOutput(out);
  199. NB.encodeInt64(out, 3, 3L);
  200. assertOutput(b(0, 0, 0, 0, 0, 0, 0, 3), out, 3);
  201. prepareOutput(out);
  202. NB.encodeInt64(out, 0, 0xdeacL);
  203. assertOutput(b(0, 0, 0, 0, 0, 0, 0xde, 0xac), out, 0);
  204. prepareOutput(out);
  205. NB.encodeInt64(out, 3, 0xdeacL);
  206. assertOutput(b(0, 0, 0, 0, 0, 0, 0xde, 0xac), out, 3);
  207. prepareOutput(out);
  208. NB.encodeInt64(out, 0, 0xdeac9853L);
  209. assertOutput(b(0, 0, 0, 0, 0xde, 0xac, 0x98, 0x53), out, 0);
  210. prepareOutput(out);
  211. NB.encodeInt64(out, 3, 0xdeac9853L);
  212. assertOutput(b(0, 0, 0, 0, 0xde, 0xac, 0x98, 0x53), out, 3);
  213. prepareOutput(out);
  214. NB.encodeInt64(out, 0, 0xac431242deac9853L);
  215. assertOutput(b(0xac, 0x43, 0x12, 0x42, 0xde, 0xac, 0x98, 0x53), out, 0);
  216. prepareOutput(out);
  217. NB.encodeInt64(out, 3, 0xac431242deac9853L);
  218. assertOutput(b(0xac, 0x43, 0x12, 0x42, 0xde, 0xac, 0x98, 0x53), out, 3);
  219. prepareOutput(out);
  220. NB.encodeInt64(out, 3, -1L);
  221. assertOutput(b(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff), out, 3);
  222. }
  223. private static void prepareOutput(final byte[] buf) {
  224. for (int i = 0; i < buf.length; i++)
  225. buf[i] = (byte) (0x77 + i);
  226. }
  227. private static void assertOutput(final byte[] expect, final byte[] buf,
  228. final int offset) {
  229. for (int i = 0; i < offset; i++)
  230. assertEquals((byte) (0x77 + i), buf[i]);
  231. for (int i = 0; i < expect.length; i++)
  232. assertEquals(expect[i], buf[offset + i]);
  233. for (int i = offset + expect.length; i < buf.length; i++)
  234. assertEquals((byte) (0x77 + i), buf[i]);
  235. }
  236. private static byte[] b(final int a, final int b) {
  237. return new byte[] { (byte) a, (byte) b };
  238. }
  239. private static byte[] padb(final int len, final int a, final int b) {
  240. final byte[] r = new byte[len + 2];
  241. for (int i = 0; i < len; i++)
  242. r[i] = (byte) 0xaf;
  243. r[len] = (byte) a;
  244. r[len + 1] = (byte) b;
  245. return r;
  246. }
  247. private static byte[] b(final int a, final int b, final int c, final int d) {
  248. return new byte[] { (byte) a, (byte) b, (byte) c, (byte) d };
  249. }
  250. private static byte[] padb(final int len, final int a, final int b,
  251. final int c, final int d) {
  252. final byte[] r = new byte[len + 4];
  253. for (int i = 0; i < len; i++)
  254. r[i] = (byte) 0xaf;
  255. r[len] = (byte) a;
  256. r[len + 1] = (byte) b;
  257. r[len + 2] = (byte) c;
  258. r[len + 3] = (byte) d;
  259. return r;
  260. }
  261. private static byte[] b(final int a, final int b, final int c, final int d,
  262. final int e, final int f, final int g, final int h) {
  263. return new byte[] { (byte) a, (byte) b, (byte) c, (byte) d, (byte) e,
  264. (byte) f, (byte) g, (byte) h };
  265. }
  266. private static byte[] padb(final int len, final int a, final int b,
  267. final int c, final int d, final int e, final int f, final int g,
  268. final int h) {
  269. final byte[] r = new byte[len + 8];
  270. for (int i = 0; i < len; i++)
  271. r[i] = (byte) 0xaf;
  272. r[len] = (byte) a;
  273. r[len + 1] = (byte) b;
  274. r[len + 2] = (byte) c;
  275. r[len + 3] = (byte) d;
  276. r[len + 4] = (byte) e;
  277. r[len + 5] = (byte) f;
  278. r[len + 6] = (byte) g;
  279. r[len + 7] = (byte) h;
  280. return r;
  281. }
  282. }