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

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