teditdistance.nim 1.6 KB

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