From 5dd98385e03b9cc7c912b1b50f7c406df117a9af Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Tue, 12 Aug 2025 00:35:18 +0200 Subject: piece-table: Don't allocate in piece_table_to_string `piece_table_to_string` now receives a buffer and we also provide a function that returns the length of the piece table. TODO: the `piece_table_to_string` adds null termination, that makes the buffer needed `piece_table_length(pt) + 1`. This we should document or do more obvious. --- tests/piece-table-insert.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'tests/piece-table-insert.c') diff --git a/tests/piece-table-insert.c b/tests/piece-table-insert.c index 90a97a2..bcf6dc5 100644 --- a/tests/piece-table-insert.c +++ b/tests/piece-table-insert.c @@ -1,19 +1,28 @@ #include +#include #include "../src/piece-table.h" int main() { - piece_table *pt = piece_table_create("1234567890"); + piece_table *pt = piece_table_create("0123456789"); + char tmp[100]; + piece_table_insert(pt, 10, "abcdefgh", 8); - piece_table_insert(pt, 18, "abcdefgh", 8); - if (strcmp(piece_table_to_string(pt), "1234567890abcdefghabcdefgh")) + piece_table_to_string(pt, tmp); + if (strcmp(tmp, "0123456789abcdefgh")) return 1; - /* TODO: must have two pieces if optimized properly - 1234567890 - abcdefghabcdefgh - TODO: check other things too */ + piece_table_insert(pt, 18, "abcdefgh", 8); + piece_table_to_string(pt, tmp); + if (strcmp(tmp, "0123456789abcdefghabcdefgh")) + return 2; + + piece_table_insert(pt, 4, "abcdefgh", 8); + piece_table_to_string(pt, tmp); + if (strcmp(tmp, "0123abcdefgh456789abcdefghabcdefgh")) + return 3; + piece_table_destroy(pt); return 0; } -- cgit v1.2.3