summaryrefslogtreecommitdiff
path: root/tests/lib
diff options
context:
space:
mode:
authorJakub Czapiga <jacz@semihalf.com>2020-10-09 10:07:47 +0200
committerPatrick Georgi <pgeorgi@google.com>2020-11-09 07:34:07 +0000
commite0c60f3d39182a1105aa9e39683abdbc2e600162 (patch)
treeaa79243e671ed620c9daf89707579acbeb4b063f /tests/lib
parent5742357e4d9f9acd2b13d6db32bf22bb58545c17 (diff)
tests: Add lib/timestamp-test test case
Signed-off-by: Jakub Czapiga <jacz@semihalf.com> Change-Id: I39abfc644fef085cef2175086a0e45a040b244de Reviewed-on: https://review.coreboot.org/c/coreboot/+/46968 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/Makefile.inc8
-rw-r--r--tests/lib/timestamp-test.c138
2 files changed, 145 insertions, 1 deletions
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc
index 3062bcaa70..27773b7f39 100644
--- a/tests/lib/Makefile.inc
+++ b/tests/lib/Makefile.inc
@@ -4,6 +4,7 @@ tests-y += string-test
tests-y += b64_decode-test
tests-y += hexstrtobin-test
tests-y += imd-test
+tests-y += timestamp-test
string-test-srcs += tests/lib/string-test.c
string-test-srcs += src/lib/string.c
@@ -17,4 +18,9 @@ hexstrtobin-test-srcs += src/lib/hexstrtobin.c
imd-test-srcs += tests/lib/imd-test.c
imd-test-srcs += tests/stubs/console.c
-imd-test-srcs += src/lib/imd.c \ No newline at end of file
+imd-test-srcs += src/lib/imd.c
+
+timestamp-test-srcs += tests/lib/timestamp-test.c
+timestamp-test-srcs += tests/stubs/timestamp.c
+timestamp-test-srcs += tests/stubs/console.c
+timestamp-test-stage := romstage
diff --git a/tests/lib/timestamp-test.c b/tests/lib/timestamp-test.c
new file mode 100644
index 0000000000..8dc103d203
--- /dev/null
+++ b/tests/lib/timestamp-test.c
@@ -0,0 +1,138 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include "../lib/timestamp.c"
+#include <commonlib/bsd/helpers.h>
+#include <tests/test.h>
+#include "stubs/timestamp.h"
+
+/* Timestamp region definition */
+#define TIMESTAMP_REGION_SIZE (1 * KiB)
+TEST_REGION(timestamp, TIMESTAMP_REGION_SIZE);
+
+void test_timestamp_init(void **state)
+{
+ timestamp_init(1000);
+
+ assert_non_null(glob_ts_table);
+}
+
+void test_timestamp_add(void **state)
+{
+ const int base_multipler = 2000;
+ const int timestamp_base = 1000;
+ struct timestamp_entry *entry;
+ int i;
+
+ timestamp_init(timestamp_base);
+
+ timestamp_add(TS_START_ROMSTAGE, base_multipler);
+
+ assert_int_equal(1, glob_ts_table->num_entries);
+
+ entry = &glob_ts_table->entries[0];
+ assert_int_equal(1, entry->entry_id);
+ assert_int_equal(base_multipler - timestamp_base, /* Added timestamp reduced by base */
+ entry->entry_stamp);
+
+ /* Add few timestamps to check if all of them will be added properly */
+ for (i = 1; i < 10; ++i)
+ timestamp_add(i + 1, base_multipler * (i + 1));
+
+ assert_int_equal(10, glob_ts_table->num_entries);
+
+ for (i = 0; i < 10; ++i) {
+ entry = &glob_ts_table->entries[i];
+ assert_int_equal(i + 1, entry->entry_id);
+ assert_int_equal(base_multipler * (i + 1) - timestamp_base,
+ entry->entry_stamp);
+ }
+}
+
+void test_timestamp_add_now(void **state)
+{
+ const int base_multipler = 2000;
+ const int timestamp_base = 1000;
+ struct timestamp_entry *entry;
+
+ /* Initialize with base timestamp of 1000.
+ * This value will be subtracted from each timestamp
+ * when adding it.
+ */
+ timestamp_init(timestamp_base);
+
+ dummy_timestamp_set(base_multipler);
+
+ timestamp_add_now(TS_START_ROMSTAGE);
+
+ assert_int_equal(1, glob_ts_table->num_entries);
+
+ entry = &glob_ts_table->entries[0];
+
+ assert_int_equal(1, entry->entry_id);
+ assert_int_equal(base_multipler - timestamp_base, /* Added timestamp reduced by base */
+ entry->entry_stamp);
+}
+
+void test_timestamp_rescale_table(void **state)
+{
+ const int base_multipler = 1000;
+ int i;
+
+ timestamp_init(0);
+
+ /* Add few timestamps to check if all of them will be rescaled properly */
+ for (i = 1; i <= 10; ++i)
+ timestamp_add(i, base_multipler * i);
+
+ /* Check if all entries were added to table */
+ assert_int_equal(10, glob_ts_table->num_entries);
+
+ timestamp_rescale_table(2, 4);
+
+ /* Check if there is the same number of entries */
+ assert_int_equal(10, glob_ts_table->num_entries);
+
+ for (i = 0; i < glob_ts_table->num_entries; ++i)
+ assert_int_equal(base_multipler * (i + 1) / 4 * 2,
+ glob_ts_table->entries[i].entry_stamp);
+}
+
+void test_get_us_since_boot(void **state)
+{
+ const int base_multipler = 10000;
+ const int timestamp_base = 1000;
+ const int freq_base = 100;
+
+ timestamp_init(timestamp_base);
+ dummy_timestamp_set(base_multipler);
+ dummy_timestamp_tick_freq_mhz_set(freq_base);
+ /* There is a need to update this field manually, because cbmem hooks are not used. */
+ glob_ts_table->tick_freq_mhz = freq_base;
+
+ assert_int_equal((base_multipler - timestamp_base) / freq_base, get_us_since_boot());
+}
+
+int setup_timestamp_and_freq(void **state)
+{
+ dummy_timestamp_set(0);
+ dummy_timestamp_tick_freq_mhz_set(1);
+
+ return 0;
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test_setup(test_timestamp_init, setup_timestamp_and_freq),
+ cmocka_unit_test_setup(test_timestamp_add, setup_timestamp_and_freq),
+ cmocka_unit_test_setup(test_timestamp_add_now, setup_timestamp_and_freq),
+ cmocka_unit_test_setup(test_timestamp_rescale_table, setup_timestamp_and_freq),
+ cmocka_unit_test_setup(test_get_us_since_boot, setup_timestamp_and_freq),
+ };
+
+#if CONFIG(COLLECT_TIMESTAMPS)
+ return cmocka_run_group_tests(tests, NULL, NULL);
+#else
+ return 0;
+#endif
+}