Call make always with CFLAGS in git.spec
[git] / test-delta.c
1 /*
2  * test-delta.c: test code to exercise diff-delta.c and patch-delta.c
3  *
4  * (C) 2005 Nicolas Pitre <nico@cam.org>
5  *
6  * This code is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include "git-compat-util.h"
12 #include "delta.h"
13
14 static const char usage[] =
15         "test-delta (-d|-p) <from_file> <data_file> <out_file>";
16
17 int main(int argc, char *argv[])
18 {
19         int fd;
20         struct stat st;
21         void *from_buf, *data_buf, *out_buf;
22         unsigned long from_size, data_size, out_size;
23
24         if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) {
25                 fprintf(stderr, "Usage: %s\n", usage);
26                 return 1;
27         }
28
29         fd = open(argv[2], O_RDONLY);
30         if (fd < 0 || fstat(fd, &st)) {
31                 perror(argv[2]);
32                 return 1;
33         }
34         from_size = st.st_size;
35         from_buf = mmap(NULL, from_size, PROT_READ, MAP_PRIVATE, fd, 0);
36         if (from_buf == MAP_FAILED) {
37                 perror(argv[2]);
38                 close(fd);
39                 return 1;
40         }
41         close(fd);
42
43         fd = open(argv[3], O_RDONLY);
44         if (fd < 0 || fstat(fd, &st)) {
45                 perror(argv[3]);
46                 return 1;
47         }
48         data_size = st.st_size;
49         data_buf = mmap(NULL, data_size, PROT_READ, MAP_PRIVATE, fd, 0);
50         if (data_buf == MAP_FAILED) {
51                 perror(argv[3]);
52                 close(fd);
53                 return 1;
54         }
55         close(fd);
56
57         if (argv[1][1] == 'd')
58                 out_buf = diff_delta(from_buf, from_size,
59                                      data_buf, data_size,
60                                      &out_size, 0);
61         else
62                 out_buf = patch_delta(from_buf, from_size,
63                                       data_buf, data_size,
64                                       &out_size);
65         if (!out_buf) {
66                 fprintf(stderr, "delta operation failed (returned NULL)\n");
67                 return 1;
68         }
69
70         fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
71         if (fd < 0 || write_in_full(fd, out_buf, out_size) != out_size) {
72                 perror(argv[4]);
73                 return 1;
74         }
75
76         return 0;
77 }