View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.util;
12  
13  import static java.nio.charset.StandardCharsets.US_ASCII;
14  import static org.eclipse.jgit.util.RawCharUtil.isWhitespace;
15  import static org.eclipse.jgit.util.RawCharUtil.trimLeadingWhitespace;
16  import static org.eclipse.jgit.util.RawCharUtil.trimTrailingWhitespace;
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  
21  import org.junit.Test;
22  
23  public class RawCharUtilTest {
24  
25  	/**
26  	 * Test method for {@link RawCharUtil#isWhitespace(byte)}.
27  	 */
28  	@Test
29  	public void testIsWhitespace() {
30  		for (byte c = -128; c < 127; c++) {
31  			switch (c) {
32  			case (byte) '\r':
33  			case (byte) '\n':
34  			case (byte) '\t':
35  			case (byte) ' ':
36  				assertTrue(isWhitespace(c));
37  				break;
38  			default:
39  				assertFalse(isWhitespace(c));
40  			}
41  		}
42  	}
43  
44  	/**
45  	 * Test method for
46  	 * {@link RawCharUtil#trimTrailingWhitespace(byte[], int, int)}.
47  	 */
48  	@Test
49  	public void testTrimTrailingWhitespace() {
50  		assertEquals(0, trimTrailingWhitespace("".getBytes(US_ASCII), 0, 0));
51  		assertEquals(0, trimTrailingWhitespace(" ".getBytes(US_ASCII), 0, 1));
52  		assertEquals(1, trimTrailingWhitespace("a ".getBytes(US_ASCII), 0, 2));
53  		assertEquals(2, trimTrailingWhitespace(" a ".getBytes(US_ASCII), 0, 3));
54  		assertEquals(3, trimTrailingWhitespace("  a".getBytes(US_ASCII), 0, 3));
55  		assertEquals(6,
56  				trimTrailingWhitespace("  test   ".getBytes(US_ASCII), 2, 9));
57  	}
58  
59  	/**
60  	 * Test method for
61  	 * {@link RawCharUtil#trimLeadingWhitespace(byte[], int, int)}.
62  	 */
63  	@Test
64  	public void testTrimLeadingWhitespace() {
65  		assertEquals(0, trimLeadingWhitespace("".getBytes(US_ASCII), 0, 0));
66  		assertEquals(1, trimLeadingWhitespace(" ".getBytes(US_ASCII), 0, 1));
67  		assertEquals(0, trimLeadingWhitespace("a ".getBytes(US_ASCII), 0, 2));
68  		assertEquals(1, trimLeadingWhitespace(" a ".getBytes(US_ASCII), 0, 3));
69  		assertEquals(2, trimLeadingWhitespace("  a".getBytes(US_ASCII), 0, 3));
70  		assertEquals(2,
71  				trimLeadingWhitespace("  test   ".getBytes(US_ASCII),
72  				2, 9));
73  	}
74  
75  }