2019-10-11 21:45:36 +08:00
|
|
|
|
#include "streams/file/ostream_file.h"
|
|
|
|
|
#include "streams/file/istream_file.h"
|
2019-08-27 17:43:56 +08:00
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
|
|
static void test_write(const char* filename) {
|
|
|
|
|
tk_ostream_t* is = tk_ostream_file_create(filename);
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(tk_ostream_write(is, (uint8_t*)"aaaa", 4), 4);
|
|
|
|
|
ASSERT_EQ(tk_ostream_write(is, (uint8_t*)"2222", 4), 4);
|
|
|
|
|
ASSERT_EQ(tk_ostream_write(is, (uint8_t*)"3333", 4), 4);
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(tk_ostream_seek(is, 0), RET_OK);
|
|
|
|
|
ASSERT_EQ(tk_ostream_write(is, (uint8_t*)"1111", 4), 4);
|
|
|
|
|
|
2021-12-02 17:57:04 +08:00
|
|
|
|
tk_object_unref(TK_OBJECT(is));
|
2019-08-27 17:43:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_read(const char* filename) {
|
|
|
|
|
uint8_t buff[4];
|
|
|
|
|
tk_istream_t* is = tk_istream_file_create(filename);
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(tk_istream_read(is, buff, 4), 4);
|
|
|
|
|
ASSERT_EQ(memcmp(buff, "1111", 4), 0);
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(tk_istream_read(is, buff, 4), 4);
|
|
|
|
|
ASSERT_EQ(memcmp(buff, "2222", 4), 0);
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(tk_istream_read(is, buff, 4), 4);
|
|
|
|
|
ASSERT_EQ(memcmp(buff, "3333", 4), 0);
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(tk_istream_read(is, buff, 4), 0);
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(tk_istream_seek(is, 0), RET_OK);
|
|
|
|
|
ASSERT_EQ(tk_istream_read(is, buff, 4), 4);
|
|
|
|
|
ASSERT_EQ(memcmp(buff, "1111", 4), 0);
|
2019-10-16 11:13:04 +08:00
|
|
|
|
|
2019-10-16 11:07:30 +08:00
|
|
|
|
ASSERT_EQ(tk_istream_seek(is, 256), RET_OK);
|
|
|
|
|
ASSERT_EQ(tk_istream_read(is, buff, 4), 0);
|
2021-12-02 17:57:04 +08:00
|
|
|
|
ASSERT_EQ(tk_object_get_prop_bool(TK_OBJECT(is), TK_STREAM_PROP_IS_EOS, FALSE), TRUE);
|
2019-08-27 17:43:56 +08:00
|
|
|
|
|
2021-12-02 17:57:04 +08:00
|
|
|
|
tk_object_unref(TK_OBJECT(is));
|
2019-08-27 17:43:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(StreamFile, basic) {
|
|
|
|
|
const char* filename = "./tests/testdata/istream.txt";
|
|
|
|
|
|
|
|
|
|
test_write(filename);
|
|
|
|
|
test_read(filename);
|
|
|
|
|
}
|