awtk/tests/istream_mem_test.cc

127 lines
3.7 KiB
C++
Raw Normal View History

2019-10-11 21:45:36 +08:00
#include "streams/mem/istream_mem.h"
2019-08-27 17:43:56 +08:00
#include "gtest/gtest.h"
TEST(IStreamMem, basic) {
uint8_t buff[4];
const char* str = "111122223333";
tk_istream_t* is = tk_istream_mem_create((uint8_t*)str, strlen(str), 0, FALSE);
ASSERT_EQ(tk_object_get_prop_bool(TK_OBJECT(is), TK_STREAM_PROP_IS_EOS, TRUE), FALSE);
2019-10-16 11:07:30 +08:00
2019-08-27 17:43:56 +08:00
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_object_get_prop_bool(TK_OBJECT(is), TK_STREAM_PROP_IS_EOS, FALSE), TRUE);
2019-08-27 17:43:56 +08:00
tk_object_unref(TK_OBJECT(is));
2019-08-27 17:43:56 +08:00
}
TEST(IStreamMem, packet_size) {
uint8_t buff[12];
const char* str = "111122223333";
tk_istream_t* is = tk_istream_mem_create((uint8_t*)str, strlen(str), 4, FALSE);
ASSERT_EQ(tk_istream_read(is, buff, 12), 4);
ASSERT_EQ(memcmp(buff, "1111", 4), 0);
ASSERT_EQ(tk_istream_read(is, buff, 10), 4);
ASSERT_EQ(memcmp(buff, "2222", 4), 0);
ASSERT_EQ(tk_istream_read(is, buff, 8), 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, 12), 4);
ASSERT_EQ(memcmp(buff, "1111", 4), 0);
tk_object_unref(TK_OBJECT(is));
2019-08-27 17:43:56 +08:00
}
TEST(IStreamMem, read_len) {
uint8_t buff[12];
const char* str = "111122223333";
tk_istream_t* is = tk_istream_mem_create((uint8_t*)str, strlen(str), 4, FALSE);
2019-09-05 16:09:38 +08:00
ASSERT_EQ(tk_istream_read_len(is, buff, 12, 3000), 12);
2019-08-27 17:43:56 +08:00
ASSERT_EQ(memcmp(buff, str, 12), 0);
tk_object_unref(TK_OBJECT(is));
2019-08-27 17:43:56 +08:00
}
2020-09-09 11:12:19 +08:00
TEST(IStreamMem, seek) {
uint8_t buff[12];
const char* str = "111122223333";
tk_istream_t* is = tk_istream_mem_create((uint8_t*)str, strlen(str), 4, FALSE);
ASSERT_EQ(tk_istream_eos(is), FALSE);
ASSERT_EQ(tk_istream_read_len(is, buff, 12, 3000), 12);
ASSERT_EQ(memcmp(buff, str, 12), 0);
ASSERT_EQ(tk_istream_eos(is), TRUE);
ASSERT_EQ(tk_istream_tell(is), 12);
ASSERT_EQ(tk_istream_seek(is, 0), RET_OK);
ASSERT_EQ(tk_istream_tell(is), 0);
ASSERT_EQ(tk_istream_read_len(is, buff, 12, 3000), 12);
ASSERT_EQ(memcmp(buff, str, 12), 0);
ASSERT_EQ(tk_istream_eos(is), TRUE);
ASSERT_EQ(tk_istream_tell(is), 12);
tk_object_unref(TK_OBJECT(is));
2020-09-09 11:12:19 +08:00
}
2022-04-19 07:37:00 +08:00
TEST(IStreamMem, read_line_str) {
2020-09-09 11:12:19 +08:00
str_t str;
const char* content = "1111\n2222\r\n\r\n3333\n";
tk_istream_t* is = tk_istream_mem_create((uint8_t*)content, strlen(content), 4, FALSE);
str_init(&str, 1024);
ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK);
ASSERT_STREQ(str.str, "1111");
2020-09-09 11:18:41 +08:00
2020-09-09 11:12:19 +08:00
ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK);
ASSERT_STREQ(str.str, "2222");
2020-09-09 11:18:41 +08:00
2020-09-09 11:12:19 +08:00
ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK);
ASSERT_STREQ(str.str, "");
2020-09-09 11:18:41 +08:00
2020-09-09 11:12:19 +08:00
ASSERT_EQ(tk_istream_read_line_str(is, &str), RET_OK);
ASSERT_STREQ(str.str, "3333");
2020-09-09 11:18:41 +08:00
2020-09-09 11:12:19 +08:00
ASSERT_NE(tk_istream_read_line_str(is, &str), RET_OK);
2020-10-18 12:17:55 +08:00
str_reset(&str);
tk_object_unref(TK_OBJECT(is));
2020-09-09 11:12:19 +08:00
}
2022-04-19 07:37:00 +08:00
TEST(IStreamMem, read_line) {
char str[128];
const char* content = "1111\n2222\r\n\r\n3333\n";
tk_istream_t* is = tk_istream_mem_create((uint8_t*)content, strlen(content), 4, FALSE);
uint32_t timeout = 100000000;
ASSERT_EQ(tk_istream_read_line(is, str, sizeof(str) - 1, timeout), 5);
ASSERT_STREQ(str, "1111\n");
ASSERT_EQ(tk_istream_read_line(is, str, sizeof(str) - 1, timeout), 6);
ASSERT_STREQ(str, "2222\r\n");
ASSERT_EQ(tk_istream_read_line(is, str, sizeof(str) - 1, timeout), 2);
ASSERT_STREQ(str, "\r\n");
ASSERT_EQ(tk_istream_read_line(is, str, sizeof(str) - 1, timeout), 5);
ASSERT_STREQ(str, "3333\n");
ASSERT_EQ(tk_istream_read_line(is, str, sizeof(str) - 1, timeout), 0);
tk_object_unref(TK_OBJECT(is));
}