Skip to content

Commit 4de6d45

Browse files
Adapt unit-tests
1 parent d097761 commit 4de6d45

File tree

2 files changed

+672
-428
lines changed

2 files changed

+672
-428
lines changed

unit-tests/lib_c_list/README.md

Lines changed: 70 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,44 @@
88

99
## Overview
1010

11-
This test suite covers all functions provided by the Generic Linked List library (`c_list.c`).
11+
This test suite covers all functions provided by the Generic Linked List library (`c_list.c`), which supports both:
1212

13-
### Test Coverage
14-
15-
The test suite includes **16 comprehensive tests** covering:
16-
17-
**Insertion Operations (3 tests):**
18-
19-
- `test_push_front` - Add nodes at the beginning
20-
- `test_push_front_errors` - Error handling for push_front
21-
- `test_push_back` - Add nodes at the end
22-
23-
**Removal Operations (2 tests):**
24-
25-
- `test_pop_front` - Remove nodes from the beginning
26-
- `test_pop_back` - Remove nodes from the end
27-
28-
**Advanced Insertion (3 tests):**
29-
30-
- `test_insert_after` - Insert after a reference node
31-
- `test_insert_before` - Insert before a reference node
32-
- `test_insert_before_errors` - Error handling for insert_before
33-
34-
**Node Removal (2 tests):**
35-
36-
- `test_remove` - Remove specific nodes
37-
- `test_remove_not_found` - Handle removal of non-existent nodes
13+
- **Forward lists** (`c_flist_*`) - Singly-linked lists (4-8 bytes/node)
14+
- **Doubly-linked lists** (`c_dlist_*`) - Doubly-linked lists (8-16 bytes/node)
3815

39-
**List Operations (1 test):**
40-
41-
- `test_clear` - Clear entire list
42-
43-
**Sorting (3 tests):**
44-
45-
- `test_sort` - Sort list with comparison function
46-
- `test_sort_empty` - Sort empty list
47-
- `test_sort_single` - Sort single-element list
48-
49-
**Utilities (2 tests):**
16+
### Test Coverage
5017

51-
- `test_size` - Count nodes in list
52-
- `test_traversal` - Traverse and manipulate list
18+
The test suite includes **24 comprehensive tests** covering:
19+
20+
**Forward Lists (c_flist_*) - 11 tests:**
21+
22+
- `test_c_flist_push_front` - Add nodes at the beginning
23+
- `test_c_flist_push_back` - Add nodes at the end
24+
- `test_c_flist_pop_front` - Remove nodes from the beginning
25+
- `test_c_flist_pop_back` - Remove nodes from the end
26+
- `test_c_flist_insert_after` - Insert after a reference node
27+
- `test_c_flist_remove` - Remove specific nodes
28+
- `test_c_flist_remove_if` - Remove nodes matching predicate
29+
- `test_c_flist_clear` - Clear entire list
30+
- `test_c_flist_sort` - Sort list with comparison function
31+
- `test_c_flist_unique` - Remove duplicate consecutive nodes
32+
- `test_c_flist_reverse` - Reverse list order
33+
34+
**Doubly-Linked Lists (c_dlist_*) - 13 tests:**
35+
36+
- `test_c_dlist_push_front` - Add nodes at the beginning
37+
- `test_c_dlist_push_back` - Add nodes at the end
38+
- `test_c_dlist_pop_front` - Remove nodes from the beginning
39+
- `test_c_dlist_pop_back` - Remove nodes from the end
40+
- `test_c_dlist_insert_after` - Insert after a reference node
41+
- `test_c_dlist_insert_before` - Insert before a reference node
42+
- `test_c_dlist_remove` - Remove specific nodes
43+
- `test_c_dlist_remove_if` - Remove nodes matching predicate
44+
- `test_c_dlist_clear` - Clear entire list
45+
- `test_c_dlist_sort` - Sort list with comparison function
46+
- `test_c_dlist_unique` - Remove duplicate consecutive nodes
47+
- `test_c_dlist_reverse` - Reverse list order
48+
- `test_c_dlist_backward_traversal` - Traverse list backward using prev pointers
5349

5450
## Building and Running Tests
5551

@@ -93,24 +89,28 @@ xdg-open coverage_html/index.html
9389
### Safety Features Tested
9490

9591
1. **NULL pointer validation** - All functions check for NULL parameters
96-
2. **Node state validation** - Insertion functions verify `node->next == NULL`
92+
2. **Node state validation** - Insertion functions verify `node->next == NULL` (flist) or `node->_list.next == NULL && node->prev == NULL` (dlist)
9793
3. **Return value checking** - All mutating operations return bool for success/failure
98-
4. **Node not found** - Functions handle missing nodes gracefully
99-
5. **Empty list operations** - Safe handling of operations on empty lists
94+
4. **Empty list operations** - Safe handling of operations on empty lists
95+
5. **Predicate filtering** - remove_if tests validate callback-based filtering
96+
6. **Duplicate removal** - unique tests validate consecutive duplicate handling
97+
7. **Bidirectional traversal** - dlist tests validate both forward and backward traversal
10098

10199
### Example Test Output
102100

103101
```bash
104-
[==========] Running 16 test(s).
105-
[ RUN ] test_push_front
106-
[ OK ] test_push_front
107-
[ RUN ] test_push_front_errors
108-
[ OK ] test_push_front_errors
109-
[ RUN ] test_push_back
110-
[ OK ] test_push_back
102+
[==========] Running 24 test(s).
103+
[ RUN ] test_c_flist_push_front
104+
[ OK ] test_c_flist_push_front
105+
[ RUN ] test_c_flist_push_back
106+
[ OK ] test_c_flist_push_back
107+
[ RUN ] test_c_flist_pop_front
108+
[ OK ] test_c_flist_pop_front
111109
...
112-
[==========] 16 test(s) run.
113-
[ PASSED ] 16 test(s).
110+
[ RUN ] test_c_dlist_backward_traversal
111+
[ OK ] test_c_dlist_backward_traversal
112+
[==========] 24 test(s) run.
113+
[ PASSED ] 24 test(s).
114114
```
115115

116116
## Memory Safety
@@ -133,7 +133,23 @@ These tests should be run:
133133

134134
To add new tests:
135135

136-
1. Add test function following the pattern:
136+
1. Define test node structures for the appropriate list type:
137+
138+
```c
139+
// For forward lists
140+
typedef struct test_flist_node_s {
141+
c_flist_node_t _list;
142+
int value;
143+
} test_flist_node_t;
144+
145+
// For doubly-linked lists
146+
typedef struct test_dlist_node_s {
147+
c_dlist_node_t _list;
148+
int value;
149+
} test_dlist_node_t;
150+
```
151+
152+
2. Add test function following the pattern:
137153

138154
```c
139155
static void test_new_feature(void **state)
@@ -143,13 +159,13 @@ static void test_new_feature(void **state)
143159
}
144160
```
145161
146-
2. Register in main():
162+
3. Register in main():
147163
148164
```c
149165
cmocka_unit_test(test_new_feature),
150166
```
151167

152-
3. Rebuild and run tests
168+
4. Rebuild and run tests
153169

154170
## Known Limitations
155171

0 commit comments

Comments
 (0)