-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.c
94 lines (77 loc) · 2.45 KB
/
test.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "levenshtein.h"
static int assertionCount = 0;
static int errorCount = 0;
static void
printResult (const char *input, const char *alternative, size_t actual, size_t expected) {
fprintf(stderr, "\033[31m");
fprintf(stderr,
" (✖) For `%s` and `%s`. Expected `%zu`, got `%zu`",
input, alternative, expected, actual
);
fprintf(stderr, "\033[0m");
fprintf(stderr, "\n");
}
static void
assertDistance(const char *input, const char *alternative, size_t expected) {
const size_t na = strlen(input);
const size_t nb = strlen(alternative);
const size_t res = levenshtein(input, alternative);
const size_t res_n = levenshtein_n(input, na, alternative, nb);
if (res != expected) {
errorCount++;
printResult(input, alternative, res, expected);
} else if (res_n != expected) {
errorCount++;
printResult(input, alternative, res_n, expected);
} else {
printf("\033[32m.\033[0m");
}
assertionCount++;
}
int
main() {
// It should work.
assertDistance("", "a", 1);
assertDistance("a", "", 1);
assertDistance("", "", 0);
assertDistance("levenshtein", "levenshtein", 0);
assertDistance("sitting", "kitten", 3);
assertDistance("gumbo", "gambol", 2);
assertDistance("saturday", "sunday", 3);
// It should match case sensitive.
assertDistance("DwAyNE", "DUANE", 2);
assertDistance("dwayne", "DuAnE", 5);
// It not care about parameter ordering.
assertDistance("aarrgh", "aargh", 1);
assertDistance("aargh", "aarrgh", 1);
// Some tests form `hiddentao/fast-levenshtein`.
assertDistance("a", "b", 1);
assertDistance("ab", "ac", 1);
assertDistance("ac", "bc", 1);
assertDistance("abc", "axc", 1);
assertDistance("xabxcdxxefxgx", "1ab2cd34ef5g6", 6);
assertDistance("xabxcdxxefxgx", "abcdefg", 6);
assertDistance("javawasneat", "scalaisgreat", 7);
assertDistance("example", "samples", 3);
assertDistance("sturgeon", "urgently", 6);
assertDistance("levenshtein", "frankenstein", 6);
assertDistance("distance", "difference", 5);
// Log total errors.
printf("\n");
if (errorCount != 0) {
printf("\033[31m");
printf("(✖) Failed on %d of %d assertions", errorCount, assertionCount);
printf("\033[0m");
printf("\n");
exit(EXIT_FAILURE);
}
// Or, log total successes.
printf("\033[32m");
printf("(✓) Passed %d assertions without errors", assertionCount);
printf("\033[0m");
printf("\n");
return 0;
}