Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/tickcounter_ut/tickcounter_ut.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #ifdef __cplus
5 #include <cstdlib>
6 #include <cstddef>
7 #else
8 #include <stdlib.h>
9 #include <stddef.h>
10 #endif
11
12 #include "azure_macro_utils/macro_utils.h"
13 #include "testrunnerswitcher.h"
14 #include "azure_c_shared_utility/threadapi.h"
15
16 static TEST_MUTEX_HANDLE g_testByTest;
17
18 void* my_gballoc_malloc(size_t size)
19 {
20 return malloc(size);
21 }
22
23 void my_gballoc_free(void* ptr)
24 {
25 free(ptr);
26 }
27
28 #include "azure_c_shared_utility/tickcounter.h"
29
30 #define ENABLE_MOCKS
31
32 #include "azure_c_shared_utility/gballoc.h"
33
34 #define BUSY_LOOP_TIME 1000000
35
36 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
37
38 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
39 {
40 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
41 }
42
43 BEGIN_TEST_SUITE(tickcounter_unittests)
44
45 TEST_SUITE_INITIALIZE(suite_init)
46 {
47 g_testByTest = TEST_MUTEX_CREATE();
48 ASSERT_IS_NOT_NULL(g_testByTest);
49
50 umock_c_init(on_umock_c_error);
51
52 REGISTER_UMOCK_ALIAS_TYPE(TICK_COUNTER_HANDLE, void*);
53
54 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
55 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
56 }
57
58 TEST_SUITE_CLEANUP(suite_cleanup)
59 {
60 umock_c_deinit();
61
62 TEST_MUTEX_DESTROY(g_testByTest);
63 }
64
65 TEST_FUNCTION_INITIALIZE(TestMethodInitialize)
66 {
67 if (TEST_MUTEX_ACQUIRE(g_testByTest))
68 {
69 ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
70 }
71
72 umock_c_reset_all_calls();
73 }
74
75 TEST_FUNCTION_CLEANUP(TestMethodCleanup)
76 {
77 TEST_MUTEX_RELEASE(g_testByTest);
78 }
79
80 TEST_FUNCTION(tickcounter_create_fails)
81 {
82 ///arrange
83 TICK_COUNTER_HANDLE tickHandle;
84 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
85 .IgnoreArgument(1)
86 .SetReturn((void*)NULL);
87
88 ///act
89 tickHandle = tickcounter_create();
90
91 ///assert
92 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
93 ASSERT_IS_NULL(tickHandle);
94 }
95
96 TEST_FUNCTION(tickcounter_create_succeed)
97 {
98 ///arrange
99 TICK_COUNTER_HANDLE tickHandle;
100 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
101 .IgnoreArgument(1);
102
103 ///act
104 tickHandle = tickcounter_create();
105
106 ///assert
107 ASSERT_IS_NOT_NULL(tickHandle);
108 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
109
110 tickcounter_destroy(tickHandle);
111 }
112
113 TEST_FUNCTION(tickcounter_destroy_tick_counter_NULL_succeed)
114 {
115 ///arrange
116
117 ///act
118 tickcounter_destroy(NULL);
119
120 ///assert
121 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
122 }
123
124 TEST_FUNCTION(tickcounter_destroy_succeed)
125 {
126 ///arrange
127 TICK_COUNTER_HANDLE tickHandle = tickcounter_create();
128 umock_c_reset_all_calls();
129
130 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
131 .IgnoreArgument(1);
132
133 ///act
134 tickcounter_destroy(tickHandle);
135
136 ///assert
137 ASSERT_IS_NOT_NULL(tickHandle);
138 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
139 }
140
141 TEST_FUNCTION(tickcounter_get_current_ms_tick_counter_NULL_fail)
142 {
143 ///arrange
144 tickcounter_ms_t current_ms = 0;
145
146 ///act
147 int result = tickcounter_get_current_ms(NULL, &current_ms);
148
149 ///assert
150 ASSERT_ARE_NOT_EQUAL(int, 0, result);
151 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
152 }
153
154 TEST_FUNCTION(tickcounter_get_current_ms_current_ms_NULL_fail)
155 {
156 ///arrange
157 int result;
158 TICK_COUNTER_HANDLE tickHandle = tickcounter_create();
159 umock_c_reset_all_calls();
160
161 ///act
162 result = tickcounter_get_current_ms(tickHandle, NULL);
163
164 ///assert
165 ASSERT_ARE_NOT_EQUAL(int, 0, result);
166 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
167
168 tickcounter_destroy(tickHandle);
169 }
170
171 TEST_FUNCTION(tickcounter_get_current_ms_succeed)
172 {
173 ///arrange
174 int result;
175 tickcounter_ms_t current_ms;
176 TICK_COUNTER_HANDLE tickHandle = tickcounter_create();
177 umock_c_reset_all_calls();
178
179 current_ms = 0;
180
181 ///act
182 result = tickcounter_get_current_ms(tickHandle, &current_ms);
183
184 ///assert
185 ASSERT_ARE_EQUAL(int, 0, result);
186 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
187
188 /// clean
189 tickcounter_destroy(tickHandle);
190 }
191
192 //TEST_FUNCTION(tickcounter_get_current_ms_validate_tick_succeed)
193 //{
194 // ///arrange
195 // CTickCounterMocks mocks;
196 // TICK_COUNTER_HANDLE tickHandle = tickcounter_create();
197 // umock_c_reset_all_calls();
198 //
199 // uint64_t first_ms = 0;
200 //
201 // ThreadAPI_Sleep(1250);
202 //
203 // ///act
204 // int result = tickcounter_get_current_ms(tickHandle, &first_ms);
205 //
206 // // busy loop here
207 // ThreadAPI_Sleep(1250);
208 //
209 // uint64_t next_ms = 0;
210 //
211 // int resultAlso = tickcounter_get_current_ms(tickHandle, &next_ms);
212 //
213 // ///assert
214 // ASSERT_ARE_EQUAL(int, 0, result);
215 // ASSERT_ARE_EQUAL(int, 0, resultAlso);
216 // ASSERT_IS_TRUE(first_ms > 0);
217 // ASSERT_IS_TRUE(next_ms > first_ms);
218 // ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
219 //
220 // /// clean
221 // tickcounter_destroy(tickHandle);
222 //}
223
224 END_TEST_SUITE(tickcounter_unittests)