bdelta: new module for binary diff/patch
[ccan] / ccan / bdelta / test / run-trivial.c
1 #include <ccan/bdelta/bdelta.h>
2 #include <ccan/bdelta/bdelta.c>
3 #include <ccan/tap/tap.h>
4
5 static int test_trivial(const char *old, const char *new_)
6 {
7         void *patch;
8         size_t patch_size;
9         BDELTAcode rc;
10         
11         void *new2;
12         size_t new2_size;
13         
14         rc = bdelta_diff(old, strlen(old), new_, strlen(new_), &patch, &patch_size);
15         if (rc != BDELTA_OK) {
16                 bdelta_perror("bdelta_diff", rc);
17                 return 0;
18         }
19         
20         if (patch_size > strlen(new_) + 1) {
21                 fprintf(stderr, "bdelta_diff produced a patch larger than a simple literal emitting the new string.\n");
22                 return 0;
23         }
24         
25         rc = bdelta_patch(old, strlen(old), patch, patch_size, &new2, &new2_size);
26         if (rc != BDELTA_OK) {
27                 bdelta_perror("bdelta_patch", rc);
28                 return 0;
29         }
30         
31         if (new2_size != strlen(new_) || strcmp(new2, new_) != 0) {
32                 fprintf(stderr, "patch(old, diff(old, new)) != new\n");
33                 return 0;
34         }
35         
36         /* Make sure bdelta_diff properly discards unwanted return values. */
37         rc = bdelta_diff(old, strlen(old), new_, strlen(new_), NULL, NULL);
38         if (rc != BDELTA_OK) {
39                 bdelta_perror("bdelta_diff (second time)", rc);
40                 return 0;
41         }
42         
43         free(new2);
44         free(patch);
45         return 1;
46 }
47
48 int main(void)
49 {
50         plan_tests(13);
51         
52         ok1(test_trivial("abcabba", "cbabac"));
53         ok1(test_trivial("aaabbbcdaabcc", "aaabbcdaabeca"));
54         ok1(test_trivial("aaaaaaaa", "bbbbbbbb"));
55         ok1(test_trivial("aaaaaaaa", ""));
56         ok1(test_trivial("", "bbbbbbbb"));
57         ok1(test_trivial("", ""));
58         ok1(test_trivial("aaaaaaaa", "aaaaaaaabbbbbbbb"));
59         ok1(test_trivial("aaaaaaaa", "bbbbbbbbaaaaaaaa"));
60         ok1(test_trivial("aaaaaaaabbbbbbbb", "aaaaaaaa"));
61         ok1(test_trivial("aaaaaaaabbbbbbbb", "bbbbbbbb"));
62         ok1(test_trivial("aaaaaaaabbbbbbbb", "bbbbbbbb"));
63         ok1(test_trivial("abababababababab", "babababababababa"));
64         ok1(test_trivial("aababcabcdabcde", "aababcabcdabcde"));
65         
66         return exit_status();
67 }