2017-10-22 22:13:53 +00:00
|
|
|
#include "test.h"
|
2017-10-10 01:33:54 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <base/system.h>
|
|
|
|
|
2017-10-13 00:48:42 +00:00
|
|
|
static const int BUF_SIZE = 64 * 1024;
|
|
|
|
|
2017-10-10 01:33:54 +00:00
|
|
|
class Async : public ::testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
ASYNCIO *m_pAio;
|
2017-10-22 22:13:53 +00:00
|
|
|
CTestInfo m_Info;
|
2017-10-10 01:33:54 +00:00
|
|
|
bool Delete;
|
|
|
|
|
2017-10-20 16:46:31 +00:00
|
|
|
void SetUp()
|
2017-10-10 01:33:54 +00:00
|
|
|
{
|
2017-10-22 22:13:53 +00:00
|
|
|
IOHANDLE File = io_open(m_Info.m_aFilename, IOFLAG_WRITE);
|
2017-10-20 16:46:31 +00:00
|
|
|
ASSERT_TRUE(File);
|
|
|
|
m_pAio = aio_new(File);
|
2017-10-10 01:33:54 +00:00
|
|
|
Delete = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Async()
|
|
|
|
{
|
|
|
|
if(Delete)
|
|
|
|
{
|
2017-10-22 22:13:53 +00:00
|
|
|
fs_remove(m_Info.m_aFilename);
|
2017-10-10 01:33:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(const char *pText)
|
|
|
|
{
|
2017-10-13 00:48:42 +00:00
|
|
|
aio_write(m_pAio, pText, str_length(pText));
|
2017-10-10 01:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Expect(const char *pOutput)
|
|
|
|
{
|
2017-10-13 00:48:42 +00:00
|
|
|
aio_close(m_pAio);
|
|
|
|
aio_wait(m_pAio);
|
|
|
|
aio_free(m_pAio);
|
2017-10-10 01:33:54 +00:00
|
|
|
|
2017-10-13 00:48:42 +00:00
|
|
|
char aBuf[BUF_SIZE];
|
2017-10-22 22:13:53 +00:00
|
|
|
IOHANDLE File = io_open(m_Info.m_aFilename, IOFLAG_READ);
|
2017-10-20 16:46:31 +00:00
|
|
|
ASSERT_TRUE(File);
|
2017-10-10 01:33:54 +00:00
|
|
|
int Read = io_read(File, aBuf, sizeof(aBuf));
|
2017-10-20 16:46:31 +00:00
|
|
|
io_close(File);
|
2017-10-10 01:33:54 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(str_length(pOutput), Read);
|
|
|
|
ASSERT_TRUE(mem_comp(aBuf, pOutput, Read) == 0);
|
|
|
|
Delete = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(Async, Empty)
|
|
|
|
{
|
|
|
|
Expect("");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Async, Simple)
|
|
|
|
{
|
|
|
|
static const char TEXT[] = "a\n";
|
|
|
|
Write(TEXT);
|
|
|
|
Expect(TEXT);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Async, Long)
|
|
|
|
{
|
2017-10-13 00:48:42 +00:00
|
|
|
char aText[BUF_SIZE + 1];
|
2017-10-10 01:33:54 +00:00
|
|
|
for(unsigned i = 0; i < sizeof(aText) - 1; i++)
|
|
|
|
{
|
|
|
|
aText[i] = 'a';
|
|
|
|
}
|
|
|
|
aText[sizeof(aText) - 1] = 0;
|
|
|
|
Write(aText);
|
|
|
|
Expect(aText);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Async, Pieces)
|
|
|
|
{
|
2017-10-13 00:48:42 +00:00
|
|
|
char aText[BUF_SIZE + 1];
|
2017-10-10 01:33:54 +00:00
|
|
|
for(unsigned i = 0; i < sizeof(aText) - 1; i++)
|
|
|
|
{
|
|
|
|
aText[i] = 'a';
|
|
|
|
}
|
|
|
|
aText[sizeof(aText) - 1] = 0;
|
|
|
|
for(unsigned i = 0; i < sizeof(aText) - 1; i++)
|
|
|
|
{
|
|
|
|
Write("a");
|
|
|
|
}
|
|
|
|
Expect(aText);
|
|
|
|
}
|
2017-10-11 22:58:47 +00:00
|
|
|
|
|
|
|
TEST_F(Async, Mixed)
|
|
|
|
{
|
2017-10-13 00:48:42 +00:00
|
|
|
char aText[BUF_SIZE + 1];
|
2017-10-11 22:58:47 +00:00
|
|
|
for(unsigned i = 0; i < sizeof(aText) - 1; i++)
|
|
|
|
{
|
|
|
|
aText[i] = 'a' + i % 26;
|
|
|
|
}
|
|
|
|
aText[sizeof(aText) - 1] = 0;
|
|
|
|
for(unsigned i = 0; i < sizeof(aText) - 1; i++)
|
|
|
|
{
|
|
|
|
char w = 'a' + i % 26;
|
2017-10-13 00:48:42 +00:00
|
|
|
aio_write(m_pAio, &w, 1);
|
|
|
|
}
|
|
|
|
Expect(aText);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Async, NonDivisor)
|
|
|
|
{
|
|
|
|
static const int NUM_LETTERS = 13;
|
|
|
|
static const int SIZE = BUF_SIZE / NUM_LETTERS * NUM_LETTERS;
|
|
|
|
char aText[SIZE + 1];
|
|
|
|
for(unsigned i = 0; i < sizeof(aText) - 1; i++)
|
|
|
|
{
|
|
|
|
aText[i] = 'a' + i % NUM_LETTERS;
|
|
|
|
}
|
|
|
|
aText[sizeof(aText) - 1] = 0;
|
|
|
|
for(unsigned i = 0; i < (sizeof(aText) - 1) / NUM_LETTERS; i++)
|
|
|
|
{
|
|
|
|
Write("abcdefghijklm");
|
2017-10-11 22:58:47 +00:00
|
|
|
}
|
|
|
|
Expect(aText);
|
|
|
|
}
|