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(new IntersectionResult(0, 0, 0), results[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              Assertions.assertFalse(results[i].equals(null), "Should not be Equal to null");
50              Assertions.assertNotEquals("Test", results[i], "Should not be Equal to a different type of object");
51          }
52      }
53  
54      @Test
55      void testHashCode() {
56          final IntersectionResult[] results = {
57                  new IntersectionResult(10, 0, 0),
58                  new IntersectionResult(10, 10, 0),
59                  new IntersectionResult(10, 10, 10),
60          };
61          final HashMap<IntersectionResult, Integer> map = new HashMap<>();
62          final int offset = 123;
63          for (int i = 0; i < results.length; i++) {
64              map.put(results[i], i + offset);
65          }
66          for (int i = 0; i < results.length; i++) {
67              Assertions.assertEquals(i + offset, map.get(results[i]));
68          }
69      }
70  
71      @Test
72      void testNewIntersectionResult_WithIntersectionAboveSizeAorB() {
73          final int sizeA = 1;
74          final int sizeB = 2;
75          final int intersection = Math.max(sizeA, sizeB) + 1;
76          Assertions.assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
77          Assertions.assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeB, sizeA, intersection));
78      }
79  
80      @Test
81      void testNewIntersectionResult_WithNegativeIntersection() {
82          final int sizeA = 0;
83          final int sizeB = 0;
84          final int intersection = -1;
85          Assertions.assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
86      }
87  
88      @Test
89      void testNewIntersectionResult_WithNegativeSizeA() {
90          final int sizeA = -1;
91          final int sizeB = 0;
92          final int intersection = 0;
93          Assertions.assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
94      }
95  
96      @Test
97      void testNewIntersectionResult_WithNegativeSizeB() {
98          final int sizeA = 0;
99          final int sizeB = -1;
100         final int intersection = 0;
101         Assertions.assertThrows(IllegalArgumentException.class, () -> new IntersectionResult(sizeA, sizeB, intersection));
102     }
103 
104     @Test
105     void testNewIntersectionResult_WithZeros() {
106         final int sizeA = 0;
107         final int sizeB = 0;
108         final int intersection = 0;
109         new IntersectionResult(sizeA, sizeB, intersection);
110     }
111 
112     @Test
113     void testProperties() {
114         final ThreadLocalRandom rand = ThreadLocalRandom.current();
115         final int max = 1024;
116         for (int i = 0; i < 5; i++) {
117             // Ensure the min is above 0
118             final int sizeA = rand.nextInt(max) + 1;
119             final int sizeB = rand.nextInt(max) + 1;
120             final int intersection = rand.nextInt(Math.min(sizeA, sizeB));
121             final IntersectionResult result = new IntersectionResult(sizeA, sizeB, intersection);
122             Assertions.assertEquals(sizeA, result.getSizeA());
123             Assertions.assertEquals(sizeB, result.getSizeB());
124             Assertions.assertEquals(intersection, result.getIntersection());
125         }
126     }
127 
128     @Test
129     void testToString() {
130         final ThreadLocalRandom rand = ThreadLocalRandom.current();
131         final int max = 9;
132         for (int i = 0; i < 5; i++) {
133             // Ensure the min is above 0
134             final int sizeA = rand.nextInt(max) + 1;
135             final int sizeB = rand.nextInt(max) + 1;
136             final int intersection = rand.nextInt(Math.min(sizeA, sizeB));
137             final IntersectionResult result = new IntersectionResult(sizeA, sizeB, intersection);
138             final String string = result.toString();
139             // Not perfect as this will match substrings too. The chance of error
140             // is limited by restricting the numbers to a max of 10.
141             Assertions.assertTrue(string.contains(String.valueOf(sizeA)));
142             Assertions.assertTrue(string.contains(String.valueOf(sizeB)));
143             Assertions.assertTrue(string.contains(String.valueOf(intersection)));
144         }
145     }
146 }