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