1#include "test.h"
2#include <gtest/gtest.h>
3
4#include <base/system.h>
5
6void TestFileRead(const char *pWritten)
7{
8 CTestInfo Info;
9 char aBuf[512] = {0};
10 IOHANDLE File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE);
11 const int WrittenLength = str_length(str: pWritten);
12 ASSERT_TRUE(File);
13 EXPECT_EQ(io_write(File, pWritten, WrittenLength), WrittenLength);
14 EXPECT_FALSE(io_close(File));
15 File = io_open(filename: Info.m_aFilename, flags: IOFLAG_READ);
16 ASSERT_TRUE(File);
17 EXPECT_EQ(io_read(File, aBuf, sizeof(aBuf)), WrittenLength);
18 EXPECT_TRUE(mem_comp(aBuf, pWritten, WrittenLength) == 0);
19 EXPECT_FALSE(io_close(File));
20
21 fs_remove(filename: Info.m_aFilename);
22}
23
24TEST(Io, Read1)
25{
26 TestFileRead(pWritten: "");
27}
28TEST(Io, Read2)
29{
30 TestFileRead(pWritten: "abc");
31}
32TEST(Io, Read3)
33{
34 TestFileRead(pWritten: "\xef\xbb\xbf");
35}
36TEST(Io, Read4)
37{
38 TestFileRead(pWritten: "\xef\xbb\xbfxyz");
39}
40
41TEST(Io, CurrentExe)
42{
43 IOHANDLE CurrentExe = io_current_exe();
44 ASSERT_TRUE(CurrentExe);
45 EXPECT_GE(io_length(CurrentExe), 1024);
46 io_close(io: CurrentExe);
47}
48TEST(Io, SyncWorks)
49{
50 CTestInfo Info;
51 IOHANDLE File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE);
52 ASSERT_TRUE(File);
53 EXPECT_EQ(io_write(File, "abc\n", 4), 4);
54 EXPECT_FALSE(io_sync(File));
55 EXPECT_FALSE(io_close(File));
56 EXPECT_FALSE(fs_remove(Info.m_aFilename));
57}
58
59TEST(Io, WriteTruncatesFile)
60{
61 CTestInfo Info;
62
63 IOHANDLE File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE);
64 ASSERT_TRUE(File);
65 EXPECT_EQ(io_write(File, "0123456789", 10), 10);
66 EXPECT_FALSE(io_close(File));
67
68 File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE);
69 EXPECT_EQ(io_write(File, "ABCDE", 5), 5);
70 EXPECT_FALSE(io_close(File));
71
72 char aBuf[16];
73 File = io_open(filename: Info.m_aFilename, flags: IOFLAG_READ);
74 ASSERT_TRUE(File);
75 EXPECT_EQ(io_read(File, aBuf, sizeof(aBuf)), 5);
76 EXPECT_TRUE(mem_comp(aBuf, "ABCDE", 5) == 0);
77 EXPECT_FALSE(io_close(File));
78
79 EXPECT_FALSE(fs_remove(Info.m_aFilename));
80}
81