]> git.ozlabs.org Git - ccan/blob - ccan/edit_distance/test/run-types-wchar.c
edit_distance: calculate edit distance between strings
[ccan] / ccan / edit_distance / test / run-types-wchar.c
1 /** @file
2  * Runnable tests for the edit_distance module using custom element and
3  * distance types.
4  *
5  * @copyright 2016 Kevin Locke <kevin@kevinlocke.name>
6  *            MIT license - see LICENSE file for details
7  */
8
9 #include <limits.h>             /* UCHAR_MAX */
10 #include <wchar.h>              /* wmemchr */
11
12 #include <ccan/array_size/array_size.h>
13 #include <ccan/tap/tap.h>
14
15 #define ed_elem wchar_t
16 #define ed_dist double
17 #define ED_HAS_ELEM(arr, elem, len) (wmemchr(arr, elem, len) != NULL)
18 /* Since we only use ASCII characters */
19 #define ED_HASH_ELEM(e) (unsigned char)e
20 #define ED_HASH_MAX UCHAR_MAX
21 #define ED_HASH_ON_STACK
22
23 #include <ccan/edit_distance/edit_distance.c>
24 #include <ccan/edit_distance/edit_distance_dl.c>
25 #include <ccan/edit_distance/edit_distance_lcs.c>
26 #include <ccan/edit_distance/edit_distance_lev.c>
27 #include <ccan/edit_distance/edit_distance_rdl.c>
28
29 int main(void)
30 {
31         const wchar_t src[] = L"abcd";
32         const wchar_t tgt[] = L"ecbf";
33         ed_size slen = ARRAY_SIZE(src) - 1;
34         ed_size tlen = ARRAY_SIZE(tgt) - 1;
35
36         plan_tests(16);
37
38         ok1(edit_distance(src, slen, tgt, tlen, EDIT_DISTANCE_LCS) == 6.0);
39         ok1(edit_distance(src, slen, tgt, tlen, EDIT_DISTANCE_LEV) == 4.0);
40         ok1(edit_distance(src, slen, tgt, tlen, EDIT_DISTANCE_RDL) == 3.0);
41         ok1(edit_distance(src, slen, tgt, tlen, EDIT_DISTANCE_DL) == 3.0);
42
43         /* Test empty strings work as expected */
44         ok1(edit_distance(src, slen, tgt, 0, EDIT_DISTANCE_LCS) == 4.0);
45         ok1(edit_distance(src, 0, tgt, tlen, EDIT_DISTANCE_LCS) == 4.0);
46
47         /* Test ED_HAS_ELEM works as expected */
48         ok1(edit_distance(L"c", 1, tgt, tlen, EDIT_DISTANCE_LCS) == 3.0);
49         ok1(edit_distance(L"z", 1, tgt, tlen, EDIT_DISTANCE_LCS) == 5.0);
50         ok1(edit_distance(src, slen, L"c", 1, EDIT_DISTANCE_LCS) == 3.0);
51         ok1(edit_distance(src, slen, L"z", 1, EDIT_DISTANCE_LCS) == 5.0);
52         ok1(edit_distance(L"z", 1, tgt, tlen, EDIT_DISTANCE_LEV) == 4.0);
53         ok1(edit_distance(src, slen, L"z", 1, EDIT_DISTANCE_LEV) == 4.0);
54         ok1(edit_distance(L"z", 1, tgt, tlen, EDIT_DISTANCE_RDL) == 4.0);
55         ok1(edit_distance(src, slen, L"z", 1, EDIT_DISTANCE_RDL) == 4.0);
56         ok1(edit_distance(L"z", 1, tgt, tlen, EDIT_DISTANCE_DL) == 4.0);
57         ok1(edit_distance(src, slen, L"z", 1, EDIT_DISTANCE_DL) == 4.0);
58
59         return exit_status();
60 }