Blob


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