Skip to content
105 changes: 105 additions & 0 deletions src/test/java/org/apache/commons/text/diff/CommandIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package org.apache.commons.text.diff;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CommandIntegrationTest {

//Create Visitor through using CommandVisitor
private static class myVisitor<T> implements CommandVisitor<T> {
private final List<String> visits = new ArrayList<>();

public List<String> getVisits() {
return visits;
}

@Override
public void visitInsertCommand(T object) {
visits.add("Insert: " + object);
}
@Override
public void visitDeleteCommand(T object) {
visits.add("Delete: " + object);
}

@Override
public void visitKeepCommand(T object) {
visits.add("Keep: " + object);
}
}

private myVisitor<String> visitor;

@BeforeEach
void InitializeVisitor(){
visitor = new myVisitor<>();
}

//Test 1 - Test all commands together with edit command
@Test
void testAllCommands() {
List<EditCommand<String>> commands = Arrays.asList(
new DeleteCommand<>("A"),
new KeepCommand<>("B"),
new InsertCommand<>("C")
);

for (EditCommand<String> commandList : commands) {
commandList.accept(visitor);
}

List<String> expectedVisits = Arrays.asList(
"Delete: A",
"Keep: B",
"Insert: C"
);

assertEquals(expectedVisits, visitor.getVisits());
}

//Test 2,3,4 - Test individual commands
@Test
void testDeleteCommand() {
DeleteCommand<String> delete = new DeleteCommand<>("D");
delete.accept(visitor);
assertEquals(Arrays.asList("Delete: D"), visitor.getVisits());
}

@Test
void testInsertCommand() {
InsertCommand<String> insert = new InsertCommand<>("E");
insert.accept(visitor);
assertEquals(Arrays.asList("Insert: E"), visitor.getVisits());
}

@Test
void testKeepCommand() {
KeepCommand<String> keep = new KeepCommand<>("F");
keep.accept(visitor);
assertEquals(Arrays.asList("Keep: F"), visitor.getVisits());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.text.similarity;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;


public class AnotherCosineDistTest {

private final CosineDistance cosineDistance = new CosineDistance();

//Test 1: Testing empty strings - should return error
@Test
void testEmptyStrings() {
assertThrows(IllegalArgumentException.class, () -> {
double distance = cosineDistance.apply("", "");
});
assertThrows(IllegalArgumentException.class, () -> {
double distance = cosineDistance.apply("", "a");
});
assertThrows(IllegalArgumentException.class, () -> {
double distance = cosineDistance.apply("a", "");
});
}

//Test 2: Testing identical strings - should return distance 0.0
@Test
void testIdenticalStrings(){
double distance = cosineDistance.apply("testing", "testing");
assertEquals(0.0, distance);
}

//Test 3: Testing different strings - should return distance 1.0
@Test
void testDifferentStrings(){
double distance = cosineDistance.apply("testing", "success");
assertEquals(1.0, distance);
}

//Test 4: Testing repeated word - result should be 0.0
@Test
void testOneRepeatedWord(){
double distance = cosineDistance.apply("testing", "testing testing");
assertEquals(0.0, distance);
}

//Test 5: Testing word with subword- result should be between 0.0 and 1.0
@Test
void testSubword(){
double distance = cosineDistance.apply("testing", "test");
assertTrue(distance != 0.0);
}

//Test 6: Testing very large identical strings - result should be 0.0
@Test
void testLargeStrings(){

String word1 = "abc";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 9999; i++) {
sb.append(word1).append("abc");
}
String word2 = word1;

double distance = cosineDistance.apply(word1, word2);
assertEquals(0.0, distance);
}

//SPECIAL CASE TEST
//Test 7: Testing case sensitivity- result should not be 0.0
@Test
void testCaseSens(){
double distance = cosineDistance.apply("test", "Test");
assertTrue(distance != 0.0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.text.similarity;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;

public class AnotherCosineSimilarityTest {

//Test 1: Null values - Should throw exception
@Test
public void testNullVectors() {
assertThrows(IllegalArgumentException.class, () -> {
new CosineSimilarity().cosineSimilarity(null, new HashMap<>());
});
assertThrows(IllegalArgumentException.class, () -> {
new CosineSimilarity().cosineSimilarity(new HashMap<>(), null);
});
assertThrows(IllegalArgumentException.class, () -> {
new CosineSimilarity().cosineSimilarity(null, null);
});
}

//Test 2: Empty vectors - Should return 0.0
@Test
public void testEmptyVectors() {
double similar = new CosineSimilarity().cosineSimilarity(new HashMap<>(), new HashMap<>());
assertEquals(0.0, similar);
}

//Test 3: Identical Vectors - Should return ~1.0
@Test
public void testIdenticalVec(){
final Map<CharSequence, Integer> vector = new HashMap<>();
vector.put("a", 1);
vector.put("b", 2);
double similar = new CosineSimilarity().cosineSimilarity(vector, vector);
assertEquals(1.0, similar, 0.00001);
}

//Test 4: Different Vectors - Should return 0.0
@Test
public void testDiffVec(){
Map<CharSequence, Integer> vector1 = new HashMap<>();
Map<CharSequence, Integer> vector2 = new HashMap<>();
vector1.put("a", 1);
vector1.put("b", 2);
vector2.put("c", 1);
vector2.put("d", 2);
double similar = new CosineSimilarity().cosineSimilarity(vector1, vector2);
assertEquals(0.0, similar);
}

//SPECIAL CASE TEST
//Test 5: Testing case sensitivity- result should be 0.0
@Test
void testCaseSens(){
Map<CharSequence, Integer> vector1 = new HashMap<>();
Map<CharSequence, Integer> vector2 = new HashMap<>();
vector1.put("a", 1);
vector1.put("b", 2);
vector2.put("A", 1);
vector2.put("B", 2);
double similar = new CosineSimilarity().cosineSimilarity(vector1, vector2);
assertEquals(0.0, similar);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.text.similarity;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;

public class AnotherJaccardDistTest {

private final JaccardDistance jacDist = new JaccardDistance();

//Test 1 - Null inputs should throw exceptions
@Test
public void testNullInput(){
assertThrows(IllegalArgumentException.class, () -> {
jacDist.apply(null, "test");
});
assertThrows(IllegalArgumentException.class, () -> {
jacDist.apply("test", null);
});
}

//Test 2 - Empty strings should return 0
@Test
public void testEmptyStrings(){
assertEquals(0.0, jacDist.apply("", ""));
}

//Test 3 - Test regular identical values
@Test
public void identValues(){
assertEquals(0.0, jacDist.apply("abcd", "abcd"));
assertEquals(0.0, jacDist.apply("testing", "testing"));

}

//Test 4 - test regular different values
@Test
public void testRegularValues(){
assertEquals(1.0, jacDist.apply("testing", "qwry"));
assertEquals(1.0, jacDist.apply("abcd", "efgh"));
assertEquals(1.0, jacDist.apply("hamburger", "pizza"), 0.1);
}

//Test 5 - test regular similar values
@Test
public void testSimilarValues(){
assertEquals(0.0, jacDist.apply("abc", "acb"));
assertEquals(0.5, jacDist.apply("test", "testing"));
}

}
Loading