| 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 __cplusplus |
| 5 |
#include <ctime> |
| 6 |
#else |
| 7 |
#include <time.h> |
| 8 |
#endif |
| 9 |
|
| 10 |
#include "testrunnerswitcher.h" |
| 11 |
#include "azure_c_shared_utility/agenttime.h" |
| 12 |
|
| 13 |
static TEST_MUTEX_HANDLE g_testByTest; |
| 14 |
|
| 15 |
time_t my_time64(time_t * _Time) |
| 16 |
{ |
| 17 |
(void)_Time; |
| 18 |
return (int64_t)-1; |
| 19 |
} |
| 20 |
|
| 21 |
struct tm* my_gmtime64(time_t* _Time) |
| 22 |
{ |
| 23 |
(void)_Time; |
| 24 |
return NULL; |
| 25 |
} |
| 26 |
|
| 27 |
struct tm* my_localtime64(time_t * _Time) |
| 28 |
{ |
| 29 |
(void)_Time; |
| 30 |
return NULL; |
| 31 |
} |
| 32 |
|
| 33 |
time_t my_mktime64(struct tm* _Time) |
| 34 |
{ |
| 35 |
(void)_Time; |
| 36 |
return (int64_t)-1; |
| 37 |
} |
| 38 |
|
| 39 |
BEGIN_TEST_SUITE(agenttime_unittests) |
| 40 |
|
| 41 |
TEST_SUITE_INITIALIZE(TestClassInitialize) |
| 42 |
{ |
| 43 |
g_testByTest = TEST_MUTEX_CREATE(); |
| 44 |
ASSERT_IS_NOT_NULL(g_testByTest); |
| 45 |
} |
| 46 |
|
| 47 |
TEST_SUITE_CLEANUP(TestClassCleanup) |
| 48 |
{ |
| 49 |
TEST_MUTEX_DESTROY(g_testByTest); |
| 50 |
} |
| 51 |
|
| 52 |
TEST_FUNCTION_INITIALIZE(TestMethodInitialize) |
| 53 |
{ |
| 54 |
if (TEST_MUTEX_ACQUIRE(g_testByTest)) |
| 55 |
{ |
| 56 |
ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework"); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
TEST_FUNCTION_CLEANUP(TestMethodCleanup) |
| 61 |
{ |
| 62 |
TEST_MUTEX_RELEASE(g_testByTest); |
| 63 |
} |
| 64 |
|
| 65 |
TEST_FUNCTION(get_time_succeed) |
| 66 |
{ |
| 67 |
///act |
| 68 |
time_t t; |
| 69 |
time_t result = get_time(&t); |
| 70 |
|
| 71 |
///assert |
| 72 |
ASSERT_ARE_EQUAL(int, (int)result, (int)t); |
| 73 |
} |
| 74 |
|
| 75 |
TEST_FUNCTION(get_gmtime_success) |
| 76 |
{ |
| 77 |
///act |
| 78 |
time_t now; |
| 79 |
struct tm* p; |
| 80 |
now = get_time(NULL); |
| 81 |
p = get_gmtime(&now); |
| 82 |
|
| 83 |
///assert |
| 84 |
ASSERT_IS_NOT_NULL(p); |
| 85 |
} |
| 86 |
|
| 87 |
TEST_FUNCTION(get_difftime_success) |
| 88 |
{ |
| 89 |
time_t now, sometimeAfterNow; |
| 90 |
double diff; |
| 91 |
now = get_time(NULL); |
| 92 |
sometimeAfterNow = now + 42; /*whatever this is*/ |
| 93 |
|
| 94 |
///act |
| 95 |
diff = get_difftime(sometimeAfterNow, now); |
| 96 |
|
| 97 |
///assert |
| 98 |
ASSERT_ARE_EQUAL(double, difftime(sometimeAfterNow, now), diff); |
| 99 |
} |
| 100 |
|
| 101 |
END_TEST_SUITE(agenttime_unittests) |