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 java.util.HashMap;
20 import java.util.concurrent.ThreadLocalRandom;
21
22 import org.junit.jupiter.api.Assertions;
23 import org.junit.jupiter.api.Test;
24
25
26
27
28 class IntersectionResultTest {
29 @Test
30 void testEquals() {
31 final IntersectionResult[] results = {
32 new IntersectionResult(0, 0, 0),
33 new IntersectionResult(10, 0, 0),
34 new IntersectionResult(10, 10, 0),
35 new IntersectionResult(10, 10, 10),
36 };
37
38
39 Assertions.assertEquals(results[0], new IntersectionResult(0, 0, 0));
40
41 final Object something = new Object();
42 for (int i = 0; i < results.length; i++) {
43 Assertions.assertNotEquals(results[i], something);
44 Assertions.assertNotEquals(null, results[i]);
45 for (int j = 0; j < results.length; j++) {
46 Assertions.assertEquals(results[i].equals(results[j]), i == j);
47 }
48 }
49 }
50
51 @Test
52 void testHashCode() {
53 final IntersectionResult[] results = {
54 new IntersectionResult(10, 0, 0),
55 new IntersectionResult(10, 10, 0),
56 new IntersectionResult(10, 10, 10),
57 };
58 final HashMap<IntersectionResult, Integer> map = new HashMap<>();
59 final int offset = 123;
60 for (int i = 0; i < results.length; i++) {
61 map.put(results[i], i + offset);
62 }
63 for (int i = 0; i < results.length; i++) {
64 Assertions.assertEquals(i + offset, map.get(results[i]));
65 }
66 }
67
68 @Test
69 void testNewIntersectionResult_WithIntersectionAboveSizeAorB() {
70 final int sizeA = 1;
71 final int sizeB = 2;
72 final int intersection = Math.max(sizeA, sizeB) + 1;
73 Assertions.assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
74 Assertions.assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeB, sizeA, intersection));
75 }
76
77 @Test
78 void testNewIntersectionResult_WithNegativeIntersection() {
79 final int sizeA = 0;
80 final int sizeB = 0;
81 final int intersection = -1;
82 Assertions.assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
83 }
84
85 @Test
86 void testNewIntersectionResult_WithNegativeSizeA() {
87 final int sizeA = -1;
88 final int sizeB = 0;
89 final int intersection = 0;
90 Assertions.assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
91 }
92
93 @Test
94 void testNewIntersectionResult_WithNegativeSizeB() {
95 final int sizeA = 0;
96 final int sizeB = -1;
97 final int intersection = 0;
98 Assertions.assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
99 }
100
101 @Test
102 void testNewIntersectionResult_WithZeros() {
103 final int sizeA = 0;
104 final int sizeB = 0;
105 final int intersection = 0;
106 new IntersectionResult(sizeA, sizeB, intersection);
107 }
108
109 @Test
110 void testProperties() {
111 final ThreadLocalRandom rand = ThreadLocalRandom.current();
112 final int max = 1024;
113 for (int i = 0; i < 5; i++) {
114
115 final int sizeA = rand.nextInt(max) + 1;
116 final int sizeB = rand.nextInt(max) + 1;
117 final int intersection = rand.nextInt(Math.min(sizeA, sizeB));
118 final IntersectionResult result = new IntersectionResult(sizeA, sizeB, intersection);
119 Assertions.assertEquals(sizeA, result.getSizeA());
120 Assertions.assertEquals(sizeB, result.getSizeB());
121 Assertions.assertEquals(intersection, result.getIntersection());
122 }
123 }
124
125 @Test
126 void testToString() {
127 final ThreadLocalRandom rand = ThreadLocalRandom.current();
128 final int max = 9;
129 for (int i = 0; i < 5; i++) {
130
131 final int sizeA = rand.nextInt(max) + 1;
132 final int sizeB = rand.nextInt(max) + 1;
133 final int intersection = rand.nextInt(Math.min(sizeA, sizeB));
134 final IntersectionResult result = new IntersectionResult(sizeA, sizeB, intersection);
135 final String string = result.toString();
136
137
138 Assertions.assertTrue(string.contains(String.valueOf(sizeA)));
139 Assertions.assertTrue(string.contains(String.valueOf(sizeB)));
140 Assertions.assertTrue(string.contains(String.valueOf(intersection)));
141 }
142 }
143 }