003.phpt 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. --TEST--
  2. levenshtein() function test
  3. --FILE--
  4. <?php
  5. function test_me($title,$expect,$text1,$text2,$cost1=0,$cost2=0,$cost3=0) {
  6. if($cost1==0)
  7. $result=levenshtein($text1,$text2);
  8. else
  9. $result=levenshtein($text1,$text2,$cost1,$cost2,$cost3);
  10. if($result==$expect) return 0;
  11. echo "$title: result is $result instead of $expect ";
  12. echo "for '$text1'/'$text2' ";
  13. if($cost1) echo "($cost1:$cost2:$cost3)";
  14. echo "\n";
  15. return 1;
  16. }
  17. $n=0;
  18. $n += test_me("equal" , 0, "12345", "12345");
  19. $n += test_me("1st empty" , 3, "", "xzy");
  20. $n += test_me("2nd empty" , 3, "xzy", "");
  21. $n += test_me("both empty" , 0, "", "");
  22. $n += test_me("1 char" , 1, "1", "2");
  23. $n += test_me("2 char swap", 2, "12", "21");
  24. $n += test_me("inexpensive delete", 2, "2121", "11", 2, 1, 1);
  25. $n += test_me("expensive delete" , 10, "2121", "11", 2, 1, 5);
  26. $n += test_me("inexpensive insert", 2, "11", "2121", 1, 1, 1);
  27. $n += test_me("expensive insert" , 10, "11", "2121", 5, 1, 1);
  28. $n += test_me("expensive replace" , 3, "111", "121", 2, 3, 2);
  29. $n += test_me("very expensive replace", 4, "111", "121", 2, 9, 2);
  30. $n += test_me("bug #7368", 2, "13458", "12345");
  31. $n += test_me("bug #7368", 2, "1345", "1234");
  32. $n += test_me("bug #6562", 1, "debugg", "debug");
  33. $n += test_me("bug #6562", 1, "ddebug", "debug");
  34. $n += test_me("bug #6562", 2, "debbbug", "debug");
  35. $n += test_me("bug #6562", 1, "debugging", "debuging");
  36. $n += test_me("bug #16473", 2, "a", "bc");
  37. $n += test_me("bug #16473", 2, "xa", "xbc");
  38. $n += test_me("bug #16473", 2, "xax", "xbcx");
  39. $n += test_me("bug #16473", 2, "ax", "bcx");
  40. echo ($n==0)?"all passed\n":"$n failed\n";
  41. ?>
  42. --EXPECT--
  43. all passed