diff options
-rw-r--r-- | test/build.mk | 3 | ||||
-rw-r--r-- | test/utils.c | 83 | ||||
-rw-r--r-- | test/utils.tasklist | 17 |
3 files changed, 102 insertions, 1 deletions
diff --git a/test/build.mk b/test/build.mk index 4a6fa6299a..fb2b780fbc 100644 --- a/test/build.mk +++ b/test/build.mk @@ -8,7 +8,7 @@ test-list=hello pingpong timer_calib timer_dos timer_jump mutex thermal test-list+=power_button kb_deghost kb_debounce scancode typematic charging -test-list+=flash_overwrite flash_rw_erase +test-list+=flash_overwrite flash_rw_erase utils #disable: powerdemo kb_deghost-y=kb_deghost.o @@ -19,6 +19,7 @@ timer_dos-y=timer_dos.o mutex-y=mutex.o flash_overwrite-y=flash.o flash_rw_erase-y=flash.o +utils-y=utils.o # Mock modules for 'thermal' chip-mock-thermal-lpc.o=mock_lpc.o diff --git a/test/utils.c b/test/utils.c new file mode 100644 index 0000000000..1441c5e185 --- /dev/null +++ b/test/utils.c @@ -0,0 +1,83 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Test common utilities. + */ + +#include "common.h" +#include "console.h" +#include "util.h" + +static int error_count; + +#define RUN_TEST(n) \ + do { \ + ccprintf("Running %s...", #n); \ + cflush(); \ + if (n()) { \ + ccputs("OK\n"); \ + } else { \ + ccputs("Fail\n"); \ + error_count++; \ + } \ + } while (0) + +static int test_strlen(void) +{ + return 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); +} + +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); +} + +static int test_atoi(void) +{ + return (atoi(" 901") == 901) && + (atoi("-12c") == -12) && + (atoi(" 0 ") == 0) && + (atoi("\t111") == 111); +} + +static int test_uint64divmod(void) +{ + uint64_t n = 8567106442584750ULL; + int d = 54870071; + int r = uint64divmod(&n, d); + + return (r == 5991285 && n == 156134415ULL); +} + +static int command_run_test(int argc, char **argv) +{ + error_count = 0; + + RUN_TEST(test_strlen); + RUN_TEST(test_strcasecmp); + RUN_TEST(test_strncasecmp); + RUN_TEST(test_atoi); + RUN_TEST(test_uint64divmod); + + if (error_count) { + ccprintf("Failed %d tests!\n", error_count); + return EC_ERROR_UNKNOWN; + } else { + ccprintf("Pass!\n"); + return EC_SUCCESS; + } +} +DECLARE_CONSOLE_COMMAND(runtest, command_run_test, + NULL, NULL, NULL); diff --git a/test/utils.tasklist b/test/utils.tasklist new file mode 100644 index 0000000000..26cfc53453 --- /dev/null +++ b/test/utils.tasklist @@ -0,0 +1,17 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * List of enabled tasks in the priority order + * + * The first one has the lowest priority. + * + * For each task, use the macro TASK_TEST(n, r, d, s) where : + * 'n' in the name of the task + * 'r' in the main routine of the task + * 'd' in an opaque parameter passed to the routine at startup + * 's' is the stack size in bytes; must be a multiple of 8 + */ +#define CONFIG_TEST_TASK_LIST /* No test task */ |