mirror of https://github.com/apache/cloudstack.git
Added unit tests to String Utils methods and updated the code
This commit is contained in:
parent
7c797230c3
commit
f72f2fdd53
|
|
@ -314,16 +314,6 @@ public class StringUtils {
|
|||
return join(csvList, ",");
|
||||
}
|
||||
|
||||
public static int min(int x, int y, int z)
|
||||
{
|
||||
if (x <= y && x <= z)
|
||||
return x;
|
||||
if (y <= x && y <= z)
|
||||
return y;
|
||||
else
|
||||
return z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates minimum number of edits required to convert from one string to another string.
|
||||
* @param str1
|
||||
|
|
@ -333,12 +323,12 @@ public class StringUtils {
|
|||
* @return minimum number of edits required to convert str1 to str2
|
||||
*/
|
||||
public static int minimumEditDistance(String str1, String str2) {
|
||||
int m = str1.length();
|
||||
int n = str2.length();
|
||||
int[][] auxiliaryArray = new int[m + 1][n + 1];
|
||||
int str1Length = str1.length();
|
||||
int str2Length = str2.length();
|
||||
int[][] auxiliaryArray = new int[str1Length + 1][str2Length + 1];
|
||||
|
||||
for (int i = 0; i <= m; i++) {
|
||||
for (int j = 0; j <= n; j++) {
|
||||
for (int i = 0; i <= str1Length; i++) {
|
||||
for (int j = 0; j <= str2Length; j++) {
|
||||
if (i == 0)
|
||||
auxiliaryArray[i][j] = j;
|
||||
else if (j == 0)
|
||||
|
|
@ -346,9 +336,9 @@ public class StringUtils {
|
|||
else if (str1.charAt(i - 1) == str2.charAt(j - 1))
|
||||
auxiliaryArray[i][j] = auxiliaryArray[i - 1][j - 1];
|
||||
else
|
||||
auxiliaryArray[i][j] = 1 + min(auxiliaryArray[i][j - 1], auxiliaryArray[i - 1][j], auxiliaryArray[i - 1][j - 1]);
|
||||
auxiliaryArray[i][j] = 1 + Integer.min(Integer.min(auxiliaryArray[i][j - 1], auxiliaryArray[i - 1][j]), auxiliaryArray[i - 1][j - 1]);
|
||||
}
|
||||
}
|
||||
return auxiliaryArray[m][n];
|
||||
return auxiliaryArray[str1Length][str2Length];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -258,4 +258,34 @@ public class StringUtilsTest {
|
|||
String output = StringUtils.toCSVList(Arrays.asList(input.split(",")));
|
||||
assertTrue(input.equals(output));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZeroEditDistance() {
|
||||
String str1 = "Other 32-bit";
|
||||
String str2 = "Other 32-bit";
|
||||
int minDistance = StringUtils.minimumEditDistance(str1, str2);
|
||||
assertEquals(minDistance, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBestMatchStringWithEditDistance() {
|
||||
String str1 = "FreeBSD 11 (32bit)";
|
||||
String str2 = "FreeBSD 12 (64bit)";
|
||||
String targetString = "FreeBSD 64bit";
|
||||
int minDistanceStr1 = StringUtils.minimumEditDistance(str1, targetString);
|
||||
int minDistanceStr2 = StringUtils.minimumEditDistance(str2, targetString);
|
||||
// the best match will be str2, so expecting less edit distance
|
||||
assertTrue(minDistanceStr2 < minDistanceStr1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompletelyDifferentStringsWithEditDistance() {
|
||||
String str1 = "Other (32-bit)";
|
||||
String str2 = "SCO OpenServer 5";
|
||||
String targetString = "Other 32-bit";
|
||||
int minDistanceStr1 = StringUtils.minimumEditDistance(str1, targetString);
|
||||
int minDistanceStr2 = StringUtils.minimumEditDistance(str2, targetString);
|
||||
// the best match will be str1, so expecting less edit distance
|
||||
assertTrue(minDistanceStr1 < minDistanceStr2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue