Add stream structure and utilities

This commit is contained in:
2026-08-30 02:18:30 +01:00
parent a51686c8f0
commit c1b2e29c07
9 changed files with 237 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
// vim:fileencoding=utf-8:foldmethod=marker
#include "test_stream.h"
#define COUNT 3
WpTestFuncResult test_stream_at_end(void) {
b8 result = true;
c8 characters[COUNT] = {'a', 'b', 'c'};
WpC8Stream stream = wpStream(c8, characters, COUNT);
for (u32 i = 0; i < COUNT; ++i) {
wpStreamConsume(c8, &stream);
result = result && (i == 2 ? wpStreamAtEnd(c8, &stream) : !wpStreamAtEnd(c8, &stream));
}
return wpTesterResult(result);
}
WpTestFuncResult test_stream_peek(void) {
b8 result = true;
c8 characters[COUNT] = {'a', 'b', 'c'};
WpC8Stream stream = wpStream(c8, characters, COUNT);
c8 *c = NULL;
while (!wpStreamAtEnd(c8, &stream)) {
c = wpStreamPeek(c8, &stream);
wpStreamConsume(c8, &stream);
result = result && (wpStreamAtEnd(c8, &stream) ? c == NULL : c != NULL);
}
return wpTesterResult(result);
}
WpTestFuncResult test_stream_consume(void) {
b8 result = true;
c8 characters[COUNT] = {'a', 'b', 'c'};
WpC8Stream stream = wpStream(c8, characters, COUNT);
c8 *c = NULL;
for (u32 i = 0; i <= COUNT; ++i) {
c = wpStreamConsume(c8, &stream);
result = result && (i == COUNT ? c == NULL : c != NULL);
}
return wpTesterResult(result);
}
+50
View File
@@ -0,0 +1,50 @@
// vim:fileencoding=utf-8:foldmethod=marker
#include "test_stream.h"
#define COUNT 3
WpTestFuncResult test_stream_at_end(void) {
b8 result = true;
c8 characters[COUNT] = {'a', 'b', 'c'};
WpC8Stream stream = wpStream(c8, characters, COUNT);
for (u32 i = 0; i < COUNT; ++i) {
wpStreamConsume(c8, &stream);
result = result && (i == 2 ? wpStreamAtEnd(c8, &stream) : !wpStreamAtEnd(c8, &stream));
}
return wpTesterResult(result);
}
WpTestFuncResult test_stream_peek(void) {
b8 result = true;
c8 characters[COUNT] = {'a', 'b', 'c'};
WpC8Stream stream = wpStream(c8, characters, COUNT);
c8 *c = NULL;
while (!wpStreamAtEnd(c8, &stream)) {
c = wpStreamPeek(c8, &stream);
wpStreamConsume(c8, &stream);
result = result && (wpStreamAtEnd(c8, &stream) ? c == NULL : c != NULL);
}
return wpTesterResult(result);
}
WpTestFuncResult test_stream_consume(void) {
b8 result = true;
c8 characters[COUNT] = {'a', 'b', 'c'};
WpC8Stream stream = wpStream(c8, characters, COUNT);
c8 *c = NULL;
for (u32 i = 0; i <= COUNT; ++i) {
c = wpStreamConsume(c8, &stream);
result = result && (i == COUNT ? c == NULL : c != NULL);
}
return wpTesterResult(result);
}
+12
View File
@@ -0,0 +1,12 @@
// vim:fileencoding=utf-8:foldmethod=marker
#ifndef TEST_STREAM_H
#define TEST_STREAM_H
#include "wapp.h"
WpTestFuncResult test_stream_at_end(void);
WpTestFuncResult test_stream_peek(void);
WpTestFuncResult test_stream_consume(void);
#endif // !TEST_STREAM_H
+4
View File
@@ -5,6 +5,7 @@
#include "test_str8_array.h"
#include "test_i32_array.h"
#include "test_queue.h"
#include "test_stream.h"
#include "test_cpath.h"
#include "test_file.h"
#include "test_shell_commander.h"
@@ -47,6 +48,9 @@ int main(void) {
test_queue_push,
test_queue_push_alloc,
test_queue_pop,
test_stream_at_end,
test_stream_peek,
test_stream_consume,
test_str8_lit,
test_str8_lit_ro,
test_str8_buf,
+4
View File
@@ -5,6 +5,7 @@
#include "test_str8_array.h"
#include "test_i32_array.h"
#include "test_queue.h"
#include "test_stream.h"
#include "test_cpath.h"
#include "test_file.h"
#include "test_shell_commander.h"
@@ -47,6 +48,9 @@ int main(void) {
test_queue_push,
test_queue_push_alloc,
test_queue_pop,
test_stream_at_end,
test_stream_peek,
test_stream_consume,
test_str8_lit,
test_str8_lit_ro,
test_str8_buf,