Skip to content

Commit 515cc1a

Browse files
apply similer scan via a finger print
1 parent 52685d5 commit 515cc1a

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

code/source/unittest/unittest.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ typedef struct {
2424
char* message;
2525
char* file;
2626
int line;
27+
unsigned long fingerprint;
2728
char* func;
2829
} assert_history_t;
2930

@@ -737,10 +738,42 @@ bool is_assert_in_history(bool expression, xassert_type_t behavior, char* messag
737738
return false;
738739
}
739740

741+
unsigned long generate_fingerprint(bool expression, xassert_type_t behavior, char* message, char* file, int line, char* func) {
742+
unsigned long hash = 5381;
743+
int c;
744+
745+
hash = ((hash << 5) + hash) + expression;
746+
hash = ((hash << 5) + hash) + behavior;
747+
748+
while ((c = *message++))
749+
hash = ((hash << 5) + hash) + c;
750+
751+
while ((c = *file++))
752+
hash = ((hash << 5) + hash) + c;
753+
754+
hash = ((hash << 5) + hash) + line;
755+
756+
while ((c = *func++))
757+
hash = ((hash << 5) + hash) + c;
758+
759+
return hash;
760+
}
761+
762+
bool is_assert_similar_in_history(unsigned long fingerprint) {
763+
for (int i = 0; i < assert_history_count; i++) {
764+
if (assert_history[i].fingerprint == fingerprint) {
765+
return true;
766+
}
767+
}
768+
return false;
769+
}
770+
740771
void _fossil_test_assert_class(bool expression, xassert_type_t behavior, char* message, char* file, int line, char* func) {
741-
if (is_assert_in_history(expression, behavior, message, file, line, func)) {
742-
// Skip the assertion as it has already been executed
743-
_ASSERT_INFO.same_assert = true;
772+
unsigned long fingerprint = generate_fingerprint(expression, behavior, message, file, line, func);
773+
774+
if (is_assert_similar_in_history(fingerprint)) {
775+
// Skip the assertion as a similar one has already been executed
776+
_ASSERT_INFO.same_assert = true;
744777
return;
745778
}
746779

@@ -760,14 +793,15 @@ void _fossil_test_assert_class(bool expression, xassert_type_t behavior, char* m
760793
_ASSERT_INFO.num_asserts++; // increment the number of asserts
761794
_ASSERT_INFO.has_assert = true; // Make note of an assert being added in a given test case
762795

763-
// Add the assertion to the history
796+
// Add the assertion to the history with its fingerprint
764797
if (assert_history_count < MAX_ASSERT_HISTORY) {
765798
assert_history[assert_history_count].expression = expression;
766799
assert_history[assert_history_count].behavior = behavior;
767800
assert_history[assert_history_count].message = message;
768801
assert_history[assert_history_count].file = file;
769802
assert_history[assert_history_count].line = line;
770803
assert_history[assert_history_count].func = func;
804+
assert_history[assert_history_count].fingerprint = fingerprint;
771805
assert_history_count++;
772806
}
773807
}

0 commit comments

Comments
 (0)