Changeset 6761 for branches/refactor/UNIT_TESTER/test_unit.h
- Timestamp:
- 04/09/10 14:42:21 (21 months ago)
- Files:
-
- 1 modified
-
branches/refactor/UNIT_TESTER/test_unit.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/refactor/UNIT_TESTER/test_unit.h
r6760 r6761 192 192 193 193 inline bool files_are_equal(const char *file1, const char *file2) { 194 const char *error = NULL; 195 FILE *fp1 = fopen(file1, "rb"); 194 const char *error = NULL; 195 FILE *fp1 = fopen(file1, "rb"); 196 FlushedOutput yes; 196 197 197 198 if (!fp1) { … … 206 207 } 207 208 else { 208 const int BLOCKSIZE = 4096; 209 char *buf1 = (char*)malloc(BLOCKSIZE); 210 char *buf2 = (char*)malloc(BLOCKSIZE); 209 const int BLOCKSIZE = 4096; 210 unsigned char *buf1 = (unsigned char*)malloc(BLOCKSIZE); 211 unsigned char *buf2 = (unsigned char*)malloc(BLOCKSIZE); 212 int equal_bytes = 0; 213 bool repositioned = false; 211 214 212 215 while (!error) { 213 int read1 = fread(buf1, 1, BLOCKSIZE, fp1); 214 int read2 = fread(buf2, 1, BLOCKSIZE, fp2); 215 216 if (read1 != read2) error = "filesizes differ"; 216 int read1 = fread(buf1, 1, BLOCKSIZE, fp1); 217 int read2 = fread(buf2, 1, BLOCKSIZE, fp2); 218 int common = read1<read2 ? read1 : read2; 219 220 if (!common) { 221 if (read1 != read2) error = "filesize differs"; 222 break; 223 } 224 225 if (memcmp(buf1, buf2, common) == 0) { 226 equal_bytes += common; 227 } 217 228 else { 218 if (!read1) break; // done 219 if (memcmp(buf1, buf2, read1) != 0) error = "content differs"; 229 int x = 0; 230 while (buf1[x] == buf2[x]) { 231 x++; 232 equal_bytes++; 233 } 234 error = "content differs"; 235 236 // x is the position inside the current block 237 const int DUMP = 7; 238 int y1 = x >= DUMP ? x-DUMP : 0; 239 int y2 = (x+DUMP)>common ? common : (x+DUMP); 240 int blockstart = equal_bytes-x; 241 242 for (int y = y1; y <= y2; y++) { 243 fprintf(stderr, "[0x%04x]", blockstart+y); 244 print_pair(buf1[y], buf2[y]); 245 fputc(' ', stderr); 246 print_hex_pair(buf1[y], buf2[y]); 247 if (x == y) fputs(" <- diff", stderr); 248 fputc('\n', stderr); 249 } 250 if (y2 == common) { 251 fputs("[end of block - truncated]\n", stderr); 252 } 220 253 } 221 254 } 255 256 if (error) FlushedOutput::printf("files_are_equal: equal_bytes=%i\n", equal_bytes); 257 test_assert(error || equal_bytes); // comparing empty files is nonsense 258 222 259 free(buf2); 223 260 free(buf1); … … 403 440 #define TEST_ASSERT_SIMILAR__BROKEN(t1,t2,epsilon) TEST_ASSERT__BROKEN(arb_test::is_similar(t1, t2, epsilon)) 404 441 405 #define TEST_ASSERT_FILES_EQUAL(f1,f2) TEST_ASSERT(arb_test::files_are_equal(f1,f2)) 442 #define TEST_ASSERT_FILES_EQUAL(f1,f2) TEST_ASSERT(arb_test::files_are_equal(f1,f2)) 443 #define TEST_ASSERT_FILES_EQUAL__BROKEN(f1,f2) TEST_ASSERT__BROKEN(arb_test::files_are_equal(f1,f2)) 406 444 407 445 #else
