From 1cc01fdcbc0a8894a8987684f17d141bf8abfcbf Mon Sep 17 00:00:00 2001 From: Vic Yang Date: Fri, 12 Apr 2013 10:20:25 +0800 Subject: Add a test for testing common utilities BUG=chrome-os-partner:18598 TEST=Run on Spring BRANCH=None Change-Id: Ie10f81783a0c22a10b9535350dc29b972bffd87e Signed-off-by: Vic Yang Reviewed-on: https://gerrit.chromium.org/gerrit/47962 --- test/utils.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 test/utils.c (limited to 'test/utils.c') 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); -- cgit v1.2.1