summaryrefslogtreecommitdiff
path: root/tests/t_iovec.c
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-10-24 01:17:50 +0200
committerDirk Engling <erdgeist@erdgeist.org>2026-10-24 01:17:50 +0200
commitf24ec48b27d4cb8dd0555840206dedbb45f1423c (patch)
tree4cf772a476be6b06f52b80abc05f952a33209711 /tests/t_iovec.c
parentf33203e15a00449152283cab2b878c0886fcede5 (diff)
Add a unit test harness and a first set of tests
make check builds tests/runner from the modules that link without the io subsystem and runs it.
Diffstat (limited to 'tests/t_iovec.c')
-rw-r--r--tests/t_iovec.c124
1 files changed, 124 insertions, 0 deletions
diff --git a/tests/t_iovec.c b/tests/t_iovec.c
new file mode 100644
index 0000000..89f3fda
--- /dev/null
+++ b/tests/t_iovec.c
@@ -0,0 +1,124 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever.
3
4 $id$ */
5
6/* System */
7#include <stdlib.h>
8#include <string.h>
9#include <sys/uio.h>
10
11/* Opentracker */
12#include "ot_iovec.h"
13#include "test.h"
14
15static void case_increase_and_length(void) {
16 size_t entries = 0;
17 struct iovec *vector = NULL;
18 void *first, *second;
19
20 test_case("increase appends an entry and length is their sum");
21 first = iovec_increase(&entries, &vector, 100);
22 CHECK(first != NULL);
23 CHECK_INT(entries, 1);
24 CHECK_INT(vector[0].iov_len, 100);
25 CHECK_PTR(vector[0].iov_base, first);
26
27 second = iovec_increase(&entries, &vector, 50);
28 CHECK(second != NULL);
29 CHECK_INT(entries, 2);
30 CHECK_INT(vector[1].iov_len, 50);
31 CHECK_INT(iovec_length(&entries, (const struct iovec **)&vector), 150);
32
33 iovec_free(&entries, &vector);
34}
35
36static void case_free_resets(void) {
37 size_t entries = 0;
38 struct iovec *vector = NULL;
39
40 test_case("free releases the entries and resets the vector");
41 CHECK(iovec_increase(&entries, &vector, 10) != NULL);
42 CHECK(iovec_increase(&entries, &vector, 10) != NULL);
43 iovec_free(&entries, &vector);
44 CHECK_INT(entries, 0);
45 CHECK_PTR(vector, NULL);
46 CHECK_INT(iovec_length(&entries, (const struct iovec **)&vector), 0);
47}
48
49static void case_fixlast_shrinks(void) {
50 size_t entries = 0;
51 struct iovec *vector = NULL;
52 char *block;
53
54 test_case("fixlast trims the last entry to the bytes actually used");
55 CHECK(iovec_increase(&entries, &vector, 64) != NULL);
56 block = iovec_increase(&entries, &vector, 1000);
57 CHECK(block != NULL);
58 memset(block, 'x', 20);
59
60 iovec_fixlast(&entries, &vector, block + 20);
61 CHECK_INT(entries, 2);
62 CHECK_INT(vector[1].iov_len, 20);
63 CHECK_INT(iovec_length(&entries, (const struct iovec **)&vector), 84);
64 CHECK_MEM(vector[1].iov_base, "xxxxxxxxxxxxxxxxxxxx", 20);
65
66 iovec_free(&entries, &vector);
67}
68
69static void case_fixlast_on_empty(void) {
70 size_t entries = 0;
71 struct iovec *vector = NULL;
72
73 test_case("fixlast on an empty vector does nothing");
74 iovec_fixlast(&entries, &vector, NULL);
75 CHECK_INT(entries, 0);
76 CHECK_PTR(vector, NULL);
77}
78
79static void case_append_takes_ownership(void) {
80 size_t entries = 0;
81 struct iovec *vector = NULL;
82 struct iovec source;
83
84 test_case("append moves the buffer and clears the source");
85 source.iov_base = malloc(10);
86 CHECK(source.iov_base != NULL);
87 memcpy(source.iov_base, "0123456789", 10);
88 source.iov_len = 10;
89
90 CHECK(iovec_append(&entries, &vector, &source) != NULL);
91 CHECK_INT(entries, 1);
92 CHECK_INT(vector[0].iov_len, 10);
93 CHECK_MEM(vector[0].iov_base, "0123456789", 10);
94 CHECK_PTR(source.iov_base, NULL);
95 CHECK_INT(source.iov_len, 0);
96
97 iovec_free(&entries, &vector);
98}
99
100static void case_fix_increase(void) {
101 size_t entries = 0;
102 struct iovec *vector = NULL;
103 char *block;
104
105 test_case("fix_increase trims the previous entry and adds a new one");
106 block = iovec_increase(&entries, &vector, 500);
107 CHECK(block != NULL);
108 block = iovec_fix_increase_or_free(&entries, &vector, block + 30, 200);
109 CHECK(block != NULL);
110 CHECK_INT(entries, 2);
111 CHECK_INT(vector[0].iov_len, 30);
112 CHECK_INT(vector[1].iov_len, 200);
113
114 iovec_free(&entries, &vector);
115}
116
117void test_iovec(void) {
118 case_increase_and_length();
119 case_free_resets();
120 case_fixlast_shrinks();
121 case_fixlast_on_empty();
122 case_append_takes_ownership();
123 case_fix_increase();
124}