awtk/tests/slist_test.cc

196 lines
4.4 KiB
C++
Raw Normal View History

2019-01-11 12:21:16 +08:00
#include "tkc/utils.h"
#include "tkc/slist.h"
#include "gtest/gtest.h"
#include <string>
#define TO_POINTER(n) (((char*)NULL) + n)
#define TO_INT(p) (((char*)(p)) - ((char*)NULL))
using std::string;
static ret_t visit_dump(void* ctx, const void* data) {
char text[32];
string& str = *(string*)ctx;
int32_t n = (const char*)data - (const char*)NULL;
tk_snprintf(text, sizeof(text), "%d:", n);
str += text;
return RET_OK;
}
TEST(SList, basic) {
slist_t slist;
slist_t* s = &slist;
slist_init(s, NULL, NULL);
ASSERT_EQ(slist_size(s), 0);
ASSERT_EQ(slist_append(s, TO_POINTER(1)), RET_OK);
2020-08-06 12:05:38 +08:00
2019-01-11 12:21:16 +08:00
ASSERT_EQ(slist_size(s), 1);
ASSERT_EQ(slist_append(s, TO_POINTER(2)), RET_OK);
2020-08-06 12:05:38 +08:00
2019-01-11 12:21:16 +08:00
ASSERT_EQ(slist_size(s), 2);
ASSERT_EQ(slist_append(s, TO_POINTER(3)), RET_OK);
2020-08-06 12:05:38 +08:00
2019-01-11 12:21:16 +08:00
ASSERT_EQ(slist_size(s), 3);
ASSERT_EQ(slist_prepend(s, TO_POINTER(4)), RET_OK);
2020-08-06 12:05:38 +08:00
2019-01-11 12:21:16 +08:00
ASSERT_EQ(slist_size(s), 4);
ASSERT_EQ(slist_prepend(s, TO_POINTER(5)), RET_OK);
2020-08-06 12:05:38 +08:00
2019-01-11 12:21:16 +08:00
ASSERT_EQ(slist_size(s), 5);
slist_deinit(s);
}
TEST(SList, find) {
slist_t slist;
slist_t* s = &slist;
slist_init(s, NULL, NULL);
ASSERT_EQ(slist_size(s), 0);
ASSERT_EQ(slist_append(s, TO_POINTER(1)), RET_OK);
2020-08-06 12:05:38 +08:00
2019-01-11 12:21:16 +08:00
ASSERT_EQ(TO_INT(slist_find(s, TO_POINTER(1))), 1);
ASSERT_EQ(slist_append(s, TO_POINTER(2)), RET_OK);
2020-08-06 12:05:38 +08:00
2019-01-11 12:21:16 +08:00
ASSERT_EQ(TO_INT(slist_find(s, TO_POINTER(2))), 2);
2020-08-06 12:05:38 +08:00
slist_deinit(s);
}
TEST(SList, tail_pop) {
slist_t slist;
slist_t* s = &slist;
slist_init(s, NULL, NULL);
ASSERT_EQ(slist_size(s), 0);
ASSERT_EQ(slist_append(s, TO_POINTER(1)), RET_OK);
ASSERT_EQ(slist_append(s, TO_POINTER(2)), RET_OK);
ASSERT_EQ(slist_append(s, TO_POINTER(3)), RET_OK);
ASSERT_EQ(slist_append(s, TO_POINTER(4)), RET_OK);
ASSERT_EQ(slist_append(s, TO_POINTER(5)), RET_OK);
ASSERT_EQ(TO_INT(slist_tail_pop(s)), 5);
ASSERT_EQ(TO_INT(slist_head_pop(s)), 1);
ASSERT_EQ(slist_size(s), 3);
2020-08-20 10:14:39 +08:00
2020-08-06 12:05:38 +08:00
ASSERT_EQ(TO_INT(slist_tail_pop(s)), 4);
ASSERT_EQ(TO_INT(slist_head_pop(s)), 2);
ASSERT_EQ(slist_size(s), 1);
ASSERT_EQ(slist_append(s, TO_POINTER(10)), RET_OK);
ASSERT_EQ(slist_prepend(s, TO_POINTER(0)), RET_OK);
ASSERT_EQ(slist_size(s), 3);
ASSERT_EQ(TO_INT(slist_tail_pop(s)), 10);
ASSERT_EQ(TO_INT(slist_head_pop(s)), 0);
ASSERT_EQ(TO_INT(slist_tail_pop(s)), 3);
ASSERT_EQ(slist_head_pop(s) == NULL, TRUE);
ASSERT_EQ(slist_tail_pop(s) == NULL, TRUE);
2019-01-11 12:21:16 +08:00
slist_deinit(s);
}
TEST(SList, remove) {
slist_t slist;
slist_t* s = &slist;
slist_init(s, NULL, NULL);
ASSERT_EQ(slist_size(s), 0);
ASSERT_EQ(slist_append(s, TO_POINTER(1)), RET_OK);
2020-08-06 12:05:38 +08:00
2019-01-11 12:21:16 +08:00
ASSERT_EQ(TO_INT(slist_find(s, TO_POINTER(1))), 1);
ASSERT_EQ(slist_remove(s, TO_POINTER(1)), RET_OK);
ASSERT_EQ(slist_count(s, TO_POINTER(1)), 0);
ASSERT_EQ(slist_append(s, TO_POINTER(2)), RET_OK);
ASSERT_EQ(TO_INT(slist_find(s, TO_POINTER(2))), 2);
ASSERT_EQ(slist_remove(s, TO_POINTER(2)), RET_OK);
ASSERT_EQ(slist_count(s, TO_POINTER(2)), 0);
slist_deinit(s);
}
TEST(SList, foreach) {
string log;
slist_t slist;
slist_t* s = &slist;
slist_init(s, NULL, NULL);
ASSERT_EQ(slist_size(s), 0);
ASSERT_EQ(slist_append(s, TO_POINTER(1)), RET_OK);
ASSERT_EQ(slist_append(s, TO_POINTER(2)), RET_OK);
log = "";
slist_foreach(s, visit_dump, &log);
ASSERT_EQ(log, "1:2:");
ASSERT_EQ(slist_append(s, TO_POINTER(1)), RET_OK);
ASSERT_EQ(slist_append(s, TO_POINTER(2)), RET_OK);
log = "";
slist_foreach(s, visit_dump, &log);
ASSERT_EQ(log, "1:2:1:2:");
ASSERT_EQ(slist_append(s, TO_POINTER(3)), RET_OK);
ASSERT_EQ(slist_append(s, TO_POINTER(4)), RET_OK);
log = "";
slist_foreach(s, visit_dump, &log);
ASSERT_EQ(log, "1:2:1:2:3:4:");
slist_deinit(s);
}
2021-01-20 18:11:29 +08:00
TEST(SList, insert) {
string log;
slist_t slist;
slist_t* s = &slist;
slist_init(s, NULL, NULL);
ASSERT_EQ(slist_size(s), 0);
ASSERT_EQ(slist_insert(s, 0, TO_POINTER(0)), RET_OK);
ASSERT_EQ(slist_insert(s, 1, TO_POINTER(2)), RET_OK);
ASSERT_EQ(slist_insert(s, 1, TO_POINTER(1)), RET_OK);
2021-02-01 18:12:36 +08:00
log = "";
2021-01-20 18:11:29 +08:00
slist_foreach(s, visit_dump, &log);
ASSERT_EQ(log, "0:1:2:");
ASSERT_EQ(slist_insert(s, 0, TO_POINTER(5)), RET_OK);
2021-02-01 18:12:36 +08:00
log = "";
2021-01-20 18:11:29 +08:00
slist_foreach(s, visit_dump, &log);
ASSERT_EQ(log, "5:0:1:2:");
2021-02-01 18:12:36 +08:00
2021-01-20 18:11:29 +08:00
ASSERT_EQ(slist_insert(s, 3, TO_POINTER(6)), RET_OK);
2021-02-01 18:12:36 +08:00
log = "";
2021-01-20 18:11:29 +08:00
slist_foreach(s, visit_dump, &log);
ASSERT_EQ(log, "5:0:1:6:2:");
2021-02-01 18:12:36 +08:00
2021-01-20 18:11:29 +08:00
ASSERT_EQ(slist_insert(s, 10, TO_POINTER(10)), RET_OK);
2021-02-01 18:12:36 +08:00
log = "";
2021-01-20 18:11:29 +08:00
slist_foreach(s, visit_dump, &log);
ASSERT_EQ(log, "5:0:1:6:2:10:");
slist_deinit(s);
}