144 lines
5.0 KiB
C
144 lines
5.0 KiB
C
#include <assert.h>
|
|
|
|
#include "c_List.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
// Custom User Type showcasing intrusive inclusion
|
|
typedef struct {
|
|
int value;
|
|
c_ListNode_t node; // Intrusive node payload linkage
|
|
} CustomData_t;
|
|
|
|
static void test2(void) {
|
|
printf("==================================================\n");
|
|
printf(" Starting Intrusive Safe-Iterator Unit Tests\n");
|
|
printf("==================================================\n\n");
|
|
|
|
c_List_t head;
|
|
c_List_Init(&head);
|
|
|
|
CustomData_t d1 = {10};
|
|
CustomData_t d2 = {20};
|
|
CustomData_t d3 = {30};
|
|
|
|
// Linking nodes to tail: [Head] <-> [10] <-> [20] <-> [30]
|
|
c_List_InsertBefore(&head, &d1.node);
|
|
c_List_InsertBefore(&head, &d2.node);
|
|
c_List_InsertBefore(&head, &d3.node);
|
|
|
|
// ==========================================
|
|
// 1. Verify Lookups via ForEachEntry
|
|
// ==========================================
|
|
CustomData_t* pos;
|
|
int index = 0;
|
|
int expected[] = {10, 20, 30};
|
|
|
|
printf("Reading nodes using standard ForEachEntry:\n");
|
|
c_List_ForEachEntry(&head, pos, CustomData_t, node) {
|
|
printf(" Found Entry value: %d\n", pos->value);
|
|
assert(pos->value == expected[index++]);
|
|
}
|
|
assert(index == 3);
|
|
printf("[PASS] Standard lookup loop complete.\n\n");
|
|
|
|
// ==========================================
|
|
// 2. Verify Mutations via ForEachEntrySafe
|
|
// ==========================================
|
|
CustomData_t* tmp; // Cache variable for safe traversal tracking
|
|
index = 0;
|
|
|
|
printf("Filtering and removing matching entry elements via Safe Loop:\n");
|
|
c_List_ForEachEntrySafe(&head, pos, tmp, CustomData_t, node) {
|
|
if (pos->value == 20) {
|
|
printf(" Modifying layout: Safely dropping element (%d)\n", pos->value);
|
|
c_List_Remove(&pos->node); // Remove middle item mid-flight
|
|
}
|
|
index++;
|
|
}
|
|
assert(index == 3); // Iterated through all three bounds
|
|
|
|
// ==========================================
|
|
// 3. Confirm Final Structure Integrity
|
|
// ==========================================
|
|
// List must now structurally bridge to bypass item 20: [10] <-> [30]
|
|
index = 0;
|
|
c_List_ForEachEntry(&head, pos, CustomData_t, node) {
|
|
if (index == 0) assert(pos->value == 10);
|
|
if (index == 1) assert(pos->value == 30);
|
|
index++;
|
|
}
|
|
assert(index == 2);
|
|
printf("[PASS] Structural integrity verified following safe removal mutation.\n");
|
|
|
|
printf("\n==================================================\n");
|
|
printf(" Success! Intrusive layout traversal operates flawlessly!\n");
|
|
printf("==================================================\n");
|
|
}
|
|
|
|
int main() {
|
|
printf("==================================================\n");
|
|
printf(" Starting Intrusive Circular Doubly Linked List Tests\n");
|
|
printf("==================================================\n\n");
|
|
|
|
// Initialize list head context anchor
|
|
c_List_t head;
|
|
c_List_Init(&head);
|
|
assert(c_List_IsEmpty(&head) == 1);
|
|
|
|
CustomData_t d1 = {100};
|
|
CustomData_t d2 = {200};
|
|
CustomData_t d3 = {300};
|
|
|
|
// ==========================================
|
|
// 1. Insertion Testing
|
|
// ==========================================
|
|
// Insert d1 after head -> List: [Head] <-> [100]
|
|
c_List_InsertAfter(&head, &d1.node);
|
|
assert(c_List_IsEmpty(&head) == 0);
|
|
|
|
// Insert d3 before head -> List: [Head] <-> [100] <-> [300]
|
|
c_List_InsertBefore(&head, &d3.node);
|
|
|
|
// Insert d2 after d1 -> List: [Head] <-> [100] <-> [200] <-> [300]
|
|
c_List_InsertAfter(&d1.node, &d2.node);
|
|
|
|
printf("[PASS] Sequence insertions complete.\n");
|
|
|
|
// ==========================================
|
|
// 2. Linear Traversal Verification
|
|
// ==========================================
|
|
c_ListNode_t* curr = c_List_Next(&head);
|
|
int expected_values[] = {100, 200, 300};
|
|
int idx = 0;
|
|
|
|
while (curr != &head) {
|
|
CustomData_t* entry = c_List_Entry(curr, CustomData_t, node);
|
|
assert(entry->value == expected_values[idx]);
|
|
curr = c_List_Next(curr);
|
|
idx++;
|
|
}
|
|
assert(idx == 3);
|
|
printf("[PASS] Intrusive forward iterator matching sequential expectations.\n");
|
|
|
|
// ==========================================
|
|
// 3. Deletion and Mutation Verification
|
|
// ==========================================
|
|
// Target and drop middle node (200)
|
|
c_List_Remove(&d2.node);
|
|
|
|
// Structure must bridge gaps smoothly: [100] <-> [300]
|
|
assert(c_List_Next(&d1.node) == &d3.node);
|
|
assert(c_List_Prev(&d3.node) == &d1.node);
|
|
|
|
// Eliminated isolated node must be self-referencing via macro assignment
|
|
assert(c_List_IsEmpty(&d2.node) == 1);
|
|
printf("[PASS] Drop operation and isolated reference initialization verified.\n");
|
|
|
|
printf("\n==================================================\n");
|
|
printf(" Success! Intrusive macro mutations match layout specs!\n");
|
|
printf("==================================================\n");
|
|
|
|
test2();
|
|
return 0;
|
|
} |