1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.text.similarity;
18
19 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import org.junit.jupiter.api.BeforeAll;
23 import org.junit.jupiter.api.Test;
24
25
26
27
28 public class JaccardDistanceTest {
29
30 private static JaccardDistance classBeingTested;
31
32 @BeforeAll
33 public static void setUp() {
34 classBeingTested = new JaccardDistance();
35 }
36
37 @Test
38 public void testGettingJaccardDistance() {
39
40 assertEquals(0.0, classBeingTested.apply("", ""));
41 assertEquals(1.0, classBeingTested.apply("left", ""));
42 assertEquals(1.0, classBeingTested.apply("", "right"));
43 assertEquals(1.0 - 3.0 / 4, classBeingTested.apply("frog", "fog"));
44 assertEquals(1.0, classBeingTested.apply("fly", "ant"));
45 assertEquals(1.0 - 2.0 / 9, classBeingTested.apply("elephant", "hippo"));
46 assertEquals(1.0 - 7.0 / 11, classBeingTested.apply("ABC Corporation", "ABC Corp"));
47 assertEquals(1.0 - 13.0 / 17,
48 classBeingTested.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."));
49 assertEquals(1.0 - 16.0 / 18,
50 classBeingTested.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
51 assertEquals(1.0 - 9.0 / 10, classBeingTested.apply("PENNSYLVANIA", "PENNCISYLVNIA"));
52 assertEquals(1.0 - 1.0 / 8, classBeingTested.apply("left", "right"));
53 assertEquals(1.0 - 1.0 / 8, classBeingTested.apply("leettteft", "ritttght"));
54 assertEquals(0.0, classBeingTested.apply("the same string", "the same string"));
55 }
56
57 @Test
58 public void testGettingJaccardDistanceNullNull() {
59 assertThatIllegalArgumentException().isThrownBy(() -> classBeingTested.apply(null, null));
60 }
61
62 @Test
63 public void testGettingJaccardDistanceNullString() {
64 assertThatIllegalArgumentException().isThrownBy(() -> classBeingTested.apply(null, "right"));
65 }
66
67 @Test
68 public void testGettingJaccardDistanceStringNull() {
69 assertThatIllegalArgumentException().isThrownBy(() -> classBeingTested.apply(" ", null));
70 }
71 }