diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2023-09-15 22:09:46 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2023-09-15 22:09:46 +0200 |
commit | 94c01870418373c93744b73d29ebc74986cedd5c (patch) | |
tree | 1635e46a41fba46f5a632d8b594da901f8afe871 /source/tests | |
parent | bdcfa4e239d2c913c7045d0aa835ddd5a47e5436 (diff) | |
download | kit-94c01870418373c93744b73d29ebc74986cedd5c.zip |
file mapping
Diffstat (limited to 'source/tests')
-rw-r--r-- | source/tests/file.test.c | 76 | ||||
-rw-r--r-- | source/tests/test_interprocess.c | 2 |
2 files changed, 71 insertions, 7 deletions
diff --git a/source/tests/file.test.c b/source/tests/file.test.c index e6071a1..2a37c07 100644 --- a/source/tests/file.test.c +++ b/source/tests/file.test.c @@ -1,6 +1,5 @@ #include "../kit/file.h" #include "../kit/string_ref.h" -#include <string.h> #define KIT_TEST_FILE file #include "../kit/kit_test.h" @@ -205,16 +204,79 @@ TEST("file path take windows disk name") { REQUIRE(AR_EQUAL(path_take(foobar, 2), bar)); } -TEST("file create folder") { } +TEST("file create folder") { + str_t folder_name = SZ("test_folder"); -TEST("file create folder recursive") { } + REQUIRE_EQ(folder_create(folder_name), KIT_OK); + REQUIRE_EQ(path_type(folder_name), PATH_FOLDER); + REQUIRE_EQ(folder_remove(folder_name), KIT_OK); + REQUIRE_EQ(path_type(folder_name), PATH_NONE); +} + +TEST("file create folder recursive") { + REQUIRE_EQ(folder_create_recursive( + SZ("test_folder" PATH_DELIM "foo" PATH_DELIM "bar")), + KIT_OK); + REQUIRE_EQ(path_type(SZ("test_folder")), PATH_FOLDER); + REQUIRE_EQ(path_type(SZ("test_folder" PATH_DELIM "foo")), + PATH_FOLDER); + REQUIRE_EQ( + path_type(SZ("test_folder" PATH_DELIM "foo" PATH_DELIM "bar")), + PATH_FOLDER); + REQUIRE_EQ(file_remove_recursive(SZ("test_folder"), NULL), KIT_OK); + REQUIRE_EQ(path_type(SZ("test_folder")), PATH_NONE); +} + +TEST("file remove") { + FILE *f = fopen("test_file", "wb"); + fclose(f); + + REQUIRE_EQ(path_type(SZ("test_file")), PATH_FILE); + REQUIRE_EQ(file_remove(SZ("test_file")), KIT_OK); + REQUIRE_EQ(path_type(SZ("test_file")), PATH_NONE); + REQUIRE_EQ(file_remove(SZ("unexisted_file_foo_bar")), + KIT_ERROR_UNLINK_FAILED); +} + +TEST("file map read") { + char buf[] = "bar"; -TEST("file remove") { } + FILE *f = fopen("foo", "wb"); + fwrite(buf, 1, 3, f); + fclose(f); -TEST("file remove folder") { } + mapped_file_t mf = file_map(SZ("foo"), 3, FILE_MAP_PRIVATE); -TEST("file remove recursive") { } + REQUIRE_EQ(mf.status, KIT_OK); + if (mf.status == KIT_OK) { + REQUIRE_EQ(memcmp(buf, mf.bytes, 3), 0); + file_unmap(&mf); + } -TEST("file enum folder") { } + file_remove(SZ("foo")); +} + +TEST("file map write") { + mapped_file_t mf = file_map(SZ("foo"), 3, FILE_MAP_SHARED); + + REQUIRE_EQ(mf.status, KIT_OK); + if (mf.status == KIT_OK) { + memcpy(mf.bytes, "bar", 3); + file_unmap(&mf); + + char buf[3]; + FILE *f = fopen("foo", "rb"); + i64 n = (i64) fread(buf, 1, 3, f); + REQUIRE_EQ(n, 3); + fclose(f); + + if (n == 3) { + printf("READ: %d %d %d \n", buf[0], buf[1], buf[2]); + REQUIRE_EQ(memcmp(buf, "bar", 3), 0); + } + + file_remove(SZ("foo")); + } +} #undef KIT_TEST_FILE diff --git a/source/tests/test_interprocess.c b/source/tests/test_interprocess.c index a69dd02..cf33086 100644 --- a/source/tests/test_interprocess.c +++ b/source/tests/test_interprocess.c @@ -10,6 +10,8 @@ enum { DATA_SIZE = 64 }; enum { STATE_INIT, STATE_READY, STATE_DONE }; int run_writer() { + kit_shared_memory_clean(SZ(NAME)); + kit_shared_memory_t mem = kit_shared_memory_open( SZ(NAME), DATA_SIZE, KIT_SHARED_MEMORY_CREATE); |