File: edit_distance.cpp

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (139 lines) | stat: -rw-r--r-- 4,312 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/// \file
/// \author Diffblue Ltd.
/// Unit tests for checking if two strings are within a certain edit distance

#include <testing-utils/use_catch.h>
#include <util/edit_distance.h>

TEST_CASE("edit distance 0", "[core][util][edit_distance]")
{
  auto const hello = levenshtein_automatont{"hello", 0};

  // Distance 0
  REQUIRE(hello.matches("hello"));
  REQUIRE(hello.get_edit_distance("hello").value() == 0);

  // Distance 1
  REQUIRE_FALSE(hello.matches("hallo"));
  REQUIRE_FALSE(hello.matches("hell"));
  REQUIRE_FALSE(hello.matches("helloo"));
  REQUIRE_FALSE(hello.matches("chello"));

  // Distance 2
  REQUIRE_FALSE(hello.matches("helol"));
  REQUIRE_FALSE(hello.matches("help"));
  REQUIRE_FALSE(hello.matches("yohello"));

  // Distance 3
  REQUIRE_FALSE(hello.matches("kelp"));
  REQUIRE_FALSE(hello.matches("hilt"));
  REQUIRE_FALSE(hello.matches("wallow"));

  // Distance > 3
  REQUIRE_FALSE(hello.matches("unrelated"));
}

TEST_CASE("edit distance 1", "[core][util][edit_distance]")
{
  auto const hello = levenshtein_automatont{"hello", 1};

  // Distance 0
  REQUIRE(hello.matches("hello"));
  REQUIRE(hello.get_edit_distance("hello").value() == 0);

  // Distance 1
  REQUIRE(hello.matches("hallo"));
  REQUIRE(hello.get_edit_distance("hallo").value() == 1);
  REQUIRE(hello.matches("hell"));
  REQUIRE(hello.get_edit_distance("hell").value() == 1);
  REQUIRE(hello.matches("helloo"));
  REQUIRE(hello.get_edit_distance("helloo").value() == 1);
  REQUIRE(hello.matches("chello"));
  REQUIRE(hello.get_edit_distance("chello").value() == 1);

  // Distance 2
  REQUIRE_FALSE(hello.matches("helol"));
  REQUIRE_FALSE(hello.matches("help"));
  REQUIRE_FALSE(hello.matches("yohello"));

  // Distance 3
  REQUIRE_FALSE(hello.matches("kelp"));
  REQUIRE_FALSE(hello.matches("hilt"));
  REQUIRE_FALSE(hello.matches("wallow"));

  // Distance > 3
  REQUIRE_FALSE(hello.matches("unrelated"));
}

TEST_CASE("edit distance 2", "[core][util][edit_distance]")
{
  auto const hello = levenshtein_automatont{"hello", 2};

  // Distance 0
  REQUIRE(hello.matches("hello"));
  REQUIRE(hello.get_edit_distance("hello").value() == 0);

  // Distance 1
  REQUIRE(hello.matches("hallo"));
  REQUIRE(hello.get_edit_distance("hallo").value() == 1);
  REQUIRE(hello.matches("hell"));
  REQUIRE(hello.get_edit_distance("hell").value() == 1);
  REQUIRE(hello.matches("helloo"));
  REQUIRE(hello.get_edit_distance("helloo").value() == 1);
  REQUIRE(hello.matches("chello"));
  REQUIRE(hello.get_edit_distance("chello").value() == 1);

  // Distance 2
  REQUIRE(hello.matches("helol"));
  REQUIRE(hello.get_edit_distance("helol").value() == 2);
  REQUIRE(hello.matches("help"));
  REQUIRE(hello.get_edit_distance("help").value() == 2);
  REQUIRE(hello.matches("yohello"));
  REQUIRE(hello.get_edit_distance("yohello").value() == 2);

  // Distance 3
  REQUIRE_FALSE(hello.matches("kelp"));
  REQUIRE_FALSE(hello.matches("hilt"));
  REQUIRE_FALSE(hello.matches("wallow"));

  // Distance > 3
  REQUIRE_FALSE(hello.matches("unrelated"));
}

TEST_CASE("edit distance 3", "[core][util][edit_distance]")
{
  auto const hello = levenshtein_automatont{"hello", 3};

  // Distance 0
  REQUIRE(hello.matches("hello"));
  REQUIRE(hello.get_edit_distance("hello").value() == 0);

  // Distance 1
  REQUIRE(hello.matches("hallo"));
  REQUIRE(hello.get_edit_distance("hallo").value() == 1);
  REQUIRE(hello.matches("hell"));
  REQUIRE(hello.get_edit_distance("hell").value() == 1);
  REQUIRE(hello.matches("helloo"));
  REQUIRE(hello.get_edit_distance("helloo").value() == 1);
  REQUIRE(hello.matches("chello"));
  REQUIRE(hello.get_edit_distance("chello").value() == 1);

  // Distance 2
  REQUIRE(hello.matches("helol"));
  REQUIRE(hello.get_edit_distance("helol").value() == 2);
  REQUIRE(hello.matches("help"));
  REQUIRE(hello.get_edit_distance("help").value() == 2);
  REQUIRE(hello.matches("yohello"));
  REQUIRE(hello.get_edit_distance("yohello").value() == 2);

  // Distance 3
  REQUIRE(hello.matches("kelp"));
  REQUIRE(hello.get_edit_distance("kelp").value() == 3);
  REQUIRE(hello.matches("hilt"));
  REQUIRE(hello.get_edit_distance("hilt").value() == 3);
  REQUIRE(hello.matches("wallow"));
  REQUIRE(hello.get_edit_distance("wallow").value() == 3);

  // Distance > 3
  REQUIRE_FALSE(hello.matches("unrelated"));
}