1*59c8e88eSDag-Erling Smørgrav #include <stdio.h>
2*59c8e88eSDag-Erling Smørgrav #include <stdint.h>
3*59c8e88eSDag-Erling Smørgrav #include <string.h>
4*59c8e88eSDag-Erling Smørgrav #include <stdbool.h>
5*59c8e88eSDag-Erling Smørgrav #include <stdlib.h>
6*59c8e88eSDag-Erling Smørgrav
7*59c8e88eSDag-Erling Smørgrav #include <arraylist.h>
8*59c8e88eSDag-Erling Smørgrav #include <diff_main.h>
9*59c8e88eSDag-Erling Smørgrav
10*59c8e88eSDag-Erling Smørgrav #include <diff_internal.h>
11*59c8e88eSDag-Erling Smørgrav #include <diff_debug.h>
12*59c8e88eSDag-Erling Smørgrav
test_minus_after_plus(void)13*59c8e88eSDag-Erling Smørgrav void test_minus_after_plus(void)
14*59c8e88eSDag-Erling Smørgrav {
15*59c8e88eSDag-Erling Smørgrav struct diff_result *result = malloc(sizeof(struct diff_result));
16*59c8e88eSDag-Erling Smørgrav struct diff_data d_left, d_right;
17*59c8e88eSDag-Erling Smørgrav char *left_data = "a\nb\nc\nd\ne\nm\nn\n";
18*59c8e88eSDag-Erling Smørgrav char *right_data = "a\nb\nj\nk\nl\nm\nn\n";
19*59c8e88eSDag-Erling Smørgrav int i;
20*59c8e88eSDag-Erling Smørgrav
21*59c8e88eSDag-Erling Smørgrav printf("\n--- %s()\n", __func__);
22*59c8e88eSDag-Erling Smørgrav
23*59c8e88eSDag-Erling Smørgrav d_left = (struct diff_data){
24*59c8e88eSDag-Erling Smørgrav .data = left_data,
25*59c8e88eSDag-Erling Smørgrav .len = strlen(left_data),
26*59c8e88eSDag-Erling Smørgrav .root = &d_left,
27*59c8e88eSDag-Erling Smørgrav };
28*59c8e88eSDag-Erling Smørgrav d_right = (struct diff_data){
29*59c8e88eSDag-Erling Smørgrav .data = right_data,
30*59c8e88eSDag-Erling Smørgrav .len = strlen(right_data),
31*59c8e88eSDag-Erling Smørgrav .root = &d_right,
32*59c8e88eSDag-Erling Smørgrav };
33*59c8e88eSDag-Erling Smørgrav *result = (struct diff_result) {
34*59c8e88eSDag-Erling Smørgrav .left = &d_left,
35*59c8e88eSDag-Erling Smørgrav .right = &d_right,
36*59c8e88eSDag-Erling Smørgrav };
37*59c8e88eSDag-Erling Smørgrav
38*59c8e88eSDag-Erling Smørgrav diff_atomize_text_by_line(NULL, result->left);
39*59c8e88eSDag-Erling Smørgrav diff_atomize_text_by_line(NULL, result->right);
40*59c8e88eSDag-Erling Smørgrav
41*59c8e88eSDag-Erling Smørgrav struct diff_state state = {
42*59c8e88eSDag-Erling Smørgrav .result = result,
43*59c8e88eSDag-Erling Smørgrav .recursion_depth_left = 32,
44*59c8e88eSDag-Erling Smørgrav };
45*59c8e88eSDag-Erling Smørgrav diff_data_init_subsection(&state.left, result->left,
46*59c8e88eSDag-Erling Smørgrav result->left->atoms.head,
47*59c8e88eSDag-Erling Smørgrav result->left->atoms.len);
48*59c8e88eSDag-Erling Smørgrav diff_data_init_subsection(&state.right, result->right,
49*59c8e88eSDag-Erling Smørgrav result->right->atoms.head,
50*59c8e88eSDag-Erling Smørgrav result->right->atoms.len);
51*59c8e88eSDag-Erling Smørgrav
52*59c8e88eSDag-Erling Smørgrav /* "same" section */
53*59c8e88eSDag-Erling Smørgrav diff_state_add_chunk(&state, true,
54*59c8e88eSDag-Erling Smørgrav &state.left.atoms.head[0], 2,
55*59c8e88eSDag-Erling Smørgrav &state.right.atoms.head[0], 2);
56*59c8e88eSDag-Erling Smørgrav
57*59c8e88eSDag-Erling Smørgrav /* "plus" section */
58*59c8e88eSDag-Erling Smørgrav diff_state_add_chunk(&state, true,
59*59c8e88eSDag-Erling Smørgrav &state.left.atoms.head[2], 0,
60*59c8e88eSDag-Erling Smørgrav &state.right.atoms.head[2], 3);
61*59c8e88eSDag-Erling Smørgrav
62*59c8e88eSDag-Erling Smørgrav /* "minus" section */
63*59c8e88eSDag-Erling Smørgrav diff_state_add_chunk(&state, true,
64*59c8e88eSDag-Erling Smørgrav &state.left.atoms.head[2], 3,
65*59c8e88eSDag-Erling Smørgrav &state.right.atoms.head[5], 0);
66*59c8e88eSDag-Erling Smørgrav
67*59c8e88eSDag-Erling Smørgrav /* "same" section */
68*59c8e88eSDag-Erling Smørgrav diff_state_add_chunk(&state, true,
69*59c8e88eSDag-Erling Smørgrav &state.left.atoms.head[5], 2,
70*59c8e88eSDag-Erling Smørgrav &state.right.atoms.head[5], 2);
71*59c8e88eSDag-Erling Smørgrav
72*59c8e88eSDag-Erling Smørgrav for (i = 0; i < result->chunks.len; i++) {
73*59c8e88eSDag-Erling Smørgrav struct diff_chunk *c = &result->chunks.head[i];
74*59c8e88eSDag-Erling Smørgrav enum diff_chunk_type t = diff_chunk_type(c);
75*59c8e88eSDag-Erling Smørgrav
76*59c8e88eSDag-Erling Smørgrav printf("[%d] %s lines L%d R%d @L %lld @R %lld\n",
77*59c8e88eSDag-Erling Smørgrav i, (t == CHUNK_MINUS ? "minus" :
78*59c8e88eSDag-Erling Smørgrav (t == CHUNK_PLUS ? "plus" :
79*59c8e88eSDag-Erling Smørgrav (t == CHUNK_SAME ? "same" : "?"))),
80*59c8e88eSDag-Erling Smørgrav c->left_count,
81*59c8e88eSDag-Erling Smørgrav c->right_count,
82*59c8e88eSDag-Erling Smørgrav (long long)(c->left_start ? diff_atom_root_idx(result->left, c->left_start) : -1LL),
83*59c8e88eSDag-Erling Smørgrav (long long)(c->right_start ? diff_atom_root_idx(result->right, c->right_start) : -1LL));
84*59c8e88eSDag-Erling Smørgrav }
85*59c8e88eSDag-Erling Smørgrav
86*59c8e88eSDag-Erling Smørgrav diff_result_free(result);
87*59c8e88eSDag-Erling Smørgrav diff_data_free(&d_left);
88*59c8e88eSDag-Erling Smørgrav diff_data_free(&d_right);
89*59c8e88eSDag-Erling Smørgrav }
90*59c8e88eSDag-Erling Smørgrav
test_plus_after_plus(void)91*59c8e88eSDag-Erling Smørgrav void test_plus_after_plus(void)
92*59c8e88eSDag-Erling Smørgrav {
93*59c8e88eSDag-Erling Smørgrav struct diff_result *result = malloc(sizeof(struct diff_result));
94*59c8e88eSDag-Erling Smørgrav struct diff_data d_left, d_right;
95*59c8e88eSDag-Erling Smørgrav char *left_data = "a\nb\nc\nd\ne\nm\nn\n";
96*59c8e88eSDag-Erling Smørgrav char *right_data = "a\nb\nj\nk\nl\nm\nn\n";
97*59c8e88eSDag-Erling Smørgrav struct diff_chunk *c;
98*59c8e88eSDag-Erling Smørgrav
99*59c8e88eSDag-Erling Smørgrav printf("\n--- %s()\n", __func__);
100*59c8e88eSDag-Erling Smørgrav
101*59c8e88eSDag-Erling Smørgrav d_left = (struct diff_data){
102*59c8e88eSDag-Erling Smørgrav .data = left_data,
103*59c8e88eSDag-Erling Smørgrav .len = strlen(left_data),
104*59c8e88eSDag-Erling Smørgrav .root = &d_left,
105*59c8e88eSDag-Erling Smørgrav };
106*59c8e88eSDag-Erling Smørgrav d_right = (struct diff_data){
107*59c8e88eSDag-Erling Smørgrav .data = right_data,
108*59c8e88eSDag-Erling Smørgrav .len = strlen(right_data),
109*59c8e88eSDag-Erling Smørgrav .root = &d_right,
110*59c8e88eSDag-Erling Smørgrav };
111*59c8e88eSDag-Erling Smørgrav *result = (struct diff_result) {
112*59c8e88eSDag-Erling Smørgrav .left = &d_left,
113*59c8e88eSDag-Erling Smørgrav .right = &d_right,
114*59c8e88eSDag-Erling Smørgrav };
115*59c8e88eSDag-Erling Smørgrav
116*59c8e88eSDag-Erling Smørgrav diff_atomize_text_by_line(NULL, result->left);
117*59c8e88eSDag-Erling Smørgrav diff_atomize_text_by_line(NULL, result->right);
118*59c8e88eSDag-Erling Smørgrav
119*59c8e88eSDag-Erling Smørgrav struct diff_state state = {
120*59c8e88eSDag-Erling Smørgrav .result = result,
121*59c8e88eSDag-Erling Smørgrav .recursion_depth_left = 32,
122*59c8e88eSDag-Erling Smørgrav };
123*59c8e88eSDag-Erling Smørgrav diff_data_init_subsection(&state.left, result->left,
124*59c8e88eSDag-Erling Smørgrav result->left->atoms.head,
125*59c8e88eSDag-Erling Smørgrav result->left->atoms.len);
126*59c8e88eSDag-Erling Smørgrav diff_data_init_subsection(&state.right, result->right,
127*59c8e88eSDag-Erling Smørgrav result->right->atoms.head,
128*59c8e88eSDag-Erling Smørgrav result->right->atoms.len);
129*59c8e88eSDag-Erling Smørgrav
130*59c8e88eSDag-Erling Smørgrav /* "same" section */
131*59c8e88eSDag-Erling Smørgrav diff_state_add_chunk(&state, true,
132*59c8e88eSDag-Erling Smørgrav &state.left.atoms.head[0], 2,
133*59c8e88eSDag-Erling Smørgrav &state.right.atoms.head[0], 2);
134*59c8e88eSDag-Erling Smørgrav
135*59c8e88eSDag-Erling Smørgrav /* "minus" section */
136*59c8e88eSDag-Erling Smørgrav diff_state_add_chunk(&state, true,
137*59c8e88eSDag-Erling Smørgrav &state.left.atoms.head[2], 3,
138*59c8e88eSDag-Erling Smørgrav &state.right.atoms.head[2], 0);
139*59c8e88eSDag-Erling Smørgrav
140*59c8e88eSDag-Erling Smørgrav /* "plus" section */
141*59c8e88eSDag-Erling Smørgrav diff_state_add_chunk(&state, true,
142*59c8e88eSDag-Erling Smørgrav &state.left.atoms.head[5], 0,
143*59c8e88eSDag-Erling Smørgrav &state.right.atoms.head[2], 1);
144*59c8e88eSDag-Erling Smørgrav /* "plus" section */
145*59c8e88eSDag-Erling Smørgrav diff_state_add_chunk(&state, true,
146*59c8e88eSDag-Erling Smørgrav &state.left.atoms.head[5], 0,
147*59c8e88eSDag-Erling Smørgrav &state.right.atoms.head[3], 2);
148*59c8e88eSDag-Erling Smørgrav
149*59c8e88eSDag-Erling Smørgrav /* "same" section */
150*59c8e88eSDag-Erling Smørgrav diff_state_add_chunk(&state, true,
151*59c8e88eSDag-Erling Smørgrav &state.left.atoms.head[5], 2,
152*59c8e88eSDag-Erling Smørgrav &state.right.atoms.head[5], 2);
153*59c8e88eSDag-Erling Smørgrav
154*59c8e88eSDag-Erling Smørgrav ARRAYLIST_FOREACH(c, result->chunks) {
155*59c8e88eSDag-Erling Smørgrav enum diff_chunk_type t = diff_chunk_type(c);
156*59c8e88eSDag-Erling Smørgrav
157*59c8e88eSDag-Erling Smørgrav printf("[%lu] %s lines L%d R%d @L %lld @R %lld\n",
158*59c8e88eSDag-Erling Smørgrav (unsigned long)ARRAYLIST_IDX(c, result->chunks),
159*59c8e88eSDag-Erling Smørgrav (t == CHUNK_MINUS ? "minus" :
160*59c8e88eSDag-Erling Smørgrav (t == CHUNK_PLUS ? "plus" :
161*59c8e88eSDag-Erling Smørgrav (t == CHUNK_SAME ? "same" : "?"))),
162*59c8e88eSDag-Erling Smørgrav c->left_count,
163*59c8e88eSDag-Erling Smørgrav c->right_count,
164*59c8e88eSDag-Erling Smørgrav (long long)(c->left_start ? diff_atom_root_idx(result->left, c->left_start) : -1LL),
165*59c8e88eSDag-Erling Smørgrav (long long)(c->right_start ? diff_atom_root_idx(result->right, c->right_start) : -1LL));
166*59c8e88eSDag-Erling Smørgrav }
167*59c8e88eSDag-Erling Smørgrav
168*59c8e88eSDag-Erling Smørgrav diff_result_free(result);
169*59c8e88eSDag-Erling Smørgrav diff_data_free(&d_left);
170*59c8e88eSDag-Erling Smørgrav diff_data_free(&d_right);
171*59c8e88eSDag-Erling Smørgrav }
172*59c8e88eSDag-Erling Smørgrav
main(void)173*59c8e88eSDag-Erling Smørgrav int main(void)
174*59c8e88eSDag-Erling Smørgrav {
175*59c8e88eSDag-Erling Smørgrav test_minus_after_plus();
176*59c8e88eSDag-Erling Smørgrav test_plus_after_plus();
177*59c8e88eSDag-Erling Smørgrav return 0;
178*59c8e88eSDag-Erling Smørgrav }
179