Add minor improvements and cleanup

This commit is contained in:
Justine Tunney 2020-10-27 03:39:46 -07:00
parent 9e3e985ae5
commit feed0d2b0e
163 changed files with 2286 additions and 2245 deletions

View file

@ -29,7 +29,6 @@
#include "libc/bits/safemacros.h"
#include "libc/conv/itoa.h"
#include "libc/errno.h"
#include "libc/escape/escape.h"
#include "libc/fmt/fmt.h"
#include "libc/limits.h"
#include "libc/math.h"

View file

@ -73,4 +73,5 @@ TEST(malloc, test) {
for (i = 0; i < ARRAYLEN(A); ++i) free(A[i]);
for (i = 0; i < ARRAYLEN(maps); ++i) munmap(maps[i], mapsizes[i]);
for (i = 0; i < ARRAYLEN(fds); ++i) close(fds[i]);
malloc_trim(0);
}

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/escape/escape.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
TEST(cescapec, test) {

View file

@ -37,8 +37,8 @@ TEST(strclen, testAegeanNumberSupplementaryPlane) {
EXPECT_EQ(18, strlen16(u"𐄷𐄸𐄹𐄺𐄻𐄼𐄽𐄾𐄿"));
EXPECT_EQ(9, wcslen(L"𐄷𐄸𐄹𐄺𐄻𐄼𐄽𐄾𐄿"));
EXPECT_EQ(9, strclen("𐄷𐄸𐄹𐄺𐄻𐄼𐄽𐄾𐄿"));
EXPECT_EQ(9, strclen(u"𐄷𐄸𐄹𐄺𐄻𐄼𐄽𐄾𐄿"));
EXPECT_EQ(9, strclen(L"𐄷𐄸𐄹𐄺𐄻𐄼𐄽𐄾𐄿"));
EXPECT_EQ(9, strclen16(u"𐄷𐄸𐄹𐄺𐄻𐄼𐄽𐄾𐄿"));
EXPECT_EQ(9, wcslen(L"𐄷𐄸𐄹𐄺𐄻𐄼𐄽𐄾𐄿"));
}
TEST(strlen16, testCoolKidNulTerminator) {

View file

@ -0,0 +1,41 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
TEST(strcpy, test) {
char buf[64];
EXPECT_STREQ("hello", strcpy(buf, "hello"));
EXPECT_STREQ("hello there what's up", strcpy(buf, "hello there what's up"));
}
BENCH(strcpy, bench) {
extern char *strcpy_(char *, const char *) asm("strcpy");
static char buf[1024], buf2[1024];
memset(buf2, -1, sizeof(buf2) - 1);
EZBENCH2("strcpy 1", donothing, strcpy_(buf, ""));
EZBENCH2("strcpy 2", donothing, strcpy_(buf, "1"));
EZBENCH2("strcpy 7", donothing, strcpy_(buf, "123456"));
EZBENCH2("strcpy 8", donothing, strcpy_(buf, "1234567"));
EZBENCH2("strcpy 9", donothing, strcpy_(buf, "12345678"));
EZBENCH2("strcpy 16", donothing, strcpy_(buf, "123456781234567"));
EZBENCH2("strcpy 1023", donothing, strcpy_(buf, buf2));
}

View file

@ -21,6 +21,7 @@
#include "libc/macros.h"
#include "libc/nexgen32e/tinystrlen.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
char u8[] = "utf-8 ☻";
@ -129,3 +130,16 @@ TEST(tinystrnlen16, test) {
EXPECT_EQ(2, tinystrnlen16(u"123", 2));
EXPECT_EQ(3, tinystrnlen16(u"123", 4));
}
BENCH(strlen, bench) {
extern size_t strlen_(const char *) asm("strlen");
static char b[1024];
memset(b, -1, sizeof(b) - 1);
EZBENCH2("strlen 1", donothing, strlen_(""));
EZBENCH2("strlen 2", donothing, strlen_("1"));
EZBENCH2("strlen 7", donothing, strlen_("123456"));
EZBENCH2("strlen 8", donothing, strlen_("1234567"));
EZBENCH2("strlen 9", donothing, strlen_("12345678"));
EZBENCH2("strlen 16", donothing, strlen_("123456781234567"));
EZBENCH2("strlen 1023", donothing, strlen_(b));
}

View file

@ -17,27 +17,11 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
#include "libc/unicode/unicode.h"
#if 0
void *isnotplaintext(const void *, size_t)
__attribute__((__pure__, __leaf__, __nothrow__));
#endif
TEST(isnotplaintext, test) {
EXPECT_EQ(NULL, isnotplaintext(kHyperion, kHyperionSize));
EXPECT_STREQ("", isnotplaintext(kHyperion, kHyperionSize + 1));
}
char *doit(char *data, size_t size) {
data = isnotplaintext(data, size);
asm volatile("" : "+r"(data));
return data;
}
BENCH(isnotplaintext, bench) {
EZBENCH(donothing, doit(kHyperion, kHyperionSize));
TEST(strwidth, test) {
EXPECT_EQ(5, strwidth("hello"));
EXPECT_EQ(5, strwidth("\1he\e[0;0mllo\e#8"));
EXPECT_EQ(10, strwidth(""));
}

View file

@ -62,10 +62,6 @@ TEST(wcwidth, testCjkWidesAndCombiningLowLines_widthIsNotLength) {
/*────────────────────────────────────────────────────┴─*/
}
TEST(strwidth, tab) {
EXPECT_EQ(32, strwidth("mov 0x0(%rip),%rcx \t"));
}
TEST(wcwidth, block) {
EXPECT_EQ(1, wcwidth(u''));
}

View file

@ -102,25 +102,27 @@ struct Machine *m;
void SetUp(void) {
m = NewMachine();
m->ip = 0;
ReserveVirtual(m, 0, 4096);
ASSERT_EQ(0x5000, m->real.i);
m->mode = XED_MACHINE_MODE_LONG_64;
m->cr3 = AllocateLinearPage(m);
ReserveVirtual(m, 0, 4096, 0x0207);
ASSERT_EQ(0x1007, Read64(m->real.p + 0x0000)); // PML4T
ASSERT_EQ(0x2007, Read64(m->real.p + 0x1000)); // PDPT
ASSERT_EQ(0x3007, Read64(m->real.p + 0x2000)); // PDE
ASSERT_EQ(0x4007, Read64(m->real.p + 0x3000)); // PT
ASSERT_EQ(0x0207, Read64(m->real.p + 0x3000)); // PT
ASSERT_EQ(0x4000, m->real.i);
ASSERT_EQ(1, m->memstat.reserved);
ASSERT_EQ(4, m->memstat.committed);
ASSERT_EQ(4, m->memstat.allocated);
ASSERT_EQ(3, m->memstat.pagetables);
Write64(m->sp, 4096);
}
void TearDown(void) {
FreeVirtual(m, 0, 4096);
ASSERT_EQ(0x5000, m->real.i);
ASSERT_EQ(0x1007, Read64(m->real.p + 0x0000)); // PML4T
ASSERT_EQ(0x2007, Read64(m->real.p + 0x1000)); // PDPT
ASSERT_EQ(0x3007, Read64(m->real.p + 0x2000)); // PDE
ASSERT_EQ(0x0000, Read64(m->real.p + 0x3000)); // PT
ASSERT_EQ(0x4000, m->realfree->i);
ASSERT_EQ(0x1000, m->realfree->n);
FreeMachine(m);
}

View file

@ -26,12 +26,12 @@
#include "libc/unicode/unicode.h"
#include "tool/build/lib/pty.h"
char *render(struct MachinePty *pty) {
char *render(struct Pty *pty) {
static struct Buffer b;
int y;
b.i = 0;
for (y = 0; y < pty->yn; ++y) {
MachinePtyAppendLine(pty, &b, y);
PtyAppendLine(pty, &b, y);
}
b.p[b.i] = 0;
return b.p;
@ -76,10 +76,10 @@ static const char widelatin_golden[] = "\
";
TEST(pty, testFunWidth) {
struct MachinePty *pty = MachinePtyNew();
MachinePtyWrite(pty, widelatin, ARRAYLEN(widelatin) - 1);
struct Pty *pty = NewPty();
PtyWrite(pty, widelatin, ARRAYLEN(widelatin) - 1);
EXPECT_STREQ(widelatin_golden, render(pty));
MachinePtyFree(pty);
FreePty(pty);
}
const char hyperion[] aligned(16) = "\
@ -130,10 +130,10 @@ m scribe my hand is in the grave.
";
TEST(pty, testPureAscii_benefitsFromVectorization) {
struct MachinePty *pty = MachinePtyNew();
MachinePtyWrite(pty, hyperion, ARRAYLEN(hyperion) - 1);
struct Pty *pty = NewPty();
PtyWrite(pty, hyperion, ARRAYLEN(hyperion) - 1);
EXPECT_STREQN(pty->wcs, hyperion_golden, ARRAYLEN(hyperion_golden) - 1);
MachinePtyFree(pty);
FreePty(pty);
}
static const char kKiloAnsi[] = "\
@ -196,7 +196,7 @@ TEST(pty, testLongestPossibleCharacter) {
EXPECT_EQ(60, strlen("\e[21;22;27;24;25;29;38;2;255;255;255;48;2;255;255;"
"255m\377\277\277\277\277\277"));
struct Buffer b = {0};
struct MachinePty *pty = MachinePtyNew();
struct Pty *pty = NewPty();
const char *s = "\e[1;2;3;4;5;6;7;9m"
"h"
"\e[0;"
@ -204,8 +204,8 @@ TEST(pty, testLongestPossibleCharacter) {
"48;2;255;255;255m"
"\377\277\277\277\277\277"
"\e[0m";
MachinePtyWrite(pty, s, strlen(s));
MachinePtyAppendLine(pty, &b, 0);
PtyWrite(pty, s, strlen(s));
PtyAppendLine(pty, &b, 0);
AppendChar(&b, '\0');
EXPECT_STREQ("\e[1;2;4;7;5;9m"
"𝒉"
@ -214,22 +214,23 @@ TEST(pty, testLongestPossibleCharacter) {
"\e[0m▂ "
" ",
b.p);
MachinePtyFree(pty);
FreePty(pty);
free(b.p);
}
TEST(pty, test) {
struct MachinePty *pty = MachinePtyNew();
MachinePtyWrite(pty, kKiloAnsi, strlen(kKiloAnsi));
struct Pty *pty = NewPty();
PtyWrite(pty, kKiloAnsi, strlen(kKiloAnsi));
EXPECT_STREQN(kKiloGolden, pty->wcs, wcslen(kKiloGolden));
MachinePtyFree(pty);
FreePty(pty);
}
BENCH(pty, bench) {
struct MachinePty *pty = MachinePtyNew();
struct Pty *pty = NewPty();
EZBENCH2("pty write ascii", donothing,
MachinePtyWrite(pty, hyperion, sizeof(hyperion) - 1));
PtyWrite(pty, hyperion, sizeof(hyperion) - 1));
EZBENCH2("pty write kilo", donothing,
MachinePtyWrite(pty, kKiloAnsi, sizeof(kKiloAnsi) - 1));
PtyWrite(pty, kKiloAnsi, sizeof(kKiloAnsi) - 1));
EZBENCH2("pty render", donothing, render(pty));
FreePty(pty);
}