1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include "../lib/timestamp.c"
4 #include <commonlib/bsd/helpers.h>
5 #include <tests/test.h>
6 #include "stubs/timestamp.h"
7
8 /* Timestamp region definition */
9 #define TIMESTAMP_REGION_SIZE (1 * KiB)
10 TEST_REGION(timestamp, TIMESTAMP_REGION_SIZE);
11
test_timestamp_init(void ** state)12 void test_timestamp_init(void **state)
13 {
14 timestamp_init(1000);
15
16 assert_non_null(glob_ts_table);
17 }
18
test_timestamp_add(void ** state)19 void test_timestamp_add(void **state)
20 {
21 const int base_multipler = 2000;
22 const int timestamp_base = 1000;
23 struct timestamp_entry *entry;
24 int i;
25
26 timestamp_init(timestamp_base);
27
28 timestamp_add(TS_ROMSTAGE_START, base_multipler);
29
30 assert_int_equal(1, glob_ts_table->num_entries);
31
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);
36
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));
40
41 assert_int_equal(10, glob_ts_table->num_entries);
42
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);
47 }
48 }
49
test_timestamp_add_now(void ** state)50 void test_timestamp_add_now(void **state)
51 {
52 const int base_multipler = 2000;
53 const int timestamp_base = 1000;
54 struct timestamp_entry *entry;
55
56 /* Initialize with base timestamp of 1000.
57 * This value will be subtracted from each timestamp
58 * when adding it.
59 */
60 timestamp_init(timestamp_base);
61
62 dummy_timestamp_set(base_multipler);
63
64 timestamp_add_now(TS_ROMSTAGE_START);
65
66 assert_int_equal(1, glob_ts_table->num_entries);
67
68 entry = &glob_ts_table->entries[0];
69
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);
73 }
74
test_timestamp_rescale_table(void ** state)75 void test_timestamp_rescale_table(void **state)
76 {
77 const int base_multipler = 1000;
78 int i;
79
80 timestamp_init(0);
81
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);
85
86 /* Check if all entries were added to table */
87 assert_int_equal(10, glob_ts_table->num_entries);
88
89 timestamp_rescale_table(2, 4);
90
91 /* Check if there is the same number of entries */
92 assert_int_equal(10, glob_ts_table->num_entries);
93
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);
97 }
98
test_get_us_since_boot(void ** state)99 void test_get_us_since_boot(void **state)
100 {
101 const int base_multipler = 10000;
102 const int timestamp_base = 1000;
103 const int freq_base = 100;
104
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;
110
111 assert_int_equal((base_multipler - timestamp_base) / freq_base, get_us_since_boot());
112 }
113
setup_timestamp_and_freq(void ** state)114 int setup_timestamp_and_freq(void **state)
115 {
116 dummy_timestamp_set(0);
117 dummy_timestamp_tick_freq_mhz_set(1);
118
119 return 0;
120 }
121
main(void)122 int main(void)
123 {
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),
130 };
131
132 #if CONFIG(COLLECT_TIMESTAMPS)
133 return cb_run_group_tests(tests, NULL, NULL);
134 #else
135 return 0;
136 #endif
137 }
138