| 1 | #include "test.h" |
| 2 | |
| 3 | #include <base/system.h> |
| 4 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | static const int BUF_SIZE = 64 * 1024; |
| 8 | |
| 9 | class Async : public ::testing::Test |
| 10 | { |
| 11 | protected: |
| 12 | ASYNCIO *m_pAio; |
| 13 | CTestInfo m_Info; |
| 14 | bool Delete; |
| 15 | |
| 16 | void SetUp() override |
| 17 | { |
| 18 | IOHANDLE File = io_open(filename: m_Info.m_aFilename, flags: IOFLAG_WRITE); |
| 19 | ASSERT_TRUE(File); |
| 20 | m_pAio = aio_new(io: File); |
| 21 | Delete = false; |
| 22 | } |
| 23 | |
| 24 | ~Async() |
| 25 | { |
| 26 | if(Delete) |
| 27 | { |
| 28 | fs_remove(filename: m_Info.m_aFilename); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void Write(const char *pText) |
| 33 | { |
| 34 | aio_write(aio: m_pAio, buffer: pText, size: str_length(str: pText)); |
| 35 | } |
| 36 | |
| 37 | void Expect(const char *pOutput) |
| 38 | { |
| 39 | aio_close(aio: m_pAio); |
| 40 | aio_wait(aio: m_pAio); |
| 41 | aio_free(aio: m_pAio); |
| 42 | |
| 43 | char aBuf[BUF_SIZE]; |
| 44 | IOHANDLE File = io_open(filename: m_Info.m_aFilename, flags: IOFLAG_READ); |
| 45 | ASSERT_TRUE(File); |
| 46 | int Read = io_read(io: File, buffer: aBuf, size: sizeof(aBuf)); |
| 47 | io_close(io: File); |
| 48 | |
| 49 | ASSERT_EQ(str_length(pOutput), Read); |
| 50 | ASSERT_TRUE(mem_comp(aBuf, pOutput, Read) == 0); |
| 51 | Delete = true; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | TEST_F(Async, Empty) |
| 56 | { |
| 57 | Expect(pOutput: "" ); |
| 58 | } |
| 59 | |
| 60 | TEST_F(Async, Simple) |
| 61 | { |
| 62 | static const char TEXT[] = "a\n" ; |
| 63 | Write(pText: TEXT); |
| 64 | Expect(pOutput: TEXT); |
| 65 | } |
| 66 | |
| 67 | TEST_F(Async, Long) |
| 68 | { |
| 69 | char aText[BUF_SIZE + 1]; |
| 70 | for(unsigned i = 0; i < sizeof(aText) - 1; i++) |
| 71 | { |
| 72 | aText[i] = 'a'; |
| 73 | } |
| 74 | aText[sizeof(aText) - 1] = 0; |
| 75 | Write(pText: aText); |
| 76 | Expect(pOutput: aText); |
| 77 | } |
| 78 | |
| 79 | TEST_F(Async, Pieces) |
| 80 | { |
| 81 | char aText[BUF_SIZE + 1]; |
| 82 | for(unsigned i = 0; i < sizeof(aText) - 1; i++) |
| 83 | { |
| 84 | aText[i] = 'a'; |
| 85 | } |
| 86 | aText[sizeof(aText) - 1] = 0; |
| 87 | for(unsigned i = 0; i < sizeof(aText) - 1; i++) |
| 88 | { |
| 89 | Write(pText: "a" ); |
| 90 | } |
| 91 | Expect(pOutput: aText); |
| 92 | } |
| 93 | |
| 94 | TEST_F(Async, Mixed) |
| 95 | { |
| 96 | char aText[BUF_SIZE + 1]; |
| 97 | for(unsigned i = 0; i < sizeof(aText) - 1; i++) |
| 98 | { |
| 99 | aText[i] = 'a' + i % 26; |
| 100 | } |
| 101 | aText[sizeof(aText) - 1] = 0; |
| 102 | for(unsigned i = 0; i < sizeof(aText) - 1; i++) |
| 103 | { |
| 104 | char w = 'a' + i % 26; |
| 105 | aio_write(aio: m_pAio, buffer: &w, size: 1); |
| 106 | } |
| 107 | Expect(pOutput: aText); |
| 108 | } |
| 109 | |
| 110 | TEST_F(Async, NonDivisor) |
| 111 | { |
| 112 | static const int NUM_LETTERS = 13; |
| 113 | static const int SIZE = BUF_SIZE / NUM_LETTERS * NUM_LETTERS; |
| 114 | char aText[SIZE + 1]; |
| 115 | for(unsigned i = 0; i < sizeof(aText) - 1; i++) |
| 116 | { |
| 117 | aText[i] = 'a' + i % NUM_LETTERS; |
| 118 | } |
| 119 | aText[sizeof(aText) - 1] = 0; |
| 120 | for(unsigned i = 0; i < (sizeof(aText) - 1) / NUM_LETTERS; i++) |
| 121 | { |
| 122 | Write(pText: "abcdefghijklm" ); |
| 123 | } |
| 124 | Expect(pOutput: aText); |
| 125 | } |
| 126 | |
| 127 | TEST_F(Async, Transaction) |
| 128 | { |
| 129 | static const int NUM_LETTERS = 13; |
| 130 | static const int SIZE = BUF_SIZE / NUM_LETTERS * NUM_LETTERS; |
| 131 | char aText[SIZE + 1]; |
| 132 | for(unsigned i = 0; i < sizeof(aText) - 1; i++) |
| 133 | { |
| 134 | aText[i] = 'a' + i % NUM_LETTERS; |
| 135 | } |
| 136 | aText[sizeof(aText) - 1] = 0; |
| 137 | for(unsigned i = 0; i < (sizeof(aText) - 1) / NUM_LETTERS; i++) |
| 138 | { |
| 139 | aio_lock(aio: m_pAio); |
| 140 | for(char c = 'a'; c < 'a' + NUM_LETTERS; c++) |
| 141 | { |
| 142 | aio_write_unlocked(aio: m_pAio, buffer: &c, size: 1); |
| 143 | } |
| 144 | aio_unlock(aio: m_pAio); |
| 145 | } |
| 146 | Expect(pOutput: aText); |
| 147 | } |
| 148 | |