#include #include "defs.h" int Expected_int = 10; /* Expected_int must be non-zero. */ check_int(actual, expected, in_struct) int actual, expected; char *in_struct; { if (actual != expected) { printf(" FAILURE: Integer%s not passed correctly. Got %d, wanted %d.\n", in_struct, actual, expected); } else { printf(" ==== Integer%s %d passed correctly\n", in_struct, actual); } } char *Expected_string = "Hello, world"; check_string(actual, expected, in_struct) char *actual, *expected; char *in_struct; { if ((char *)0 == actual) { printf(" FAILURE: String%s not passed correctly. Got null; wanted \"%s\".\n", in_struct, expected); } else if ('\0' != actual[strlen(expected)]) { printf(" FAILURE: String%s not passed correctly. Got garbage; wanted \"%s\".\n", in_struct, expected); } else if (strcmp(actual, expected)) { printf(" FAILURE: String%s not passed correctly. Got \"%s\"; wanted \"%s\".\n", in_struct, actual, expected); } else { printf(" ==== String%s \"%s\" passed correctly\n", in_struct, actual); } } double Expected_double = -44.3; /* Must be > -5000 */ check_double(actual, expected, in_struct) double actual, expected; char *in_struct; { if (actual != expected) { printf(" FAILURE: Double%s not passed correctly. Got %lf, wanted %lf.\n", in_struct, actual, expected); } else { printf(" ==== Double%s %lf passed correctly\n", in_struct, actual); } } /* First field must be non-zero, third must be > -5000. */ struct combo_t Expected_combo = { -33, "foo", 55.6}; /* Fields checked individually. */ expect_truth(value) int value; { if (! value) { printf(" FAILURE: Instrumented code returned wrong value.\n"); } } void variable(); /* Varargs routine, in stdarg.c or varargs.c */ void usage(me) char *me; { fprintf(stderr, "Usage: %s [-normal | -struct]\n", me); } main(argc, argv) int argc; char **argv; { if (argc != 2) { usage(argv[0]); exit(1); } if (!strcmp(argv[1], "-normal")) { /* * Try several permutations to increase chance of error. Make * sure each is first and last in list. */ printf("== Testing integers, strings, and doubles.\n"); printf("== Trial 1\n"); variable(DUMMY, PARAM_INT, Expected_int, Expected_int, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_STRING, Expected_string, Expected_string, PARAM_INT, Expected_int, Expected_int, PARAM_END); printf("== Trial 2\n"); variable(DUMMY, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_STRING, Expected_string, Expected_string, PARAM_INT, Expected_int, Expected_int, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_END); printf("== Trial 3\n"); variable(DUMMY, PARAM_STRING, Expected_string, Expected_string, PARAM_INT, Expected_int, Expected_int, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_STRING, Expected_string, Expected_string, PARAM_END); printf("== Trial 4\n"); /* Use a lot to overflow registers. */ variable(DUMMY, PARAM_INT, Expected_int, Expected_int, PARAM_STRING, Expected_string, Expected_string, PARAM_INT, Expected_int, Expected_int, PARAM_STRING, Expected_string, Expected_string, PARAM_INT, Expected_int, Expected_int, PARAM_INT, Expected_int, Expected_int, PARAM_STRING, Expected_string, Expected_string, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_STRING, Expected_string, Expected_string, PARAM_INT, Expected_int, Expected_int, PARAM_STRING, Expected_string, Expected_string, PARAM_STRING, Expected_string, Expected_string, PARAM_INT, Expected_int, Expected_int, PARAM_INT, Expected_int, Expected_int, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_STRING, Expected_string, Expected_string, PARAM_INT, Expected_int, Expected_int, PARAM_INT, Expected_int, Expected_int, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_STRING, Expected_string, Expected_string, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_INT, Expected_int, Expected_int, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_STRING, Expected_string, Expected_string, PARAM_INT, Expected_int, Expected_int, PARAM_STRING, Expected_string, Expected_string, PARAM_INT, Expected_int, Expected_int, PARAM_DOUBLE, Expected_double, Expected_double, PARAM_STRING, Expected_string, Expected_string, PARAM_STRING, Expected_string, Expected_string, PARAM_END); } else if (!strcmp(argv[1], "-struct")) { printf("== Testing structures.\n"); printf("== Trial 1\n"); variable(DUMMY, PARAM_INT, Expected_int, Expected_int, PARAM_COMBO, Expected_combo, Expected_combo, PARAM_INT, Expected_int, Expected_int, PARAM_END); printf("== Trial 2\n"); variable(DUMMY, PARAM_INT, Expected_int, Expected_int, PARAM_COMBO, Expected_combo, Expected_combo, PARAM_END); printf("== Trial 3\n"); variable(DUMMY, PARAM_COMBO, Expected_combo, Expected_combo, PARAM_INT, Expected_int, Expected_int, PARAM_END); } else { usage(argv[0]); exit(1); } exit(0); }