| 1 | /* Copyright (C) 2011 Omnibond, LLC
|
|---|
| 2 | Client testing - support routines */
|
|---|
| 3 |
|
|---|
| 4 | #include <stdlib.h>
|
|---|
| 5 | #include <stdio.h>
|
|---|
| 6 | #include <string.h>
|
|---|
| 7 |
|
|---|
| 8 | #include "test-support.h"
|
|---|
| 9 |
|
|---|
| 10 | const char *ops[] = {"=", "<>", "<", ">", "=<", ">="};
|
|---|
| 11 |
|
|---|
| 12 | /* allocate and concatenate a string */
|
|---|
| 13 | char *quickcat(const char *str1, const char *str2)
|
|---|
| 14 | {
|
|---|
| 15 | char *str;
|
|---|
| 16 |
|
|---|
| 17 | str = (char *) malloc(strlen(str1) + strlen(str2) + 1);
|
|---|
| 18 |
|
|---|
| 19 | sprintf(str, "%s%s", str1, str2);
|
|---|
| 20 |
|
|---|
| 21 | return str;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | /* generate a random dir name... caller must free */
|
|---|
| 25 | char *randdir(const char *root)
|
|---|
| 26 | {
|
|---|
| 27 | char *slashdir, name[16], *dir;
|
|---|
| 28 |
|
|---|
| 29 | if (!root)
|
|---|
| 30 | return NULL;
|
|---|
| 31 |
|
|---|
| 32 | /* append trailing slash */
|
|---|
| 33 | if (strlen(root) > 0 && root[strlen(root)-1] != '\\')
|
|---|
| 34 | slashdir = quickcat(root, "\\");
|
|---|
| 35 | else
|
|---|
| 36 | slashdir = _strdup(root);
|
|---|
| 37 |
|
|---|
| 38 | /* generate random dir name */
|
|---|
| 39 | sprintf(name, "dir%05d", rand());
|
|---|
| 40 |
|
|---|
| 41 | dir = quickcat(slashdir, name);
|
|---|
| 42 |
|
|---|
| 43 | free(slashdir);
|
|---|
| 44 |
|
|---|
| 45 | return dir;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /* generate a random file name... caller must free */
|
|---|
| 49 | char *randfile(const char *root)
|
|---|
| 50 | {
|
|---|
| 51 | char *slashdir, name[16], *file;
|
|---|
| 52 |
|
|---|
| 53 | if (!root)
|
|---|
| 54 | return NULL;
|
|---|
| 55 |
|
|---|
| 56 | /* append trailing slash */
|
|---|
| 57 | if (strlen(root) > 0 && root[strlen(root)-1] != '\\')
|
|---|
| 58 | slashdir = quickcat(root, "\\");
|
|---|
| 59 | else
|
|---|
| 60 | slashdir = _strdup(root);
|
|---|
| 61 |
|
|---|
| 62 | /* generate random dir name */
|
|---|
| 63 | sprintf(name, "f%05d.tst", rand());
|
|---|
| 64 |
|
|---|
| 65 | file = quickcat(slashdir, name);
|
|---|
| 66 |
|
|---|
| 67 | free(slashdir);
|
|---|
| 68 |
|
|---|
| 69 | return file;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | void report_error(op_options *options,
|
|---|
| 73 | const char *msg)
|
|---|
| 74 | {
|
|---|
| 75 | /* report to console and/or file */
|
|---|
| 76 | if (options->report_flags & REPORT_CONSOLE)
|
|---|
| 77 | printf("%s\n", msg);
|
|---|
| 78 |
|
|---|
| 79 | if (options->report_flags & REPORT_FILE)
|
|---|
| 80 | {
|
|---|
| 81 | fprintf(options->freport, "%s\n", msg);
|
|---|
| 82 | fflush(options->freport);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void report_result(op_options *options,
|
|---|
| 88 | const char *test_name,
|
|---|
| 89 | int expected_result,
|
|---|
| 90 | int expected_code,
|
|---|
| 91 | int code_operation,
|
|---|
| 92 | int actual_code)
|
|---|
| 93 | {
|
|---|
| 94 | int comp, ret;
|
|---|
| 95 | char *ok_str, *line;
|
|---|
| 96 | size_t line_size = 0;
|
|---|
| 97 |
|
|---|
| 98 | /* determine result of test */
|
|---|
| 99 | switch (code_operation)
|
|---|
| 100 | {
|
|---|
| 101 | case OPER_EQUAL:
|
|---|
| 102 | comp = actual_code == expected_code;
|
|---|
| 103 | break;
|
|---|
| 104 | case OPER_NOTEQUAL:
|
|---|
| 105 | comp = actual_code != expected_code;
|
|---|
| 106 | break;
|
|---|
| 107 | case OPER_LESS:
|
|---|
| 108 | comp = actual_code < expected_code;
|
|---|
| 109 | break;
|
|---|
| 110 | case OPER_GREATER:
|
|---|
| 111 | comp = actual_code > expected_code;
|
|---|
| 112 | break;
|
|---|
| 113 | case OPER_LESSOREQUAL:
|
|---|
| 114 | comp = actual_code <= expected_code;
|
|---|
| 115 | break;
|
|---|
| 116 | case OPER_GREATEROREQUAL:
|
|---|
| 117 | comp = actual_code >= expected_code;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | ok_str = comp ? "OK" : "NOT_OK";
|
|---|
| 121 |
|
|---|
| 122 | /* determine size of line buffer */
|
|---|
| 123 | line_size += strlen(test_name);
|
|---|
| 124 | line_size += 14; /* EXPECT_SUCCESS */
|
|---|
| 125 | line_size += 11; /* max decimal size -- actual_code */
|
|---|
| 126 | line_size += 2; /* operator */
|
|---|
| 127 | line_size += 11; /* expected_code */
|
|---|
| 128 | line_size += 6; /* NOT_OK */
|
|---|
| 129 | line_size += 6; /* spaces & null */
|
|---|
| 130 |
|
|---|
| 131 | line = (char *) malloc(line_size);
|
|---|
| 132 |
|
|---|
| 133 | /* print to buffer */
|
|---|
| 134 | ret = _snprintf(line, line_size, "%s %s %d %2s %d %s",
|
|---|
| 135 | test_name,
|
|---|
| 136 | (expected_result == RESULT_SUCCESS) ? "EXPECT_SUCCESS" : "EXPECT_FAILURE",
|
|---|
| 137 | actual_code, ops[code_operation], expected_code,
|
|---|
| 138 | ok_str);
|
|---|
| 139 | if (ret < 0)
|
|---|
| 140 | sprintf(line, "LINE BUFFER OVERFLOW");
|
|---|
| 141 |
|
|---|
| 142 | /* report to console and/or file */
|
|---|
| 143 | if (options->report_flags & REPORT_CONSOLE)
|
|---|
| 144 | printf("%s\n", line);
|
|---|
| 145 |
|
|---|
| 146 | if (options->report_flags & REPORT_FILE)
|
|---|
| 147 | {
|
|---|
| 148 | fprintf(options->freport, "%s\n", line);
|
|---|
| 149 | fflush(options->freport);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | free(line);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|