• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thetic / cpputest / 12942590675

24 Jan 2025 03:17AM UTC coverage: 96.261% (-2.3%) from 98.514%
12942590675

push

github

thetic
fixup! Delete leak macros

6334 of 6580 relevant lines covered (96.26%)

17570.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.94
/src/CppUTest/TestHarness_c.cpp
1
/*
2
 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *     * Redistributions of source code must retain the above copyright
8
 *       notice, this list of conditions and the following disclaimer.
9
 *     * Redistributions in binary form must reproduce the above copyright
10
 *       notice, this list of conditions and the following disclaimer in the
11
 *       documentation and/or other materials provided with the distribution.
12
 *     * Neither the name of the <organization> nor the
13
 *       names of its contributors may be used to endorse or promote products
14
 *       derived from this software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ''AS IS'' AND ANY
17
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27

28
#include "CppUTest/TestHarness.h"
29
#include "CppUTest/MemoryLeakDetector.h"
30
#include "CppUTest/TestMemoryAllocator.h"
31
#include "CppUTest/PlatformSpecificFunctions.h"
32
#include "CppUTest/TestHarness_c.h"
33

34
extern "C"
35
{
36

37
void CHECK_EQUAL_C_BOOL_LOCATION(int expected, int actual, const char* text, const char* fileName, size_t lineNumber)
6✔
38
{
39
    UtestShell::getCurrent()->assertEquals(!!expected != !!actual, expected ? "true" : "false", actual ? "true" : "false", text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
6✔
40
}
4✔
41

42
void CHECK_EQUAL_C_INT_LOCATION(int expected, int actual, const char* text, const char* fileName, size_t lineNumber)
4✔
43
{
44
    UtestShell::getCurrent()->assertLongsEqual((long)expected, (long)actual, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
45
}
2✔
46

47
void CHECK_EQUAL_C_UINT_LOCATION(unsigned int expected, unsigned int actual, const char* text, const char* fileName, size_t lineNumber)
4✔
48
{
49
    UtestShell::getCurrent()->assertUnsignedLongsEqual((unsigned long)expected, (unsigned long)actual, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
50
}
2✔
51

52
void CHECK_EQUAL_C_LONG_LOCATION(long expected, long actual, const char* text, const char* fileName, size_t lineNumber)
4✔
53
{
54
    UtestShell::getCurrent()->assertLongsEqual(expected, actual, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
55
}
2✔
56

57
void CHECK_EQUAL_C_ULONG_LOCATION(unsigned long expected, unsigned long actual, const char* text, const char* fileName, size_t lineNumber)
4✔
58
{
59
    UtestShell::getCurrent()->assertUnsignedLongsEqual(expected, actual, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
60
}
2✔
61

62
void CHECK_EQUAL_C_LONGLONG_LOCATION(cpputest_longlong expected, cpputest_longlong actual, const char* text, const char* fileName, size_t lineNumber)
4✔
63
{
64
    UtestShell::getCurrent()->assertLongLongsEqual(expected, actual, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
65
}
2✔
66

67
void CHECK_EQUAL_C_ULONGLONG_LOCATION(cpputest_ulonglong expected, cpputest_ulonglong actual, const char* text, const char* fileName, size_t lineNumber)
4✔
68
{
69
    UtestShell::getCurrent()->assertUnsignedLongLongsEqual(expected, actual, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
70
}
2✔
71

72
void CHECK_EQUAL_C_REAL_LOCATION(double expected, double actual, double threshold, const char* text, const char* fileName, size_t lineNumber)
4✔
73
{
74
    UtestShell::getCurrent()->assertDoublesEqual(expected, actual, threshold, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
75
}
2✔
76

77
void CHECK_EQUAL_C_CHAR_LOCATION(char expected, char actual, const char* text, const char* fileName, size_t lineNumber)
4✔
78
{
79
    UtestShell::getCurrent()->assertEquals(((expected) != (actual)), StringFrom(expected).asCharString(), StringFrom(actual).asCharString(), text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
80
}
2✔
81

82
extern void CHECK_EQUAL_C_UBYTE_LOCATION(unsigned char expected, unsigned char actual, const char* text, const char* fileName, size_t lineNumber)\
4✔
83
{
84
    UtestShell::getCurrent()->assertEquals(((expected) != (actual)),StringFrom((int)expected).asCharString(), StringFrom((int) actual).asCharString(), text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
85
}
2✔
86

87
void CHECK_EQUAL_C_SBYTE_LOCATION(char signed expected, signed char actual, const char* text, const char* fileName, size_t lineNumber)
4✔
88
{
89
    UtestShell::getCurrent()->assertEquals(((expected) != (actual)),StringFrom((int)expected).asCharString(), StringFrom((int) actual).asCharString(), text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
90
}
2✔
91

92
void CHECK_EQUAL_C_STRING_LOCATION(const char* expected, const char* actual, const char* text, const char* fileName, size_t lineNumber)
4✔
93
{
94
    UtestShell::getCurrent()->assertCstrEqual(expected, actual, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
95
}
2✔
96

97
void CHECK_EQUAL_C_POINTER_LOCATION(const void* expected, const void* actual, const char* text, const char* fileName, size_t lineNumber)
4✔
98
{
99
    UtestShell::getCurrent()->assertPointersEqual(expected, actual, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
100
}
2✔
101

102
extern void CHECK_EQUAL_C_MEMCMP_LOCATION(const void* expected, const void* actual, size_t size, const char* text, const char* fileName, size_t lineNumber)
4✔
103
{
104
    UtestShell::getCurrent()->assertBinaryEqual(expected, actual, size, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
105
}
2✔
106

107
extern void CHECK_EQUAL_C_BITS_LOCATION(unsigned int expected, unsigned int actual, unsigned int mask, size_t size, const char* text, const char* fileName, size_t lineNumber)
4✔
108
{
109
    UtestShell::getCurrent()->assertBitsEqual(expected, actual, mask, size, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
4✔
110
}
2✔
111

112
void FAIL_TEXT_C_LOCATION(const char* text, const char* fileName, size_t lineNumber)
1✔
113
{
114
    UtestShell::getCurrent()->fail(text,  fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
1✔
115
} // LCOV_EXCL_LINE
116

117
void FAIL_C_LOCATION(const char* fileName, size_t lineNumber)
3✔
118
{
119
    UtestShell::getCurrent()->fail("",  fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
3✔
120
} // LCOV_EXCL_LINE
121

122
void CHECK_C_LOCATION(int condition, const char* conditionString, const char* text, const char* fileName, size_t lineNumber)
9✔
123
{
124
    UtestShell::getCurrent()->assertTrue(condition != 0, "CHECK_C", conditionString, text, fileName, lineNumber, UtestShell::getCurrentTestTerminatorWithoutExceptions());
9✔
125
}
7✔
126

127
enum { NO_COUNTDOWN = -1, OUT_OF_MEMORRY = 0 };
128
static int malloc_out_of_memory_counter = NO_COUNTDOWN;
129
static int malloc_count = 0;
130

131
void cpputest_malloc_count_reset(void)
×
132
{
133
    malloc_count = 0;
×
134
}
×
135

136
int cpputest_malloc_get_count()
×
137
{
138
    return malloc_count;
×
139
}
140

141
static TestMemoryAllocator* originalAllocator = NULLPTR;
142

143
void cpputest_malloc_set_out_of_memory()
×
144
{
145
    if (originalAllocator == NULLPTR)
×
146
        originalAllocator = getCurrentMallocAllocator();
×
147
    setCurrentMallocAllocator(NullUnknownAllocator::defaultAllocator());
×
148
}
×
149

150
void cpputest_malloc_set_not_out_of_memory()
×
151
{
152
    malloc_out_of_memory_counter = NO_COUNTDOWN;
×
153
    setCurrentMallocAllocator(originalAllocator);
×
154
    originalAllocator = NULLPTR;
×
155
}
×
156

157
void cpputest_malloc_set_out_of_memory_countdown(int count)
×
158
{
159
    malloc_out_of_memory_counter = count;
×
160
    if (malloc_out_of_memory_counter == OUT_OF_MEMORRY)
×
161
        cpputest_malloc_set_out_of_memory();
×
162
}
×
163

164
void* cpputest_malloc(size_t size)
×
165
{
166
    return cpputest_malloc_location(size, "<unknown>", 0);
×
167
}
168

169
char* cpputest_strdup(const char* str)
×
170
{
171
    return cpputest_strdup_location(str, "<unknown>", 0);
×
172
}
173

174
char* cpputest_strndup(const char* str, size_t n)
×
175
{
176
    return cpputest_strndup_location(str, n, "<unknown>", 0);
×
177
}
178

179
void* cpputest_calloc(size_t num, size_t size)
×
180
{
181
    return cpputest_calloc_location(num, size, "<unknown>", 0);
×
182
}
183

184
void* cpputest_realloc(void* ptr, size_t size)
×
185
{
186
    return cpputest_realloc_location(ptr, size, "<unknown>", 0);
×
187
}
188

189
void cpputest_free(void* buffer)
×
190
{
191
    cpputest_free_location(buffer, "<unknown>", 0);
×
192
}
×
193

194
static void countdown()
×
195
{
196
    if (malloc_out_of_memory_counter <= NO_COUNTDOWN)
×
197
        return;
×
198

199
    if (malloc_out_of_memory_counter == OUT_OF_MEMORRY)
×
200
        return;
×
201

202
    malloc_out_of_memory_counter--;
×
203

204
    if (malloc_out_of_memory_counter == OUT_OF_MEMORRY)
×
205
        cpputest_malloc_set_out_of_memory();
×
206
}
207

208
void* cpputest_malloc_location(size_t size, const char* file, size_t line)
×
209
{
210
    countdown();
×
211
    malloc_count++;
×
212
    return cpputest_malloc_location_with_leak_detection(size, file, line);
×
213
}
214

215
static size_t test_harness_c_strlen(const char * str)
×
216
{
217
    size_t n = 0;
×
218
    while (*str++) n++;
×
219
    return n;
×
220
}
221

222
static char* strdup_alloc(const char * str, size_t size, const char* file, size_t line)
×
223
{
224
    char* result = (char*) cpputest_malloc_location(size, file, line);
×
225
    PlatformSpecificMemCpy(result, str, size);
×
226
    result[size-1] = '\0';
×
227
    return result;
×
228
}
229

230
char* cpputest_strdup_location(const char * str, const char* file, size_t line)
×
231
{
232
    size_t length = 1 + test_harness_c_strlen(str);
×
233
    return strdup_alloc(str, length, file, line);
×
234
}
235

236
char* cpputest_strndup_location(const char * str, size_t n, const char* file, size_t line)
×
237
{
238
    size_t length = test_harness_c_strlen(str);
×
239
    length = length < n ? length : n;
×
240
    length = length + 1;
×
241
    return strdup_alloc(str, length, file, line);
×
242
}
243

244

245
void* cpputest_calloc_location(size_t num, size_t size, const char* file, size_t line)
×
246
{
247
    void* mem = cpputest_malloc_location(num * size, file, line);
×
248
    if (mem)
×
249
        PlatformSpecificMemset(mem, 0, num*size);
×
250
    return mem;
×
251
}
252

253
void* cpputest_realloc_location(void* memory, size_t size, const char* file, size_t line)
×
254
{
255
    return cpputest_realloc_location_with_leak_detection(memory, size, file, line);
×
256
}
257

258
void cpputest_free_location(void* buffer, const char* file, size_t line)
×
259
{
260
    cpputest_free_location_with_leak_detection(buffer, file, line);
×
261
}
×
262

263
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc