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