85 lines
1.7 KiB
C
85 lines
1.7 KiB
C
#include "test.h"
|
|
|
|
void
|
|
str_init_utf8(struct ct *t)
|
|
{
|
|
char full[Maxrunes+2];
|
|
char incomplete[] = { (char)0xea, (char)0xb0 };
|
|
Str s;
|
|
|
|
s = mkstr("A한😀");
|
|
CT_EQ_INT(t, 3, s.n);
|
|
checkstr(t, "round trip", "A한😀", &s);
|
|
sinit(&s, incomplete, sizeof incomplete);
|
|
CT_EQ_INT(t, 0, s.n);
|
|
memset(full, 'a', sizeof full);
|
|
full[sizeof full-1] = '\0';
|
|
sinit(&s, full, strlen(full));
|
|
CT_EQ_INT(t, Maxrunes, s.n);
|
|
}
|
|
|
|
void
|
|
str_edit_and_alias(struct ct *t)
|
|
{
|
|
Str s;
|
|
int i;
|
|
|
|
s = mkstr("ab");
|
|
sappend(&s, &s);
|
|
checkstr(t, "self append", "abab", &s);
|
|
for(i = 0; i < 40; i++)
|
|
s.r[i] = 'a' + i % 26;
|
|
s.n = 40;
|
|
sappend(&s, &s);
|
|
CT_EQ_INT(t, Maxrunes, s.n);
|
|
for(i = 0; i < 24; i++)
|
|
if(s.r[40+i] != s.r[i])
|
|
CT_ERRORF(t, "capped self append differs at rune %d", i);
|
|
}
|
|
|
|
void
|
|
str_utf8_capacity(struct ct *t)
|
|
{
|
|
static const struct {
|
|
int size;
|
|
int n;
|
|
char *want;
|
|
} cases[] = {
|
|
{ 0, 0, nil },
|
|
{ 1, 0, "" },
|
|
{ 2, 1, "a" },
|
|
{ 3, 1, "a" },
|
|
{ 4, 1, "a" },
|
|
{ 5, 4, "a한" },
|
|
{ 6, 5, "a한b" },
|
|
};
|
|
char buf[16];
|
|
Str s;
|
|
int i, n;
|
|
|
|
s = mkstr("a한b");
|
|
for(i = 0; i < nelem(cases); i++){
|
|
memset(buf, 'Z', sizeof buf);
|
|
n = stoutf(&s, buf, cases[i].size);
|
|
if(n != cases[i].n)
|
|
CT_ERRORF(t, "size %d: want length %d, got %d",
|
|
cases[i].size, cases[i].n, n);
|
|
if(cases[i].size == 0){
|
|
CT_EQ_INT(t, 'Z', buf[0]);
|
|
continue;
|
|
}
|
|
if(strcmp(cases[i].want, buf) != 0)
|
|
CT_ERRORF(t, "size %d: want \"%s\", got \"%s\"",
|
|
cases[i].size, cases[i].want, buf);
|
|
CT_EQ_INT(t, 'Z', buf[cases[i].size]);
|
|
}
|
|
|
|
s = mkstr("😀");
|
|
memset(buf, 'Z', sizeof buf);
|
|
CT_EQ_INT(t, 0, stoutf(&s, buf, 4));
|
|
CT_EQ_STR(t, "", buf);
|
|
CT_EQ_INT(t, 4, stoutf(&s, buf, 5));
|
|
CT_EQ_STR(t, "😀", buf);
|
|
CT_EQ_INT(t, 'Z', buf[5]);
|
|
}
|