47 lines
1.8 KiB
C
47 lines
1.8 KiB
C
#include "c_StrIndexKmp.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
void test_log(const char* test_name) {
|
|
printf("[PASS] %s\n", test_name);
|
|
}
|
|
|
|
int main() {
|
|
printf("==================================================\n");
|
|
printf(" Starting KMP String Search Algorithm Unit Tests\n");
|
|
printf("==================================================\n\n");
|
|
|
|
// Test 1: Standard match discovery
|
|
const char* text1 = "ABABDABACDABABCABAB";
|
|
long long idx1 = c_StrIndexKmp(text1, "ABABCABAB");
|
|
assert(idx1 == 10);
|
|
test_log("1. Pattern found at index 10 successfully");
|
|
|
|
// Test 2: Repeating partial pattern fallback (Corrected text and pattern)
|
|
// At index 0, text matches "ABABA" but mismatches on the 6th char ('B' vs 'C').
|
|
// The LPS table causes j to backtrack smoothly, discovering the real match starting at index 2.
|
|
const char* text2 = "ABABABACATA";
|
|
long long idx2 = c_StrIndexKmp(text2, "ABABAC");
|
|
assert(idx2 == 2);
|
|
test_log("2. Partial-match backtracking table lookup verified at index 2");
|
|
|
|
// Test 3: Pattern not present in text
|
|
long long idx3 = c_StrIndexKmp(text1, "XYZ");
|
|
assert(idx3 == -1);
|
|
test_log("3. Non-existent substring pattern returns -1 safely");
|
|
|
|
// Test 4: Empty pattern handling boundary check
|
|
long long idx4 = c_StrIndexKmp(text1, "");
|
|
assert(idx4 == 0);
|
|
test_log("4. Empty string matches target index 0");
|
|
|
|
// Test 5: Pattern longer than string payload boundary check
|
|
long long idx5 = c_StrIndexKmp("short", "extremely_long_pattern");
|
|
assert(idx5 == -1);
|
|
test_log("5. Length mismatch constraints handled gracefully");
|
|
|
|
printf("\n==================================================\n");
|
|
printf(" Success! All KMP test assertions passed successfully!\n");
|
|
printf("==================================================\n");
|
|
return 0;
|
|
} |