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.

RawParseUtils_HexParseTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2009, 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.fail;
  46. import org.eclipse.jgit.lib.Constants;
  47. import org.junit.Test;
  48. public class RawParseUtils_HexParseTest {
  49. @Test
  50. public void testInt4_1() {
  51. assertEquals(0, RawParseUtils.parseHexInt4((byte) '0'));
  52. assertEquals(1, RawParseUtils.parseHexInt4((byte) '1'));
  53. assertEquals(2, RawParseUtils.parseHexInt4((byte) '2'));
  54. assertEquals(3, RawParseUtils.parseHexInt4((byte) '3'));
  55. assertEquals(4, RawParseUtils.parseHexInt4((byte) '4'));
  56. assertEquals(5, RawParseUtils.parseHexInt4((byte) '5'));
  57. assertEquals(6, RawParseUtils.parseHexInt4((byte) '6'));
  58. assertEquals(7, RawParseUtils.parseHexInt4((byte) '7'));
  59. assertEquals(8, RawParseUtils.parseHexInt4((byte) '8'));
  60. assertEquals(9, RawParseUtils.parseHexInt4((byte) '9'));
  61. assertEquals(10, RawParseUtils.parseHexInt4((byte) 'a'));
  62. assertEquals(11, RawParseUtils.parseHexInt4((byte) 'b'));
  63. assertEquals(12, RawParseUtils.parseHexInt4((byte) 'c'));
  64. assertEquals(13, RawParseUtils.parseHexInt4((byte) 'd'));
  65. assertEquals(14, RawParseUtils.parseHexInt4((byte) 'e'));
  66. assertEquals(15, RawParseUtils.parseHexInt4((byte) 'f'));
  67. assertEquals(10, RawParseUtils.parseHexInt4((byte) 'A'));
  68. assertEquals(11, RawParseUtils.parseHexInt4((byte) 'B'));
  69. assertEquals(12, RawParseUtils.parseHexInt4((byte) 'C'));
  70. assertEquals(13, RawParseUtils.parseHexInt4((byte) 'D'));
  71. assertEquals(14, RawParseUtils.parseHexInt4((byte) 'E'));
  72. assertEquals(15, RawParseUtils.parseHexInt4((byte) 'F'));
  73. assertNotHex('q');
  74. assertNotHex(' ');
  75. assertNotHex('.');
  76. }
  77. private static void assertNotHex(final char c) {
  78. try {
  79. RawParseUtils.parseHexInt4((byte) c);
  80. fail("Incorrectly acccepted " + c);
  81. } catch (ArrayIndexOutOfBoundsException e) {
  82. // pass
  83. }
  84. }
  85. @Test
  86. public void testInt16() {
  87. assertEquals(0x0000, parse16("0000"));
  88. assertEquals(0x0001, parse16("0001"));
  89. assertEquals(0x1234, parse16("1234"));
  90. assertEquals(0xdead, parse16("dead"));
  91. assertEquals(0xBEEF, parse16("BEEF"));
  92. assertEquals(0x4321, parse16("4321"));
  93. assertEquals(0xffff, parse16("ffff"));
  94. try {
  95. parse16("noth");
  96. fail("Incorrectly acccepted \"noth\"");
  97. } catch (ArrayIndexOutOfBoundsException e) {
  98. // pass
  99. }
  100. try {
  101. parse16("01");
  102. fail("Incorrectly acccepted \"01\"");
  103. } catch (ArrayIndexOutOfBoundsException e) {
  104. // pass
  105. }
  106. try {
  107. parse16("000.");
  108. fail("Incorrectly acccepted \"000.\"");
  109. } catch (ArrayIndexOutOfBoundsException e) {
  110. // pass
  111. }
  112. }
  113. private static int parse16(final String str) {
  114. return RawParseUtils.parseHexInt16(Constants.encodeASCII(str), 0);
  115. }
  116. @Test
  117. public void testInt32() {
  118. assertEquals(0x00000000, parse32("00000000"));
  119. assertEquals(0x00000001, parse32("00000001"));
  120. assertEquals(0xc0ffEE42, parse32("c0ffEE42"));
  121. assertEquals(0xffffffff, parse32("ffffffff"));
  122. assertEquals(-1, parse32("ffffffff"));
  123. try {
  124. parse32("noth");
  125. fail("Incorrectly acccepted \"noth\"");
  126. } catch (ArrayIndexOutOfBoundsException e) {
  127. // pass
  128. }
  129. try {
  130. parse32("notahexs");
  131. fail("Incorrectly acccepted \"notahexs\"");
  132. } catch (ArrayIndexOutOfBoundsException e) {
  133. // pass
  134. }
  135. try {
  136. parse32("01");
  137. fail("Incorrectly acccepted \"01\"");
  138. } catch (ArrayIndexOutOfBoundsException e) {
  139. // pass
  140. }
  141. try {
  142. parse32("0000000.");
  143. fail("Incorrectly acccepted \"0000000.\"");
  144. } catch (ArrayIndexOutOfBoundsException e) {
  145. // pass
  146. }
  147. }
  148. private static int parse32(final String str) {
  149. return RawParseUtils.parseHexInt32(Constants.encodeASCII(str), 0);
  150. }
  151. }