From 3b66a08d63c11c9fab09e31f8d7b965d822fac8e Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 6 Feb 2026 06:06:10 +0000 Subject: [PATCH] test_stream: implement stream_test_substr --- alisp.org | 2 +- test/test_stream.c | 105 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 2 deletions(-) diff --git a/alisp.org b/alisp.org index 5937cf4..1e22327 100644 --- a/alisp.org +++ b/alisp.org @@ -70,7 +70,7 @@ Also ensure stream_eoc is false. - Seeking forward/backward on a bad stream (should stop at 0) - Seeking forward/backward too far (should clamp) - Seeking forward/backward zero sum via relative index (stream_seek) -**** TODO Test substring +**** DONE Test substring [[file:test/test_stream.c::void stream_test_substr(void)]] - Substr on bad stream (NULL sv) - Substr on bad position/size (NULL sv) diff --git a/test/test_stream.c b/test/test_stream.c index 84bc9fe..292fe02 100644 --- a/test/test_stream.c +++ b/test/test_stream.c @@ -277,7 +277,109 @@ void stream_test_seek(void) void stream_test_substr(void) { TEST_START(); - TODO("Not implemented"); + u64 size = rand() % (ARRSIZE(words_text) - 1); + u64 position = ARRSIZE(words_text) - size - 1; + + // Taking substrings of invalid streams + { + stream_t stream = {0}; + stream_init_file(&stream, NULL, invalid_fp); + + // Relative + { + sv_t result = stream_substr(&stream, size); + TEST(result.data == NULL && result.size == 0, + "Relative substring with size %lu on invalid stream should be NULL", + size); + } + + // Absolute + { + sv_t result = stream_substr_abs(&stream, position, size); + TEST(result.data == NULL && result.size == 0, + "Absolute substring @%lu with size %lu on invalid stream should be " + "NULL", + position, size); + } + + stream_stop(&stream); + } + + // Taking substrings of valid streams + { + stream_t stream = {0}; + stream_init_file(&stream, valid_filename, valid_fp); + + // Absolute + { + sv_t result = stream_substr_abs(&stream, position, size); + TEST(result.data && result.size, + "Absolute substring @%lu with size %lu on valid stream should be " + "nonzero", + position, size); + + TEST(result.size == size, "Substring has right size (%lu)", result.size); + + sv_t expected = SV((char *)words_text + position, size); + TEST(strncmp(result.data, expected.data, result.size) == 0, + "Expect the substring to be the same as the data we put in"); + } + + // Relative + { + sv_t result = stream_substr(&stream, size); + TEST(result.data && result.size, + "Relative substring with size %lu should be nonzero", size); + + TEST(result.size == size, "Substring has right size (%lu)", result.size); + + sv_t expected = SV((char *)words_text, size); + TEST(strncmp(result.data, expected.data, result.size) == 0, + "Expect the substring to be the same as the data we put in"); + } + + // Relative substring after seeking + { + // Shift forward to a random position + assert(stream_seek_forward(&stream, position)); // not a test + sv_t result = stream_substr(&stream, size); + TEST(result.data && result.size, + "Relative substring with size %lu after seeking %lu bytes should be " + "nonzero", + size, position); + + TEST(result.size == size, "Substring has right size (%lu)", result.size); + + sv_t expected = SV((char *)words_text + position, size); + TEST(strncmp(result.data, expected.data, result.size) == 0, + "Expect the substring to be the same as the data we put in"); + + // Shift back to the original position. + assert(stream_seek_backward(&stream, position)); // not a test + } + + // Bad substrings + { + { + sv_t result = stream_substr_abs(&stream, stream_size(&stream), 100); + TEST(!result.data && !result.size, + "Absolute substring at %lu of 100 bytes is invalid", + stream_size(&stream)); + } + + assert(stream_seek_forward(&stream, stream_size(&stream))); // not a test + { + sv_t result = stream_substr(&stream, 100); + TEST(!result.data && !result.size, + "Relative substring with size 100 after seeking %lu bytes is " + "invalid", + stream.position); + } + } + + stream_stop(&stream); + } + TEST_END(); } void stream_test_till(void) @@ -305,6 +407,7 @@ MAKE_TEST_SUITE(STREAM_SUITE, "Stream Tests", MAKE_TEST_FN(stream_test_file), MAKE_TEST_FN(stream_test_peek_next), MAKE_TEST_FN(stream_test_seek), + MAKE_TEST_FN(stream_test_substr), MAKE_TEST_FN(stream_test_epilogue), ); /* Copyright (C) 2026 Aryadev Chavali