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  
18  package org.apache.commons.text.similarity;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import org.junit.jupiter.api.BeforeAll;
24  import org.junit.jupiter.api.Test;
25  import org.junit.jupiter.params.ParameterizedTest;
26  import org.junit.jupiter.params.provider.MethodSource;
27  
28  /**
29   * Tests {@link HammingDistance}.
30   */
31  class HammingDistanceTest {
32  
33      private static HammingDistance distance;
34  
35      @BeforeAll
36      public static void setUp() {
37          distance = new HammingDistance();
38      }
39  
40      @Test
41      void testApply_DifferentSimilarityInputLength() {
42          assertThrows(IllegalArgumentException.class, () -> distance.apply(new SimilarityCharacterInput("a"), new SimilarityCharacterInput("ab")));
43      }
44  
45      @Test
46      void testApply_NullSimilarityInput() {
47          assertThrows(IllegalArgumentException.class, () -> distance.apply(null, new SimilarityCharacterInput("a")));
48      }
49  
50      @Test
51      void testApply_SimilarityInputNull() {
52          assertThrows(IllegalArgumentException.class, () -> distance.apply(new SimilarityCharacterInput("a"), null));
53      }
54  
55      @ParameterizedTest
56      @MethodSource("org.apache.commons.text.similarity.SimilarityInputTest#similarityInputsEquals()")
57      void testHammingDistance(final Class<?> cls) {
58          assertEquals(0, distance.apply(SimilarityInputTest.build(cls, ""), SimilarityInputTest.build(cls, "")));
59          assertEquals(0, distance.apply(SimilarityInputTest.build(cls, "pappa"), SimilarityInputTest.build(cls, "pappa")));
60          assertEquals(1, distance.apply(SimilarityInputTest.build(cls, "papaa"), SimilarityInputTest.build(cls, "pappa")));
61          assertEquals(3, distance.apply(SimilarityInputTest.build(cls, "karolin"), SimilarityInputTest.build(cls, "kathrin")));
62          assertEquals(3, distance.apply(SimilarityInputTest.build(cls, "karolin"), SimilarityInputTest.build(cls, "kerstin")));
63          assertEquals(2, distance.apply(SimilarityInputTest.build(cls, "1011101"), SimilarityInputTest.build(cls, "1001001")));
64          assertEquals(3, distance.apply(SimilarityInputTest.build(cls, "2173896"), SimilarityInputTest.build(cls, "2233796")));
65          assertEquals(2, distance.apply(SimilarityInputTest.build(cls, "ATCG"), SimilarityInputTest.build(cls, "ACCC")));
66      }
67  
68      @Test
69      void testHammingDistance_nullLeftValue() {
70          assertThrows(IllegalArgumentException.class, () -> distance.apply(null, ""));
71      }
72  
73      @Test
74      void testHammingDistance_nullRightValue() {
75          assertThrows(IllegalArgumentException.class, () -> distance.apply("", null));
76      }
77  
78      @Test
79      void testHammingDistanceCharSequence() {
80          assertEquals(0, distance.apply("", ""));
81          assertEquals(0, distance.apply("pappa", "pappa"));
82          assertEquals(1, distance.apply("papaa", "pappa"));
83          assertEquals(3, distance.apply("karolin", "kathrin"));
84          assertEquals(3, distance.apply("karolin", "kerstin"));
85          assertEquals(2, distance.apply("1011101", "1001001"));
86          assertEquals(3, distance.apply("2173896", "2233796"));
87          assertEquals(2, distance.apply("ATCG", "ACCC"));
88      }
89  }