View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.text.similarity;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotEquals;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.HashMap;
26  import java.util.concurrent.ThreadLocalRandom;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link IntersectionResult}.
32   */
33  class IntersectionResultTest {
34      @Test
35      void testEquals() {
36          final IntersectionResult[] results = {
37                  new IntersectionResult(0, 0, 0),
38                  new IntersectionResult(10, 0, 0),
39                  new IntersectionResult(10, 10, 0),
40                  new IntersectionResult(10, 10, 10),
41          };
42  
43          // Test a different instance with same values
44          assertEquals(new IntersectionResult(0, 0, 0), results[0]);
45  
46          final Object something = new Object();
47          for (int i = 0; i < results.length; i++) {
48              assertNotEquals(results[i], something);
49              assertNotEquals(null, results[i]);
50              for (int j = 0; j < results.length; j++) {
51                  assertEquals(results[i].equals(results[j]), i == j);
52              }
53  
54              assertFalse(results[i].equals(null), "Should not be Equal to null");
55              assertNotEquals("Test", results[i], "Should not be Equal to a different type of object");
56          }
57      }
58  
59      @Test
60      void testHashCode() {
61          final IntersectionResult[] results = {
62                  new IntersectionResult(10, 0, 0),
63                  new IntersectionResult(10, 10, 0),
64                  new IntersectionResult(10, 10, 10),
65          };
66          final HashMap<IntersectionResult, Integer> map = new HashMap<>();
67          final int offset = 123;
68          for (int i = 0; i < results.length; i++) {
69              map.put(results[i], i + offset);
70          }
71          for (int i = 0; i < results.length; i++) {
72              assertEquals(i + offset, map.get(results[i]));
73          }
74      }
75  
76      @Test
77      void testNewIntersectionResult_WithIntersectionAboveSizeAorB() {
78          final int sizeA = 1;
79          final int sizeB = 2;
80          final int intersection = Math.max(sizeA, sizeB) + 1;
81          assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
82          assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeB, sizeA, intersection));
83      }
84  
85      @Test
86      void testNewIntersectionResult_WithNegativeIntersection() {
87          final int sizeA = 0;
88          final int sizeB = 0;
89          final int intersection = -1;
90          assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
91      }
92  
93      @Test
94      void testNewIntersectionResult_WithNegativeSizeA() {
95          final int sizeA = -1;
96          final int sizeB = 0;
97          final int intersection = 0;
98          assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
99      }
100 
101     @Test
102     void testNewIntersectionResult_WithNegativeSizeB() {
103         final int sizeA = 0;
104         final int sizeB = -1;
105         final int intersection = 0;
106         assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
107     }
108 
109     @Test
110     void testNewIntersectionResult_WithZeros() {
111         final int sizeA = 0;
112         final int sizeB = 0;
113         final int intersection = 0;
114         new IntersectionResult(sizeA, sizeB, intersection);
115     }
116 
117     @Test
118     void testProperties() {
119         final ThreadLocalRandom rand = ThreadLocalRandom.current();
120         final int max = 1024;
121         for (int i = 0; i < 5; i++) {
122             // Ensure the min is above 0
123             final int sizeA = rand.nextInt(max) + 1;
124             final int sizeB = rand.nextInt(max) + 1;
125             final int intersection = rand.nextInt(Math.min(sizeA, sizeB));
126             final IntersectionResult result = new IntersectionResult(sizeA, sizeB, intersection);
127             assertEquals(sizeA, result.getSizeA());
128             assertEquals(sizeB, result.getSizeB());
129             assertEquals(intersection, result.getIntersection());
130         }
131     }
132 
133     @Test
134     void testToString() {
135         final ThreadLocalRandom rand = ThreadLocalRandom.current();
136         final int max = 9;
137         for (int i = 0; i < 5; i++) {
138             // Ensure the min is above 0
139             final int sizeA = rand.nextInt(max) + 1;
140             final int sizeB = rand.nextInt(max) + 1;
141             final int intersection = rand.nextInt(Math.min(sizeA, sizeB));
142             final IntersectionResult result = new IntersectionResult(sizeA, sizeB, intersection);
143             final String string = result.toString();
144             // Not perfect as this will match substrings too. The chance of error
145             // is limited by restricting the numbers to a max of 10.
146             assertTrue(string.contains(String.valueOf(sizeA)));
147             assertTrue(string.contains(String.valueOf(sizeB)));
148             assertTrue(string.contains(String.valueOf(intersection)));
149         }
150     }
151 }