Skip to content

Commit 3df1cc0

Browse files
Merge pull request #45 from dreamer-coding/pr_bughunt
Pull Request small bug hunt
2 parents 62929cb + 734c169 commit 3df1cc0

File tree

12 files changed

+659
-10
lines changed

12 files changed

+659
-10
lines changed

code/logic/fossil/test/mocking.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@
9292
* @param existing_type The existing type to create an alias for.
9393
*/
9494
#define _FOSSIL_MOCK_ALIAS(new_type, existing_type) \
95-
typedef existing_type fossil_mockup_##new_type##_type; \
96-
fossil_mockup_##new_type##_type fossil_mockup_##new_type(void)
95+
typedef existing_type new_type; \
96+
new_type fossil_mockup_##new_type(void)
9797

9898
/**
9999
* @def _FOSSIL_MOCK_STRUCT

code/logic/fossil/test/testing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ void fossil_test_run_all(fossil_test_env_t *env);
362362
#define _FOSSIL_TEST_SKIP(test_name, message) \
363363
test_name##_test_case.status = TEST_STATUS_SKIP; \
364364
test_name##_test_case.failure_message = message; \
365-
printf(FOSSIL_TEST_COLOR_YELLOW "SKIP: %s - %s\n" FOSSIL_TEST_COLOR_RESET, #test_name, message); \
365+
printf(FOSSIL_TEST_COLOR_YELLOW "SKIPPED: %s - %s\n" FOSSIL_TEST_COLOR_RESET, #test_name, message); \
366366

367367
/**
368368
* @brief Macro to define a test case.

code/logic/marking.c

Lines changed: 64 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) {
@@ -119,6 +124,16 @@ void fossil_test_benchmark(char* duration_type, double expected, double actual)
119124
} // end of func
120125

121126
void fossil_benchmark_init(fossil_benchmark_t* benchmark, const char* name) {
127+
if (benchmark == NULL) {
128+
printf("Error: benchmark is NULL\n");
129+
return;
130+
}
131+
132+
if (name == NULL) {
133+
printf("Error: name is NULL\n");
134+
return;
135+
}
136+
122137
benchmark->name = name;
123138
benchmark->num_samples = 0;
124139
benchmark->total_duration = 0.0;
@@ -128,13 +143,23 @@ void fossil_benchmark_init(fossil_benchmark_t* benchmark, const char* name) {
128143
}
129144

130145
void fossil_benchmark_start(fossil_benchmark_t* benchmark) {
146+
if (benchmark == NULL) {
147+
printf("Error: benchmark is NULL\n");
148+
return;
149+
}
150+
131151
if (!benchmark->running) {
132152
benchmark->start_time = clock();
133153
benchmark->running = 1;
134154
}
135155
}
136156

137157
void fossil_benchmark_stop(fossil_benchmark_t* benchmark) {
158+
if (benchmark == NULL) {
159+
printf("Error: benchmark is NULL\n");
160+
return;
161+
}
162+
138163
if (benchmark->running) {
139164
benchmark->end_time = clock();
140165
double elapsed = ((double)(benchmark->end_time - benchmark->start_time)) / CLOCKS_PER_SEC;
@@ -151,29 +176,53 @@ void fossil_benchmark_stop(fossil_benchmark_t* benchmark) {
151176
}
152177

153178
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+
}
154183
return benchmark->total_duration;
155184
}
156185

157186
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+
}
158191
return benchmark->min_duration;
159192
}
160193

161194
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+
}
162199
return benchmark->max_duration;
163200
}
164201

165202
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+
}
166207
return benchmark->num_samples > 0 ? benchmark->total_duration / benchmark->num_samples : 0.0;
167208
}
168209

169210
void fossil_benchmark_reset(fossil_benchmark_t* benchmark) {
211+
if (benchmark == NULL) {
212+
printf("Error: benchmark is NULL\n");
213+
return;
214+
}
170215
benchmark->num_samples = 0;
171216
benchmark->total_duration = 0.0;
172217
benchmark->min_duration = DBL_MAX;
173218
benchmark->max_duration = 0.0;
174219
}
175220

176221
void fossil_benchmark_report(const fossil_benchmark_t* benchmark) {
222+
if (benchmark == NULL) {
223+
printf("Error: benchmark is NULL\n");
224+
return;
225+
}
177226
printf("\033[1;36mBenchmark : %s\n", benchmark->name);
178227
printf("\033[1;32mTotal Time: %.6f seconds\n", fossil_benchmark_elapsed_seconds(benchmark));
179228
printf("\033[1;32mMin Time : %.6f seconds\n", fossil_benchmark_min_time(benchmark));
@@ -182,10 +231,25 @@ void fossil_benchmark_report(const fossil_benchmark_t* benchmark) {
182231
}
183232

184233
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+
185244
scoped_benchmark->benchmark = benchmark;
186245
fossil_benchmark_start(scoped_benchmark->benchmark);
187246
}
188247

189248
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+
190254
fossil_benchmark_stop(scoped_benchmark->benchmark);
191255
}

code/logic/mocking.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@
1919
extern char *_custom_fossil_test_strdup(const char *str);
2020

2121
void fossil_mock_init(MockCallList *list) {
22+
if (!list) {
23+
return;
24+
}
2225
list->head = NULL;
2326
list->tail = NULL;
2427
list->size = 0;
2528
}
2629

2730
void fossil_mock_destroy(MockCallList *list) {
31+
if (!list) {
32+
return;
33+
}
34+
2835
MockCall *current = list->head;
2936
while (current) {
3037
MockCall *next = current->next;
@@ -36,15 +43,47 @@ void fossil_mock_destroy(MockCallList *list) {
3643
free(current);
3744
current = next;
3845
}
46+
list->head = NULL;
47+
list->tail = NULL;
48+
list->size = 0;
3949
}
4050

4151
void fossil_mock_add_call(MockCallList *list, const char *function_name, char **arguments, int num_args) {
52+
if (!list || !function_name || !arguments) {
53+
return;
54+
}
55+
4256
MockCall *call = (MockCall *)malloc(sizeof(MockCall));
57+
if (!call) {
58+
return;
59+
}
60+
4361
call->function_name = _custom_fossil_test_strdup(function_name);
62+
if (!call->function_name) {
63+
free(call);
64+
return;
65+
}
66+
4467
call->arguments = (char **)malloc(num_args * sizeof(char *));
68+
if (!call->arguments) {
69+
free(call->function_name);
70+
free(call);
71+
return;
72+
}
73+
4574
for (int i = 0; i < num_args; ++i) {
4675
call->arguments[i] = _custom_fossil_test_strdup(arguments[i]);
76+
if (!call->arguments[i]) {
77+
for (int j = 0; j < i; ++j) {
78+
free(call->arguments[j]);
79+
}
80+
free(call->arguments);
81+
free(call->function_name);
82+
free(call);
83+
return;
84+
}
4785
}
86+
4887
call->num_args = num_args;
4988
call->next = NULL;
5089

@@ -58,6 +97,10 @@ void fossil_mock_add_call(MockCallList *list, const char *function_name, char **
5897
}
5998

6099
void fossil_mock_print(MockCallList *list) {
100+
if (!list) {
101+
return;
102+
}
103+
61104
MockCall *current = list->head;
62105
while (current) {
63106
printf("Function: %s\n", current->function_name);

code/logic/testing.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,13 @@ void fossil_test_assert_internal(bool condition, const char *message, const char
586586
_ASSERT_COUNT++; // Increment the assertion count
587587

588588
if (!condition) {
589-
// Check if the current assertion is the same as the last one
590-
if (last_message && strcmp(last_message, message) == 0 &&
589+
// Check if the current assertion is the same or similar to the last one
590+
if (last_message && strstr(message, last_message) != NULL &&
591591
last_file && strcmp(last_file, file) == 0 &&
592592
last_line == line &&
593593
last_func && strcmp(last_func, func) == 0) {
594594
anomaly_count++;
595-
printf(FOSSIL_TEST_COLOR_YELLOW "Duplicate assertion detected: %s (%s:%d in %s) [Anomaly Count: %d]\n" FOSSIL_TEST_COLOR_RESET, message, file, line, func, anomaly_count);
595+
printf(FOSSIL_TEST_COLOR_YELLOW "Duplicate or similar assertion detected: %s (%s:%d in %s) [Anomaly Count: %d]\n" FOSSIL_TEST_COLOR_RESET, message, file, line, func, anomaly_count);
596596
} else {
597597
anomaly_count = 0; // Reset anomaly count for new assertion
598598
printf(FOSSIL_TEST_COLOR_RED "Assertion failed: %s (%s:%d in %s)\n" FOSSIL_TEST_COLOR_RESET, message, file, line, func);
@@ -635,14 +635,14 @@ void fossil_test_run_case(test_case_t *test_case, fossil_test_env_t *env) {
635635
}
636636
} else {
637637
test_case->status = TEST_STATUS_FAIL;
638-
printf(FOSSIL_TEST_COLOR_RED "FAIL: " FOSSIL_TEST_COLOR_BLUE " %s\n", test_case->name);
638+
printf(FOSSIL_TEST_COLOR_RED "FAILED: " FOSSIL_TEST_COLOR_BLUE " %s\n", test_case->name);
639639
printf("Failure Message: %s\n" FOSSIL_TEST_COLOR_RESET, test_case->failure_message);
640640
}
641641
test_case->execution_time = (double)(clock() - test_start_time) / CLOCKS_PER_SEC;
642642

643643
// Check if the test case is empty
644644
if (_ASSERT_COUNT == 0) {
645-
printf(FOSSIL_TEST_COLOR_YELLOW "WARNING: " FOSSIL_TEST_COLOR_BLUE " %s contains no assertions\n" FOSSIL_TEST_COLOR_RESET, test_case->name);
645+
printf(FOSSIL_TEST_COLOR_YELLOW "WARNING: %s contains no assertions\n" FOSSIL_TEST_COLOR_RESET, test_case->name);
646646
}
647647

648648
// Run teardown
@@ -651,7 +651,7 @@ void fossil_test_run_case(test_case_t *test_case, fossil_test_env_t *env) {
651651
// Log result
652652
if (test_case->status == TEST_STATUS_PASS) {
653653
if (env->options.show_info) {
654-
printf(FOSSIL_TEST_COLOR_GREEN "PASS: " FOSSIL_TEST_COLOR_BLUE " %s (%.3f seconds)\n" FOSSIL_TEST_COLOR_RESET, test_case->name, test_case->execution_time);
654+
printf(FOSSIL_TEST_COLOR_GREEN "PASSED: " FOSSIL_TEST_COLOR_BLUE " %s (%.3f seconds)\n" FOSSIL_TEST_COLOR_RESET, test_case->name, test_case->execution_time);
655655
}
656656
} else if (test_case->status == TEST_STATUS_FAIL) {
657657
env->fail_count++;

code/tests/cases/test_bdd.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,63 @@ FOSSIL_TEST_CASE(xbdd_invalid_login) {
158158
}
159159
} // end of case
160160

161+
FOSSIL_TEST_CASE(xbdd_insufficient_balance) {
162+
GIVEN("a user's account with insufficient balance") {
163+
// Set up the context
164+
float accountBalance = 100.0;
165+
float withdrawalAmount = 200.0;
166+
167+
WHEN("the user requests a withdrawal of $200") {
168+
// Perform the withdrawal action
169+
bool withdrawalSuccess = false;
170+
if (accountBalance >= withdrawalAmount) {
171+
accountBalance -= withdrawalAmount;
172+
withdrawalSuccess = true;
173+
}
174+
175+
THEN("the withdrawal should fail and balance should remain unchanged") {
176+
// Check the expected outcome
177+
FOSSIL_TEST_ASSUME(!withdrawalSuccess, "Withdrawal should not succeed");
178+
FOSSIL_TEST_ASSUME(accountBalance == 100.0, "Account balance should remain unchanged");
179+
}
180+
}
181+
}
182+
} // end of case
183+
184+
FOSSIL_TEST_CASE(xbdd_add_multiple_items_to_cart) {
185+
GIVEN("a user with an empty shopping cart") {
186+
// Set up the context
187+
int cartItemCount = 0;
188+
189+
WHEN("the user adds three products to the cart") {
190+
// Perform the action of adding products
191+
cartItemCount += 3;
192+
193+
THEN("the cart item count should increase by 3") {
194+
// Check the expected outcome
195+
FOSSIL_TEST_ASSUME(cartItemCount == 3, "Cart item count should have increased by 3");
196+
}
197+
}
198+
}
199+
} // end of case
200+
201+
FOSSIL_TEST_CASE(xbdd_remove_item_from_cart) {
202+
GIVEN("a user with a shopping cart containing 2 items") {
203+
// Set up the context
204+
int cartItemCount = 2;
205+
206+
WHEN("the user removes one product from the cart") {
207+
// Perform the action of removing a product
208+
cartItemCount--;
209+
210+
THEN("the cart item count should decrease by 1") {
211+
// Check the expected outcome
212+
FOSSIL_TEST_ASSUME(cartItemCount == 1, "Cart item count should have decreased by 1");
213+
}
214+
}
215+
}
216+
} // end of case
217+
161218
// * * * * * * * * * * * * * * * * * * * * * * * *
162219
// * Fossil Logic Test Pool
163220
// * * * * * * * * * * * * * * * * * * * * * * * *
@@ -167,6 +224,9 @@ FOSSIL_TEST_GROUP(c_bdd_test_cases) {
167224
FOSSIL_TEST_ADD(bdd_suite, xbdd_empty_cart);
168225
FOSSIL_TEST_ADD(bdd_suite, xbdd_valid_login);
169226
FOSSIL_TEST_ADD(bdd_suite, xbdd_invalid_login);
227+
FOSSIL_TEST_ADD(bdd_suite, xbdd_insufficient_balance);
228+
FOSSIL_TEST_ADD(bdd_suite, xbdd_add_multiple_items_to_cart);
229+
FOSSIL_TEST_ADD(bdd_suite, xbdd_remove_item_from_cart);
170230

171231
FOSSIL_TEST_REGISTER(bdd_suite);
172232
} // end of group

0 commit comments

Comments
 (0)