ec/google/chromeec: Publish LPC GMR address range via CREC _CRS
[coreboot2.git] / tests / lib / timestamp-test.c
bloba3f50dbbb3a7905522c92062eda8f0ecb8c67258
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include "../lib/timestamp.c"
4 #include <commonlib/bsd/helpers.h>
5 #include <tests/test.h>
6 #include "stubs/timestamp.h"
8 /* Timestamp region definition */
9 #define TIMESTAMP_REGION_SIZE (1 * KiB)
10 TEST_REGION(timestamp, TIMESTAMP_REGION_SIZE);
12 void test_timestamp_init(void **state)
14 timestamp_init(1000);
16 assert_non_null(glob_ts_table);
19 void test_timestamp_add(void **state)
21 const int base_multipler = 2000;
22 const int timestamp_base = 1000;
23 struct timestamp_entry *entry;
24 int i;
26 timestamp_init(timestamp_base);
28 timestamp_add(TS_ROMSTAGE_START, base_multipler);
30 assert_int_equal(1, glob_ts_table->num_entries);
32 entry = &glob_ts_table->entries[0];
33 assert_int_equal(1, entry->entry_id);
34 assert_int_equal(base_multipler - timestamp_base, /* Added timestamp reduced by base */
35 entry->entry_stamp);
37 /* Add few timestamps to check if all of them will be added properly */
38 for (i = 1; i < 10; ++i)
39 timestamp_add(i + 1, base_multipler * (i + 1));
41 assert_int_equal(10, glob_ts_table->num_entries);
43 for (i = 0; i < 10; ++i) {
44 entry = &glob_ts_table->entries[i];
45 assert_int_equal(i + 1, entry->entry_id);
46 assert_int_equal(base_multipler * (i + 1) - timestamp_base, entry->entry_stamp);
50 void test_timestamp_add_now(void **state)
52 const int base_multipler = 2000;
53 const int timestamp_base = 1000;
54 struct timestamp_entry *entry;
56 /* Initialize with base timestamp of 1000.
57 * This value will be subtracted from each timestamp
58 * when adding it.
60 timestamp_init(timestamp_base);
62 dummy_timestamp_set(base_multipler);
64 timestamp_add_now(TS_ROMSTAGE_START);
66 assert_int_equal(1, glob_ts_table->num_entries);
68 entry = &glob_ts_table->entries[0];
70 assert_int_equal(1, entry->entry_id);
71 assert_int_equal(base_multipler - timestamp_base, /* Added timestamp reduced by base */
72 entry->entry_stamp);
75 void test_timestamp_rescale_table(void **state)
77 const int base_multipler = 1000;
78 int i;
80 timestamp_init(0);
82 /* Add few timestamps to check if all of them will be rescaled properly */
83 for (i = 1; i <= 10; ++i)
84 timestamp_add(i, base_multipler * i);
86 /* Check if all entries were added to table */
87 assert_int_equal(10, glob_ts_table->num_entries);
89 timestamp_rescale_table(2, 4);
91 /* Check if there is the same number of entries */
92 assert_int_equal(10, glob_ts_table->num_entries);
94 for (i = 0; i < glob_ts_table->num_entries; ++i)
95 assert_int_equal(base_multipler * (i + 1) / 4 * 2,
96 glob_ts_table->entries[i].entry_stamp);
99 void test_get_us_since_boot(void **state)
101 const int base_multipler = 10000;
102 const int timestamp_base = 1000;
103 const int freq_base = 100;
105 timestamp_init(timestamp_base);
106 dummy_timestamp_set(base_multipler);
107 dummy_timestamp_tick_freq_mhz_set(freq_base);
108 /* There is a need to update this field manually, because cbmem hooks are not used. */
109 glob_ts_table->tick_freq_mhz = freq_base;
111 assert_int_equal((base_multipler - timestamp_base) / freq_base, get_us_since_boot());
114 int setup_timestamp_and_freq(void **state)
116 dummy_timestamp_set(0);
117 dummy_timestamp_tick_freq_mhz_set(1);
119 return 0;
122 int main(void)
124 const struct CMUnitTest tests[] = {
125 cmocka_unit_test_setup(test_timestamp_init, setup_timestamp_and_freq),
126 cmocka_unit_test_setup(test_timestamp_add, setup_timestamp_and_freq),
127 cmocka_unit_test_setup(test_timestamp_add_now, setup_timestamp_and_freq),
128 cmocka_unit_test_setup(test_timestamp_rescale_table, setup_timestamp_and_freq),
129 cmocka_unit_test_setup(test_get_us_since_boot, setup_timestamp_and_freq),
132 #if CONFIG(COLLECT_TIMESTAMPS)
133 return cb_run_group_tests(tests, NULL, NULL);
134 #else
135 return 0;
136 #endif