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>
This commit is contained in:
Jakub Czapiga 2020-10-09 10:07:47 +02:00 committed by Patrick Georgi
parent 5742357e4d
commit e0c60f3d39
4 changed files with 180 additions and 1 deletions

View File

@ -0,0 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <types.h>
void dummy_timestamp_set(uint64_t v);
void dummy_timestamp_tick_freq_mhz_set(int v);

View File

@ -4,6 +4,7 @@ tests-y += string-test
tests-y += b64_decode-test tests-y += b64_decode-test
tests-y += hexstrtobin-test tests-y += hexstrtobin-test
tests-y += imd-test tests-y += imd-test
tests-y += timestamp-test
string-test-srcs += tests/lib/string-test.c string-test-srcs += tests/lib/string-test.c
string-test-srcs += src/lib/string.c string-test-srcs += src/lib/string.c
@ -18,3 +19,8 @@ hexstrtobin-test-srcs += src/lib/hexstrtobin.c
imd-test-srcs += tests/lib/imd-test.c imd-test-srcs += tests/lib/imd-test.c
imd-test-srcs += tests/stubs/console.c imd-test-srcs += tests/stubs/console.c
imd-test-srcs += src/lib/imd.c 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

138
tests/lib/timestamp-test.c Normal file
View File

@ -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
}

29
tests/stubs/timestamp.c Normal file
View File

@ -0,0 +1,29 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "stubs/timestamp.h"
static uint64_t timestamp_value = 0;
static int timestamp_tick_freq_mhz_value = 1;
/* Provides way to control timestamp value */
void dummy_timestamp_set(uint64_t v)
{
timestamp_value = v;
}
/* Provides way to control timestamp tick frequency MHz value */
void dummy_timestamp_tick_freq_mhz_set(int v)
{
timestamp_tick_freq_mhz_value = v;
}
/* Reimplementation of timestamp getter to control behaviour */
uint64_t timestamp_get(void)
{
return timestamp_value;
}
int timestamp_tick_freq_mhz(void)
{
return timestamp_tick_freq_mhz_value;
}