-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathctracker_test.cpp
More file actions
196 lines (148 loc) · 4.85 KB
/
Copy pathctracker_test.cpp
File metadata and controls
196 lines (148 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <gtest/gtest.h>
#include "ctracker.hpp"
// #define C_TRACKER_VERBOSE 1
// The singleton tracks ALL allocations globally (including gtest internals),
// so tests measure deltas from a baseline snapshot rather than absolute values.
// Helper: snapshot the current tracker state before test-controlled allocations
struct TrackerSnapshot
{
size_t record_count;
size_t total_allocated;
};
static TrackerSnapshot TakeSnapshot()
{
auto *t = CTrackerMetrics::GetTracker();
return {t->RecordCount, t->TotalAllocated()};
}
// --- Allocation Tracking ---
TEST(CTrackerTest, TrackSingleAllocation)
{
auto before = TakeSnapshot();
int *p = new int[10]; // 40 bytes
auto *t = CTrackerMetrics::GetTracker();
EXPECT_EQ(t->RecordCount, before.record_count + 1);
EXPECT_EQ(t->TotalAllocated(), before.total_allocated + sizeof(int) * 10);
delete[] p;
EXPECT_EQ(t->RecordCount, before.record_count);
EXPECT_EQ(t->TotalAllocated(), before.total_allocated);
}
TEST(CTrackerTest, TrackMultipleAllocations)
{
auto before = TakeSnapshot();
float *a = new float[5]; // 20 bytes
char *b = new char[100]; // 100 bytes
double *c = new double[8]; // 64 bytes
auto *t = CTrackerMetrics::GetTracker();
size_t expected_size = sizeof(float) * 5 + sizeof(char) * 100 + sizeof(double) * 8;
EXPECT_EQ(t->RecordCount, before.record_count + 3);
EXPECT_EQ(t->TotalAllocated(), before.total_allocated + expected_size);
delete[] a;
delete[] b;
delete[] c;
EXPECT_EQ(t->RecordCount, before.record_count);
EXPECT_EQ(t->TotalAllocated(), before.total_allocated);
}
// --- Free Tracking ---
TEST(CTrackerTest, FreeMiddleAllocation)
{
auto before = TakeSnapshot();
int *a = new int[1];
int *b = new int[1];
int *c = new int[1];
auto *t = CTrackerMetrics::GetTracker();
EXPECT_EQ(t->RecordCount, before.record_count + 3);
// Free the middle one — record count should drop by 1
delete[] b;
EXPECT_EQ(t->RecordCount, before.record_count + 2);
EXPECT_EQ(t->TotalAllocated(), before.total_allocated + sizeof(int) * 2);
delete[] a;
delete[] c;
EXPECT_EQ(t->RecordCount, before.record_count);
}
TEST(CTrackerTest, FreeUnknownPointerIsNoOp)
{
auto before = TakeSnapshot();
// Allocate via malloc (bypasses our operator new), then try to free-track it.
// CfreeTrack should silently do nothing.
void *raw = std::malloc(64);
CTrackerMetrics::GetTracker()->CfreeTrack(raw);
EXPECT_EQ(CTrackerMetrics::GetTracker()->RecordCount, before.record_count);
std::free(raw);
}
// --- Fragmentation ---
TEST(CTrackerTest, FragmentationIncreasesAfterFreeingMiddle)
{
// Allocate three contiguous-ish blocks, measure fragmentation,
// free the middle one, and verify fragmentation increases.
int *a = new int[100];
int *b = new int[100];
int *c = new int[100];
a[4] = 4;
b[4] = 4;
c[4] = 4;
auto *t = CTrackerMetrics::GetTracker();
float frag_before = t->FragmentationIndex();
delete[] b; // Creates a gap between a and c
float frag_after = t->FragmentationIndex();
EXPECT_GT(frag_after, frag_before);
delete[] a;
delete[] c;
}
// --- Largest Free Block ---
TEST(CTrackerTest, LargestFreeBlockPositiveAfterFree)
{
// Allocate 3 blocks; freeing the middle should produce a gap.
// We can't predict absolute gap sizes because gtest's own allocations interleave,
// but the largest gap should be at least as big as our created gap.
int *a = new int[10];
int *b = new int[200];
int *c = new int[10];
a[4] = 4;
b[4] = 4;
c[4] = 4;
auto *t = CTrackerMetrics::GetTracker();
delete[] b; // Creates a gap between a and c
size_t gap = t->FindLargestFreeBlock();
EXPECT_GE(gap, sizeof(int) * 200);
delete[] a;
delete[] c;
}
// --- Sorted Order ---
TEST(CTrackerTest, RecordsAreSortedByAddress)
{
int *a = new int[10];
int *b = new int[10];
int *c = new int[10];
a[4] = 4;
b[4] = 4;
c[4] = 4;
auto *t = CTrackerMetrics::GetTracker();
// Walk the linked list and verify addresses are in ascending order
AllocationRecord *prev = nullptr;
AllocationRecord *cur = t->RecordsHead;
while (cur)
{
if (prev)
{
EXPECT_LT(reinterpret_cast<uintptr_t>(prev->ptr),
reinterpret_cast<uintptr_t>(cur->ptr));
}
prev = cur;
cur = cur->next;
}
delete[] a;
delete[] b;
delete[] c;
}
// --- TotalAllocated ---
TEST(CTrackerTest, TotalAllocatedMatchesKnownSizes)
{
auto before = TakeSnapshot();
char *x = new char[256];
double *y = new double[32];
size_t expected_delta = 256 + sizeof(double) * 32;
EXPECT_EQ(CTrackerMetrics::GetTracker()->TotalAllocated(),
before.total_allocated + expected_delta);
delete[] x;
delete[] y;
}