Add IO type checking functions, add ReadLine

This commit is contained in:
2025-11-08 18:50:13 +03:00
parent 44057e96f3
commit 6d02598e20
14 changed files with 310 additions and 0 deletions

View File

@@ -2,10 +2,13 @@
#define BH_UNIT_H
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef int (*BH_UnitCallback)(void);
#define BH_VERIFY(e) \
do { \
if (!(e)) { \
@@ -35,6 +38,17 @@ typedef int (*BH_UnitCallback)(void);
} while(0)
#define BH_VERIFY_STR_EQ(actual, expected) \
do { \
BH_VERIFY((actual) != NULL && (expected) != NULL); \
if (strcmp((actual), (expected)) != 0) { \
printf("%s:%d\tExpected '%s', got '%s'\n", \
__FILE__, __LINE__, (expected), (actual)); \
return -1; \
} \
} while(0)
#define BH_UNIT_TEST(name) \
static int unit##name(void)

View File

@@ -7,6 +7,7 @@
BH_UNIT_TEST(Null)
{
BH_VERIFY(BH_BufferNew(NULL, 0, NULL) == NULL);
BH_VERIFY(BH_IOIsBuffer(NULL) == 0);
return 0;
}
@@ -21,6 +22,7 @@ BH_UNIT_TEST(Write)
memset(data, 0, 16);
BH_VERIFY((io = BH_BytesNew(data, 16, NULL)) != NULL);
BH_VERIFY((buffer = BH_BufferNew(io, 4, NULL)) != NULL);
BH_VERIFY(BH_IOIsBuffer(buffer));
BH_VERIFY(BH_IOWrite(buffer, "1234567", 7, &size) == BH_OK);
BH_VERIFY(size == 7);

View File

@@ -10,6 +10,8 @@ BH_UNIT_TEST(Null)
BH_VERIFY(BH_BytesNew(NULL, 0, NULL) == NULL);
BH_VERIFY(BH_BytesNew(&data, 0, NULL) == NULL);
BH_VERIFY(BH_BytesNew(NULL, 1234, NULL) == NULL);
BH_VERIFY(BH_IOIsBytes(NULL) == 0);
return 0;
}
@@ -22,6 +24,7 @@ BH_UNIT_TEST(Read)
size_t size;
BH_VERIFY((io = BH_BytesNew(buffer1, 14, NULL)) != NULL);
BH_VERIFY(BH_IOIsBytes(io));
BH_VERIFY(BH_IORead(io, buffer2, 14, &size) == BH_OK);
BH_VERIFY(size == 14);
BH_VERIFY(memcmp(buffer1, buffer2, 14) == 0);

View File

@@ -40,6 +40,7 @@ static int checkNull(void)
BH_VERIFY(BH_IOCap(NULL, 0) != BH_OK);
BH_VERIFY(BH_IOEndOfFile(NULL) != BH_OK);
BH_VERIFY(BH_IOError(NULL) != BH_OK);
BH_VERIFY(BH_IOIsFile(NULL) == 0);
BH_IOFree(NULL);
return 0;
@@ -58,6 +59,7 @@ static int checkNormal(void)
/* Check operations for write only access */
BH_VERIFY((io = BH_FileNew(FILENAME1, BH_FILE_WRITE, NULL)) != NULL);
BH_VERIFY(BH_IOIsFile(io));
BH_VERIFY(BH_IOWrite(io, "1234567890", 10, &actual) == BH_OK);
BH_VERIFY(actual == 10);

76
test/tests/TestReadLine.c Normal file
View File

@@ -0,0 +1,76 @@
#include <BH/Unit.h>
#include <BH/IO.h>
#include <string.h>
static const char testInput[] =
"this is a very long line that exceeds the buffer\n"
"short\n"
"another line\n";
BH_UNIT_TEST(ReadLine)
{
char buffer[32], *result;
BH_IO *io;
BH_VERIFY((io = BH_BytesNew((char*)testInput, strlen(testInput), NULL)));
/* Check splited read */
BH_VERIFY((result = BH_IOReadLine(io, buffer, sizeof(buffer))));
BH_VERIFY_STR_EQ(buffer, "this is a very long line that e");
BH_VERIFY((result = BH_IOReadLine(io, buffer, sizeof(buffer))));
BH_VERIFY_STR_EQ(buffer, "xceeds the buffer\n");
/* Check smaller reads */
BH_VERIFY((result = BH_IOReadLine(io, buffer, sizeof(buffer))));
BH_VERIFY_STR_EQ(buffer, "short\n");
BH_VERIFY((result = BH_IOReadLine(io, buffer, sizeof(buffer))));
BH_VERIFY_STR_EQ(buffer, "another line\n");
/* Check EOF */
BH_VERIFY(!(result = BH_IOReadLine(io, buffer, sizeof(buffer))));
BH_IOFree(io);
return 0;
}
BH_UNIT_TEST(ReadLineFull)
{
char buffer[32], *result;
BH_IO *io;
BH_VERIFY((io = BH_BytesNew((char*)testInput, strlen(testInput), NULL)));
/* Check truncated read */
BH_VERIFY((result = BH_IOReadLineFull(io, buffer, sizeof(buffer))));
BH_VERIFY_STR_EQ(buffer, "this is a very long line that e");
/* Check smaller reads */
BH_VERIFY((result = BH_IOReadLineFull(io, buffer, sizeof(buffer))));
BH_VERIFY_STR_EQ(buffer, "short\n");
BH_VERIFY((result = BH_IOReadLineFull(io, buffer, sizeof(buffer))));
BH_VERIFY_STR_EQ(buffer, "another line\n");
/* Check EOF */
BH_VERIFY(!(result = BH_IOReadLineFull(io, buffer, sizeof(buffer))));
BH_IOFree(io);
return 0;
}
int main(int argc, char **argv)
{
BH_UNUSED(argc);
BH_UNUSED(argv);
BH_UNIT_ADD(ReadLine);
BH_UNIT_ADD(ReadLineFull);
return BH_UnitRun();
}