teditdistance.nim 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import std/editdistance
  2. doAssert editDistance("", "") == 0
  3. doAssert editDistance("kitten", "sitting") == 3 # from Wikipedia
  4. doAssert editDistance("flaw", "lawn") == 2 # from Wikipedia
  5. doAssert editDistance("привет", "превет") == 1
  6. doAssert editDistance("Åge", "Age") == 1
  7. # editDistance, one string is longer in bytes, but shorter in rune length
  8. # first string: 4 bytes, second: 6 bytes, but only 3 runes
  9. doAssert editDistance("aaaa", "×××") == 4
  10. block veryLongStringEditDistanceTest:
  11. const cap = 256
  12. var
  13. s1 = newStringOfCap(cap)
  14. s2 = newStringOfCap(cap)
  15. while len(s1) < cap:
  16. s1.add 'a'
  17. while len(s2) < cap:
  18. s2.add 'b'
  19. doAssert editDistance(s1, s2) == cap
  20. block combiningCodePointsEditDistanceTest:
  21. const s = "A\xCC\x8Age"
  22. doAssert editDistance(s, "Age") == 1
  23. doAssert editDistanceAscii("", "") == 0
  24. doAssert editDistanceAscii("kitten", "sitting") == 3 # from Wikipedia
  25. doAssert editDistanceAscii("flaw", "lawn") == 2 # from Wikipedia
  26. doAssert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)
  27. doAssert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)
  28. doAssert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5)
  29. doAssert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3)
  30. doAssert(editDistance("prefix__hallo_suffix", "prefix") == 14)
  31. doAssert(editDistance("prefix__hallo_suffix", "suffix") == 14)
  32. doAssert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2)
  33. doAssert(editDistance("main", "malign") == 2)