[firefly] [PATCH 1/3] selftest: add abstractions for net selftests

Daniel Baluta daniel.baluta at gmail.com
Tue Apr 9 12:01:31 EEST 2013


On Sat, Apr 6, 2013 at 8:46 PM, Alexandru Copot <alex.mihai.c at gmail.com> wrote:
> +int run_all_tests(struct generic_test *test, void *param)
> +{
> +       int i;
> +       int rc, allrc;
> +       char *ptr;
> +
> +       rc = test->prepare ? test->prepare(param) : 0;
> +       if (rc)
> +               return rc;
> +
> +       allrc = 0;
> +       printf("Testing: %s ", test->name);
> +       ptr = test->testcases;
> +       for (i = 0; i < test->testcase_count; i++) {
> +               rc = test->run(ptr);
> +               allrc |= rc;
> +               ptr += test->testcase_size;
> +       }
> +       printf("\t\t%s\n", allrc ? "[FAIL]" : "[PASS]");
> +       return allrc;
> +}
With this approach is hard to figure out which test failed. Perhaps we can add
a parameter to generic_test structure named abort_on_fail?

> diff --git a/tools/testing/selftests/net/selftests.h b/tools/testing/selftests/net/selftests.h
> new file mode 100644
> index 0000000..0197c6a
> --- /dev/null
> +++ b/tools/testing/selftests/net/selftests.h
> @@ -0,0 +1,42 @@
> +#ifndef SELFTESTS_H
> +#define SELFTESTS_H
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <errno.h>
> +
> +#define ASSERT(cond) do {                                                              \
> +       if (!(cond))     {                                                              \
> +                       fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, #cond);       \
> +                       perror("");                                                     \
> +                       exit(EXIT_FAILURE);                                             \
> +               }                                                                       \
> +} while (0)
> +
> +#define CHECK(cond,fmt,...)                            \
> +       do {                                            \
> +               if (!(cond)) {                          \
> +                       fprintf(stderr, "(%s, %d): " fmt,       \
> +                                       __FILE__, __LINE__, ##__VA_ARGS__); \
> +                       perror("");                     \
> +                       return 1;                       \
> +               }                                       \
> +       } while (0)
> +
These macros are ok for now. Later on we can move them to an util.h file.
> +struct generic_test {
> +       const char *name;
> +       void *private;
> +       void *testcases;
> +       int testcase_size;
> +       int testcase_count;
> +
> +       int (*prepare)(void *);
> +       int (*run)(void *);
> +       int (*cleanup)(void *);
> +};
> +
> +int run_all_tests(struct generic_test *test, void *param);

thanks,
daniel.


More information about the firefly mailing list