Initial JGit contribution to eclipse.org
[jgit/MarioXXX.git] / org.eclipse.jgit.test / tst / org / eclipse / jgit / util / RawParseUtils_HexParseTest.java
bloba2c9e9dbd12eea3c1ea71fbc559ea67a31d0e017
1 /*
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
14 * conditions are met:
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
27 * written permission.
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'));
76 assertNotHex('q');
77 assertNotHex(' ');
78 assertNotHex('.');
81 private static void assertNotHex(final char c) {
82 try {
83 RawParseUtils.parseHexInt4((byte) c);
84 fail("Incorrectly acccepted " + c);
85 } catch (ArrayIndexOutOfBoundsException e) {
86 // pass
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"));
99 try {
100 parse16("noth");
101 fail("Incorrectly acccepted \"noth\"");
102 } catch (ArrayIndexOutOfBoundsException e) {
103 // pass
106 try {
107 parse16("01");
108 fail("Incorrectly acccepted \"01\"");
109 } catch (ArrayIndexOutOfBoundsException e) {
110 // pass
113 try {
114 parse16("000.");
115 fail("Incorrectly acccepted \"000.\"");
116 } catch (ArrayIndexOutOfBoundsException e) {
117 // pass
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"));
132 try {
133 parse32("noth");
134 fail("Incorrectly acccepted \"noth\"");
135 } catch (ArrayIndexOutOfBoundsException e) {
136 // pass
139 try {
140 parse32("notahexs");
141 fail("Incorrectly acccepted \"notahexs\"");
142 } catch (ArrayIndexOutOfBoundsException e) {
143 // pass
146 try {
147 parse32("01");
148 fail("Incorrectly acccepted \"01\"");
149 } catch (ArrayIndexOutOfBoundsException e) {
150 // pass
153 try {
154 parse32("0000000.");
155 fail("Incorrectly acccepted \"0000000.\"");
156 } catch (ArrayIndexOutOfBoundsException e) {
157 // pass
161 private static int parse32(final String str) {
162 return RawParseUtils.parseHexInt32(Constants.encodeASCII(str), 0);