edit_distance: calculate edit distance between strings
[ccan] / ccan / edit_distance / test / run-types-struct.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
11 #include <ccan/array_size/array_size.h>
12 #include <ccan/tap/tap.h>
13
14 struct color16 {
15         unsigned char r:5;
16         unsigned char g:6;
17         unsigned char b:5;
18 };
19
20 #define ed_elem struct color16
21 #define ED_ELEM_EQUAL(e, f) (e.r == f.r && e.g == f.g && e.b == f.b)
22 #define ED_HASH_ELEM(e) ((e.r << 11) | (e.g << 5) | e.b)
23 #define ED_HASH_MAX USHRT_MAX
24
25 #include <ccan/edit_distance/edit_distance.c>
26 #include <ccan/edit_distance/edit_distance_dl.c>
27 #include <ccan/edit_distance/edit_distance_lcs.c>
28 #include <ccan/edit_distance/edit_distance_lev.c>
29 #include <ccan/edit_distance/edit_distance_rdl.c>
30
31 int main(void)
32 {
33         const struct color16 src[] = {
34                 {0, 0, 0},
35                 {1, 1, 1},
36                 {2, 2, 2},
37                 {3, 3, 3}
38         };
39         const struct color16 tgt[] = {
40                 {4, 4, 4},
41                 {2, 2, 2},
42                 {1, 1, 1},
43                 {5, 5, 5}
44         };
45         ed_size slen = ARRAY_SIZE(src);
46         ed_size tlen = ARRAY_SIZE(tgt);
47
48         plan_tests(4);
49
50         ok1(edit_distance(src, slen, tgt, tlen, EDIT_DISTANCE_LCS) == 6);
51         ok1(edit_distance(src, slen, tgt, tlen, EDIT_DISTANCE_LEV) == 4);
52         ok1(edit_distance(src, slen, tgt, tlen, EDIT_DISTANCE_RDL) == 3);
53         ok1(edit_distance(src, slen, tgt, tlen, EDIT_DISTANCE_DL) == 3);
54
55         return exit_status();
56 }