1#include <malloc.h> 2#include <stdlib.h> 3#include <string.h> 4#include "../utils.h" 5 6#define SIZE 256 7#define ITERATIONS 10000 8 9int test_memcmp(const void *s1, const void *s2, size_t n); 10 11/* test all offsets and lengths */ 12static void test_one(char *s1, char *s2) 13{ 14 unsigned long offset, size; 15 16 for (offset = 0; offset < SIZE; offset++) { 17 for (size = 0; size < (SIZE-offset); size++) { 18 int x, y; 19 unsigned long i; 20 21 y = memcmp(s1+offset, s2+offset, size); 22 x = test_memcmp(s1+offset, s2+offset, size); 23 24 if (((x ^ y) < 0) && /* Trick to compare sign */ 25 ((x | y) != 0)) { /* check for zero */ 26 printf("memcmp returned %d, should have returned %d (offset %ld size %ld)\n", x, y, offset, size); 27 28 for (i = offset; i < offset+size; i++) 29 printf("%02x ", s1[i]); 30 printf("\n"); 31 32 for (i = offset; i < offset+size; i++) 33 printf("%02x ", s2[i]); 34 printf("\n"); 35 abort(); 36 } 37 } 38 } 39} 40 41static int testcase(void) 42{ 43 char *s1; 44 char *s2; 45 unsigned long i; 46 47 s1 = memalign(128, SIZE); 48 if (!s1) { 49 perror("memalign"); 50 exit(1); 51 } 52 53 s2 = memalign(128, SIZE); 54 if (!s2) { 55 perror("memalign"); 56 exit(1); 57 } 58 59 srandom(1); 60 61 for (i = 0; i < ITERATIONS; i++) { 62 unsigned long j; 63 unsigned long change; 64 65 for (j = 0; j < SIZE; j++) 66 s1[j] = random(); 67 68 memcpy(s2, s1, SIZE); 69 70 /* change one byte */ 71 change = random() % SIZE; 72 s2[change] = random() & 0xff; 73 74 test_one(s1, s2); 75 } 76 77 srandom(1); 78 79 for (i = 0; i < ITERATIONS; i++) { 80 unsigned long j; 81 unsigned long change; 82 83 for (j = 0; j < SIZE; j++) 84 s1[j] = random(); 85 86 memcpy(s2, s1, SIZE); 87 88 /* change multiple bytes, 1/8 of total */ 89 for (j = 0; j < SIZE / 8; j++) { 90 change = random() % SIZE; 91 s2[change] = random() & 0xff; 92 } 93 94 test_one(s1, s2); 95 } 96 97 return 0; 98} 99 100int main(void) 101{ 102 return test_harness(testcase, "memcmp"); 103} 104