2 * Copyright (C) 2009, Google Inc.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
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
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'));
75 private static void assertNotHex(final char c
) {
77 RawParseUtils
.parseHexInt4((byte) c
);
78 fail("Incorrectly acccepted " + c
);
79 } catch (ArrayIndexOutOfBoundsException e
) {
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"));
95 fail("Incorrectly acccepted \"noth\"");
96 } catch (ArrayIndexOutOfBoundsException e
) {
102 fail("Incorrectly acccepted \"01\"");
103 } catch (ArrayIndexOutOfBoundsException e
) {
109 fail("Incorrectly acccepted \"000.\"");
110 } catch (ArrayIndexOutOfBoundsException e
) {
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"));
128 fail("Incorrectly acccepted \"noth\"");
129 } catch (ArrayIndexOutOfBoundsException e
) {
135 fail("Incorrectly acccepted \"notahexs\"");
136 } catch (ArrayIndexOutOfBoundsException e
) {
142 fail("Incorrectly acccepted \"01\"");
143 } catch (ArrayIndexOutOfBoundsException e
) {
149 fail("Incorrectly acccepted \"0000000.\"");
150 } catch (ArrayIndexOutOfBoundsException e
) {
155 private static int parse32(final String str
) {
156 return RawParseUtils
.parseHexInt32(Constants
.encodeASCII(str
), 0);