unit-tests: Add environment variable to only run specific iterations

Helpful when running with increased verbosity and only specific iterations
fail.
This commit is contained in:
Tobias Brunner 2022-08-22 11:48:29 +02:00
parent a345e635c4
commit 644f74ad8f

View File

@ -568,6 +568,37 @@ static double end_timing(struct timespec *start)
#endif /* CLOCK_THREAD_CPUTIME_ID */
/**
* Determine the configured iterations to run
*/
static hashtable_t *get_iterations()
{
enumerator_t *enumerator;
hashtable_t *config = NULL;
char *iterations, *iter;
iterations = getenv("TESTS_ITERATIONS");
if (!iterations)
{
return NULL;
}
config = hashtable_create(hashtable_hash_ptr, hashtable_equals_ptr, 8);
enumerator = enumerator_create_token(iterations, ",", " ");
while (enumerator->enumerate(enumerator, &iter))
{
/* add 1 so we can store 0 */
config->put(config, (void*)(uintptr_t)atoi(iter)+1, config);
}
enumerator->destroy(enumerator);
if (!config->get_count(config))
{
config->destroy(config);
config = NULL;
}
return config;
}
/**
* Run a single test case with fixtures
*/
@ -576,6 +607,7 @@ static bool run_case(test_case_t *tcase, test_runner_init_t init, char *cfg,
{
enumerator_t *enumerator;
test_function_t *tfun;
hashtable_t *iterations;
double *times;
double total_time = 0;
int tests = 0, ti = 0, passed = 0;
@ -596,13 +628,20 @@ static bool run_case(test_case_t *tcase, test_runner_init_t init, char *cfg,
fprintf(stderr, " Running case '%s': ", tcase->name);
fflush(stderr);
iterations = get_iterations();
enumerator = array_create_enumerator(tcase->functions);
while (enumerator->enumerate(enumerator, &tfun))
{
int i, rounds = 0;
int i, rounds = 0, skipped = 0;
for (i = tfun->start; i < tfun->end; i++)
{
if (iterations && !iterations->get(iterations, (void*)(uintptr_t)i+1))
{
skipped++;
continue;
}
if (pre_test(init, cfg))
{
struct timespec start;
@ -671,7 +710,7 @@ static bool run_case(test_case_t *tcase, test_runner_init_t init, char *cfg,
}
}
fflush(stderr);
if (rounds == tfun->end - tfun->start)
if (rounds == tfun->end - tfun->start - skipped)
{
passed++;
}
@ -698,6 +737,7 @@ static bool run_case(test_case_t *tcase, test_runner_init_t init, char *cfg,
print_failures(failures, FALSE);
array_destroy(failures);
array_destroy(warnings);
DESTROY_IF(iterations);
return passed == array_count(tcase->functions);
}