teditdistance.nim 1.5 KB

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