test: iob_init and iob_close tests (+ small typo fix in src)

This commit is contained in:
Gu://em_ 2026-01-08 16:59:37 +01:00 committed by Matteo Flebus
parent a653c02c9f
commit e5925d69ec
3 changed files with 124 additions and 2 deletions

View file

@ -17,7 +17,7 @@ static enum iob_state state = IOB_STATE_NOT_INITIALIZED;
int iob_init(struct iob_context *ctx)
{
if (state != IOB_STATE_NOT_INITIALIZED)
return IOB_ERROR_MODULE_NOT_INITIALIZED;
return IOB_ERROR_MODULE_ALREADY_INITIALIZED;
context = *ctx;

View file

@ -7,7 +7,7 @@
#define IOB_ERROR_GENERIC -1
#define IOB_ERROR_BAD_ARG -2
#define IOB_ERROR_MODULE_NOT_INITIALIZED -3
#define IOB_ERROR_MODULE_ALREADY_INITALIZED -4
#define IOB_ERROR_MODULE_ALREADY_INITIALIZED -4
#define IOB_ERROR_CANNOT_OPEN_FILE -5
enum iob_mode {

View file

@ -0,0 +1,122 @@
#include <criterion/criterion.h>
#include <criterion/new/assert.h>
#include <io_backend/io_backend.h>
#include <stdio.h>
TestSuite(IO_Backend);
// IOB Init
Test(IO_Backend, init_null)
{
struct iob_context ctx = {
.iob_mode = IOB_MODE_NULL;
.args = NULL;
};
int actual = iob_init(ctx);
int expected = IOB_ERROR_BAD_ARG;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
Test(IO_Backend, init_stdin)
{
struct iob_context ctx = {
.iob_mode = IOB_MODE_STDIN;
.args = NULL;
};
int actual = iob_init(ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
}
// WARNING: this one could fail because of iob_close in the previous test
// Same applies for other tests
Test(IO_Backend, init_script)
{
char* script_name = "script.tmp"
struct iob_context ctx = {
.iob_mode = IOB_MODE_SCRIPT;
.args = script_name;
};
// Create file
FILE* f = fopen(script_name, "w");
fclose(f);
int actual = iob_init(ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
remove(script_name);
}
Test(IO_Backend, init_script_not_a_file)
{
char* script_name = "not_a_file.tmp"
struct iob_context ctx = {
.iob_mode = IOB_MODE_SCRIPT;
.args = script_name;
};
int actual = iob_init(ctx);
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
Test(IO_Backend, init_script_null)
{
struct iob_context ctx = {
.iob_mode = IOB_MODE_SCRIPT;
.args = NULL;
};
int actual = iob_init(ctx);
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
Test(IO_Backend, init_cmd)
{
char* cmd = "iamacommand --yesido"
struct iob_context ctx = {
.iob_mode = IOB_MODE_CMD;
.args = cmd;
};
int actual = iob_init(ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
}
Test(IO_Backend, init_cmd_null)
{
struct iob_context ctx = {
.iob_mode = IOB_MODE_CMD;
.args = NULL;
};
int actual = iob_init(ctx);
int expected = IOB_ERROR_BAD_ARG;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
Test(IO_Backend, init_already_init)
{
char* cmd = "iamacommand --yesido"
struct iob_context ctx = {
.iob_mode = IOB_MODE_CMD;
.args = cmd;
};
iob_init(ctx);
int actual = iob_init(ctx);
int expected = IOB_ERROR_ALREADY_INITIALIZED;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
}
Test(IO_Backend, close_not_init)
{
iob_close(); // Shouldn't do anything
}
// IOB Stream
// TODO