-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
CheckAnagramsTest.java
69 lines (57 loc) · 2.74 KB
/
CheckAnagramsTest.java
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
package com.thealgorithms.strings;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CheckAnagramsTest {
private static final String MESSAGE = "Strings must contain only lowercase English letters!";
// CHECK METHOD isAnagrams()
@Test
public void testCheckAnagrams() {
String testString1 = "STUDY";
String testString2 = "DUSTY";
Assertions.assertTrue(CheckAnagrams.isAnagrams(testString1, testString2));
}
@Test
public void testCheckFalseAnagrams() {
String testString1 = "STUDY";
String testString2 = "random";
Assertions.assertFalse(CheckAnagrams.isAnagrams(testString1, testString2));
}
@Test
public void testCheckSameWordAnagrams() {
String testString1 = "STUDY";
Assertions.assertTrue(CheckAnagrams.isAnagrams(testString1, testString1));
}
@Test
public void testCheckDifferentCasesAnagram() {
String testString1 = "STUDY";
String testString2 = "dusty";
Assertions.assertTrue(CheckAnagrams.isAnagrams(testString1, testString2));
}
// CHECK METHOD isAnagramsUnicode()
// Below tests work with strings which consist of Unicode symbols & the algorithm is case-sensitive.
@Test
public void testStringAreValidAnagramsCaseSensitive() {
Assertions.assertTrue(CheckAnagrams.isAnagramsUnicode("Silent", "liSten"));
Assertions.assertTrue(CheckAnagrams.isAnagramsUnicode("This is a string", "is This a string"));
}
@Test
public void testStringAreNotAnagramsCaseSensitive() {
Assertions.assertFalse(CheckAnagrams.isAnagramsUnicode("Silent", "Listen"));
Assertions.assertFalse(CheckAnagrams.isAnagramsUnicode("This is a string", "Is this a string"));
}
// CHECK METHOD isAnagramsOptimised()
// Below tests work with strings which consist of only lowercase English letters
@Test
public void testOptimisedAlgorithmStringsAreValidAnagrams() {
Assertions.assertTrue(CheckAnagrams.isAnagramsOptimised("silent", "listen"));
Assertions.assertTrue(CheckAnagrams.isAnagramsOptimised("mam", "amm"));
}
@Test
public void testOptimisedAlgorithmShouldThrowExceptionWhenStringsContainUppercaseLetters() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> CheckAnagrams.isAnagramsOptimised("Silent", "Listen"));
Assertions.assertEquals(exception.getMessage(), MESSAGE);
exception = assertThrows(IllegalArgumentException.class, () -> Assertions.assertFalse(CheckAnagrams.isAnagramsOptimised("This is a string", "Is this a string")));
Assertions.assertEquals(exception.getMessage(), MESSAGE);
}
}