View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
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
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
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.
23   *
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.
28   *
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.
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  			// pass
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 			// pass
108 		}
109 
110 		try {
111 			parse16("01");
112 			fail("Incorrectly acccepted \"01\"");
113 		} catch (ArrayIndexOutOfBoundsException e) {
114 			// pass
115 		}
116 
117 		try {
118 			parse16("000.");
119 			fail("Incorrectly acccepted \"000.\"");
120 		} catch (ArrayIndexOutOfBoundsException e) {
121 			// pass
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 			// pass
142 		}
143 
144 		try {
145 			parse32("notahexs");
146 			fail("Incorrectly acccepted \"notahexs\"");
147 		} catch (ArrayIndexOutOfBoundsException e) {
148 			// pass
149 		}
150 
151 		try {
152 			parse32("01");
153 			fail("Incorrectly acccepted \"01\"");
154 		} catch (ArrayIndexOutOfBoundsException e) {
155 			// pass
156 		}
157 
158 		try {
159 			parse32("0000000.");
160 			fail("Incorrectly acccepted \"0000000.\"");
161 		} catch (ArrayIndexOutOfBoundsException e) {
162 			// pass
163 		}
164 	}
165 
166 	private static int parse32(final String str) {
167 		return RawParseUtils.parseHexInt32(Constants.encodeASCII(str), 0);
168 	}
169 }