1 | #include "test.h" |
2 | #include <gtest/gtest.h> |
3 | |
4 | #include <base/system.h> |
5 | |
6 | void TestFileRead(const char *pWritten, bool SkipBom, const char *pRead) |
7 | { |
8 | CTestInfo Info; |
9 | char aBuf[512] = {0}; |
10 | IOHANDLE File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE); |
11 | ASSERT_TRUE(File); |
12 | EXPECT_EQ(io_write(File, pWritten, str_length(pWritten)), str_length(pWritten)); |
13 | EXPECT_FALSE(io_close(File)); |
14 | File = io_open(filename: Info.m_aFilename, flags: IOFLAG_READ | (SkipBom ? IOFLAG_SKIP_BOM : 0)); |
15 | ASSERT_TRUE(File); |
16 | EXPECT_EQ(io_read(File, aBuf, sizeof(aBuf)), str_length(pRead)); |
17 | EXPECT_TRUE(mem_comp(aBuf, pRead, str_length(pRead)) == 0); |
18 | EXPECT_FALSE(io_close(File)); |
19 | |
20 | fs_remove(filename: Info.m_aFilename); |
21 | } |
22 | |
23 | TEST(Io, Read1) |
24 | { |
25 | TestFileRead(pWritten: "" , SkipBom: false, pRead: "" ); |
26 | } |
27 | TEST(Io, Read2) |
28 | { |
29 | TestFileRead(pWritten: "abc" , SkipBom: false, pRead: "abc" ); |
30 | } |
31 | TEST(Io, Read3) |
32 | { |
33 | TestFileRead(pWritten: "\xef\xbb\xbf" , SkipBom: false, pRead: "\xef\xbb\xbf" ); |
34 | } |
35 | TEST(Io, Read4) |
36 | { |
37 | TestFileRead(pWritten: "\xef\xbb\xbfxyz" , SkipBom: false, pRead: "\xef\xbb\xbfxyz" ); |
38 | } |
39 | |
40 | TEST(Io, ReadBom1) |
41 | { |
42 | TestFileRead(pWritten: "" , SkipBom: true, pRead: "" ); |
43 | } |
44 | TEST(Io, ReadBom2) |
45 | { |
46 | TestFileRead(pWritten: "abc" , SkipBom: true, pRead: "abc" ); |
47 | } |
48 | TEST(Io, ReadBom3) |
49 | { |
50 | TestFileRead(pWritten: "\xef\xbb\xbf" , SkipBom: true, pRead: "" ); |
51 | } |
52 | TEST(Io, ReadBom4) |
53 | { |
54 | TestFileRead(pWritten: "\xef\xbb\xbfxyz" , SkipBom: true, pRead: "xyz" ); |
55 | } |
56 | TEST(Io, ReadBom5) |
57 | { |
58 | TestFileRead(pWritten: "\xef\xbb\xbf\xef\xbb\xbf" , SkipBom: true, pRead: "\xef\xbb\xbf" ); |
59 | } |
60 | TEST(Io, ReadBom6) |
61 | { |
62 | TestFileRead(pWritten: "\xef\xbb\xbfxyz\xef\xbb\xbf" , SkipBom: true, pRead: "xyz\xef\xbb\xbf" ); |
63 | } |
64 | TEST(Io, CurrentExe) |
65 | { |
66 | IOHANDLE CurrentExe = io_current_exe(); |
67 | ASSERT_TRUE(CurrentExe); |
68 | EXPECT_GE(io_length(CurrentExe), 1024); |
69 | io_close(io: CurrentExe); |
70 | } |
71 | TEST(Io, SyncWorks) |
72 | { |
73 | CTestInfo Info; |
74 | IOHANDLE File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE); |
75 | ASSERT_TRUE(File); |
76 | EXPECT_EQ(io_write(File, "abc\n" , 4), 4); |
77 | EXPECT_FALSE(io_sync(File)); |
78 | EXPECT_FALSE(io_close(File)); |
79 | EXPECT_FALSE(fs_remove(Info.m_aFilename)); |
80 | } |
81 | |
82 | TEST(Io, WriteTruncatesFile) |
83 | { |
84 | CTestInfo Info; |
85 | |
86 | IOHANDLE File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE); |
87 | ASSERT_TRUE(File); |
88 | EXPECT_EQ(io_write(File, "0123456789" , 10), 10); |
89 | EXPECT_FALSE(io_close(File)); |
90 | |
91 | File = io_open(filename: Info.m_aFilename, flags: IOFLAG_WRITE); |
92 | EXPECT_EQ(io_write(File, "ABCDE" , 5), 5); |
93 | EXPECT_FALSE(io_close(File)); |
94 | |
95 | char aBuf[16]; |
96 | File = io_open(filename: Info.m_aFilename, flags: IOFLAG_READ); |
97 | ASSERT_TRUE(File); |
98 | EXPECT_EQ(io_read(File, aBuf, sizeof(aBuf)), 5); |
99 | EXPECT_TRUE(mem_comp(aBuf, "ABCDE" , 5) == 0); |
100 | EXPECT_FALSE(io_close(File)); |
101 | |
102 | EXPECT_FALSE(fs_remove(Info.m_aFilename)); |
103 | } |
104 | |