awtk/tests/timer_test.cc

226 lines
5.4 KiB
C++
Raw Normal View History

2018-06-27 13:46:42 +08:00
#include <string>
#include "tkc/utils.h"
2018-02-22 09:04:05 +08:00
#include "base/timer.h"
#include "gtest/gtest.h"
using std::string;
static string s_log;
2018-05-16 10:32:51 +08:00
static uint32_t s_now = 0;
2018-06-03 10:19:34 +08:00
static uint32_t timer_get_time() {
return s_now;
}
2018-05-16 10:32:51 +08:00
2018-06-03 10:19:34 +08:00
static void timer_set_time(uint32_t now) {
s_now = now;
}
2018-05-16 10:32:51 +08:00
2018-06-03 10:19:34 +08:00
static void timer_clear_log(void) {
s_log = "";
}
2018-05-16 10:32:51 +08:00
2018-03-06 21:56:52 +08:00
static ret_t timer_once(const timer_info_t* timer) {
2018-02-22 09:04:05 +08:00
s_log += "o:";
if (timer->user_changed_time) {
s_log += "[uct]";
}
2018-02-22 09:04:05 +08:00
return RET_OK;
}
2018-03-06 21:56:52 +08:00
static ret_t timer_repeat(const timer_info_t* timer) {
2018-02-22 09:04:05 +08:00
s_log += "r:";
return RET_REPEAT;
}
2018-05-16 10:32:51 +08:00
static ret_t timer_remove_in_timer(const timer_info_t* timer) {
s_log += "rm:";
uint32_t id = (char*)(timer->ctx) - (char*)NULL;
timer_manager_remove(timer->timer_manager, id);
return RET_REPEAT;
}
static ret_t timer_add_in_timer(const timer_info_t* timer) {
s_log += "a:";
timer_manager_add(timer->timer_manager, timer_repeat, NULL, 0);
timer_manager_add(timer->timer_manager, timer_repeat, NULL, 100);
return RET_REPEAT;
}
static string repeat_str(const string& substr, uint32_t nr) {
string str;
2018-05-17 11:14:41 +08:00
while (nr > 0) {
2018-05-16 10:32:51 +08:00
str += substr;
nr--;
}
return str;
}
2018-11-04 11:10:40 +08:00
#define NR 100
2018-05-16 10:32:51 +08:00
2018-02-22 09:04:05 +08:00
TEST(Timer, once) {
uint32_t i = 0;
2018-05-16 10:32:51 +08:00
uint32_t ids[NR];
timer_set_time(0);
2018-05-16 10:32:51 +08:00
timer_manager_t* tm = timer_manager_create(timer_get_time);
2018-02-22 09:04:05 +08:00
2018-05-16 10:32:51 +08:00
for (i = 0; i < NR; i++) {
2018-05-17 11:14:41 +08:00
ids[i] = timer_manager_add(tm, timer_once, NULL, i + 1);
2018-05-16 14:02:23 +08:00
ASSERT_EQ(timer_manager_next_time(tm), 1);
2018-05-16 10:32:51 +08:00
ASSERT_EQ(ids[i] > 0, true);
ASSERT_EQ(timer_manager_find(tm, ids[i])->id, ids[i]);
ASSERT_EQ(timer_manager_count(tm), i + 1);
2018-02-22 09:04:05 +08:00
}
2018-05-16 10:32:51 +08:00
timer_clear_log();
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(timer_manager_count(tm), NR);
2018-02-22 09:04:05 +08:00
ASSERT_EQ(s_log, "");
2018-05-16 10:32:51 +08:00
timer_set_time(100);
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(timer_manager_count(tm), 0);
ASSERT_EQ(s_log, repeat_str("o:", NR));
2018-02-22 09:04:05 +08:00
2018-05-16 10:32:51 +08:00
timer_manager_destroy(tm);
2018-02-22 09:04:05 +08:00
}
2019-02-20 14:59:49 +08:00
TEST(Timer, reset) {
timer_manager_t* tm = timer_manager_create(timer_get_time);
uint32_t id = timer_manager_add(tm, timer_once, NULL, 100);
timer_set_time(100);
timer_reset(id);
timer_clear_log();
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(timer_manager_count(tm), 1);
ASSERT_EQ(s_log, "");
2019-02-21 11:08:36 +08:00
timer_set_time(200);
2019-02-20 14:59:49 +08:00
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(timer_manager_count(tm), 0);
ASSERT_EQ(s_log, "o:");
timer_manager_destroy(tm);
}
TEST(Timer, modify) {
timer_manager_t* tm = timer_manager_create(timer_get_time);
uint32_t id = timer_manager_add(tm, timer_once, NULL, 100);
timer_set_time(200);
timer_modify(id, 200);
timer_clear_log();
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(timer_manager_count(tm), 1);
ASSERT_EQ(s_log, "");
timer_set_time(400);
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(timer_manager_count(tm), 0);
ASSERT_EQ(s_log, "o:");
timer_manager_destroy(tm);
}
2018-02-22 09:04:05 +08:00
TEST(Timer, repeat) {
uint32_t i = 0;
2018-05-16 10:32:51 +08:00
uint32_t ids[NR];
timer_set_time(0);
2018-05-16 10:32:51 +08:00
timer_manager_t* tm = timer_manager_create(timer_get_time);
2018-02-22 09:04:05 +08:00
2018-05-16 10:32:51 +08:00
for (i = 0; i < NR; i++) {
2018-05-17 11:14:41 +08:00
ids[i] = timer_manager_add(tm, timer_repeat, NULL, i + 1);
2018-05-16 14:02:23 +08:00
ASSERT_EQ(timer_manager_next_time(tm), 1);
2018-05-16 10:32:51 +08:00
ASSERT_EQ(timer_manager_find(tm, ids[i])->id, ids[i]);
2018-03-06 21:56:52 +08:00
ASSERT_EQ(ids[i] > 0, true);
2018-05-16 10:32:51 +08:00
ASSERT_EQ(timer_manager_count(tm), i + 1);
2018-02-22 09:04:05 +08:00
}
2018-05-17 11:14:41 +08:00
2018-05-16 10:32:51 +08:00
timer_clear_log();
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(timer_manager_count(tm), NR);
2018-02-22 09:04:05 +08:00
ASSERT_EQ(s_log, "");
2018-05-16 10:32:51 +08:00
timer_clear_log();
timer_set_time(100);
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(timer_manager_count(tm), NR);
ASSERT_EQ(s_log, repeat_str("r:", NR));
2018-02-22 09:04:05 +08:00
2018-05-16 10:32:51 +08:00
for (i = 0; i < NR; i++) {
2018-03-06 21:56:52 +08:00
uint32_t id = ids[i];
2018-05-16 10:32:51 +08:00
ASSERT_EQ(timer_manager_remove(tm, id), RET_OK);
ASSERT_EQ(timer_manager_count(tm), NR - i - 1);
2018-02-22 09:04:05 +08:00
}
2018-05-16 10:32:51 +08:00
ASSERT_EQ(timer_manager_count(tm), 0);
timer_manager_destroy(tm);
2018-02-22 09:04:05 +08:00
}
2018-05-16 10:32:51 +08:00
TEST(Timer, removeInTimer) {
timer_set_time(0);
2018-05-16 10:32:51 +08:00
timer_manager_t* tm = timer_manager_create(timer_get_time);
uint32_t id1 = timer_manager_add(tm, timer_repeat, NULL, 200);
2018-05-17 11:14:41 +08:00
timer_manager_add(tm, timer_remove_in_timer, (char*)NULL + id1, 100);
2018-05-16 10:32:51 +08:00
timer_clear_log();
timer_set_time(100);
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(timer_manager_count(tm), 1);
ASSERT_EQ(s_log, "rm:");
timer_manager_destroy(tm);
}
TEST(Timer, addInTimer) {
timer_set_time(0);
2018-05-16 10:32:51 +08:00
timer_manager_t* tm = timer_manager_create(timer_get_time);
timer_manager_add(tm, timer_add_in_timer, NULL, 100);
2018-05-16 14:02:23 +08:00
ASSERT_EQ(timer_manager_next_time(tm), 100);
2018-05-16 10:32:51 +08:00
timer_clear_log();
timer_set_time(100);
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(timer_manager_count(tm), 3);
ASSERT_EQ(s_log, "a:r:");
2018-05-17 11:14:41 +08:00
2018-05-16 10:32:51 +08:00
timer_manager_destroy(tm);
}
TEST(Timer, UserChangedTime) {
timer_set_time(100 * 1000);
timer_manager_t* tm = timer_manager_create(timer_get_time);
timer_manager_add(tm, timer_once, NULL, 100);
ASSERT_EQ(tm->last_dispatch_time, 100 * 1000);
ASSERT_EQ(timer_manager_next_time(tm), tm->last_dispatch_time + 100);
timer_clear_log();
timer_set_time(0);
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(tm->last_dispatch_time, 0);
ASSERT_EQ(s_log, "");
timer_clear_log();
2019-01-12 08:29:38 +08:00
timer_set_time(10000);
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
ASSERT_EQ(tm->last_dispatch_time, 10000);
timer_clear_log();
timer_set_time(10200);
ASSERT_EQ(timer_manager_dispatch(tm), RET_OK);
2019-01-12 08:29:38 +08:00
ASSERT_EQ(tm->last_dispatch_time, 10200);
ASSERT_EQ(s_log, "o:[uct]");
timer_manager_destroy(tm);
}