| 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 <cstdlib> |
| 6 |
#else |
| 7 |
#include <stdlib.h> |
| 8 |
#endif |
| 9 |
|
| 10 |
#include "testrunnerswitcher.h" |
| 11 |
#include "umock_c/umockstring.h" |
| 12 |
#include "umock_c/umock_log.h" |
| 13 |
|
| 14 |
void UMOCK_LOG(const char* format, ...) |
| 15 |
{ |
| 16 |
(void)format; |
| 17 |
} |
| 18 |
|
| 19 |
typedef struct test_malloc_CALL_TAG |
| 20 |
{ |
| 21 |
size_t size; |
| 22 |
} test_malloc_CALL; |
| 23 |
|
| 24 |
static test_malloc_CALL* test_malloc_calls; |
| 25 |
static size_t test_malloc_call_count; |
| 26 |
static size_t when_shall_malloc_fail; |
| 27 |
|
| 28 |
#ifdef __cplusplus |
| 29 |
extern "C" { |
| 30 |
#endif |
| 31 |
|
| 32 |
void* mock_malloc(size_t size) |
| 33 |
{ |
| 34 |
void* result; |
| 35 |
|
| 36 |
test_malloc_CALL* new_calls = (test_malloc_CALL*)realloc(test_malloc_calls, sizeof(test_malloc_CALL) * (test_malloc_call_count + 1)); |
| 37 |
if (new_calls != NULL) |
| 38 |
{ |
| 39 |
test_malloc_calls = new_calls; |
| 40 |
test_malloc_calls[test_malloc_call_count].size = size; |
| 41 |
test_malloc_call_count++; |
| 42 |
} |
| 43 |
|
| 44 |
if (when_shall_malloc_fail == test_malloc_call_count) |
| 45 |
{ |
| 46 |
result = NULL; |
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
result = malloc(size); |
| 51 |
} |
| 52 |
|
| 53 |
return result; |
| 54 |
} |
| 55 |
|
| 56 |
#ifdef __cplusplus |
| 57 |
} |
| 58 |
#endif |
| 59 |
|
| 60 |
static TEST_MUTEX_HANDLE test_mutex; |
| 61 |
static TEST_MUTEX_HANDLE global_mutex; |
| 62 |
|
| 63 |
BEGIN_TEST_SUITE(umockstring_unittests) |
| 64 |
|
| 65 |
TEST_SUITE_INITIALIZE(suite_init) |
| 66 |
{ |
| 67 |
test_mutex = TEST_MUTEX_CREATE(); |
| 68 |
ASSERT_IS_NOT_NULL(test_mutex); |
| 69 |
} |
| 70 |
|
| 71 |
TEST_SUITE_CLEANUP(suite_cleanup) |
| 72 |
{ |
| 73 |
TEST_MUTEX_DESTROY(test_mutex); |
| 74 |
} |
| 75 |
|
| 76 |
TEST_FUNCTION_INITIALIZE(test_function_init) |
| 77 |
{ |
| 78 |
int mutex_acquire_result = TEST_MUTEX_ACQUIRE(test_mutex); |
| 79 |
ASSERT_ARE_EQUAL(int, 0, mutex_acquire_result); |
| 80 |
|
| 81 |
when_shall_malloc_fail = 0; |
| 82 |
|
| 83 |
test_malloc_calls = NULL; |
| 84 |
test_malloc_call_count = 0; |
| 85 |
} |
| 86 |
|
| 87 |
TEST_FUNCTION_CLEANUP(test_function_cleanup) |
| 88 |
{ |
| 89 |
free(test_malloc_calls); |
| 90 |
test_malloc_calls = NULL; |
| 91 |
test_malloc_call_count = 0; |
| 92 |
|
| 93 |
TEST_MUTEX_RELEASE(test_mutex); |
| 94 |
} |
| 95 |
|
| 96 |
/* umockstring_clone */ |
| 97 |
|
| 98 |
/* Tests_UMOCK_STRING_01_001: [ umockstring_clone shall allocate memory for the cloned string (including the NULL terminator). ]*/ |
| 99 |
/* Tests_UMOCK_STRING_01_002: [ umockstring_clone shall copy the string to the newly allocated memory (including the NULL terminator). ]*/ |
| 100 |
/* Tests_UMOCK_STRING_01_003: [ On success umockstring_clone shall return a pointer to the newly allocated memory containing the copy of the string. ]*/ |
| 101 |
TEST_FUNCTION(umockstring_clone_with_an_empty_string_succeeds) |
| 102 |
{ |
| 103 |
// arrange |
| 104 |
char* result; |
| 105 |
|
| 106 |
// act |
| 107 |
result = umockstring_clone(""); |
| 108 |
|
| 109 |
// assert |
| 110 |
ASSERT_ARE_EQUAL(char_ptr, "", result); |
| 111 |
ASSERT_ARE_EQUAL(size_t, 1, test_malloc_call_count); |
| 112 |
ASSERT_ARE_EQUAL(size_t, 1, test_malloc_calls[0].size); |
| 113 |
|
| 114 |
// cleanup |
| 115 |
free(result); |
| 116 |
} |
| 117 |
|
| 118 |
/* Tests_UMOCK_STRING_01_001: [ umockstring_clone shall allocate memory for the cloned string (including the NULL terminator). ]*/ |
| 119 |
/* Tests_UMOCK_STRING_01_002: [ umockstring_clone shall copy the string to the newly allocated memory (including the NULL terminator). ]*/ |
| 120 |
/* Tests_UMOCK_STRING_01_003: [ On success umockstring_clone shall return a pointer to the newly allocated memory containing the copy of the string. ]*/ |
| 121 |
TEST_FUNCTION(umockstring_clone_with_a_one_char_string_succeeds) |
| 122 |
{ |
| 123 |
// arrange |
| 124 |
char* result; |
| 125 |
|
| 126 |
// act |
| 127 |
result = umockstring_clone("a"); |
| 128 |
|
| 129 |
// assert |
| 130 |
ASSERT_ARE_EQUAL(char_ptr, "a", result); |
| 131 |
ASSERT_ARE_EQUAL(size_t, 1, test_malloc_call_count); |
| 132 |
ASSERT_ARE_EQUAL(size_t, 2, test_malloc_calls[0].size); |
| 133 |
|
| 134 |
// cleanup |
| 135 |
free(result); |
| 136 |
} |
| 137 |
|
| 138 |
/* Tests_UMOCK_STRING_01_001: [ umockstring_clone shall allocate memory for the cloned string (including the NULL terminator). ]*/ |
| 139 |
/* Tests_UMOCK_STRING_01_002: [ umockstring_clone shall copy the string to the newly allocated memory (including the NULL terminator). ]*/ |
| 140 |
/* Tests_UMOCK_STRING_01_003: [ On success umockstring_clone shall return a pointer to the newly allocated memory containing the copy of the string. ]*/ |
| 141 |
TEST_FUNCTION(umockstring_clone_with_a_longer_string_succeeds) |
| 142 |
{ |
| 143 |
// arrange |
| 144 |
char* result; |
| 145 |
|
| 146 |
// act |
| 147 |
result = umockstring_clone("Management takes the code out of you"); |
| 148 |
|
| 149 |
// assert |
| 150 |
ASSERT_ARE_EQUAL(char_ptr, "Management takes the code out of you", result); |
| 151 |
ASSERT_ARE_EQUAL(size_t, 1, test_malloc_call_count); |
| 152 |
ASSERT_ARE_EQUAL(size_t, strlen("Management takes the code out of you") + 1, test_malloc_calls[0].size); |
| 153 |
|
| 154 |
// cleanup |
| 155 |
free(result); |
| 156 |
} |
| 157 |
|
| 158 |
/* Tests_UMOCK_STRING_01_004: [ If allocating the memory fails, umockstring_clone shall return NULL. ]*/ |
| 159 |
TEST_FUNCTION(when_allocating_memory_fails_umockstring_clone_fails) |
| 160 |
{ |
| 161 |
// arrange |
| 162 |
char* result; |
| 163 |
|
| 164 |
when_shall_malloc_fail = 1; |
| 165 |
|
| 166 |
// act |
| 167 |
result = umockstring_clone("Management takes the code out of you"); |
| 168 |
|
| 169 |
// assert |
| 170 |
ASSERT_IS_NULL(result); |
| 171 |
ASSERT_ARE_EQUAL(size_t, 1, test_malloc_call_count); |
| 172 |
ASSERT_ARE_EQUAL(size_t, strlen("Management takes the code out of you") + 1, test_malloc_calls[0].size); |
| 173 |
} |
| 174 |
|
| 175 |
/* Tests_UMOCK_STRING_01_005: [ If umockstring_clone is called with a NULL source, it shall return NULL. ]*/ |
| 176 |
TEST_FUNCTION(umockstring_clone_called_with_NULL_fails) |
| 177 |
{ |
| 178 |
// arrange |
| 179 |
char* result; |
| 180 |
|
| 181 |
// act |
| 182 |
result = umockstring_clone(NULL); |
| 183 |
|
| 184 |
// assert |
| 185 |
ASSERT_IS_NULL(result); |
| 186 |
ASSERT_ARE_EQUAL(size_t, 0, test_malloc_call_count); |
| 187 |
} |
| 188 |
|
| 189 |
END_TEST_SUITE(umockstring_unittests) |