1#include "test.h"
2#include <gtest/gtest.h>
3
4#include <base/system.h>
5
6TEST(Filesystem, Filename)
7{
8 EXPECT_STREQ(fs_filename(""), "");
9 EXPECT_STREQ(fs_filename("a"), "a");
10 EXPECT_STREQ(fs_filename("abc"), "abc");
11 EXPECT_STREQ(fs_filename("a/b"), "b");
12 EXPECT_STREQ(fs_filename("a/b/c"), "c");
13 EXPECT_STREQ(fs_filename("aaaaa/bbbb/ccc"), "ccc");
14 EXPECT_STREQ(fs_filename("aaaaa\\bbbb\\ccc"), "ccc");
15 EXPECT_STREQ(fs_filename("aaaaa/bbbb\\ccc"), "ccc");
16 EXPECT_STREQ(fs_filename("aaaaa\\bbbb/ccc"), "ccc");
17}
18
19TEST(Filesystem, SplitFileExtension)
20{
21 char aName[IO_MAX_PATH_LENGTH];
22 char aExt[IO_MAX_PATH_LENGTH];
23
24 fs_split_file_extension(filename: "", name: aName, name_size: sizeof(aName), extension: aExt, extension_size: sizeof(aExt));
25 EXPECT_STREQ(aName, "");
26 EXPECT_STREQ(aExt, "");
27
28 fs_split_file_extension(filename: "name.ext", name: aName, name_size: sizeof(aName), extension: aExt, extension_size: sizeof(aExt));
29 EXPECT_STREQ(aName, "name");
30 EXPECT_STREQ(aExt, "ext");
31
32 fs_split_file_extension(filename: "name.ext", name: aName, name_size: sizeof(aName)); // extension parameter is optional
33 EXPECT_STREQ(aName, "name");
34
35 fs_split_file_extension(filename: "name.ext", name: nullptr, name_size: 0, extension: aExt, extension_size: sizeof(aExt)); // name parameter is optional
36 EXPECT_STREQ(aExt, "ext");
37
38 fs_split_file_extension(filename: "archive.tar.gz", name: aName, name_size: sizeof(aName), extension: aExt, extension_size: sizeof(aExt));
39 EXPECT_STREQ(aName, "archive.tar");
40 EXPECT_STREQ(aExt, "gz");
41
42 fs_split_file_extension(filename: "no_dot", name: aName, name_size: sizeof(aName), extension: aExt, extension_size: sizeof(aExt));
43 EXPECT_STREQ(aName, "no_dot");
44 EXPECT_STREQ(aExt, "");
45
46 fs_split_file_extension(filename: ".dot_first", name: aName, name_size: sizeof(aName), extension: aExt, extension_size: sizeof(aExt));
47 EXPECT_STREQ(aName, ".dot_first");
48 EXPECT_STREQ(aExt, "");
49}
50
51TEST(Filesystem, CreateCloseDelete)
52{
53 CTestInfo Info;
54
55 IOHANDLE File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE);
56 ASSERT_TRUE(File);
57 EXPECT_FALSE(io_close(File));
58 EXPECT_FALSE(fs_remove(Info.m_aFilename));
59}
60
61TEST(Filesystem, CreateDeleteDirectory)
62{
63 CTestInfo Info;
64 char aFilename[IO_MAX_PATH_LENGTH];
65 str_format(buffer: aFilename, buffer_size: sizeof(aFilename), format: "%s/test.txt", Info.m_aFilename);
66
67 EXPECT_FALSE(fs_makedir(Info.m_aFilename));
68 IOHANDLE File = io_open(filename: aFilename, flags: IOFLAG_WRITE);
69 ASSERT_TRUE(File);
70 EXPECT_FALSE(io_close(File));
71
72 // Directory removal fails if there are any files left in the directory.
73 EXPECT_TRUE(fs_removedir(Info.m_aFilename));
74
75 EXPECT_FALSE(fs_remove(aFilename));
76 EXPECT_FALSE(fs_removedir(Info.m_aFilename));
77}
78
79TEST(Filesystem, CantDeleteDirectoryWithRemove)
80{
81 CTestInfo Info;
82 EXPECT_FALSE(fs_makedir(Info.m_aFilename));
83 EXPECT_TRUE(fs_remove(Info.m_aFilename)); // Cannot remove directory with file removal function.
84 EXPECT_FALSE(fs_removedir(Info.m_aFilename));
85}
86
87TEST(Filesystem, CantDeleteFileWithRemoveDirectory)
88{
89 CTestInfo Info;
90 IOHANDLE File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE);
91 ASSERT_TRUE(File);
92 EXPECT_FALSE(io_close(File));
93 EXPECT_TRUE(fs_removedir(Info.m_aFilename)); // Cannot remove file with directory removal function.
94 EXPECT_FALSE(fs_remove(Info.m_aFilename));
95}
96