Make improvements

This commit is contained in:
Justine Tunney 2020-12-01 03:43:40 -08:00
parent 3e4fd4b0ad
commit e44a0cf6f8
256 changed files with 23100 additions and 2294 deletions

View file

@ -24,7 +24,7 @@
char16_t p[PATH_MAX];
TEST(mkntpath, testEmpty) {
EXPECT_EQ(0, mkntpath("", p));
EXPECT_EQ(0, __mkntpath("", p));
EXPECT_STREQ(u"", p);
}
@ -35,11 +35,11 @@ TEST(mkntpath, testSlashes) {
* all it takes to make the feature entirely useless to us, similar to
* the law of noncontradiction. We address the issue as follows:
*/
EXPECT_EQ(9, mkntpath("o/foo.com", p));
EXPECT_EQ(9, __mkntpath("o/foo.com", p));
EXPECT_STREQ(u"o\\foo.com", p);
}
TEST(mkntpath, testUnicode) {
EXPECT_EQ(20, mkntpath("C:\\𐌰𐌱𐌲𐌳\\𐌴𐌵𐌶𐌷", p));
EXPECT_EQ(20, __mkntpath("C:\\𐌰𐌱𐌲𐌳\\𐌴𐌵𐌶𐌷", p));
EXPECT_STREQ(u"C:\\𐌰𐌱𐌲𐌳\\𐌴𐌵𐌶𐌷", p);
}

View file

@ -19,10 +19,12 @@
*/
#include "libc/bits/bits.h"
#include "libc/conv/conv.h"
#include "libc/mem/mem.h"
#include "libc/testlib/testlib.h"
TEST(basename, test) {
EXPECT_STREQ("", basename(""));
EXPECT_STREQ("/", basename("/"));
EXPECT_STREQ("hello", basename("hello"));
EXPECT_STREQ("there", basename("hello/there"));
EXPECT_STREQ("yo", basename("hello/there/yo"));
@ -42,3 +44,13 @@ TEST(basename, testWindows_isGrantedRespect) {
EXPECT_STREQ("there", basename("hello\\there"));
EXPECT_STREQ("yo", basename("hello\\there\\yo"));
}
TEST(dirname, test) {
EXPECT_STREQ("/usr", dirname(strdup("/usr/lib")));
EXPECT_STREQ("usr", dirname(strdup("usr/lib")));
EXPECT_STREQ("/", dirname(strdup("/usr/")));
EXPECT_STREQ("/", dirname(strdup("/")));
EXPECT_STREQ(".", dirname(strdup("hello")));
EXPECT_STREQ(".", dirname(strdup(".")));
EXPECT_STREQ(".", dirname(strdup("..")));
}

View file

@ -27,6 +27,7 @@ TEST_LIBC_CONV_DIRECTDEPS = \
LIBC_CONV \
LIBC_FMT \
LIBC_LOG \
LIBC_MEM \
LIBC_STDIO \
LIBC_NEXGEN32E \
LIBC_STUBS \

View file

@ -44,6 +44,8 @@
#include "libc/intrin/pcmpgtb.h"
#include "libc/intrin/pcmpgtd.h"
#include "libc/intrin/pcmpgtw.h"
#include "libc/intrin/pdep.h"
#include "libc/intrin/pext.h"
#include "libc/intrin/phaddd.h"
#include "libc/intrin/phaddsw.h"
#include "libc/intrin/phaddw.h"
@ -101,7 +103,7 @@
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/nexgen32e/kcpuids.h"
#include "libc/rand/lcg.h"
#include "libc/rand/lcg.internal.h"
#include "libc/rand/rand.h"
#include "libc/runtime/gc.h"
#include "libc/stdio/stdio.h"
@ -2062,6 +2064,26 @@ TEST(pcmpeqw, test2) {
}
}
TEST(pdep, fuzz) {
int i;
uint64_t x, y;
for (i = 0; i < 1000; ++i) {
x = rand64();
y = rand64();
ASSERT_EQ(pdep(x, y), (pdep)(x, y));
}
}
TEST(pext, fuzz) {
int i;
uint64_t x, y;
for (i = 0; i < 1000; ++i) {
x = rand64();
y = rand64();
ASSERT_EQ(pext(x, y), (pext)(x, y));
}
}
BENCH(psrldq, bench) {
volatile uint8_t A[16];
volatile uint8_t B[16];

View file

@ -30,6 +30,7 @@ TEST_LIBC_INTRIN_DIRECTDEPS = \
LIBC_RAND \
LIBC_LOG \
LIBC_STUBS \
LIBC_STR \
LIBC_TESTLIB \
LIBC_TINYMATH \
LIBC_RUNTIME \

View file

@ -20,7 +20,7 @@
#include "libc/macros.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/str/knuthmultiplicativehash.h"
#include "libc/str/knuthmultiplicativehash.internal.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/runtime/getdosenviron.internal.h"
#include "libc/runtime/internal.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
@ -98,6 +98,6 @@ TEST(GetDosEnviron, testEmpty_zeroTerminatesWheneverPossible_3) {
size_t size = 2;
char *block = tmalloc(size);
EXPECT_EQ(0, GetDosEnviron(u"", block, size, NULL, 0));
EXPECT_BINEQ(u"  ", block);
EXPECT_BINEQ(u" ", block);
tfree(block);
}

View file

@ -17,9 +17,17 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/stdio/stdio.h"
#include "libc/calls/calls.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/testlib.h"
TEST(system, nullParam_testsIfSystemHasShell) { ASSERT_EQ(true, system(NULL)); }
TEST(system, test) { ASSERT_EQ(42, WEXITSTATUS(system("exit 42"))); }
TEST(system, nullParam_testsIfSystemHasShell) {
ASSERT_EQ(true, system(NULL));
}
TEST(system, test) {
int rc;
rc = system("exit 42");
ASSERT_NE(-1, rc);
ASSERT_EQ(42, WEXITSTATUS(rc));
}

View file

@ -21,7 +21,6 @@
#include "libc/dce.h"
#include "libc/macros.h"
#include "libc/nexgen32e/cachesize.h"
#include "libc/nexgen32e/tinystrcmp.internal.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/rand/rand.h"
#include "libc/stdio/stdio.h"
@ -29,35 +28,15 @@
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
int memcmp$sse2(const void *, const void *, size_t) hidden;
int memcmp$avx2(const void *, const void *, size_t) hidden;
int (*memcmpi)(const void *, const void *, size_t) = memcmp$sse2;
int strcmp$k8(const char *, const char *) hidden;
int strcmp$avx(const char *, const char *) hidden;
int (*strcmpi)(const char *, const char *) = tinystrcmp;
int strncmp$k8(const char *, const char *, size_t) hidden;
int strncmp$avx(const char *, const char *, size_t) hidden;
int (*strncmpi)(const char *, const char *, size_t) = tinystrncmp;
FIXTURE(strcmp, avx) {
if (X86_HAVE(AVX)) {
strcmpi = strcmp$avx;
strncmpi = strncmp$avx;
}
if (X86_HAVE(AVX2)) {
memcmpi = memcmp$avx2;
}
}
int (*memcmpi)(const void *, const void *, size_t) = memcmp;
/*───────────────────────────────────────────────────────────────────────────│─╗
test/libc/str/strcmp_test.c § emptiness
*/
TEST(strcmp, emptyString) {
EXPECT_EQ(0, strcmpi("", ""));
EXPECT_NE(0, strcmpi("", "a"));
EXPECT_EQ(0, strcmp("", ""));
EXPECT_NE(0, strcmp("", "a"));
}
TEST(strcasecmp, emptyString) {
@ -88,11 +67,11 @@ TEST(wcscasecmp, emptyString) {
TEST(strncmp, emptyString) {
char *s1 = strcpy(tmalloc(1), "");
char *s2 = strcpy(tmalloc(1), "");
ASSERT_EQ(0, strncmpi(s1, s2, 0));
ASSERT_EQ(0, strncmpi(s1, s2, 1));
ASSERT_EQ(0, strncmpi(s1, s2, -1));
ASSERT_EQ(0, strncmpi(s1, s1, -1));
ASSERT_EQ(0, strncmpi(s2, s2, -1));
ASSERT_EQ(0, strncmp(s1, s2, 0));
ASSERT_EQ(0, strncmp(s1, s2, 1));
ASSERT_EQ(0, strncmp(s1, s2, -1));
ASSERT_EQ(0, strncmp(s1, s1, -1));
ASSERT_EQ(0, strncmp(s2, s2, -1));
tfree(s2);
tfree(s1);
}
@ -116,9 +95,9 @@ TEST(strncasecmp, emptyString) {
TEST(strncmp, testInequality) {
char *s1 = strcpy(tmalloc(2), "1");
char *s2 = strcpy(tmalloc(1), "");
ASSERT_EQ(0, strncmpi(s1, s2, 0));
ASSERT_EQ('1', strncmpi(s1, s2, 1));
ASSERT_EQ(-'1', strncmpi(s2, s1, 1));
ASSERT_EQ(0, strncmp(s1, s2, 0));
ASSERT_EQ('1', strncmp(s1, s2, 1));
ASSERT_EQ(-'1', strncmp(s2, s1, 1));
tfree(s2);
tfree(s1);
}
@ -166,37 +145,37 @@ TEST(memcmp, test) {
}
TEST(strcmp, testItWorks) {
EXPECT_EQ(strcmpi("", ""), 0);
EXPECT_EQ(strcmpi("a", "a"), 0);
EXPECT_GT(strcmpi("a", "A"), 0);
EXPECT_LT(strcmpi("A", "a"), 0);
EXPECT_LT(strcmpi("\001", "\377"), 0);
EXPECT_GT(strcmpi("\377", "\001"), 0);
EXPECT_LT(strcmpi("a", "aa"), 0);
EXPECT_GT(strcmpi("aa", "a"), 0);
EXPECT_LT(strcmpi("a\000", "aa\000"), 0);
EXPECT_GT(strcmpi("aa\000", "a\000"), 0);
EXPECT_LT(strcmpi("aaaaaaaaaaaaaaa\001", "aaaaaaaaaaaaaaa\377"), 0);
EXPECT_GT(strcmpi("aaaaaaaaaaaaaaa\377", "aaaaaaaaaaaaaaa\001"), 0);
EXPECT_LT(strcmpi("aaaaaaaaaaaaaaaa\001", "aaaaaaaaaaaaaaaa\377"), 0);
EXPECT_GT(strcmpi("aaaaaaaaaaaaaaaa\377", "aaaaaaaaaaaaaaaa\001"), 0);
EXPECT_LT(strcmpi("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaa\001",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaa\377"),
EXPECT_EQ(strcmp("", ""), 0);
EXPECT_EQ(strcmp("a", "a"), 0);
EXPECT_GT(strcmp("a", "A"), 0);
EXPECT_LT(strcmp("A", "a"), 0);
EXPECT_LT(strcmp("\001", "\377"), 0);
EXPECT_GT(strcmp("\377", "\001"), 0);
EXPECT_LT(strcmp("a", "aa"), 0);
EXPECT_GT(strcmp("aa", "a"), 0);
EXPECT_LT(strcmp("a\000", "aa\000"), 0);
EXPECT_GT(strcmp("aa\000", "a\000"), 0);
EXPECT_LT(strcmp("aaaaaaaaaaaaaaa\001", "aaaaaaaaaaaaaaa\377"), 0);
EXPECT_GT(strcmp("aaaaaaaaaaaaaaa\377", "aaaaaaaaaaaaaaa\001"), 0);
EXPECT_LT(strcmp("aaaaaaaaaaaaaaaa\001", "aaaaaaaaaaaaaaaa\377"), 0);
EXPECT_GT(strcmp("aaaaaaaaaaaaaaaa\377", "aaaaaaaaaaaaaaaa\001"), 0);
EXPECT_LT(strcmp("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaa\001",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaa\377"),
0);
EXPECT_GT(strcmpi("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaa\377",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaa\001"),
EXPECT_GT(strcmp("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaa\377",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaa\001"),
0);
EXPECT_LT(strcmpi("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa\001",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa\377"),
EXPECT_LT(strcmp("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa\001",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa\377"),
0);
EXPECT_GT(strcmpi("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa\377",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa\001"),
EXPECT_GT(strcmp("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa\377",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa\001"),
0);
EXPECT_LT(strcmpi("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaa\001",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaa\377"),
EXPECT_LT(strcmp("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaa\001",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaa\377"),
0);
EXPECT_GT(strcmpi("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaa\377",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaa\001"),
EXPECT_GT(strcmp("aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaa\377",
"aaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaa\001"),
0);
}
@ -347,8 +326,8 @@ TEST(strncmp, testEqualManyNs) {
s1[PAGESIZE - 1] = '\0';
s2[PAGESIZE - 1] = '\0';
for (unsigned i = 1; i <= 128; ++i) {
ASSERT_EQ(0, strncmpi(s1 + PAGESIZE - i, s2 + PAGESIZE - i, i + 0));
ASSERT_EQ(0, strncmpi(s1 + PAGESIZE - i, s2 + PAGESIZE - i, i + 1));
ASSERT_EQ(0, strncmp(s1 + PAGESIZE - i, s2 + PAGESIZE - i, i + 0));
ASSERT_EQ(0, strncmp(s1 + PAGESIZE - i, s2 + PAGESIZE - i, i + 1));
}
tfree(s2);
tfree(s1);
@ -362,8 +341,8 @@ TEST(strncmp, testNotEqualManyNs) {
memset(s2, 7, PAGESIZE);
s1[PAGESIZE - 1] = (unsigned char)0;
s2[PAGESIZE - 1] = (unsigned char)255;
ASSERT_EQ(-255, strncmpi(s1 + PAGESIZE - i, s2 + PAGESIZE - i, i + 0));
ASSERT_EQ(-255, strncmpi(s1 + PAGESIZE - i, s2 + PAGESIZE - i, i + 1));
ASSERT_EQ(-255, strncmp(s1 + PAGESIZE - i, s2 + PAGESIZE - i, i + 0));
ASSERT_EQ(-255, strncmp(s1 + PAGESIZE - i, s2 + PAGESIZE - i, i + 1));
}
tfree(s2);
tfree(s1);
@ -379,9 +358,9 @@ TEST(strncmp, testStringNulTerminatesBeforeExplicitLength) {
char *rdi = memcpy(tmalloc(sizeof(kRdi)), kRdi, sizeof(kRdi));
char *rsi = memcpy(tmalloc(sizeof(kRsi)), kRsi, sizeof(kRsi));
size_t rdx = 3;
EXPECT_EQ(strncmpi(rdi, rdi, rdx), 0);
EXPECT_LT(strncmpi(rdi, rsi, rdx), 0);
EXPECT_GT(strncmpi(rsi, rdi, rdx), 0);
EXPECT_EQ(strncmp(rdi, rdi, rdx), 0);
EXPECT_LT(strncmp(rdi, rsi, rdx), 0);
EXPECT_GT(strncmp(rsi, rdi, rdx), 0);
tfree(rsi);
tfree(rdi);
}
@ -417,9 +396,9 @@ TEST(strncmp16, testStringNulTerminatesBeforeExplicitLength) {
*/
TEST(strcmp, testTwosComplementBane_hasUnsignedBehavior) {
EXPECT_EQ(strcmpi("\200", "\200"), 0);
EXPECT_LT(strcmpi("\x7f", "\x80"), 0);
EXPECT_GT(strcmpi("\x80", "\x7f"), 0);
EXPECT_EQ(strcmp("\200", "\200"), 0);
EXPECT_LT(strcmp("\x7f", "\x80"), 0);
EXPECT_GT(strcmp("\x80", "\x7f"), 0);
}
TEST(strcasecmp, testTwosComplementBane_hasUnsignedBehavior) {
@ -468,7 +447,7 @@ TEST(strncmp16, testTwosComplementBane_hasUnsignedBehavior) {
tfree(B1);
}
TEST(wcscmp, testTwosComplementBane_hasSignedBehavior) {
TEST(wcscmp, testTwosComplementBane) {
wchar_t *B1 = tmalloc(8);
wchar_t *B2 = tmalloc(8);
B1[1] = L'\0';
@ -476,28 +455,27 @@ TEST(wcscmp, testTwosComplementBane_hasSignedBehavior) {
EXPECT_EQ(wcscmp(memcpy(B1, "\x00\x00\x00\x80", 4),
memcpy(B2, "\x00\x00\x00\x80", 4)),
0);
EXPECT_GT(wcscmp(memcpy(B1, "\xff\xff\xff\x7f", 4),
memcpy(B2, "\x00\x00\x00\x80", 4)),
0);
EXPECT_LT(wcscmp(memcpy(B1, "\x00\x00\x00\x80", 4),
EXPECT_EQ(-1, wcscmp(memcpy(B1, "\xff\xff\xff\x7f", 4),
memcpy(B2, "\x00\x00\x00\x80", 4)));
EXPECT_EQ(wcscmp(memcpy(B1, "\x00\x00\x00\x80", 4),
memcpy(B2, "\xff\xff\xff\x7f", 4)),
0);
1);
tfree(B2);
tfree(B1);
}
TEST(wcsncmp, testTwosComplementBane_hasSignedBehavior) {
TEST(wcsncmp, testTwosComplementBane) {
wchar_t *B1 = tmalloc(4);
wchar_t *B2 = tmalloc(4);
EXPECT_EQ(wcsncmp(memcpy(B1, "\x00\x00\x00\x80", 4),
memcpy(B2, "\x00\x00\x00\x80", 4), 1),
0);
EXPECT_GT(wcsncmp(memcpy(B1, "\xff\xff\xff\x7f", 4),
EXPECT_EQ(wcsncmp(memcpy(B1, "\xff\xff\xff\x7f", 4),
memcpy(B2, "\x00\x00\x00\x80", 4), 1),
0);
EXPECT_LT(wcsncmp(memcpy(B1, "\x00\x00\x00\x80", 4),
-1);
EXPECT_EQ(wcsncmp(memcpy(B1, "\x00\x00\x00\x80", 4),
memcpy(B2, "\xff\xff\xff\x7f", 4), 1),
0);
1);
tfree(B2);
tfree(B1);
}
@ -555,12 +533,42 @@ BENCH(bench_00_strcmp, bench) {
PAGESIZE);
data = tgc(tmalloc(size));
dupe = tgc(tmalloc(size));
fprintf(stderr, "\n");
EZBENCH2("strcmp [identity]", longstringislong(size, data),
EXPROPRIATE(strcmp(VEIL("r", data), data)));
fprintf(stderr, "\n");
EZBENCH2("strcmp [2 diff]", donothing,
EXPROPRIATE(strcmp(VEIL("r", "hi"), VEIL("r", "there"))));
EZBENCH2("strcmp$pure [2 diff]", donothing,
EXPROPRIATE(strcmp$pure(VEIL("r", "hi"), VEIL("r", "there"))));
fprintf(stderr, "\n");
EZBENCH2("strcmp [2 dupe]", randomize_buf2str_dupe(2, data, dupe),
EXPROPRIATE(strcmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp$pure [2 dupe]", randomize_buf2str_dupe(2, data, dupe),
EXPROPRIATE(strcmp$pure(VEIL("r", data), VEIL("r", dupe))));
fprintf(stderr, "\n");
EZBENCH2("strcmp [4 dupe]", randomize_buf2str_dupe(4, data, dupe),
EXPROPRIATE(strcmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp$pure [4 dupe]", randomize_buf2str_dupe(4, data, dupe),
EXPROPRIATE(strcmp$pure(VEIL("r", data), VEIL("r", dupe))));
fprintf(stderr, "\n");
EZBENCH2("strcmp [8 dupe]", randomize_buf2str_dupe(8, data, dupe),
EXPROPRIATE(strcmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp$pure [8 dupe]", randomize_buf2str_dupe(8, data, dupe),
EXPROPRIATE(strcmp$pure(VEIL("r", data), VEIL("r", dupe))));
fprintf(stderr, "\n");
EZBENCH2("strcmp [short dupe]", randomize_buf2str_dupe(size, data, dupe),
EXPROPRIATE(strcmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp$pure [short dupe]", randomize_buf2str_dupe(size, data, dupe),
EXPROPRIATE(strcmp$pure(VEIL("r", data), VEIL("r", dupe))));
fprintf(stderr, "\n");
EZBENCH2("strcmp [long dupe]", longstringislong_dupe(size, data, dupe),
EXPROPRIATE(strcmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcmp$pure [long dupe]", longstringislong_dupe(size, data, dupe),
@ -574,13 +582,19 @@ BENCH(bench_01_strcasecmp, bench) {
PAGESIZE);
data = tgc(tmalloc(size));
dupe = tgc(tmalloc(size));
fprintf(stderr, "\n");
EZBENCH2("strcasecmp [identity]", longstringislong(size, data),
EXPROPRIATE(strcasecmp(VEIL("r", data), data)));
fprintf(stderr, "\n");
EZBENCH2("strcasecmp [short dupe]", randomize_buf2str_dupe(size, data, dupe),
EXPROPRIATE(strcasecmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcasecmp$pure [short dupe]",
randomize_buf2str_dupe(size, data, dupe),
EXPROPRIATE(strcasecmp$pure(VEIL("r", data), VEIL("r", dupe))));
fprintf(stderr, "\n");
EZBENCH2("strcasecmp [long dupe]", longstringislong_dupe(size, data, dupe),
EXPROPRIATE(strcasecmp(VEIL("r", data), VEIL("r", dupe))));
EZBENCH2("strcasecmp$pure [long dupe]",

View file

@ -17,11 +17,50 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
TEST(tprecode16to8, test) {
char b[128];
EXPECT_EQ(69, tprecode16to8(b, 128, u"(╯°□°)╯︵L┻━┻ 𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹"));
EXPECT_EQ(69, tprecode16to8(b, 128, u"(╯°□°)╯︵L┻━┻ 𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹").ax);
EXPECT_STREQ("(╯°□°)╯︵L┻━┻ 𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹", b);
}
TEST(tprecode16to8, testEmptyOut_doesNothingButStillCountsSrcLength) {
axdx_t r;
r = tprecode16to8(NULL, 0, u"hi");
EXPECT_EQ(0, r.ax);
EXPECT_EQ(3, r.dx);
}
TEST(tprecode16to8, testTooLittle_stillNulTerminates) {
axdx_t r;
char b[2] = {1, 2};
r = tprecode16to8(b, 2, u"hi");
EXPECT_EQ(1, r.ax);
EXPECT_EQ(3, r.dx);
EXPECT_EQ('h', b[0]);
EXPECT_EQ(0, b[1]);
}
TEST(tprecode16to8, testAscii_vectorSpeedupWorks) {
size_t size = 32;
char *buf = tmalloc(size);
EXPECT_EQ(31,
tprecode16to8(buf, size, u"babaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").ax);
EXPECT_STREQ("babaaaaaaaaaaaaaaaaaaaaaaaaaaaa", buf);
tfree(buf);
}
BENCH(tprecode16to8, bench) {
char *buf8 = malloc(kHyperionSize + 1);
char16_t *buf16 = malloc((kHyperionSize + 1) * 2);
tprecode8to16(buf16, kHyperionSize + 1, kHyperion);
EZBENCH2("tprecode16to8", donothing,
tprecode16to8(buf8, kHyperionSize + 1, buf16));
free(buf16);
free(buf8);
}

View file

@ -18,19 +18,64 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
TEST(tprecode8to16, test) {
size_t size = 8;
char16_t *buf = tmalloc(size * sizeof(char16_t));
EXPECT_EQ(7, tprecode8to16(buf, size, "hello☻♥"));
EXPECT_EQ(7, tprecode8to16(buf, size, "hello☻♥").ax);
EXPECT_STREQ(u"hello☻♥", buf);
tfree(buf);
}
TEST(tprecode8to16, testEmptyOut_doesNothingButStillCountsSrcLength) {
axdx_t r;
r = tprecode8to16(NULL, 0, "hi");
EXPECT_EQ(0, r.ax);
EXPECT_EQ(3, r.dx);
}
TEST(tprecode8to16, testOnlyRoomForNul_writesIt) {
axdx_t r;
char16_t b[1] = {1};
r = tprecode8to16(b, 1, "hi");
EXPECT_EQ(0, r.ax);
EXPECT_EQ(3, r.dx);
EXPECT_EQ(0, b[0]);
}
TEST(tprecode8to16, testTooLittle_stillNulTerminates) {
axdx_t r;
char16_t b[2] = {1, 2};
r = tprecode8to16(b, 2, "hi");
EXPECT_EQ(1, r.ax);
EXPECT_EQ(3, r.dx);
EXPECT_EQ('h', b[0]);
EXPECT_EQ(0, b[1]);
}
TEST(tprecode8to16, test2) {
char16_t b[128];
EXPECT_EQ(34, tprecode8to16(b, 128, "(╯°□°)╯︵L┻━┻ 𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹"));
EXPECT_EQ(34, tprecode8to16(b, 128, "(╯°□°)╯︵L┻━┻ 𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹").ax);
EXPECT_STREQ(u"(╯°□°)╯︵L┻━┻ 𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹", b);
}
TEST(tprecode8to16, testAscii_vectorSpeedupWorks) {
size_t size = 32;
char16_t *buf = tmalloc(size * sizeof(char16_t));
EXPECT_EQ(31,
tprecode8to16(buf, size, "babaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").ax);
EXPECT_STREQ(u"babaaaaaaaaaaaaaaaaaaaaaaaaaaaa", buf);
tfree(buf);
}
BENCH(tprecode8to16, bench) {
char16_t *buf = malloc((kHyperionSize + 1) * 2);
EZBENCH2("tprecode8to16", donothing,
tprecode8to16(buf, kHyperionSize, kHyperion));
free(buf);
}

View file

@ -25,7 +25,6 @@
#include "libc/mem/mem.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/rbx.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/stdio/stdio.h"