2019-11-24 17:36:46 +00:00
|
|
|
#include <notcurses.h>
|
|
|
|
#include "egcpool.h"
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
class EGCPoolTest : public :: testing::Test {
|
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
2019-11-25 18:36:16 +00:00
|
|
|
setlocale(LC_ALL, nullptr);
|
2019-11-24 17:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override {
|
|
|
|
egcpool_dump(&pool_);
|
|
|
|
}
|
|
|
|
|
|
|
|
egcpool pool_{};
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(EGCPoolTest, Initialized) {
|
|
|
|
EXPECT_EQ(nullptr, pool_.pool);
|
|
|
|
EXPECT_EQ(0, pool_.poolsize);
|
|
|
|
EXPECT_EQ(0, pool_.poolwrite);
|
|
|
|
EXPECT_EQ(0, pool_.poolused);
|
|
|
|
}
|
|
|
|
|
2019-11-25 18:36:16 +00:00
|
|
|
TEST_F(EGCPoolTest, UTF8EGC) {
|
|
|
|
const char* wstr = "☢";
|
|
|
|
int c;
|
2019-11-26 02:11:27 +00:00
|
|
|
auto ulen = utf8_egc_len(wstr, &c);
|
2019-11-25 18:36:16 +00:00
|
|
|
ASSERT_LT(0, ulen);
|
|
|
|
EXPECT_LT(0, c);
|
2019-12-01 17:59:16 +00:00
|
|
|
wstr = "▓";
|
|
|
|
ulen = utf8_egc_len(wstr, &c);
|
|
|
|
ASSERT_LT(0, ulen);
|
|
|
|
EXPECT_LT(0, c);
|
2019-11-25 18:36:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 01:45:44 +00:00
|
|
|
// we're gonna run both a composed latin a with grave, and then a latin a with
|
|
|
|
// a combining nonspacing grave
|
|
|
|
TEST_F(EGCPoolTest, UTF8EGCCombining) {
|
2019-11-27 15:43:03 +00:00
|
|
|
const char* w1 = "\u00e0"; // (utf8: c3 a0)
|
|
|
|
const char* w2 = "\u0061\u0300"; // (utf8: 61 cc 80)
|
|
|
|
const char* w3 = "\u0061"; // (utf8: 61)
|
2019-11-26 02:11:27 +00:00
|
|
|
int c1, c2, c3;
|
|
|
|
auto u1 = utf8_egc_len(w1, &c1);
|
|
|
|
auto u2 = utf8_egc_len(w2, &c2);
|
|
|
|
auto u3 = utf8_egc_len(w3, &c3);
|
2019-11-26 01:45:44 +00:00
|
|
|
ASSERT_EQ(2, u1);
|
|
|
|
ASSERT_EQ(3, u2);
|
2019-11-26 02:11:27 +00:00
|
|
|
ASSERT_EQ(1, u3);
|
2019-11-26 01:45:44 +00:00
|
|
|
ASSERT_EQ(1, c1);
|
|
|
|
ASSERT_EQ(1, c2);
|
2019-11-26 02:11:27 +00:00
|
|
|
ASSERT_EQ(1, c3);
|
2019-11-26 01:45:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 17:36:46 +00:00
|
|
|
TEST_F(EGCPoolTest, AddAndRemove) {
|
2019-11-27 15:43:03 +00:00
|
|
|
const char* wstr = "\ufdfd"; // bismallih
|
|
|
|
int c;
|
|
|
|
auto ulen = utf8_egc_len(wstr, &c);
|
|
|
|
ASSERT_LE(0, egcpool_stash(&pool_, wstr, ulen));
|
|
|
|
ASSERT_EQ(1, c); // not considered wide, believe it or not
|
2019-11-24 17:36:46 +00:00
|
|
|
EXPECT_NE(nullptr, pool_.pool);
|
|
|
|
EXPECT_STREQ(pool_.pool, wstr);
|
|
|
|
EXPECT_LT(0, pool_.poolsize);
|
2019-11-24 19:00:48 +00:00
|
|
|
EXPECT_EQ(ulen + 1, pool_.poolused);
|
2019-11-24 17:36:46 +00:00
|
|
|
EXPECT_LT(0, pool_.poolwrite);
|
|
|
|
EXPECT_LE(pool_.poolused, pool_.poolsize);
|
|
|
|
egcpool_release(&pool_, 0);
|
|
|
|
EXPECT_EQ('\0', *pool_.pool);
|
|
|
|
EXPECT_LT(0, pool_.poolsize);
|
|
|
|
EXPECT_EQ(0, pool_.poolused);
|
|
|
|
EXPECT_LT(0, pool_.poolwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EGCPoolTest, AddTwiceRemoveFirst) {
|
2019-11-27 15:43:03 +00:00
|
|
|
const char* wstr = "\u8840"; // cjk unified ideograph, wide
|
2019-11-25 18:36:16 +00:00
|
|
|
int c1, c2; // column counts
|
2019-11-27 15:43:03 +00:00
|
|
|
auto u1 = utf8_egc_len(wstr, &c1); // bytes consumed
|
|
|
|
auto u2 = utf8_egc_len(wstr, &c2);
|
|
|
|
int o1 = egcpool_stash(&pool_, wstr, u1);
|
|
|
|
int o2 = egcpool_stash(&pool_, wstr, u2);
|
2019-11-25 18:36:16 +00:00
|
|
|
ASSERT_LE(0, o1);
|
2019-11-24 17:36:46 +00:00
|
|
|
ASSERT_LT(o1, o2);
|
2019-11-27 15:43:03 +00:00
|
|
|
ASSERT_EQ(2, c1);
|
2019-11-25 18:36:16 +00:00
|
|
|
ASSERT_EQ(c1, c2);
|
2019-11-24 17:36:46 +00:00
|
|
|
EXPECT_NE(nullptr, pool_.pool);
|
2019-11-24 19:00:48 +00:00
|
|
|
EXPECT_STREQ(pool_.pool + o1, wstr);
|
|
|
|
EXPECT_STREQ(pool_.pool + o2, wstr);
|
2019-11-24 17:36:46 +00:00
|
|
|
EXPECT_LT(0, pool_.poolsize);
|
2019-11-24 19:00:48 +00:00
|
|
|
EXPECT_EQ(u1 + u2 + 2, pool_.poolused);
|
|
|
|
EXPECT_EQ(u1 + u2 + 2, pool_.poolwrite);
|
2019-11-24 17:36:46 +00:00
|
|
|
EXPECT_LE(pool_.poolused, pool_.poolsize);
|
|
|
|
egcpool_release(&pool_, o1);
|
|
|
|
EXPECT_EQ('\0', pool_.pool[o1]);
|
2019-11-24 19:00:48 +00:00
|
|
|
EXPECT_EQ(u2 + 1, pool_.poolused);
|
2019-11-24 17:36:46 +00:00
|
|
|
EXPECT_LT(0, pool_.poolwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EGCPoolTest, AddTwiceRemoveSecond) {
|
2019-11-27 15:43:03 +00:00
|
|
|
const char* wstr = "\u8840"; // cjk unified ideograph, wide
|
2019-11-25 18:36:16 +00:00
|
|
|
int c1, c2; // column counts
|
2019-11-27 15:43:03 +00:00
|
|
|
auto u1 = utf8_egc_len(wstr, &c1); // bytes consumed
|
|
|
|
auto u2 = utf8_egc_len(wstr, &c2);
|
|
|
|
int o1 = egcpool_stash(&pool_, wstr, u1);
|
|
|
|
int o2 = egcpool_stash(&pool_, wstr, u2);
|
2019-11-24 17:36:46 +00:00
|
|
|
ASSERT_LT(o1, o2);
|
2019-11-27 15:43:03 +00:00
|
|
|
ASSERT_EQ(2, c1);
|
2019-11-25 18:36:16 +00:00
|
|
|
ASSERT_EQ(c1, c2);
|
2019-11-24 17:36:46 +00:00
|
|
|
EXPECT_NE(nullptr, pool_.pool);
|
2019-11-24 19:00:48 +00:00
|
|
|
EXPECT_STREQ(pool_.pool + o1, wstr);
|
|
|
|
EXPECT_STREQ(pool_.pool + o2, wstr);
|
2019-11-24 17:36:46 +00:00
|
|
|
EXPECT_LT(0, pool_.poolsize);
|
2019-11-24 19:00:48 +00:00
|
|
|
EXPECT_EQ(u1 + u2 + 2, pool_.poolused);
|
|
|
|
EXPECT_EQ(u1 + u2 + 2, pool_.poolwrite);
|
2019-11-24 17:36:46 +00:00
|
|
|
EXPECT_LE(pool_.poolused, pool_.poolsize);
|
|
|
|
egcpool_release(&pool_, o2);
|
|
|
|
EXPECT_EQ('\0', pool_.pool[o2]);
|
2019-11-24 19:00:48 +00:00
|
|
|
EXPECT_EQ(u2 + 1, pool_.poolused);
|
2019-11-24 17:36:46 +00:00
|
|
|
EXPECT_LT(0, pool_.poolwrite);
|
|
|
|
}
|
2019-11-27 18:33:43 +00:00
|
|
|
|
|
|
|
// POOL_MINIMUM_ALLOC is the minimum size of an egcpool once it goes active.
|
|
|
|
// add EGCs to it past this boundary, and verify that they're all still
|
|
|
|
// accurate.
|
|
|
|
TEST_F(EGCPoolTest, ForceReallocation) {
|
2019-11-27 18:58:23 +00:00
|
|
|
std::vector<int> candidates;
|
2019-11-27 18:33:43 +00:00
|
|
|
char* firstalloc = nullptr;
|
2019-11-27 18:58:23 +00:00
|
|
|
for(auto i = 0u ; i < 1u << 20u ; ++i){
|
2019-11-27 18:33:43 +00:00
|
|
|
char mb[MB_CUR_MAX + 1];
|
|
|
|
wchar_t wcs = i + 0x80;
|
|
|
|
auto r = wctomb(mb, wcs);
|
|
|
|
if(r < 0){
|
2019-11-27 18:58:23 +00:00
|
|
|
candidates.push_back(-1);
|
2019-11-27 18:33:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ASSERT_GE(sizeof(mb), r);
|
|
|
|
mb[r] = '\0';
|
2019-11-27 18:58:23 +00:00
|
|
|
candidates.push_back(egcpool_stash(&pool_, mb, r));
|
2019-11-27 18:33:43 +00:00
|
|
|
ASSERT_GT(1u << 24u, candidates[i]);
|
|
|
|
if(!firstalloc){
|
|
|
|
firstalloc = pool_.pool;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// verify that we moved the pool at least once
|
|
|
|
ASSERT_NE(pool_.pool, firstalloc);
|
2019-11-27 18:58:23 +00:00
|
|
|
for(auto i = 0u ; i < candidates.size() ; ++i){
|
2019-11-27 18:33:43 +00:00
|
|
|
auto stored = pool_.pool + candidates[i];
|
|
|
|
char mb[MB_CUR_MAX + 1];
|
|
|
|
wchar_t wcs = i + 0x80;
|
|
|
|
auto r = wctomb(mb, wcs);
|
|
|
|
if(r < 0){
|
|
|
|
ASSERT_EQ(-1, candidates[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ASSERT_LT(0, r);
|
|
|
|
mb[r] = '\0';
|
|
|
|
EXPECT_STREQ(mb, stored);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// POOL_MINIMUM_ALLOC is the minimum size of an egcpool once it goes active.
|
|
|
|
// add EGCs to it past this boundary, and verify that they're all still
|
|
|
|
// accurate.
|
|
|
|
TEST_F(EGCPoolTest, ForceReallocationWithRemovals) {
|
2019-11-27 18:58:23 +00:00
|
|
|
std::vector<int> candidates;
|
2019-11-27 18:33:43 +00:00
|
|
|
char* curpool = nullptr;
|
2019-11-27 18:58:23 +00:00
|
|
|
for(auto i = 0u ; i < 1u << 20u ; ++i){
|
2019-11-27 18:33:43 +00:00
|
|
|
char mb[MB_CUR_MAX + 1];
|
2019-11-27 18:58:23 +00:00
|
|
|
wchar_t wcs = (i % 0x1000) + 0x80;
|
2019-11-27 18:33:43 +00:00
|
|
|
auto r = wctomb(mb, wcs);
|
|
|
|
if(r < 0){
|
2019-11-27 18:58:23 +00:00
|
|
|
candidates.push_back(-1);
|
2019-11-27 18:33:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ASSERT_GE(sizeof(mb), r);
|
|
|
|
mb[r] = '\0';
|
2019-11-27 18:58:23 +00:00
|
|
|
candidates.push_back(egcpool_stash(&pool_, mb, r));
|
|
|
|
|
2019-11-27 18:33:43 +00:00
|
|
|
ASSERT_GT(1u << 24u, candidates[i]);
|
|
|
|
if(pool_.pool != curpool){
|
|
|
|
// cut through and release a bunch of them
|
|
|
|
if(curpool){
|
2019-11-27 18:58:23 +00:00
|
|
|
for(auto j = 0u ; j < i ; j += 13){
|
2019-11-27 18:33:43 +00:00
|
|
|
if(candidates[j] >= 0){
|
|
|
|
egcpool_release(&pool_, candidates[j]);
|
|
|
|
candidates[j] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curpool = pool_.pool;
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 18:58:23 +00:00
|
|
|
int no = 0;
|
|
|
|
for(auto i = 0u ; i < candidates.size() ; ++i){
|
|
|
|
if(candidates[i] == -1){
|
|
|
|
++no;
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-27 18:33:43 +00:00
|
|
|
auto stored = pool_.pool + candidates[i];
|
|
|
|
char mb[MB_CUR_MAX + 1];
|
2019-11-27 18:58:23 +00:00
|
|
|
wchar_t wcs = (i % 0x1000) + 0x80;
|
2019-11-27 18:33:43 +00:00
|
|
|
auto r = wctomb(mb, wcs);
|
|
|
|
if(r < 0){
|
|
|
|
ASSERT_EQ(-1, candidates[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ASSERT_LT(0, r);
|
|
|
|
mb[r] = '\0';
|
2019-11-27 18:58:23 +00:00
|
|
|
EXPECT_STREQ(mb, stored);
|
2019-11-27 18:33:43 +00:00
|
|
|
}
|
2019-11-27 18:58:23 +00:00
|
|
|
ASSERT_GT(candidates.size() / 13, no);
|
2019-11-27 18:33:43 +00:00
|
|
|
}
|