Skip to content

Commit b5dc7fa

Browse files
smokeyrobotjzheaux
authored andcommitted
Bael 1765 (eugenp#4342)
Guide to java.util.Arrays Issue: BAEL-1765
1 parent a54c9e0 commit b5dc7fa

1 file changed

Lines changed: 153 additions & 0 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.baeldung.arrays;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Before;
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
import org.junit.rules.ExpectedException;
9+
10+
import java.util.Arrays;
11+
import java.util.List;
12+
import java.util.stream.Stream;
13+
14+
public class ArraysUnitTest {
15+
private String[] intro;
16+
17+
@Rule
18+
public final ExpectedException exception = ExpectedException.none();
19+
20+
@Before
21+
public void setup() {
22+
intro = new String[] { "once", "upon", "a", "time" };
23+
}
24+
25+
@Test
26+
public void whenCopyOfRange_thenAbridgedArray() {
27+
String[] abridgement = Arrays.copyOfRange(intro, 0, 3);
28+
29+
assertArrayEquals(new String[] { "once", "upon", "a" }, abridgement);
30+
assertFalse(Arrays.equals(intro, abridgement));
31+
}
32+
33+
@Test
34+
public void whenCopyOf_thenNullElement() {
35+
String[] revised = Arrays.copyOf(intro, 3);
36+
String[] expanded = Arrays.copyOf(intro, 5);
37+
38+
assertArrayEquals(Arrays.copyOfRange(intro, 0, 3), revised);
39+
assertNull(expanded[4]);
40+
}
41+
42+
@Test
43+
public void whenFill_thenAllMatch() {
44+
String[] stutter = new String[3];
45+
Arrays.fill(stutter, "once");
46+
47+
assertTrue(Stream.of(stutter).allMatch(el -> "once".equals(el)));
48+
}
49+
50+
@Test
51+
public void whenEqualsContent_thenMatch() {
52+
assertTrue(Arrays.equals(new String[] { "once", "upon", "a", "time" }, intro));
53+
assertFalse(Arrays.equals(new String[] { "once", "upon", "a", null }, intro));
54+
}
55+
56+
@Test
57+
public void whenNestedArrays_thenDeepEqualsPass() {
58+
String[] end = { "the", "end" };
59+
Object[] story = new Object[] { intro, new String[] { "chapter one", "chapter two" }, end };
60+
Object[] copy = new Object[] { intro, new String[] { "chapter one", "chapter two" }, end };
61+
62+
assertTrue(Arrays.deepEquals(story, copy));
63+
assertFalse(Arrays.equals(story, copy));
64+
}
65+
66+
@Test
67+
public void whenSort_thenArraySorted() {
68+
String[] sorted = Arrays.copyOf(intro, 4);
69+
Arrays.sort(sorted);
70+
71+
assertArrayEquals(new String[] { "a", "once", "time", "upon" }, sorted);
72+
}
73+
74+
@Test
75+
public void whenBinarySearch_thenFindElements() {
76+
String[] sorted = Arrays.copyOf(intro, 4);
77+
Arrays.sort(sorted);
78+
int exact = Arrays.binarySearch(sorted, "time");
79+
int caseInsensitive = Arrays.binarySearch(sorted, "TiMe", String::compareToIgnoreCase);
80+
81+
assertEquals("time", sorted[exact]);
82+
assertEquals(2, exact);
83+
assertEquals(exact, caseInsensitive);
84+
}
85+
86+
@Test
87+
public void whenNullElement_thenArraysHashCodeNotEqual() {
88+
int beforeChange = Arrays.hashCode(intro);
89+
int before = intro.hashCode();
90+
intro[3] = null;
91+
int after = intro.hashCode();
92+
int afterChange = Arrays.hashCode(intro);
93+
94+
assertNotEquals(beforeChange, afterChange);
95+
assertEquals(before, after);
96+
}
97+
98+
@Test
99+
public void whenNestedArrayNullElement_thenEqualsFailDeepHashPass() {
100+
Object[] looping = new Object[] { intro, intro };
101+
int deepHashBefore = Arrays.deepHashCode(looping);
102+
int hashBefore = Arrays.hashCode(looping);
103+
104+
intro[3] = null;
105+
106+
int hashAfter = Arrays.hashCode(looping);
107+
int deepHashAfter = Arrays.deepHashCode(looping);
108+
109+
assertEquals(hashAfter, hashBefore);
110+
assertNotEquals(deepHashAfter, deepHashBefore);
111+
}
112+
113+
@Test
114+
public void whenStreamBadIndex_thenException() {
115+
assertEquals(Arrays.stream(intro).count(), 4);
116+
117+
exception.expect(ArrayIndexOutOfBoundsException.class);
118+
Arrays.stream(intro, 2, 1).count();
119+
}
120+
121+
@Test
122+
public void whenSetAllToUpper_thenAppliedToAllElements() {
123+
String[] longAgo = new String[4];
124+
Arrays.setAll(longAgo, i -> intro[i].toUpperCase());
125+
126+
assertArrayEquals(longAgo, new String[] { "ONCE", "UPON", "A", "TIME" });
127+
}
128+
129+
@Test
130+
public void whenToString_thenFormattedArrayString() {
131+
assertEquals("[once, upon, a, time]", Arrays.toString(intro));
132+
}
133+
134+
@Test
135+
public void whenNestedArrayDeepString_thenFormattedArraysString() {
136+
String[] end = { "the", "end" };
137+
Object[] story = new Object[] { intro, new String[] { "chapter one", "chapter two" }, end };
138+
139+
assertEquals("[[once, upon, a, time], [chapter one, chapter two], [the, end]]", Arrays.deepToString(story));
140+
}
141+
142+
@Test
143+
public void whenAsList_thenImmutableArray() {
144+
List<String> rets = Arrays.asList(intro);
145+
146+
assertTrue(rets.contains("upon"));
147+
assertTrue(rets.contains("time"));
148+
assertEquals(rets.size(), 4);
149+
150+
exception.expect(UnsupportedOperationException.class);
151+
rets.add("the");
152+
}
153+
}

0 commit comments

Comments
 (0)