2018-06-27 13:46:42 +08:00
|
|
|
|
#include "base/glyph_cache.h"
|
2018-06-10 08:24:42 +08:00
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
|
|
TEST(GlyphCache, basic) {
|
|
|
|
|
uint16_t i = 0;
|
|
|
|
|
uint16_t size = 10;
|
|
|
|
|
uint16_t nr = 10000;
|
|
|
|
|
glyph_cache_t cache;
|
|
|
|
|
glyph_t g;
|
2019-01-19 12:06:25 +08:00
|
|
|
|
glyph_cache_t* c = glyph_cache_init(&cache, 64, (tk_destroy_t)glyph_destroy);
|
2018-06-10 08:24:42 +08:00
|
|
|
|
|
2020-06-27 11:00:37 +08:00
|
|
|
|
memset(&g, 0x00, sizeof(g));
|
2018-06-10 17:39:20 +08:00
|
|
|
|
for (i = 0; i < nr; i++) {
|
2018-06-10 08:24:42 +08:00
|
|
|
|
ASSERT_EQ(glyph_cache_lookup(c, i, size, &g) == RET_NOT_FOUND, true);
|
2018-12-27 17:15:22 +08:00
|
|
|
|
ASSERT_EQ(glyph_cache_add(c, i, size, glyph_clone(&g)) == RET_OK, true);
|
2018-06-10 08:24:42 +08:00
|
|
|
|
ASSERT_EQ(glyph_cache_lookup(c, i, size, &g) == RET_OK, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glyph_cache_deinit(c);
|
|
|
|
|
}
|
2020-06-27 11:00:37 +08:00
|
|
|
|
|
|
|
|
|
TEST(GlyphCache, shrink) {
|
|
|
|
|
uint16_t i = 0;
|
|
|
|
|
uint16_t size = 10;
|
|
|
|
|
uint16_t nr = 64;
|
|
|
|
|
glyph_cache_t cache;
|
|
|
|
|
glyph_t g;
|
|
|
|
|
glyph_cache_t* c = glyph_cache_init(&cache, nr, (tk_destroy_t)glyph_destroy);
|
|
|
|
|
|
|
|
|
|
memset(&g, 0x00, sizeof(g));
|
|
|
|
|
for (i = 0; i < nr; i++) {
|
|
|
|
|
ASSERT_EQ(glyph_cache_add(c, i, size, glyph_clone(&g)) == RET_OK, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < nr; i++) {
|
|
|
|
|
glyph_cache_item_t* item = c->items + i;
|
|
|
|
|
item->last_access_time = i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(glyph_cache_shrink(c, nr / 4), RET_OK);
|
2021-09-16 16:06:55 +08:00
|
|
|
|
ASSERT_EQ(c->size, (uint32_t)(nr / 4));
|
|
|
|
|
ASSERT_EQ(c->items[0].last_access_time, (uint64_t)(nr - 1));
|
|
|
|
|
ASSERT_EQ(c->items[1].last_access_time, (uint64_t)(nr - 2));
|
|
|
|
|
ASSERT_EQ(c->items[2].last_access_time, (uint64_t)(nr - 3));
|
2020-06-27 11:00:37 +08:00
|
|
|
|
|
|
|
|
|
ASSERT_EQ(glyph_cache_shrink(c, nr / 8), RET_OK);
|
2021-09-16 16:06:55 +08:00
|
|
|
|
ASSERT_EQ(c->size, (uint32_t)(nr / 8));
|
|
|
|
|
ASSERT_EQ(c->items[0].last_access_time, (uint64_t)(nr - 1));
|
2020-06-27 11:00:37 +08:00
|
|
|
|
|
|
|
|
|
ASSERT_EQ(glyph_cache_shrink(c, nr / 4), RET_OK);
|
2021-09-16 16:06:55 +08:00
|
|
|
|
ASSERT_EQ(c->size, (uint32_t)(nr / 8));
|
2020-06-27 11:00:37 +08:00
|
|
|
|
|
|
|
|
|
ASSERT_EQ(glyph_cache_shrink(c, 0), RET_OK);
|
2021-09-16 16:06:55 +08:00
|
|
|
|
ASSERT_EQ(c->size, 0u);
|
2020-06-27 11:00:37 +08:00
|
|
|
|
|
|
|
|
|
glyph_cache_deinit(c);
|
|
|
|
|
}
|