Debian Codegraph @

tests/CppUTestExt/CodeMemoryReporterTest.cpp

raw symbols SCIP
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/TestOutput.h"
30 #include "CppUTestExt/MemoryReportAllocator.h"
31 #include "CppUTestExt/CodeMemoryReportFormatter.h"
32
33 #define TESTOUTPUT_EQUAL(a) STRCMP_EQUAL_LOCATION(a, testOutput.getOutput().asCharString(), "", __FILE__, __LINE__)
34 #define TESTOUTPUT_CONTAINS(a) STRCMP_CONTAINS_LOCATION(a, testOutput.getOutput().asCharString(), "", __FILE__, __LINE__)
35
36 TEST_GROUP(CodeMemoryReportFormatter)
37 {
38 TestMemoryAllocator* cAllocator;
39 TestMemoryAllocator* newAllocator;
40 TestMemoryAllocator* newArrayAllocator;
41 char* memory01;
42 char* memory02;
43
44 StringBufferTestOutput testOutput;
45 TestResult* testResult;
46 CodeMemoryReportFormatter* formatter;
47
48 void setup()
49 {
50 cAllocator = defaultMallocAllocator();
51 newAllocator = defaultNewAllocator();
52 newArrayAllocator= defaultNewArrayAllocator();
53 memory01 = (char*) 0x01;
54 memory02 = (char*) 0x02;
55
56 formatter = new CodeMemoryReportFormatter(cAllocator);
57 testResult = new TestResult(testOutput);
58 }
59
60 void teardown()
61 {
62 delete testResult;
63 delete formatter;
64 }
65 };
66
67
68 TEST(CodeMemoryReportFormatter, mallocCreatesAnMallocCall)
69 {
70 formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "file", 9);
71 TESTOUTPUT_EQUAL("\tvoid* file_9_1 = malloc(10);\n");
72 }
73
74 TEST(CodeMemoryReportFormatter, freeCreatesAnFreeCall)
75 {
76 formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "file", 9);
77 testOutput.flush();
78 formatter->report_free_memory(testResult, cAllocator, memory01, "boo", 6);
79 TESTOUTPUT_EQUAL("\tfree(file_9_1); /* at boo:6 */\n");
80 }
81
82 TEST(CodeMemoryReportFormatter, twoMallocAndTwoFree)
83 {
84 formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "file", 2);
85 formatter->report_alloc_memory(testResult, cAllocator, 10, memory02, "boo", 4);
86 testOutput.flush();
87 formatter->report_free_memory(testResult, cAllocator, memory01, "foo", 6);
88 formatter->report_free_memory(testResult, cAllocator, memory02, "bar", 8);
89 TESTOUTPUT_CONTAINS("\tfree(file_2_1); /* at foo:6 */\n");
90 TESTOUTPUT_CONTAINS("\tfree(boo_4_1); /* at bar:8 */\n");
91 }
92
93 TEST(CodeMemoryReportFormatter, variableNamesShouldNotContainSlahses)
94 {
95 formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "dir/file", 2);
96 TESTOUTPUT_CONTAINS("\tvoid* file_2");
97 }
98
99 TEST(CodeMemoryReportFormatter, variableNamesShouldNotContainDotButUseUnderscore)
100 {
101 formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "foo.cpp", 2);
102 TESTOUTPUT_CONTAINS("foo_cpp");
103 }
104
105 TEST(CodeMemoryReportFormatter, newArrayAllocatorGeneratesNewArrayCode)
106 {
107 formatter->report_alloc_memory(testResult, newArrayAllocator, 10, memory01, "file", 8);
108 TESTOUTPUT_CONTAINS("char* file_8_1 = new char[10]; /* using new [] */");
109 }
110
111 TEST(CodeMemoryReportFormatter, newArrayGeneratesNewCode)
112 {
113 formatter->report_alloc_memory(testResult, newAllocator, 6, memory01, "file", 4);
114 TESTOUTPUT_CONTAINS("new char[6]; /* using new */");
115 }
116
117 TEST(CodeMemoryReportFormatter, NewAllocatorGeneratesDeleteCode)
118 {
119 formatter->report_alloc_memory(testResult, newAllocator, 10, memory01, "file", 8);
120 testOutput.flush();
121 formatter->report_free_memory(testResult, newAllocator, memory01, "boo", 4);
122 TESTOUTPUT_CONTAINS("delete [] file_8_1; /* using delete at boo:4 */");
123 }
124
125 TEST(CodeMemoryReportFormatter, DeleteNullWorksFine)
126 {
127 formatter->report_free_memory(testResult, newAllocator, NULLPTR, "boo", 4);
128 TESTOUTPUT_CONTAINS("delete [] NULL; /* using delete at boo:4 */");
129 }
130
131 TEST(CodeMemoryReportFormatter, NewArrayAllocatorGeneratesDeleteArrayCode)
132 {
133 formatter->report_alloc_memory(testResult, newArrayAllocator, 10, memory01, "file", 8);
134 testOutput.flush();
135 formatter->report_free_memory(testResult, newArrayAllocator, memory01, "boo", 4);
136 TESTOUTPUT_CONTAINS("delete [] file_8_1; /* using delete [] at boo:4 */");
137 }
138
139 TEST(CodeMemoryReportFormatter, allocationUsingMallocOnTheSameLineDoesntGenerateTheSameVariableTwice)
140 {
141 formatter->report_alloc_memory(testResult, cAllocator, 10, memory01, "file", 8);
142 testOutput.flush();
143 formatter->report_alloc_memory(testResult, cAllocator, 10, memory02, "file", 8);
144 CHECK(testOutput.getOutput().contains("2"));
145 }
146
147 TEST(CodeMemoryReportFormatter, allocationUsingNewcOnTheSameLineDoesntGenerateTheSameVariableTwice)
148 {
149 formatter->report_alloc_memory(testResult, newAllocator, 10, memory01, "file", 8);
150 testOutput.flush();
151 formatter->report_alloc_memory(testResult, newAllocator, 10, memory01, "file", 8);
152 CHECK(testOutput.getOutput().contains("2"));
153 }
154
155 TEST(CodeMemoryReportFormatter, allocationUsingNewcOnTheSameLineDoesntGenerateVariableTwiceExceptWhenInANewTest)
156 {
157 formatter->report_alloc_memory(testResult, newAllocator, 10, memory01, "file", 8);
158 formatter->report_test_start(testResult, *UtestShell::getCurrent());
159 testOutput.flush();
160 formatter->report_alloc_memory(testResult, newAllocator, 10, memory01, "file", 8);
161 CHECK(testOutput.getOutput().contains("char*"));
162 }
163
164 TEST(CodeMemoryReportFormatter, testStartGeneratesTESTcode)
165 {
166 UtestShell test("groupName", "testName", "fileName", 1);
167 formatter->report_test_start(testResult, test);
168 TESTOUTPUT_EQUAL("*/\nTEST(groupName_memoryReport, testName)\n{ /* at fileName:1 */\n");
169 }
170
171 TEST(CodeMemoryReportFormatter, testEndGeneratesTESTcode)
172 {
173 UtestShell test("groupName", "testName", "fileName", 1);
174 formatter->report_test_end(testResult, test);
175 TESTOUTPUT_EQUAL("}/*");
176 }
177
178 TEST(CodeMemoryReportFormatter, TestGroupGeneratesTestGroupCode)
179 {
180 UtestShell test("groupName", "testName", "fileName", 1);
181 formatter->report_testgroup_start(testResult, test);
182 TESTOUTPUT_EQUAL("*/TEST_GROUP(groupName_memoryReport)\n{\n};\n/*");
183 }
184
185 TEST(CodeMemoryReportFormatter, VariableFromFileLineInfoAlreadyExists)
186 {
187 for(int i = 1; i < 100; i++) {
188 formatter->report_alloc_memory(testResult, newArrayAllocator, 10, memory01, "file", 8);
189 }
190 formatter->report_alloc_memory(testResult, newArrayAllocator, 10, memory01, "file", 8);
191 testOutput.flush();
192 formatter->report_free_memory(testResult, newArrayAllocator, memory01, "boo", 8);
193 TESTOUTPUT_CONTAINS("delete [] ; /* using delete [] at boo:8 */");
194 }
195
196 // TODO: do!
197 /* Dealloc without alloc */
198 /* Remove the ugly comments by controlling the output! */
199 /* Write tests for the variable name lengths */