File: teditdistance.nim

package info (click to toggle)
nim 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,911,644 kB
  • sloc: sh: 24,603; ansic: 1,761; python: 1,492; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (45 lines) | stat: -rw-r--r-- 1,605 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
discard """
  matrix: "--mm:refc; --mm:orc"
"""

import std/editdistance
import std/assertions

doAssert editDistance("", "") == 0
doAssert editDistance("kitten", "sitting") == 3 # from Wikipedia
doAssert editDistance("flaw", "lawn") == 2 # from Wikipedia

doAssert editDistance("привет", "превет") == 1
doAssert editDistance("Åge", "Age") == 1
# editDistance, one string is longer in bytes, but shorter in rune length
# first string: 4 bytes, second: 6 bytes, but only 3 runes
doAssert editDistance("aaaa", "×××") == 4

block veryLongStringEditDistanceTest:
  const cap = 256
  var
    s1 = newStringOfCap(cap)
    s2 = newStringOfCap(cap)
  while len(s1) < cap:
    s1.add 'a'
  while len(s2) < cap:
    s2.add 'b'
  doAssert editDistance(s1, s2) == cap

block combiningCodePointsEditDistanceTest:
  const s = "A\xCC\x8Age"
  doAssert editDistance(s, "Age") == 1

doAssert editDistanceAscii("", "") == 0
doAssert editDistanceAscii("kitten", "sitting") == 3 # from Wikipedia
doAssert editDistanceAscii("flaw", "lawn") == 2 # from Wikipedia


doAssert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)
doAssert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)
doAssert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5)
doAssert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3)
doAssert(editDistance("prefix__hallo_suffix", "prefix") == 14)
doAssert(editDistance("prefix__hallo_suffix", "suffix") == 14)
doAssert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2)
doAssert(editDistance("main", "malign") == 2)