aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/StringUtilsTest.java
blob: aa7247e105aefb941868cbf706c7ab1e0e3eedd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2009, Google Inc. and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class StringUtilsTest {
	@Test
	public void testToLowerCaseChar() {
		assertEquals('a', StringUtils.toLowerCase('A'));
		assertEquals('z', StringUtils.toLowerCase('Z'));

		assertEquals('a', StringUtils.toLowerCase('a'));
		assertEquals('z', StringUtils.toLowerCase('z'));

		assertEquals((char) 0, StringUtils.toLowerCase((char) 0));
		assertEquals((char) 0xffff, StringUtils.toLowerCase((char) 0xffff));
	}

	@Test
	public void testToLowerCaseString() {
		assertEquals("\n abcdefghijklmnopqrstuvwxyz\n", StringUtils
				.toLowerCase("\n ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"));
	}

	@Test
	public void testEqualsIgnoreCase1() {
		final String a = "FOO";
		assertTrue(StringUtils.equalsIgnoreCase(a, a));
	}

	@Test
	public void testEqualsIgnoreCase2() {
		assertFalse(StringUtils.equalsIgnoreCase("a", ""));
	}

	@Test
	public void testEqualsIgnoreCase3() {
		assertFalse(StringUtils.equalsIgnoreCase("a", "b"));
		assertFalse(StringUtils.equalsIgnoreCase("ac", "ab"));
	}

	@Test
	public void testEqualsIgnoreCase4() {
		assertTrue(StringUtils.equalsIgnoreCase("a", "a"));
		assertTrue(StringUtils.equalsIgnoreCase("A", "a"));
		assertTrue(StringUtils.equalsIgnoreCase("a", "A"));
	}

	@Test
	public void testReplaceLineBreaks() {
		assertEquals("a b c ",
				StringUtils.replaceLineBreaksWithSpace("a b\nc\r"));
		assertEquals("a b c ",
				StringUtils.replaceLineBreaksWithSpace("a b\nc\n"));
		assertEquals("a b c ",
				StringUtils.replaceLineBreaksWithSpace("a b\nc\r\n"));
		assertEquals("a b c d",
				StringUtils.replaceLineBreaksWithSpace("a\r\nb\nc d"));
	}

	@Test
	public void testFormatWithSuffix() {
		assertEquals("1023", StringUtils.formatWithSuffix(1023));
		assertEquals("1k", StringUtils.formatWithSuffix(1024));
		assertEquals("1025", StringUtils.formatWithSuffix(1025));
		assertEquals("1048575", StringUtils.formatWithSuffix(1024 * 1024 - 1));
		assertEquals("1m", StringUtils.formatWithSuffix(1024 * 1024));
		assertEquals("1048577", StringUtils.formatWithSuffix(1024 * 1024 + 1));
		assertEquals("1073741823",
				StringUtils.formatWithSuffix(1024 * 1024 * 1024 - 1));
		assertEquals("1g", StringUtils.formatWithSuffix(1024 * 1024 * 1024));
		assertEquals("1073741825",
				StringUtils.formatWithSuffix(1024 * 1024 * 1024 + 1));
		assertEquals("3k", StringUtils.formatWithSuffix(3 * 1024));
		assertEquals("3m", StringUtils.formatWithSuffix(3 * 1024 * 1024));
		assertEquals("2050k",
				StringUtils.formatWithSuffix(2 * 1024 * 1024 + 2048));
		assertEquals("3g",
				StringUtils.formatWithSuffix(3L * 1024 * 1024 * 1024));
		assertEquals("3000", StringUtils.formatWithSuffix(3000));
		assertEquals("3000000", StringUtils.formatWithSuffix(3_000_000));
		assertEquals("1953125k", StringUtils.formatWithSuffix(2_000_000_000));
		assertEquals("2000000010", StringUtils.formatWithSuffix(2_000_000_010));
		assertEquals("3000000000",
				StringUtils.formatWithSuffix(3_000_000_000L));
	}

	@Test
	public void testParseWithSuffix() {
		assertEquals(1024, StringUtils.parseIntWithSuffix("1k", true));
		assertEquals(1024, StringUtils.parseIntWithSuffix("1 k", true));
		assertEquals(1024, StringUtils.parseIntWithSuffix("1  k", true));
		assertEquals(1024, StringUtils.parseIntWithSuffix(" \t1  k  \n", true));
		assertEquals(1024, StringUtils.parseIntWithSuffix("1k", false));
		assertEquals(1024, StringUtils.parseIntWithSuffix("1K", false));
		assertEquals(1024 * 1024, StringUtils.parseIntWithSuffix("1m", false));
		assertEquals(1024 * 1024, StringUtils.parseIntWithSuffix("1M", false));
		assertEquals(-1024 * 1024,
				StringUtils.parseIntWithSuffix("-1M", false));
		assertEquals(1_000_000,
				StringUtils.parseIntWithSuffix("  1000000\r\n", false));
		assertEquals(1024 * 1024 * 1024,
				StringUtils.parseIntWithSuffix("1g", false));
		assertEquals(1024 * 1024 * 1024,
				StringUtils.parseIntWithSuffix("1G", false));
		assertEquals(3L * 1024 * 1024 * 1024,
				StringUtils.parseLongWithSuffix("3g", false));
		assertEquals(3L * 1024 * 1024 * 1024,
				StringUtils.parseLongWithSuffix("3G", false));
		assertThrows(NumberFormatException.class,
				() -> StringUtils.parseIntWithSuffix("2G", false));
		assertEquals(2L * 1024 * 1024 * 1024,
				StringUtils.parseLongWithSuffix("2G", false));
		assertThrows(NumberFormatException.class,
				() -> StringUtils.parseLongWithSuffix("-1m", true));
		assertThrows(NumberFormatException.class,
				() -> StringUtils.parseLongWithSuffix("-1000", true));
		assertThrows(StringIndexOutOfBoundsException.class,
				() -> StringUtils.parseLongWithSuffix("", false));
		assertThrows(StringIndexOutOfBoundsException.class,
				() -> StringUtils.parseLongWithSuffix("   \t   \n", false));
		assertThrows(StringIndexOutOfBoundsException.class,
				() -> StringUtils.parseLongWithSuffix("k", false));
		assertThrows(StringIndexOutOfBoundsException.class,
				() -> StringUtils.parseLongWithSuffix("m", false));
		assertThrows(StringIndexOutOfBoundsException.class,
				() -> StringUtils.parseLongWithSuffix("g", false));
		assertThrows(NumberFormatException.class,
				() -> StringUtils.parseLongWithSuffix("1T", false));
		assertThrows(NumberFormatException.class,
				() -> StringUtils.parseLongWithSuffix("1t", false));
		assertThrows(NumberFormatException.class,
				() -> StringUtils.parseLongWithSuffix("Nonumber", false));
		assertThrows(NumberFormatException.class,
				() -> StringUtils.parseLongWithSuffix("0x001f", false));
		assertThrows(NumberFormatException.class,
				() -> StringUtils.parseLongWithSuffix("beef", false));
		assertThrows(NumberFormatException.class,
				() -> StringUtils.parseLongWithSuffix("8000000000000000000G",
						false));
	}
}