Perform some code cleanup

This commit is contained in:
Justine Tunney 2022-06-23 10:21:07 -07:00
parent 0dd9629562
commit a4601a24d3
63 changed files with 350 additions and 1643 deletions

View file

@ -33,8 +33,23 @@
#include "libc/testlib/testlib.h"
#include "tool/net/sandbox.h"
// It's been reported that Chromebooks return EINVAL here.
bool CanUseSeccomp(void) {
int ws, pid;
ASSERT_NE(-1, (pid = fork()));
if (!pid) {
if (seccomp(SECCOMP_SET_MODE_STRICT, 0, 0) != -1) {
_Exit1(0);
} else {
_Exit1(1);
}
}
EXPECT_NE(-1, wait(&ws));
return WIFEXITED(ws) && !WEXITSTATUS(ws);
}
void SetUp(void) {
if (!__is_linux_2_6_23()) {
if (!__is_linux_2_6_23() || !CanUseSeccomp()) {
exit(0);
}
}

View file

@ -131,7 +131,7 @@ TEST(pthread_mutex_lock, contention) {
for (i = 0; i < THREADS; ++i) {
munmap(stack[i], GetStackSize());
}
pthread_mutex_destroy(&lock);
EXPECT_EQ(0, pthread_mutex_destroy(&lock));
}
TEST(pthread_mutex_lock, rcontention) {
@ -159,7 +159,7 @@ TEST(pthread_mutex_lock, rcontention) {
for (i = 0; i < THREADS; ++i) {
munmap(stack[i], GetStackSize());
}
pthread_mutex_destroy(&lock);
EXPECT_EQ(0, pthread_mutex_destroy(&lock));
}
TEST(pthread_mutex_lock, econtention) {
@ -187,7 +187,7 @@ TEST(pthread_mutex_lock, econtention) {
for (i = 0; i < THREADS; ++i) {
munmap(stack[i], GetStackSize());
}
pthread_mutex_destroy(&lock);
EXPECT_EQ(0, pthread_mutex_destroy(&lock));
}
int SpinlockWorker(void *p) {

View file

@ -106,9 +106,9 @@ TEST(strnlen, nulNotFound_ReturnsSize) {
}
}
TEST(strnlen_s, nulNotFound_ReturnsZero) {
TEST(strnlen_s, nulNotFound) {
char buf[3] = {1, 2, 3};
ASSERT_EQ(0, strnlen_s(buf, 3));
ASSERT_EQ(3, strnlen_s(buf, 3));
}
TEST(strlen, fuzz) {

View file

@ -1,101 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/runtime/buffer.h"
#include "libc/runtime/gc.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"
#define ALIGN 128
#define BUFSIZE (8 * 32)
#define MASKSIZE (BUFSIZE / CHAR_BIT)
const char kX[] = "aaaaaaaaeeeeeeeeeeeeeeeeeeeeeeee"
"e e"
"e e"
"e e"
"e e"
"e e"
"e e"
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeee-e";
const char kY[] = "aaaaaaaaefffffffffffeffffffffff-"
"f z-"
"f f"
"f f"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"f f"
"f f"
"ffffffffffffffffffffffffffffff-f";
const char kM[] = "11111111100000000000100000000000"
"01111111111111111111111111111100"
"01111111111111111111111111111110"
"01111111111111111111111111111110"
"00000000000000000000000000000000"
"01111111111111111111111111111110"
"01111111111111111111111111111110"
"00000000000000000000000000000010";
dontdiscard char *binify(uint8_t *data, size_t size) {
uint8_t b;
size_t i, j;
char *s, *p;
p = s = xmalloc(size * CHAR_BIT + 1);
for (i = 0; i < size; ++i) {
b = data[i];
for (j = 0; j < CHAR_BIT; ++j) {
*p++ = "01"[b & 1];
b >>= 1;
}
}
*p = '\0';
return s;
}
TEST(memeqmask, test) {
struct GuardedBuffer x = {}, y = {}, m = {};
memcpy(balloc(&x, ALIGN, BUFSIZE), kX, BUFSIZE);
memcpy(balloc(&y, ALIGN, BUFSIZE), kY, BUFSIZE);
balloc(&m, ALIGN, MASKSIZE);
EXPECT_EQ((intptr_t)m.p, (intptr_t)memeqmask(m.p, x.p, y.p, BUFSIZE));
EXPECT_STREQ(kM, gc(binify(m.p, MASKSIZE)));
bfree(&m);
bfree(&x);
bfree(&y);
}
#if 0
#include "libc/rand/rand.h"
#include "libc/testlib/ezbench.h"
TEST(memeqmask, bench) {
size_t len = 64 * 1024;
char *m = xmemalign(64, DIMMASK(len));
char *x = xmemalign(64, len);
char *y = xmemalign(64, len);
EZBENCH(
{
rngset(x, len, rand64, -1);
rngset(y, len, rand64, -1);
},
memeqmask(m, x, y, len));
}
#endif

View file

@ -19,6 +19,7 @@
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
TEST(memrchr16, test) {
@ -31,4 +32,6 @@ TEST(memrchr16, test) {
BENCH(memrchr16, bench) {
EZBENCH2("memrchr16", donothing,
EXPROPRIATE(memrchr16(u"yo.hi.there", '.', 11)));
EZBENCH2("memrchr16 hyperion", donothing,
EXPROPRIATE(memrchr16(kHyperion, '.', kHyperionSize / 2)));
}

View file

View file

@ -1,7 +1,7 @@
/*-*- 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
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
@ -16,32 +16,21 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
TEST(sidiv, smoke) {
EXPECT_EQ(13373133731337 / 10, div10int64(13373133731337));
EXPECT_EQ(13373133731337 / 100, div100int64(13373133731337));
EXPECT_EQ(13373133731337 / 1000, div1000int64(13373133731337));
EXPECT_EQ(13373133731337 / 10000, div10000int64(13373133731337));
EXPECT_EQ(13373133731337 / 1000000, div1000000int64(13373133731337));
EXPECT_EQ(13373133731337 / 1000000000, div1000000000int64(13373133731337));
TEST(wmemrchr, test) {
EXPECT_EQ(NULL, wmemrchr(L"yo.hi.thereeuhcruhrceeuhcre", '-', 27));
EXPECT_STREQ(L".there", wmemrchr(L"yo.hi.there", '.', 11));
EXPECT_STREQ(L".thereeuhcruhrceeuhcre",
wmemrchr(L"yo.hi.thereeuhcruhrceeuhcre", '.', 27));
}
TEST(sirem, smoke) {
EXPECT_EQ(13373133731337 % 10, rem10int64(13373133731337));
EXPECT_EQ(13373133731337 % 100, rem100int64(13373133731337));
EXPECT_EQ(13373133731337 % 1000, rem1000int64(13373133731337));
EXPECT_EQ(13373133731337 % 10000, rem10000int64(13373133731337));
EXPECT_EQ(13373133731337 % 1000000, rem1000000int64(13373133731337));
EXPECT_EQ(13373133731337 % 1000000000, rem1000000000int64(13373133731337));
}
TEST(rem, euclid) {
ASSERT_EQ(-2, rem10int64(-12));
ASSERT_EQ(-1, rem10int64(-1));
ASSERT_EQ(0, rem10int64(0));
ASSERT_EQ(1, rem10int64(1));
ASSERT_EQ(9, rem10int64(9));
ASSERT_EQ(1, rem10int64(11));
BENCH(wmemrchr, bench) {
EZBENCH2("wmemrchr", donothing,
EXPROPRIATE(wmemrchr(L"yo.hi.there", '.', 11)));
EZBENCH2("wmemrchr hyperion", donothing,
EXPROPRIATE(wmemrchr(kHyperion, '.', kHyperionSize / 4)));
}