Hash :
f990b98e
Author :
Thomas de Grivel
Date :
2022-12-06T13:37:43
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/* c3
* Copyright 2022 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software excepted
* on Apple computers granted the above copyright notice and
* this permission paragraph are included in all copies and
* substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include "test.h"
#define TARGET_NAME_MAX 32
#define TARGETS_MAX 1000
const char *g_test_context = NULL;
long g_test_count = 0;
long g_test_ko = 0;
long g_test_last_ok = 0;
long g_test_ok = 0;
const char *g_test_targets_default[] = {NULL};
char g_test_targets_env[TARGETS_MAX * TARGET_NAME_MAX];
char *g_test_targets_env_v[TARGETS_MAX + 1];
const char **g_test_targets = {NULL};
void test_context (const char *context)
{
g_test_context = context;
}
int test_file_compare (const char *path_a, const char *path_b)
{
FILE *fp_a = fopen(path_a, "rb");
FILE *fp_b = fopen(path_b, "rb");
char buf_a[1024];
char buf_b[1024];
int r;
if (fp_a == fp_b)
return 0;
if (fp_a == NULL)
return -1;
if (fp_b == NULL)
return 1;
/* TODO: use fread and check how many bytes are read */
while (fgets(buf_a, 1024, fp_a) != NULL) {
if (fgets(buf_b, 1024, fp_b) == NULL) {
test_ko();
return 1;
}
/* TODO: use memcmp for NUL bytes */
if ((r = strcmp(buf_a, buf_b))) {
test_ko();
return r;
}
}
if (fgets(buf_b, 1024, fp_b) != NULL) {
test_ko();
return -1;
}
test_ok();
return 0;
}
void test_init (int argc, char **argv)
{
const char **t;
if (argv[argc] != NULL)
err(1, "argv[argc] != NULL");
g_test_context = NULL;
g_test_count = 0;
g_test_ko = 0;
g_test_last_ok = 0;
g_test_ok = 0;
g_test_targets = g_test_targets_default;
if (argc > 1) {
g_test_targets = (const char **) argv + 1;
}
else {
const char *env_target;
env_target = getenv("C3_TEST_TARGET");
if (env_target) {
char **ap;
size_t len = 0;
char *target;
len = strlen(env_target);
target = g_test_targets_env;
if (len + 1 > sizeof(g_test_targets_env))
err(1, "too many targets");
memcpy(target, env_target, len + 1);
ap = g_test_targets_env_v;
while (ap < g_test_targets_env_v + TARGETS_MAX &&
(*ap = strsep(&target, " \t")) != NULL)
if (**ap != '\0')
ap++;
*ap = NULL;
g_test_targets = (const char **) g_test_targets_env_v;
}
}
printf("%s", argv[0]);
t = g_test_targets;
while (*t) {
printf(" %s", *t);
t++;
}
printf("\n");
}
void test_ko ()
{
g_test_count++;
g_test_ko++;
fprintf(stderr, "%sF%s", TEST_COLOR_KO, TEST_COLOR_RESET);
if (g_test_context)
printf("\n%s%s%s", TEST_COLOR_KO, g_test_context,
TEST_COLOR_RESET);
g_test_last_ok = 0;
}
void test_ok ()
{
g_test_count++;
g_test_ok++;
fprintf(stderr, "%s.%s", TEST_COLOR_OK, TEST_COLOR_RESET);
g_test_last_ok = 1;
}
void test_shutdown ()
{
}
void test_summary ()
{
fprintf(stderr, "\n");
printf("Total %ld tests. "
"%sOK %ld (%.1f%%)%s. "
"%sKO %ld (%.1f%%)%s.\n",
g_test_count,
TEST_COLOR_OK,
g_test_ok,
g_test_ok * 100.0f / g_test_count,
TEST_COLOR_RESET,
TEST_COLOR_KO,
g_test_ko,
g_test_ko * 100.0f / g_test_count,
TEST_COLOR_RESET);
}
int test_target (const char *target)
{
const char **i;
i = g_test_targets;
if (! *i)
return 1;
while (*i) {
if (strcmp(target, *i) == 0)
return 1;
i++;
}
return 0;
}