blob: 8dc103d20367353c14b143b291f31fbb6d7bffae [file] [log] [blame]
Jakub Czapigae0c60f32020-10-09 10:07:47 +02001/* 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)
10TEST_REGION(timestamp, TIMESTAMP_REGION_SIZE);
11
12void test_timestamp_init(void **state)
13{
14 timestamp_init(1000);
15
16 assert_non_null(glob_ts_table);
17}
18
19void 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_START_ROMSTAGE, 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,
47 entry->entry_stamp);
48 }
49}
50
51void test_timestamp_add_now(void **state)
52{
53 const int base_multipler = 2000;
54 const int timestamp_base = 1000;
55 struct timestamp_entry *entry;
56
57 /* Initialize with base timestamp of 1000.
58 * This value will be subtracted from each timestamp
59 * when adding it.
60 */
61 timestamp_init(timestamp_base);
62
63 dummy_timestamp_set(base_multipler);
64
65 timestamp_add_now(TS_START_ROMSTAGE);
66
67 assert_int_equal(1, glob_ts_table->num_entries);
68
69 entry = &glob_ts_table->entries[0];
70
71 assert_int_equal(1, entry->entry_id);
72 assert_int_equal(base_multipler - timestamp_base, /* Added timestamp reduced by base */
73 entry->entry_stamp);
74}
75
76void test_timestamp_rescale_table(void **state)
77{
78 const int base_multipler = 1000;
79 int i;
80
81 timestamp_init(0);
82
83 /* Add few timestamps to check if all of them will be rescaled properly */
84 for (i = 1; i <= 10; ++i)
85 timestamp_add(i, base_multipler * i);
86
87 /* Check if all entries were added to table */
88 assert_int_equal(10, glob_ts_table->num_entries);
89
90 timestamp_rescale_table(2, 4);
91
92 /* Check if there is the same number of entries */
93 assert_int_equal(10, glob_ts_table->num_entries);
94
95 for (i = 0; i < glob_ts_table->num_entries; ++i)
96 assert_int_equal(base_multipler * (i + 1) / 4 * 2,
97 glob_ts_table->entries[i].entry_stamp);
98}
99
100void test_get_us_since_boot(void **state)
101{
102 const int base_multipler = 10000;
103 const int timestamp_base = 1000;
104 const int freq_base = 100;
105
106 timestamp_init(timestamp_base);
107 dummy_timestamp_set(base_multipler);
108 dummy_timestamp_tick_freq_mhz_set(freq_base);
109 /* There is a need to update this field manually, because cbmem hooks are not used. */
110 glob_ts_table->tick_freq_mhz = freq_base;
111
112 assert_int_equal((base_multipler - timestamp_base) / freq_base, get_us_since_boot());
113}
114
115int setup_timestamp_and_freq(void **state)
116{
117 dummy_timestamp_set(0);
118 dummy_timestamp_tick_freq_mhz_set(1);
119
120 return 0;
121}
122
123int main(void)
124{
125 const struct CMUnitTest tests[] = {
126 cmocka_unit_test_setup(test_timestamp_init, setup_timestamp_and_freq),
127 cmocka_unit_test_setup(test_timestamp_add, setup_timestamp_and_freq),
128 cmocka_unit_test_setup(test_timestamp_add_now, setup_timestamp_and_freq),
129 cmocka_unit_test_setup(test_timestamp_rescale_table, setup_timestamp_and_freq),
130 cmocka_unit_test_setup(test_get_us_since_boot, setup_timestamp_and_freq),
131 };
132
133#if CONFIG(COLLECT_TIMESTAMPS)
134 return cmocka_run_group_tests(tests, NULL, NULL);
135#else
136 return 0;
137#endif
138}