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 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   * Tests {@link IntersectionResult}.
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          // Test a different instance with same values
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             // Ensure the min is above 0
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             // Ensure the min is above 0
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             // Not perfect as this will match substrings too. The chance of error
137             // is limited by restricting the numbers to a max of 10.
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 }