]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
unit-tests: Add environment variable to only run specific iterations
authorTobias Brunner <tobias@strongswan.org>
Mon, 22 Aug 2022 09:48:29 +0000 (11:48 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 12 Sep 2022 14:13:51 +0000 (16:13 +0200)
Helpful when running with increased verbosity and only specific iterations
fail.

src/libstrongswan/tests/test_runner.c

index 71da513f0f40e4eb5ab9881fa03ea9beb4fb861a..af1faf47306b0627a0543d70bc66d28fbe6ff84e 100644 (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);
 }