2 * Copyright (C) 2009, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
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
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
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.
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
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.
44 package org
.eclipse
.jgit
.util
;
46 import junit
.framework
.TestCase
;
48 import org
.eclipse
.jgit
.lib
.Constants
;
50 public class RawParseUtils_HexParseTest
extends TestCase
{
51 public void testInt4_1() {
52 assertEquals(0, RawParseUtils
.parseHexInt4((byte) '0'));
53 assertEquals(1, RawParseUtils
.parseHexInt4((byte) '1'));
54 assertEquals(2, RawParseUtils
.parseHexInt4((byte) '2'));
55 assertEquals(3, RawParseUtils
.parseHexInt4((byte) '3'));
56 assertEquals(4, RawParseUtils
.parseHexInt4((byte) '4'));
57 assertEquals(5, RawParseUtils
.parseHexInt4((byte) '5'));
58 assertEquals(6, RawParseUtils
.parseHexInt4((byte) '6'));
59 assertEquals(7, RawParseUtils
.parseHexInt4((byte) '7'));
60 assertEquals(8, RawParseUtils
.parseHexInt4((byte) '8'));
61 assertEquals(9, RawParseUtils
.parseHexInt4((byte) '9'));
62 assertEquals(10, RawParseUtils
.parseHexInt4((byte) 'a'));
63 assertEquals(11, RawParseUtils
.parseHexInt4((byte) 'b'));
64 assertEquals(12, RawParseUtils
.parseHexInt4((byte) 'c'));
65 assertEquals(13, RawParseUtils
.parseHexInt4((byte) 'd'));
66 assertEquals(14, RawParseUtils
.parseHexInt4((byte) 'e'));
67 assertEquals(15, RawParseUtils
.parseHexInt4((byte) 'f'));
69 assertEquals(10, RawParseUtils
.parseHexInt4((byte) 'A'));
70 assertEquals(11, RawParseUtils
.parseHexInt4((byte) 'B'));
71 assertEquals(12, RawParseUtils
.parseHexInt4((byte) 'C'));
72 assertEquals(13, RawParseUtils
.parseHexInt4((byte) 'D'));
73 assertEquals(14, RawParseUtils
.parseHexInt4((byte) 'E'));
74 assertEquals(15, RawParseUtils
.parseHexInt4((byte) 'F'));
81 private static void assertNotHex(final char c
) {
83 RawParseUtils
.parseHexInt4((byte) c
);
84 fail("Incorrectly acccepted " + c
);
85 } catch (ArrayIndexOutOfBoundsException e
) {
90 public void testInt16() {
91 assertEquals(0x0000, parse16("0000"));
92 assertEquals(0x0001, parse16("0001"));
93 assertEquals(0x1234, parse16("1234"));
94 assertEquals(0xdead, parse16("dead"));
95 assertEquals(0xBEEF, parse16("BEEF"));
96 assertEquals(0x4321, parse16("4321"));
97 assertEquals(0xffff, parse16("ffff"));
101 fail("Incorrectly acccepted \"noth\"");
102 } catch (ArrayIndexOutOfBoundsException e
) {
108 fail("Incorrectly acccepted \"01\"");
109 } catch (ArrayIndexOutOfBoundsException e
) {
115 fail("Incorrectly acccepted \"000.\"");
116 } catch (ArrayIndexOutOfBoundsException e
) {
121 private static int parse16(final String str
) {
122 return RawParseUtils
.parseHexInt16(Constants
.encodeASCII(str
), 0);
125 public void testInt32() {
126 assertEquals(0x00000000, parse32("00000000"));
127 assertEquals(0x00000001, parse32("00000001"));
128 assertEquals(0xc0ffEE42, parse32("c0ffEE42"));
129 assertEquals(0xffffffff, parse32("ffffffff"));
130 assertEquals(-1, parse32("ffffffff"));
134 fail("Incorrectly acccepted \"noth\"");
135 } catch (ArrayIndexOutOfBoundsException e
) {
141 fail("Incorrectly acccepted \"notahexs\"");
142 } catch (ArrayIndexOutOfBoundsException e
) {
148 fail("Incorrectly acccepted \"01\"");
149 } catch (ArrayIndexOutOfBoundsException e
) {
155 fail("Incorrectly acccepted \"0000000.\"");
156 } catch (ArrayIndexOutOfBoundsException e
) {
161 private static int parse32(final String str
) {
162 return RawParseUtils
.parseHexInt32(Constants
.encodeASCII(str
), 0);