Skip to content

Commit 734c169

Browse files
adding error checking to marking
1 parent b71b8bf commit 734c169

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

code/logic/marking.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ void assume_duration(double expected, double actual, double unit) {
9393

9494
// Marks a test case as timeout with a specified time and prints it to stderr.
9595
void fossil_test_benchmark(char* duration_type, double expected, double actual) {
96+
if (duration_type == NULL) {
97+
printf("Error: duration_type is NULL\n");
98+
return;
99+
}
100+
96101
if (strcmp(duration_type, "minutes") == 0) {
97102
assume_duration(expected, actual, 60.0);
98103
} else if (strcmp(duration_type, "seconds") == 0) {
@@ -120,10 +125,12 @@ void fossil_test_benchmark(char* duration_type, double expected, double actual)
120125

121126
void fossil_benchmark_init(fossil_benchmark_t* benchmark, const char* name) {
122127
if (benchmark == NULL) {
128+
printf("Error: benchmark is NULL\n");
123129
return;
124130
}
125131

126132
if (name == NULL) {
133+
printf("Error: name is NULL\n");
127134
return;
128135
}
129136

@@ -137,6 +144,7 @@ void fossil_benchmark_init(fossil_benchmark_t* benchmark, const char* name) {
137144

138145
void fossil_benchmark_start(fossil_benchmark_t* benchmark) {
139146
if (benchmark == NULL) {
147+
printf("Error: benchmark is NULL\n");
140148
return;
141149
}
142150

@@ -147,6 +155,11 @@ void fossil_benchmark_start(fossil_benchmark_t* benchmark) {
147155
}
148156

149157
void fossil_benchmark_stop(fossil_benchmark_t* benchmark) {
158+
if (benchmark == NULL) {
159+
printf("Error: benchmark is NULL\n");
160+
return;
161+
}
162+
150163
if (benchmark->running) {
151164
benchmark->end_time = clock();
152165
double elapsed = ((double)(benchmark->end_time - benchmark->start_time)) / CLOCKS_PER_SEC;
@@ -163,29 +176,53 @@ void fossil_benchmark_stop(fossil_benchmark_t* benchmark) {
163176
}
164177

165178
double fossil_benchmark_elapsed_seconds(const fossil_benchmark_t* benchmark) {
179+
if (benchmark == NULL) {
180+
printf("Error: benchmark is NULL\n");
181+
return 0.0;
182+
}
166183
return benchmark->total_duration;
167184
}
168185

169186
double fossil_benchmark_min_time(const fossil_benchmark_t* benchmark) {
187+
if (benchmark == NULL) {
188+
printf("Error: benchmark is NULL\n");
189+
return 0.0;
190+
}
170191
return benchmark->min_duration;
171192
}
172193

173194
double fossil_benchmark_max_time(const fossil_benchmark_t* benchmark) {
195+
if (benchmark == NULL) {
196+
printf("Error: benchmark is NULL\n");
197+
return 0.0;
198+
}
174199
return benchmark->max_duration;
175200
}
176201

177202
double fossil_benchmark_avg_time(const fossil_benchmark_t* benchmark) {
203+
if (benchmark == NULL) {
204+
printf("Error: benchmark is NULL\n");
205+
return 0.0;
206+
}
178207
return benchmark->num_samples > 0 ? benchmark->total_duration / benchmark->num_samples : 0.0;
179208
}
180209

181210
void fossil_benchmark_reset(fossil_benchmark_t* benchmark) {
211+
if (benchmark == NULL) {
212+
printf("Error: benchmark is NULL\n");
213+
return;
214+
}
182215
benchmark->num_samples = 0;
183216
benchmark->total_duration = 0.0;
184217
benchmark->min_duration = DBL_MAX;
185218
benchmark->max_duration = 0.0;
186219
}
187220

188221
void fossil_benchmark_report(const fossil_benchmark_t* benchmark) {
222+
if (benchmark == NULL) {
223+
printf("Error: benchmark is NULL\n");
224+
return;
225+
}
189226
printf("\033[1;36mBenchmark : %s\n", benchmark->name);
190227
printf("\033[1;32mTotal Time: %.6f seconds\n", fossil_benchmark_elapsed_seconds(benchmark));
191228
printf("\033[1;32mMin Time : %.6f seconds\n", fossil_benchmark_min_time(benchmark));
@@ -194,10 +231,25 @@ void fossil_benchmark_report(const fossil_benchmark_t* benchmark) {
194231
}
195232

196233
void fossil_scoped_benchmark_init(scoped_benchmark_t* scoped_benchmark, fossil_benchmark_t* benchmark) {
234+
if (scoped_benchmark == NULL) {
235+
printf("Error: scoped_benchmark is NULL\n");
236+
return;
237+
}
238+
239+
if (benchmark == NULL) {
240+
printf("Error: benchmark is NULL\n");
241+
return;
242+
}
243+
197244
scoped_benchmark->benchmark = benchmark;
198245
fossil_benchmark_start(scoped_benchmark->benchmark);
199246
}
200247

201248
void fossil_scoped_benchmark_destroy(scoped_benchmark_t* scoped_benchmark) {
249+
if (scoped_benchmark == NULL) {
250+
printf("Error: scoped_benchmark is NULL\n");
251+
return;
252+
}
253+
202254
fossil_benchmark_stop(scoped_benchmark->benchmark);
203255
}

0 commit comments

Comments
 (0)