awtk/tests/iostream_noisy_test.cc

80 lines
2.4 KiB
C++
Raw Normal View History

2019-10-15 14:11:15 +08:00
#include "gtest/gtest.h"
#include "streams/mem/iostream_mem.h"
#include "streams/noisy/ostream_noisy.h"
#include "streams/noisy/iostream_noisy.h"
2019-10-15 14:41:10 +08:00
TEST(IOStreamNoisy, noerror) {
2019-10-15 14:11:15 +08:00
uint8_t buff[62];
uint8_t rbuff[16];
const char* str = "12345";
2021-09-16 16:06:55 +08:00
int32_t size = strlen(str) + 1;
2019-10-15 14:11:15 +08:00
tk_iostream_t* mem = tk_iostream_mem_create(buff, sizeof(buff), buff, sizeof(buff), FALSE);
tk_iostream_t* io = tk_iostream_noisy_create(mem);
tk_ostream_t* os = tk_iostream_get_ostream(io);
tk_istream_t* is = tk_iostream_get_istream(io);
tk_object_set_prop_int(TK_OBJECT(os), TK_OSTREAM_NOISY_PROP_ERROR_LEVEL, 0);
2019-10-15 14:11:15 +08:00
ASSERT_EQ(tk_ostream_write(os, (uint8_t*)str, size), size);
2019-10-15 14:41:10 +08:00
ASSERT_EQ(tk_istream_read(is, rbuff, size), size);
2019-10-15 14:11:15 +08:00
ASSERT_STREQ((char*)rbuff, str);
TK_OBJECT_UNREF(io);
TK_OBJECT_UNREF(mem);
2019-10-15 14:11:15 +08:00
}
2019-10-15 14:41:10 +08:00
TEST(IOStreamNoisy, all_error) {
uint8_t buff[62];
uint8_t rbuff[16];
const char* str = "12345";
2021-09-16 16:06:55 +08:00
int32_t size = strlen(str) + 1;
2019-10-15 14:41:10 +08:00
tk_iostream_t* mem = tk_iostream_mem_create(buff, sizeof(buff), buff, sizeof(buff), FALSE);
tk_iostream_t* io = tk_iostream_noisy_create(mem);
tk_ostream_t* os = tk_iostream_get_ostream(io);
tk_istream_t* is = tk_iostream_get_istream(io);
tk_object_set_prop_int(TK_OBJECT(os), TK_OSTREAM_NOISY_PROP_ERROR_LEVEL, 1);
2019-10-15 14:41:10 +08:00
ASSERT_EQ(tk_ostream_write(os, (uint8_t*)str, size), size);
ASSERT_EQ(tk_istream_read(is, rbuff, size), size);
ASSERT_STRNE((char*)rbuff, str);
TK_OBJECT_UNREF(io);
TK_OBJECT_UNREF(mem);
2019-10-15 14:41:10 +08:00
}
static void test_n(tk_ostream_t* os, tk_istream_t* is, uint32_t level) {
uint32_t i = 0;
uint8_t rbuff[16];
const char* str = "12345";
2021-09-16 16:06:55 +08:00
int32_t size = strlen(str) + 1;
tk_object_set_prop_int(TK_OBJECT(os), TK_OSTREAM_NOISY_PROP_ERROR_LEVEL, level);
2019-10-15 14:41:10 +08:00
for (i = 0; i < level; i++) {
ASSERT_EQ(tk_ostream_write(os, (uint8_t*)str, size), size);
ASSERT_EQ(tk_istream_read(is, rbuff, size), size);
if (i % level == 0) {
ASSERT_STRNE((char*)rbuff, str);
} else {
ASSERT_STREQ((char*)rbuff, str);
}
}
}
TEST(IOStreamNoisy, error_n) {
uint32_t i = 0;
uint8_t buff[1024];
for (i = 2; i < 10; i++) {
tk_iostream_t* mem = tk_iostream_mem_create(buff, sizeof(buff), buff, sizeof(buff), FALSE);
tk_iostream_t* io = tk_iostream_noisy_create(mem);
tk_ostream_t* os = tk_iostream_get_ostream(io);
tk_istream_t* is = tk_iostream_get_istream(io);
test_n(os, is, i);
TK_OBJECT_UNREF(io);
TK_OBJECT_UNREF(mem);
2019-10-15 14:41:10 +08:00
}
}