diff options
Diffstat (limited to 'test/utils.c')
-rw-r--r-- | test/utils.c | 70 |
1 files changed, 55 insertions, 15 deletions
diff --git a/test/utils.c b/test/utils.c index 1441c5e185..1c04c0d06d 100644 --- a/test/utils.c +++ b/test/utils.c @@ -7,6 +7,8 @@ #include "common.h" #include "console.h" +#include "shared_mem.h" +#include "timer.h" #include "util.h" static int error_count; @@ -15,7 +17,7 @@ static int error_count; do { \ ccprintf("Running %s...", #n); \ cflush(); \ - if (n()) { \ + if (n() == EC_SUCCESS) { \ ccputs("OK\n"); \ } else { \ ccputs("Fail\n"); \ @@ -23,33 +25,47 @@ static int error_count; } \ } while (0) +#define TEST_ASSERT(n) \ + do { \ + if (!(n)) \ + return EC_ERROR_UNKNOWN; \ + } while (0) + +#define TEST_CHECK(n) \ + do { \ + if (n) \ + return EC_SUCCESS; \ + else \ + return EC_ERROR_UNKNOWN; \ + } while (0) + static int test_strlen(void) { - return strlen("this is a string") == 16; + TEST_CHECK(strlen("this is a string") == 16); } static int test_strcasecmp(void) { - return (strcasecmp("test string", "TEST strIng") == 0) && - (strcasecmp("test123!@#", "TesT123!@#") == 0) && - (strcasecmp("lower", "UPPER") != 0); + TEST_CHECK((strcasecmp("test string", "TEST strIng") == 0) && + (strcasecmp("test123!@#", "TesT123!@#") == 0) && + (strcasecmp("lower", "UPPER") != 0)); } static int test_strncasecmp(void) { - return (strncasecmp("test string", "TEST str", 4) == 0) && - (strncasecmp("test string", "TEST str", 8) == 0) && - (strncasecmp("test123!@#", "TesT321!@#", 5) != 0) && - (strncasecmp("test123!@#", "TesT321!@#", 4) == 0) && - (strncasecmp("1test123!@#", "1TesT321!@#", 5) == 0); + TEST_CHECK((strncasecmp("test string", "TEST str", 4) == 0) && + (strncasecmp("test string", "TEST str", 8) == 0) && + (strncasecmp("test123!@#", "TesT321!@#", 5) != 0) && + (strncasecmp("test123!@#", "TesT321!@#", 4) == 0) && + (strncasecmp("1test123!@#", "1TesT321!@#", 5) == 0)); } static int test_atoi(void) { - return (atoi(" 901") == 901) && - (atoi("-12c") == -12) && - (atoi(" 0 ") == 0) && - (atoi("\t111") == 111); + TEST_CHECK((atoi(" 901") == 901) && + (atoi("-12c") == -12) && + (atoi(" 0 ") == 0) && + (atoi("\t111") == 111)); } static int test_uint64divmod(void) @@ -58,7 +74,30 @@ static int test_uint64divmod(void) int d = 54870071; int r = uint64divmod(&n, d); - return (r == 5991285 && n == 156134415ULL); + TEST_CHECK(r == 5991285 && n == 156134415ULL); +} + +static int test_shared_mem(void) +{ + int i, j; + int sz = shared_mem_size(); + char *mem; + + TEST_ASSERT(shared_mem_acquire(sz, &mem) == EC_SUCCESS); + TEST_ASSERT(shared_mem_acquire(sz, &mem) == EC_ERROR_BUSY); + + for (i = 0; i < 256; ++i) { + memset(mem, i, sz); + for (j = 0; j < sz; ++j) + TEST_ASSERT(mem[j] == i); + + if ((i & 0xf) == 0) + msleep(20); /* Yield to other tasks */ + } + + shared_mem_release(mem); + + return EC_SUCCESS; } static int command_run_test(int argc, char **argv) @@ -70,6 +109,7 @@ static int command_run_test(int argc, char **argv) RUN_TEST(test_strncasecmp); RUN_TEST(test_atoi); RUN_TEST(test_uint64divmod); + RUN_TEST(test_shared_mem); if (error_count) { ccprintf("Failed %d tests!\n", error_count); |