bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1 | /** @brief Unit-test for DRD's vector clock implementation. */ |
| 2 | |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include "drd/drd_vc.c" |
| 9 | |
| 10 | |
| 11 | /* Replacements for Valgrind core functionality. */ |
| 12 | |
| 13 | void* VG_(malloc)(HChar* cc, SizeT nbytes) |
| 14 | { return malloc(nbytes); } |
| 15 | void* VG_(realloc)(HChar* cc, void* p, SizeT size) |
| 16 | { return realloc(p, size); } |
| 17 | void VG_(free)(void* p) |
| 18 | { return free(p); } |
| 19 | void VG_(assert_fail)(Bool isCore, const Char* assertion, const Char* file, |
| 20 | Int line, const Char* function, const HChar* format, |
| 21 | ...) |
| 22 | { |
| 23 | fprintf(stderr, |
| 24 | "%s:%u: %s%sAssertion `%s' failed.\n", |
| 25 | file, |
| 26 | line, |
| 27 | function ? (char*)function : "", |
| 28 | function ? ": " : "", |
| 29 | assertion); |
| 30 | fflush(stdout); |
| 31 | fflush(stderr); |
| 32 | abort(); |
| 33 | } |
| 34 | |
| 35 | void* VG_(memset)(void *s, Int c, SizeT sz) |
| 36 | { return memset(s, c, sz); } |
| 37 | void* VG_(memcpy)(void *d, const void *s, SizeT sz) |
| 38 | { return memcpy(d, s, sz); } |
| 39 | Int VG_(memcmp)(const void* s1, const void* s2, SizeT n) |
| 40 | { return memcmp(s1, s2, n); } |
| 41 | UInt VG_(printf)(const HChar *format, ...) |
| 42 | { UInt ret; va_list vargs; va_start(vargs, format); ret = vprintf(format, vargs); va_end(vargs); return ret; } |
| 43 | UInt VG_(snprintf)(Char* buf, Int size, const HChar *format, ...) |
| 44 | { UInt ret; va_list vargs; va_start(vargs, format); ret = vsnprintf(buf, size, format, vargs); va_end(vargs); return ret; } |
| 45 | SizeT VG_(strlen)(const Char* str) { return strlen(str); } |
| 46 | UInt VG_(message)(VgMsgKind kind, const HChar* format, ...) |
| 47 | { UInt ret; va_list vargs; va_start(vargs, format); ret = vprintf(format, vargs); va_end(vargs); printf("\n"); return ret; } |
| 48 | Bool DRD_(is_suppressed)(const Addr a1, const Addr a2) |
| 49 | { assert(0); } |
| 50 | |
| 51 | |
| 52 | /* Actual unit test */ |
| 53 | |
| 54 | static void vc_unittest(void) |
| 55 | { |
| 56 | int i; |
| 57 | char *str; |
| 58 | VectorClock vc1; |
| 59 | VCElem vc1elem[] = { { 3, 7 }, { 5, 8 }, }; |
| 60 | VectorClock vc2; |
| 61 | VCElem vc2elem[] = { { 1, 4 }, { 3, 9 }, }; |
| 62 | VectorClock vc3; |
| 63 | VCElem vc4elem[] = { { 1, 3 }, { 2, 1 }, }; |
| 64 | VectorClock vc4; |
| 65 | VCElem vc5elem[] = { { 1, 4 }, }; |
| 66 | VectorClock vc5; |
| 67 | |
| 68 | DRD_(vc_init)(&vc1, vc1elem, sizeof(vc1elem)/sizeof(vc1elem[0])); |
| 69 | DRD_(vc_init)(&vc2, vc2elem, sizeof(vc2elem)/sizeof(vc2elem[0])); |
| 70 | DRD_(vc_init)(&vc3, 0, 0); |
| 71 | DRD_(vc_init)(&vc4, vc4elem, sizeof(vc4elem)/sizeof(vc4elem[0])); |
| 72 | DRD_(vc_init)(&vc5, vc5elem, sizeof(vc5elem)/sizeof(vc5elem[0])); |
| 73 | |
| 74 | DRD_(vc_combine)(&vc3, &vc1); |
| 75 | DRD_(vc_combine)(&vc3, &vc2); |
| 76 | |
| 77 | fprintf(stderr, "vc1: %s", (str = DRD_(vc_aprint)(&vc1))); |
| 78 | free(str); |
| 79 | fprintf(stderr, "\nvc2: %s", (str = DRD_(vc_aprint)(&vc2))); |
| 80 | free(str); |
| 81 | fprintf(stderr, "\nvc3: %s", (str = DRD_(vc_aprint)(&vc3))); |
| 82 | free(str); |
| 83 | fprintf(stderr, "\n"); |
| 84 | fprintf(stderr, "vc_lte(vc1, vc2) = %d, vc_lte(vc1, vc3) = %d," |
| 85 | " vc_lte(vc2, vc3) = %d\nvc_lte(", |
| 86 | DRD_(vc_lte)(&vc1, &vc2), DRD_(vc_lte)(&vc1, &vc3), |
| 87 | DRD_(vc_lte)(&vc2, &vc3)); |
| 88 | fprintf(stderr, "%s", (str = DRD_(vc_aprint)(&vc4))); |
| 89 | free(str); |
| 90 | fprintf(stderr, ", "); |
| 91 | fprintf(stderr, "%s", (str = DRD_(vc_aprint)(&vc5))); |
| 92 | free(str); |
| 93 | fprintf(stderr, ") = %d sw %d\n", |
| 94 | DRD_(vc_lte)(&vc4, &vc5), DRD_(vc_lte)(&vc5, &vc4)); |
| 95 | |
| 96 | for (i = 0; i < 64; i++) |
| 97 | DRD_(vc_reserve)(&vc1, i); |
| 98 | for (i = 64; i > 0; i--) |
| 99 | DRD_(vc_reserve)(&vc1, i); |
| 100 | |
| 101 | DRD_(vc_cleanup)(&vc1); |
| 102 | DRD_(vc_cleanup)(&vc2); |
| 103 | DRD_(vc_cleanup)(&vc3); |
| 104 | } |
| 105 | |
| 106 | int main(int argc, char** argv) |
| 107 | { |
| 108 | vc_unittest(); |
| 109 | return 0; |
| 110 | } |