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

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