Debian Codegraph @

crt/aws-c-common/tests/clock_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/clock.h>
7
8 #include <aws/common/thread.h>
9 #include <aws/testing/aws_test_harness.h>
10
11 static int s_test_high_res_clock_increments(struct aws_allocator *allocator, void *ctx) {
12 (void)allocator;
13 (void)ctx;
14
15 uint64_t ticks = 0, prev = 0;
16
17 for (unsigned i = 0; i < 100; ++i) {
18 ASSERT_SUCCESS(
19 aws_high_res_clock_get_ticks(&ticks), "High res get ticks failed with error %d", aws_last_error());
20 ASSERT_TRUE(
21 ticks >= prev,
22 "Next get ticks should have been greater than or equal to previous. previous %llu current %llu",
23 (long long unsigned int)prev,
24 (long long unsigned int)ticks);
25 aws_thread_current_sleep(1000000);
26 prev = ticks;
27 }
28
29 return 0;
30 }
31
32 static int s_test_sys_clock_increments(struct aws_allocator *allocator, void *ctx) {
33 (void)allocator;
34 (void)ctx;
35
36 uint64_t ticks = 0, prev = 0;
37
38 for (unsigned i = 0; i < 100; ++i) {
39 ASSERT_SUCCESS(
40 aws_sys_clock_get_ticks(&ticks), "Sys clock res get ticks failed with error %d", aws_last_error());
41 ASSERT_TRUE(
42 ticks >= prev,
43 "Next get ticks should have been greater than or equal to previous. previous %llu current %llu",
44 (long long unsigned int)prev,
45 (long long unsigned int)ticks);
46 aws_thread_current_sleep(1000000);
47 prev = ticks;
48 }
49
50 return 0;
51 }
52
53 static int s_test_sec_and_millis_conversion(struct aws_allocator *allocator, void *ctx) {
54 (void)allocator;
55 (void)ctx;
56
57 uint64_t secs = 10;
58 ASSERT_UINT_EQUALS(10000, aws_timestamp_convert(secs, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL));
59 ASSERT_UINT_EQUALS(secs, aws_timestamp_convert(10000, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_SECS, NULL));
60
61 return 0;
62 }
63
64 static int s_test_sec_and_micros_conversion(struct aws_allocator *allocator, void *ctx) {
65 (void)allocator;
66 (void)ctx;
67
68 uint64_t secs = 10;
69 ASSERT_UINT_EQUALS(10000000, aws_timestamp_convert(secs, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MICROS, NULL));
70 ASSERT_UINT_EQUALS(secs, aws_timestamp_convert(10000000, AWS_TIMESTAMP_MICROS, AWS_TIMESTAMP_SECS, NULL));
71
72 return 0;
73 }
74
75 static int s_test_sec_and_nanos_conversion(struct aws_allocator *allocator, void *ctx) {
76 (void)allocator;
77 (void)ctx;
78
79 uint64_t secs = 10;
80 ASSERT_UINT_EQUALS(10000000000, aws_timestamp_convert(secs, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
81 ASSERT_UINT_EQUALS(secs, aws_timestamp_convert(10000000000, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL));
82
83 return 0;
84 }
85
86 static int s_test_milli_and_micros_conversion(struct aws_allocator *allocator, void *ctx) {
87 (void)allocator;
88 (void)ctx;
89
90 uint64_t ms = 10;
91 ASSERT_UINT_EQUALS(10000, aws_timestamp_convert(ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_MICROS, NULL));
92 ASSERT_UINT_EQUALS(ms, aws_timestamp_convert(10000, AWS_TIMESTAMP_MICROS, AWS_TIMESTAMP_MILLIS, NULL));
93
94 return 0;
95 }
96
97 static int s_test_milli_and_nanos_conversion(struct aws_allocator *allocator, void *ctx) {
98 (void)allocator;
99 (void)ctx;
100
101 uint64_t ms = 10;
102 ASSERT_UINT_EQUALS(10000000, aws_timestamp_convert(ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL));
103 ASSERT_UINT_EQUALS(ms, aws_timestamp_convert(10000000, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MILLIS, NULL));
104
105 return 0;
106 }
107
108 static int s_test_micro_and_nanos_conversion(struct aws_allocator *allocator, void *ctx) {
109 (void)allocator;
110 (void)ctx;
111
112 uint64_t micros = 10;
113 ASSERT_UINT_EQUALS(10000, aws_timestamp_convert(micros, AWS_TIMESTAMP_MICROS, AWS_TIMESTAMP_NANOS, NULL));
114 ASSERT_UINT_EQUALS(micros, aws_timestamp_convert(10000, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_MICROS, NULL));
115
116 return 0;
117 }
118
119 static int s_test_precision_loss_remainders_conversion(struct aws_allocator *allocator, void *ctx) {
120 (void)allocator;
121 (void)ctx;
122
123 uint64_t nanos = 10123456789;
124 uint64_t remainder = 0;
125 ASSERT_UINT_EQUALS(10, aws_timestamp_convert(nanos, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder));
126 ASSERT_UINT_EQUALS(123456789, remainder);
127
128 return 0;
129 }
130
131 static int s_test_overflow_conversion(struct aws_allocator *allocator, void *ctx) {
132 (void)allocator;
133 (void)ctx;
134
135 ASSERT_UINT_EQUALS(
136 UINT64_MAX, aws_timestamp_convert(100000000000ULL, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL));
137
138 ASSERT_UINT_EQUALS(
139 UINT64_MAX, aws_timestamp_convert(100000000000000ULL, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MICROS, NULL));
140 ASSERT_UINT_EQUALS(
141 UINT64_MAX, aws_timestamp_convert(100000000000000ULL, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL));
142
143 ASSERT_UINT_EQUALS(
144 UINT64_MAX, aws_timestamp_convert(100000000000000000ULL, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL));
145 ASSERT_UINT_EQUALS(
146 UINT64_MAX, aws_timestamp_convert(100000000000000000ULL, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_MICROS, NULL));
147 ASSERT_UINT_EQUALS(
148 UINT64_MAX, aws_timestamp_convert(100000000000000000ULL, AWS_TIMESTAMP_MICROS, AWS_TIMESTAMP_NANOS, NULL));
149 return 0;
150 }
151
152 AWS_TEST_CASE(high_res_clock_increments_test, s_test_high_res_clock_increments)
153 AWS_TEST_CASE(sys_clock_increments_test, s_test_sys_clock_increments)
154 AWS_TEST_CASE(test_sec_and_millis_conversions, s_test_sec_and_millis_conversion)
155 AWS_TEST_CASE(test_sec_and_micros_conversions, s_test_sec_and_micros_conversion)
156 AWS_TEST_CASE(test_sec_and_nanos_conversions, s_test_sec_and_nanos_conversion)
157 AWS_TEST_CASE(test_milli_and_micros_conversion, s_test_milli_and_micros_conversion)
158 AWS_TEST_CASE(test_milli_and_nanos_conversion, s_test_milli_and_nanos_conversion)
159 AWS_TEST_CASE(test_micro_and_nanos_conversion, s_test_micro_and_nanos_conversion)
160 AWS_TEST_CASE(test_precision_loss_remainders_conversion, s_test_precision_loss_remainders_conversion)
161 AWS_TEST_CASE(test_overflow_conversion, s_test_overflow_conversion)
162
163 #define ONE_MHZ 1000000ULL
164 #define THREE_MHZ 3000000ULL
165 #define TEN_MHZ 10000000ULL
166 #define SIXTEEN_MHZ 16000000ULL
167 #define ONE_GHZ 1000000000ULL
168 #define TWO_GHZ 2000000000ULL
169 #define THIRTY_DAYS_IN_SECONDS (30ULL * 24 * 3600)
170 #define SIXTY_DAYS_IN_SECONDS (60ULL * 24 * 3600)
171 #define FIVE_YEARS_IN_SECONDS (5ULL * 365 * 24 * 3600)
172
173 /*
174 * A test for a variety of edge cases that would unnecessarily overflow the old conversion logic.
175 */
176 static int s_test_old_overflow_cases(struct aws_allocator *allocator, void *ctx) {
177 (void)allocator;
178 (void)ctx;
179
180 uint64_t timestamp = 0;
181
182 /*
183 * https://github.com/awslabs/aws-c-common/issues/790
184 */
185 timestamp = aws_timestamp_convert_u64(18446744073710ULL, ONE_MHZ, AWS_TIMESTAMP_NANOS, NULL);
186 ASSERT_UINT_EQUALS(timestamp, 18446744073710000ULL);
187
188 /*
189 * 30 days of ticks at 3Mhz to nanos: https://github.com/awslabs/aws-c-common/pull/791#issuecomment-821784745
190 */
191 timestamp = aws_timestamp_convert_u64(THIRTY_DAYS_IN_SECONDS * THREE_MHZ, THREE_MHZ, AWS_TIMESTAMP_NANOS, NULL);
192 ASSERT_UINT_EQUALS(timestamp, 2592000000000000ULL);
193
194 /*
195 * Same duration, but at 16Mhz
196 */
197 timestamp = aws_timestamp_convert_u64(THIRTY_DAYS_IN_SECONDS * SIXTEEN_MHZ, SIXTEEN_MHZ, AWS_TIMESTAMP_NANOS, NULL);
198 ASSERT_UINT_EQUALS(timestamp, 2592000000000000ULL);
199
200 /*
201 * 60 days at 1ghz (could be shortcutted internally since frequencies are equal)
202 */
203 timestamp = aws_timestamp_convert_u64(SIXTY_DAYS_IN_SECONDS * ONE_GHZ, ONE_GHZ, AWS_TIMESTAMP_NANOS, NULL);
204 ASSERT_UINT_EQUALS(timestamp, 5184000000000000ULL);
205
206 /*
207 * 60 days at 2ghz
208 */
209 timestamp = aws_timestamp_convert_u64(SIXTY_DAYS_IN_SECONDS * TWO_GHZ, TWO_GHZ, AWS_TIMESTAMP_NANOS, NULL);
210 ASSERT_UINT_EQUALS(timestamp, 5184000000000000ULL);
211
212 /*
213 * 60 days at 2ghz with a little bit more for some remainder
214 */
215 timestamp = aws_timestamp_convert_u64(SIXTY_DAYS_IN_SECONDS * TWO_GHZ + 123, TWO_GHZ, AWS_TIMESTAMP_NANOS, NULL);
216 ASSERT_UINT_EQUALS(timestamp, 5184000000000061ULL);
217
218 /*
219 * Five years at 10mhz + remainder
220 */
221 timestamp = aws_timestamp_convert_u64(FIVE_YEARS_IN_SECONDS * TEN_MHZ + 5, TEN_MHZ, AWS_TIMESTAMP_NANOS, NULL);
222 ASSERT_UINT_EQUALS(timestamp, 157680000000000500ULL);
223
224 /*
225 * large ns -> 1mhz
226 */
227 timestamp = aws_timestamp_convert_u64(THIRTY_DAYS_IN_SECONDS * ONE_GHZ + 123456789, ONE_GHZ, ONE_MHZ, NULL);
228 ASSERT_UINT_EQUALS(timestamp, 2592000000000ULL + 123456);
229
230 /*
231 * large ns -> 3mhz
232 */
233 timestamp = aws_timestamp_convert_u64(FIVE_YEARS_IN_SECONDS * ONE_GHZ + 1001, ONE_GHZ, THREE_MHZ, NULL);
234 ASSERT_UINT_EQUALS(timestamp, 473040000000000ULL + 3);
235
236 return 0;
237 }
238
239 AWS_TEST_CASE(test_old_overflow_cases, s_test_old_overflow_cases)