awtk/tests/dirty_rects_test.cc

177 lines
4.3 KiB
C++
Raw Normal View History

2021-06-24 08:10:31 +08:00
#include "gtest/gtest.h"
#define TK_MAX_DIRTY_RECT_NR 3
#include "base/dirty_rects.h"
TEST(DirtyRects, basic) {
rect_t r;
dirty_rects_t dr;
dirty_rects_init(&dr);
r = rect_init(0, 0, 5, 5);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects, sizeof(rect_t)), 0);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 22:14:27 +08:00
2021-06-24 08:10:31 +08:00
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects, sizeof(rect_t)), 0);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 22:14:27 +08:00
2021-06-24 08:10:31 +08:00
r = rect_init(3, 3, 5, 5);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 08:10:31 +08:00
r = rect_init(0, 0, 8, 8);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects, sizeof(rect_t)), 0);
2021-06-24 08:10:31 +08:00
r = rect_init(10, 10, 10, 10);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 2u);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects + 1, sizeof(rect_t)), 0);
2021-06-24 08:10:31 +08:00
r = rect_init(10, 10, 10, 10);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 2u);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects + 1, sizeof(rect_t)), 0);
2021-06-24 08:10:31 +08:00
r = rect_init(20, 20, 10, 10);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 3u);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects + 2, sizeof(rect_t)), 0);
2021-06-24 08:10:31 +08:00
r = rect_init(100, 20, 10, 10);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 2u);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects + 1, sizeof(rect_t)), 0);
2021-06-24 08:10:31 +08:00
dirty_rects_reset(&dr);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 0u);
2021-06-24 22:14:27 +08:00
2021-06-24 08:10:31 +08:00
dirty_rects_deinit(&dr);
}
TEST(DirtyRects, add_remove) {
rect_t r;
dirty_rects_t dr;
dirty_rects_init(&dr);
r = rect_init(0, 0, 5, 5);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects, sizeof(rect_t)), 0);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 08:10:31 +08:00
ASSERT_EQ(dirty_rects_remove(&dr, 0), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 0u);
2021-06-24 22:14:27 +08:00
2021-06-24 08:10:31 +08:00
r = rect_init(0, 0, 5, 5);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 08:10:31 +08:00
r = rect_init(10, 0, 5, 5);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 2u);
2021-06-24 22:14:27 +08:00
2021-06-24 08:10:31 +08:00
r = rect_init(10, 10, 5, 5);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 3u);
2021-06-24 08:10:31 +08:00
ASSERT_EQ(dirty_rects_remove(&dr, 1), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 2u);
2021-06-24 22:14:27 +08:00
2021-06-24 08:10:31 +08:00
ASSERT_EQ(dirty_rects_remove(&dr, 1), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 22:14:27 +08:00
2021-06-24 08:10:31 +08:00
ASSERT_EQ(dirty_rects_remove(&dr, 1), RET_BAD_PARAMS);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 22:14:27 +08:00
2021-06-24 08:10:31 +08:00
ASSERT_EQ(dirty_rects_remove(&dr, 0), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 0u);
2021-06-24 08:10:31 +08:00
dirty_rects_deinit(&dr);
}
TEST(DirtyRects, overlap1) {
rect_t r;
dirty_rects_t dr;
dirty_rects_init(&dr);
r = rect_init(0, 0, 20, 20);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects, sizeof(rect_t)), 0);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 08:10:31 +08:00
r = rect_init(25, 0, 5, 5);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects + 1, sizeof(rect_t)), 0);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 2u);
2021-06-24 08:10:31 +08:00
r = rect_init(15, 10, 20, 5);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 22:14:27 +08:00
2021-06-24 08:10:31 +08:00
r = rect_init(0, 0, 35, 20);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects, sizeof(rect_t)), 0);
ASSERT_EQ(memcmp(&r, &(dr.max), sizeof(rect_t)), 0);
2021-06-24 08:10:31 +08:00
}
TEST(DirtyRects, overlap2) {
rect_t r;
dirty_rects_t dr;
dirty_rects_init(&dr);
r = rect_init(0, 0, 20, 20);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects, sizeof(rect_t)), 0);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 08:10:31 +08:00
r = rect_init(50, 0, 5, 5);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects + 1, sizeof(rect_t)), 0);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 2u);
2021-06-24 08:10:31 +08:00
r = rect_init(15, 10, 20, 5);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 2u);
2021-06-24 22:14:27 +08:00
2021-06-24 08:10:31 +08:00
r = rect_init(0, 0, 35, 20);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects, sizeof(rect_t)), 0);
2021-06-24 08:10:31 +08:00
r = rect_init(0, 0, 55, 20);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, &(dr.max), sizeof(rect_t)), 0);
2021-06-24 08:10:31 +08:00
dirty_rects_deinit(&dr);
}
TEST(DirtyRects, overlap3) {
rect_t r;
uint32_t i = 0;
dirty_rects_t dr;
dirty_rects_init(&dr);
2021-06-24 22:14:27 +08:00
for (i = 1; i < 100; i++) {
2021-06-24 08:10:31 +08:00
r = rect_init(0, 0, i, i);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects, sizeof(rect_t)), 0);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 08:10:31 +08:00
}
dirty_rects_deinit(&dr);
}
TEST(DirtyRects, overlap4) {
rect_t r;
int32_t i = 0;
dirty_rects_t dr;
dirty_rects_init(&dr);
2021-06-24 22:14:27 +08:00
for (i = 1000; i > 0; i--) {
2021-06-24 08:10:31 +08:00
r = rect_init(0, 0, i, i);
ASSERT_EQ(dirty_rects_add(&dr, &r), RET_OK);
r = rect_init(0, 0, 1000, 1000);
2021-06-24 22:14:27 +08:00
ASSERT_EQ(memcmp(&r, dr.rects, sizeof(rect_t)), 0);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(dr.nr, 1u);
2021-06-24 08:10:31 +08:00
}
dirty_rects_deinit(&dr);
}