1 #include <r_diff.h>
2 
cb(struct r_diff_t * d,void * user,struct r_diff_op_t * op)3 int cb(struct r_diff_t *d, void *user, struct r_diff_op_t *op)
4 {
5 	int i;
6 
7 	printf(" 0x%08"PFMT64x"  ", op->a_off);
8 	for(i = 0;i<op->a_len;i++)
9 		printf("%02x", op->a_buf[i]);
10 	printf(" => ");
11 	for(i = 0;i<op->b_len;i++)
12 		printf("%02x", op->b_buf[i]);
13 	printf("  0x%08"PFMT64x"\n", op->b_off);
14 	return 1;
15 }
16 
test_equal()17 int test_equal()
18 {
19 	struct r_diff_t *d;
20 	char *bufa = "helloworld";
21 	char *bufb = "heprswarld";
22 
23 	printf("Diffing '%s' vs '%s'\n", bufa, bufb);
24 	d = r_diff_new (0, 0);
25 	r_diff_set_delta(d, 0);
26 	r_diff_set_callback(d, &cb, NULL);
27 	r_diff_buffers(d, (ut8*)bufa, strlen(bufa), (ut8*)bufb, strlen((char*)bufb));
28 	return 1;
29 }
30 
test_diff()31 int test_diff()
32 {
33 	struct r_diff_t *d;
34 	char *bufa = "hello";
35 	char *bufb = "hellpworld";
36 
37 	printf("Truncated diffing '%s' vs '%s'\n", bufa, bufb);
38 	d = r_diff_new (0, 0);
39 	r_diff_set_delta(d, 0);
40 	r_diff_set_callback(d, &cb, NULL);
41 	r_diff_buffers(d, (ut8*)bufa, strlen(bufa), (ut8*)bufb, strlen(bufb));
42 	return 1;
43 }
44 
test_delta()45 int test_delta()
46 {
47 	struct r_diff_t *d;
48 	char *bufa = "hello";
49 	char *bufb = "heprpworld";
50 
51 	printf("Delta diffing '%s' vs '%s'\n", bufa, bufb);
52 	d = r_diff_new(0, 0);
53 	r_diff_set_delta(d, 1);
54 	r_diff_set_callback(d, &cb, NULL);
55 	r_diff_buffers(d, (ut8*)bufa, strlen(bufa), (ut8*)bufb, strlen(bufb));
56 	return 1;
57 }
58 
test_distance()59 int test_distance()
60 {
61 	char *bufa = "hello";
62 	char *bufb = "heprpworld";
63 	ut32 distance = 0;
64 	double similarity = 0;
65 
66 	printf("Similarity: '%s' vs '%s'\n", bufa, bufb);
67 	r_diff_buffers_distance(NULL, (ut8*)bufa, strlen(bufa), (ut8*)bufb, strlen(bufb),
68 		&distance, &similarity);
69 	printf("Levenshtein distance = %i\nSimilarity = %f\n",
70 			distance, similarity);
71 	return 1;
72 }
73 
74 #if 0
75 int test_lines(char *file1, char *file2)
76 {
77 	int ret;
78 	char *b1, *b2;
79 	int s1, s2;
80 
81 	b1 = r_file_slurp(file1, &s1);
82 	b2 = r_file_slurp(file2, &s2);
83 	if (b1==NULL || b2 == NULL) {
84 		eprintf ("Cannot open %s or %s\n", file1, file2);
85 		return 0;
86 	}
87 	ret = r_diff_lines(file1, b1, s1, file2, b2, s2);
88 	printf("Differences: %d\n", ret);
89 	return ret;
90 }
91 #endif
92 
main()93 int main()
94 {
95 	test_equal();
96 	printf("--\n");
97 	test_equal();
98 	printf("--\n");
99 	test_diff();
100 	printf("--\n");
101 	test_delta();
102 	printf("--\n");
103 	test_distance();
104 	printf("--\n");
105 //	test_lines("file1", "file2");
106 
107 
108 	return 0;
109 }
110