From 890d91ff3584fcdcf9dc5ff0b516dcd3a5e55108 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 2 Nov 2024 13:37:01 -0600 Subject: test: Move time_ut test into lib This test doesn't belong at the top level. Move it into the lib/ directory, to match its implementation. Rename it to drop the unnecessary _ut suffix. Signed-off-by: Simon Glass Tested-by: Tom Rini # rpi_3, rpi_4, rpi_arm64, am64x_evm_a53, am64-sk --- test/Makefile | 1 - test/lib/Makefile | 1 + test/lib/time.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/time_ut.c | 132 ------------------------------------------------------ 4 files changed, 133 insertions(+), 133 deletions(-) create mode 100644 test/lib/time.c delete mode 100644 test/time_ut.c diff --git a/test/Makefile b/test/Makefile index 0b1affcf445..47a07d653a9 100644 --- a/test/Makefile +++ b/test/Makefile @@ -14,7 +14,6 @@ endif ifneq ($(CONFIG_HUSH_PARSER),) obj-$(CONFIG_$(XPL_)CMDLINE) += hush/ endif -obj-$(CONFIG_UT_TIME) += time_ut.o obj-y += ut.o ifeq ($(CONFIG_XPL_BUILD),) diff --git a/test/lib/Makefile b/test/lib/Makefile index c36c8b0917d..217c3baf881 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -27,6 +27,7 @@ obj-$(CONFIG_AES) += test_aes.o obj-$(CONFIG_GETOPT) += getopt.o obj-$(CONFIG_CRC8) += test_crc8.o obj-$(CONFIG_UT_LIB_CRYPT) += test_crypt.o +obj-$(CONFIG_UT_TIME) += time.o obj-$(CONFIG_$(XPL_)UT_UNICODE) += unicode.o obj-$(CONFIG_LIB_UUID) += uuid.o else diff --git a/test/lib/time.c b/test/lib/time.c new file mode 100644 index 00000000000..149c4b58f4a --- /dev/null +++ b/test/lib/time.c @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2015 Google, Inc + * Written by Simon Glass + */ + +#include +#include +#include +#include + +static int test_get_timer(void) +{ + ulong base, start, next, diff; + int iter; + + base = get_timer(0); + start = get_timer(0); + for (iter = 0; iter < 10; iter++) { + do { + next = get_timer(0); + } while (start == next); + + if (start + 1 != next) { + printf("%s: iter=%d, start=%lu, next=%lu, expected a difference of 1\n", + __func__, iter, start, next); + return -EINVAL; + } + start++; + } + + /* + * Check that get_timer(base) matches our elapsed time, allowing that + * an extra millisecond may have passed. + */ + diff = get_timer(base); + if (diff != iter && diff != iter + 1) { + printf("%s: expected get_timer(base) to match elapsed time: diff=%lu, expected=%d\n", + __func__, diff, iter); + return -EINVAL; + } + + return 0; +} + +static int test_timer_get_us(void) +{ + ulong prev, next, min = 1000000; + long delta; + int iter; + + /* Find the minimum delta we can measure, in microseconds */ + prev = timer_get_us(); + for (iter = 0; iter < 100; ) { + next = timer_get_us(); + if (next != prev) { + delta = next - prev; + if (delta < 0) { + printf("%s: timer_get_us() went backwards from %lu to %lu\n", + __func__, prev, next); + return -EINVAL; + } else if (delta != 0) { + if (delta < min) + min = delta; + prev = next; + iter++; + } + } + } + + if (min != 1) { + printf("%s: Minimum microsecond delta should be 1 but is %lu\n", + __func__, min); + return -EINVAL; + } + + return 0; +} + +static int test_time_comparison(void) +{ + ulong start_us, end_us, delta_us; + long error; + ulong start; + + start = get_timer(0); + start_us = timer_get_us(); + while (get_timer(start) < 1000) + ; + end_us = timer_get_us(); + delta_us = end_us - start_us; + error = delta_us - 1000000; + printf("%s: Microsecond time for 1 second: %lu, error = %ld\n", + __func__, delta_us, error); + if (abs(error) > 1000) + return -EINVAL; + + return 0; +} + +static int test_udelay(void) +{ + long error; + ulong start, delta; + int iter; + + start = get_timer(0); + for (iter = 0; iter < 1000; iter++) + udelay(1000); + delta = get_timer(start); + error = delta - 1000; + printf("%s: Delay time for 1000 udelay(1000): %lu ms, error = %ld\n", + __func__, delta, error); + if (abs(error) > 100) + return -EINVAL; + + return 0; +} + +int do_ut_time(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) +{ + int ret = 0; + + ret |= test_get_timer(); + ret |= test_timer_get_us(); + ret |= test_time_comparison(); + ret |= test_udelay(); + + printf("Test %s\n", ret ? "failed" : "passed"); + + return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS; +} diff --git a/test/time_ut.c b/test/time_ut.c deleted file mode 100644 index 149c4b58f4a..00000000000 --- a/test/time_ut.c +++ /dev/null @@ -1,132 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0+ -/* - * Copyright (c) 2015 Google, Inc - * Written by Simon Glass - */ - -#include -#include -#include -#include - -static int test_get_timer(void) -{ - ulong base, start, next, diff; - int iter; - - base = get_timer(0); - start = get_timer(0); - for (iter = 0; iter < 10; iter++) { - do { - next = get_timer(0); - } while (start == next); - - if (start + 1 != next) { - printf("%s: iter=%d, start=%lu, next=%lu, expected a difference of 1\n", - __func__, iter, start, next); - return -EINVAL; - } - start++; - } - - /* - * Check that get_timer(base) matches our elapsed time, allowing that - * an extra millisecond may have passed. - */ - diff = get_timer(base); - if (diff != iter && diff != iter + 1) { - printf("%s: expected get_timer(base) to match elapsed time: diff=%lu, expected=%d\n", - __func__, diff, iter); - return -EINVAL; - } - - return 0; -} - -static int test_timer_get_us(void) -{ - ulong prev, next, min = 1000000; - long delta; - int iter; - - /* Find the minimum delta we can measure, in microseconds */ - prev = timer_get_us(); - for (iter = 0; iter < 100; ) { - next = timer_get_us(); - if (next != prev) { - delta = next - prev; - if (delta < 0) { - printf("%s: timer_get_us() went backwards from %lu to %lu\n", - __func__, prev, next); - return -EINVAL; - } else if (delta != 0) { - if (delta < min) - min = delta; - prev = next; - iter++; - } - } - } - - if (min != 1) { - printf("%s: Minimum microsecond delta should be 1 but is %lu\n", - __func__, min); - return -EINVAL; - } - - return 0; -} - -static int test_time_comparison(void) -{ - ulong start_us, end_us, delta_us; - long error; - ulong start; - - start = get_timer(0); - start_us = timer_get_us(); - while (get_timer(start) < 1000) - ; - end_us = timer_get_us(); - delta_us = end_us - start_us; - error = delta_us - 1000000; - printf("%s: Microsecond time for 1 second: %lu, error = %ld\n", - __func__, delta_us, error); - if (abs(error) > 1000) - return -EINVAL; - - return 0; -} - -static int test_udelay(void) -{ - long error; - ulong start, delta; - int iter; - - start = get_timer(0); - for (iter = 0; iter < 1000; iter++) - udelay(1000); - delta = get_timer(start); - error = delta - 1000; - printf("%s: Delay time for 1000 udelay(1000): %lu ms, error = %ld\n", - __func__, delta, error); - if (abs(error) > 100) - return -EINVAL; - - return 0; -} - -int do_ut_time(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) -{ - int ret = 0; - - ret |= test_get_timer(); - ret |= test_timer_get_us(); - ret |= test_time_comparison(); - ret |= test_udelay(); - - printf("Test %s\n", ret ? "failed" : "passed"); - - return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS; -} -- cgit v1.2.3