1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.fail;
15
16 import org.eclipse.jgit.lib.Constants;
17 import org.junit.Test;
18
19 public class RawParseUtils_HexParseTest {
20 @Test
21 public void testInt4_1() {
22 assertEquals(0, RawParseUtils.parseHexInt4((byte) '0'));
23 assertEquals(1, RawParseUtils.parseHexInt4((byte) '1'));
24 assertEquals(2, RawParseUtils.parseHexInt4((byte) '2'));
25 assertEquals(3, RawParseUtils.parseHexInt4((byte) '3'));
26 assertEquals(4, RawParseUtils.parseHexInt4((byte) '4'));
27 assertEquals(5, RawParseUtils.parseHexInt4((byte) '5'));
28 assertEquals(6, RawParseUtils.parseHexInt4((byte) '6'));
29 assertEquals(7, RawParseUtils.parseHexInt4((byte) '7'));
30 assertEquals(8, RawParseUtils.parseHexInt4((byte) '8'));
31 assertEquals(9, RawParseUtils.parseHexInt4((byte) '9'));
32 assertEquals(10, RawParseUtils.parseHexInt4((byte) 'a'));
33 assertEquals(11, RawParseUtils.parseHexInt4((byte) 'b'));
34 assertEquals(12, RawParseUtils.parseHexInt4((byte) 'c'));
35 assertEquals(13, RawParseUtils.parseHexInt4((byte) 'd'));
36 assertEquals(14, RawParseUtils.parseHexInt4((byte) 'e'));
37 assertEquals(15, RawParseUtils.parseHexInt4((byte) 'f'));
38
39 assertEquals(10, RawParseUtils.parseHexInt4((byte) 'A'));
40 assertEquals(11, RawParseUtils.parseHexInt4((byte) 'B'));
41 assertEquals(12, RawParseUtils.parseHexInt4((byte) 'C'));
42 assertEquals(13, RawParseUtils.parseHexInt4((byte) 'D'));
43 assertEquals(14, RawParseUtils.parseHexInt4((byte) 'E'));
44 assertEquals(15, RawParseUtils.parseHexInt4((byte) 'F'));
45
46 assertNotHex('q');
47 assertNotHex(' ');
48 assertNotHex('.');
49 }
50
51 private static void assertNotHex(char c) {
52 try {
53 RawParseUtils.parseHexInt4((byte) c);
54 fail("Incorrectly acccepted " + c);
55 } catch (ArrayIndexOutOfBoundsException e) {
56
57 }
58 }
59
60 @Test
61 public void testInt16() {
62 assertEquals(0x0000, parse16("0000"));
63 assertEquals(0x0001, parse16("0001"));
64 assertEquals(0x1234, parse16("1234"));
65 assertEquals(0xdead, parse16("dead"));
66 assertEquals(0xBEEF, parse16("BEEF"));
67 assertEquals(0x4321, parse16("4321"));
68 assertEquals(0xffff, parse16("ffff"));
69
70 try {
71 parse16("noth");
72 fail("Incorrectly acccepted \"noth\"");
73 } catch (ArrayIndexOutOfBoundsException e) {
74
75 }
76
77 try {
78 parse16("01");
79 fail("Incorrectly acccepted \"01\"");
80 } catch (ArrayIndexOutOfBoundsException e) {
81
82 }
83
84 try {
85 parse16("000.");
86 fail("Incorrectly acccepted \"000.\"");
87 } catch (ArrayIndexOutOfBoundsException e) {
88
89 }
90 }
91
92 private static int parse16(final String str) {
93 return RawParseUtils.parseHexInt16(Constants.encodeASCII(str), 0);
94 }
95
96 @Test
97 public void testInt32() {
98 assertEquals(0x00000000, parse32("00000000"));
99 assertEquals(0x00000001, parse32("00000001"));
100 assertEquals(0xc0ffEE42, parse32("c0ffEE42"));
101 assertEquals(0xffffffff, parse32("ffffffff"));
102 assertEquals(-1, parse32("ffffffff"));
103
104 try {
105 parse32("noth");
106 fail("Incorrectly acccepted \"noth\"");
107 } catch (ArrayIndexOutOfBoundsException e) {
108
109 }
110
111 try {
112 parse32("notahexs");
113 fail("Incorrectly acccepted \"notahexs\"");
114 } catch (ArrayIndexOutOfBoundsException e) {
115
116 }
117
118 try {
119 parse32("01");
120 fail("Incorrectly acccepted \"01\"");
121 } catch (ArrayIndexOutOfBoundsException e) {
122
123 }
124
125 try {
126 parse32("0000000.");
127 fail("Incorrectly acccepted \"0000000.\"");
128 } catch (ArrayIndexOutOfBoundsException e) {
129
130 }
131 }
132
133 private static int parse32(final String str) {
134 return RawParseUtils.parseHexInt32(Constants.encodeASCII(str), 0);
135 }
136 }