Add tests for RawParseUtil's hex string parsing
[egit/graphgui.git] / org.spearce.jgit.test / tst / org / spearce / jgit / util / RawParseUtils_HexParseTest.java
blob0f0dddee3f0fd495ac89aa3aa2faa9f4e17e9910
1 /*
2 * Copyright (C) 2009, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.util;
40 import junit.framework.TestCase;
42 import org.spearce.jgit.lib.Constants;
44 public class RawParseUtils_HexParseTest extends TestCase {
45 public void testInt4_1() {
46 assertEquals(0, RawParseUtils.parseHexInt4((byte) '0'));
47 assertEquals(1, RawParseUtils.parseHexInt4((byte) '1'));
48 assertEquals(2, RawParseUtils.parseHexInt4((byte) '2'));
49 assertEquals(3, RawParseUtils.parseHexInt4((byte) '3'));
50 assertEquals(4, RawParseUtils.parseHexInt4((byte) '4'));
51 assertEquals(5, RawParseUtils.parseHexInt4((byte) '5'));
52 assertEquals(6, RawParseUtils.parseHexInt4((byte) '6'));
53 assertEquals(7, RawParseUtils.parseHexInt4((byte) '7'));
54 assertEquals(8, RawParseUtils.parseHexInt4((byte) '8'));
55 assertEquals(9, RawParseUtils.parseHexInt4((byte) '9'));
56 assertEquals(10, RawParseUtils.parseHexInt4((byte) 'a'));
57 assertEquals(11, RawParseUtils.parseHexInt4((byte) 'b'));
58 assertEquals(12, RawParseUtils.parseHexInt4((byte) 'c'));
59 assertEquals(13, RawParseUtils.parseHexInt4((byte) 'd'));
60 assertEquals(14, RawParseUtils.parseHexInt4((byte) 'e'));
61 assertEquals(15, RawParseUtils.parseHexInt4((byte) 'f'));
63 assertEquals(10, RawParseUtils.parseHexInt4((byte) 'A'));
64 assertEquals(11, RawParseUtils.parseHexInt4((byte) 'B'));
65 assertEquals(12, RawParseUtils.parseHexInt4((byte) 'C'));
66 assertEquals(13, RawParseUtils.parseHexInt4((byte) 'D'));
67 assertEquals(14, RawParseUtils.parseHexInt4((byte) 'E'));
68 assertEquals(15, RawParseUtils.parseHexInt4((byte) 'F'));
70 assertNotHex('q');
71 assertNotHex(' ');
72 assertNotHex('.');
75 private static void assertNotHex(final char c) {
76 try {
77 RawParseUtils.parseHexInt4((byte) c);
78 fail("Incorrectly acccepted " + c);
79 } catch (ArrayIndexOutOfBoundsException e) {
80 // pass
84 public void testInt16() {
85 assertEquals(0x0000, parse16("0000"));
86 assertEquals(0x0001, parse16("0001"));
87 assertEquals(0x1234, parse16("1234"));
88 assertEquals(0xdead, parse16("dead"));
89 assertEquals(0xBEEF, parse16("BEEF"));
90 assertEquals(0x4321, parse16("4321"));
91 assertEquals(0xffff, parse16("ffff"));
93 try {
94 parse16("noth");
95 fail("Incorrectly acccepted \"noth\"");
96 } catch (ArrayIndexOutOfBoundsException e) {
97 // pass
100 try {
101 parse16("01");
102 fail("Incorrectly acccepted \"01\"");
103 } catch (ArrayIndexOutOfBoundsException e) {
104 // pass
107 try {
108 parse16("000.");
109 fail("Incorrectly acccepted \"000.\"");
110 } catch (ArrayIndexOutOfBoundsException e) {
111 // pass
115 private static int parse16(final String str) {
116 return RawParseUtils.parseHexInt16(Constants.encodeASCII(str), 0);
119 public void testInt32() {
120 assertEquals(0x00000000, parse32("00000000"));
121 assertEquals(0x00000001, parse32("00000001"));
122 assertEquals(0xc0ffEE42, parse32("c0ffEE42"));
123 assertEquals(0xffffffff, parse32("ffffffff"));
124 assertEquals(-1, parse32("ffffffff"));
126 try {
127 parse32("noth");
128 fail("Incorrectly acccepted \"noth\"");
129 } catch (ArrayIndexOutOfBoundsException e) {
130 // pass
133 try {
134 parse32("notahexs");
135 fail("Incorrectly acccepted \"notahexs\"");
136 } catch (ArrayIndexOutOfBoundsException e) {
137 // pass
140 try {
141 parse32("01");
142 fail("Incorrectly acccepted \"01\"");
143 } catch (ArrayIndexOutOfBoundsException e) {
144 // pass
147 try {
148 parse32("0000000.");
149 fail("Incorrectly acccepted \"0000000.\"");
150 } catch (ArrayIndexOutOfBoundsException e) {
151 // pass
155 private static int parse32(final String str) {
156 return RawParseUtils.parseHexInt32(Constants.encodeASCII(str), 0);