From a662454ea707c96d4ab5f48b1a81c0c8548888f9 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Thu, 5 Feb 2026 20:47:28 +0000 Subject: [PATCH] test_stream: implement stream_test_peek_next --- alisp.org | 2 +- test/test_stream.c | 49 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/alisp.org b/alisp.org index 1e72b42..b052255 100644 --- a/alisp.org +++ b/alisp.org @@ -58,7 +58,7 @@ other state do we need to encode? Ensure stream_size is 0 i.e. we don't read anything on creation. Also ensure stream_eoc is false. ***** DONE Test failed init from fake files -**** TODO Test peeking and next +**** DONE Test peeking and next [[file:test/test_stream.c::void stream_test_peek_next(void)]] - Peeking with bad streams ('\0' return) - Peeking with good streams (no effect on position) diff --git a/test/test_stream.c b/test/test_stream.c index 75cf001..56ce471 100644 --- a/test/test_stream.c +++ b/test/test_stream.c @@ -116,7 +116,53 @@ void stream_test_file(void) void stream_test_peek_next(void) { TEST_START(); - TODO("Not implemented"); + + // Valid streams + { + stream_t stream = {0}; + stream_init_file(&stream, valid_filename, valid_fp); + + u64 old_position = stream.position; + char c1 = stream_peek(&stream); + TEST(c1 != '\0', "Peek should provide a normal character (%c)", c1); + TEST(stream.position == old_position, + "Peek should not shift the position (%lu -> %lu)", old_position, + stream.position); + + char c2 = stream_next(&stream); + TEST(c2 != '\0', "Next should provide a normal character (%c)", c2); + TEST(stream.position > old_position, + "Next should shift the position (%lu -> %lu)", old_position, + stream.position); + TEST(c2 != c1, + "Next should yield a different character (%c) to the previous peek " + "(%c)", + c2, c1); + + char c3 = stream_peek(&stream); + TEST(c3 == c2, + "Peeking should yield the same character (%c) as the previous next " + "(%c)", + c3, c2); + + stream_stop(&stream); + } + + // Invalid streams + { + stream_t stream = {0}; + stream_init_file(&stream, NULL, invalid_fp); + char c = stream_peek(&stream); + TEST(c == '\0', "Invalid streams should have an invalid peek (%c)", c); + + u64 old_position = stream.position; + c = stream_next(&stream); + TEST(c == '\0', "Invalid streams should have an invalid next (%c)", c); + TEST(old_position == stream.position, + "Next on an invalid stream should not affect position (%lu -> %lu)", + old_position, stream.position); + } + TEST_END(); } void stream_test_seek(void) @@ -154,6 +200,7 @@ MAKE_TEST_SUITE(STREAM_SUITE, "Stream Tests", MAKE_TEST_FN(stream_test_prologue), MAKE_TEST_FN(stream_test_string), MAKE_TEST_FN(stream_test_file), + MAKE_TEST_FN(stream_test_peek_next), MAKE_TEST_FN(stream_test_epilogue), ); /* Copyright (C) 2026 Aryadev Chavali