mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 22:02:27 +00:00
Make more libc improvements
- Make memmem() faster - Make readdir() thread safe - Remove 64kb limit from mkdeps.com - Add old crypt() function from Musl - Improve new fix-third-party.py tool - Improve libc/isystem/ headers and fix bugs
This commit is contained in:
parent
a8cf0f7e89
commit
6a5717a48f
50 changed files with 3783 additions and 280 deletions
33
test/libc/stdio/crypt_test.c
Normal file
33
test/libc/stdio/crypt_test.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*-*- 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 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 │
|
||||
│ 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/testlib/testlib.h"
|
||||
#include "third_party/musl/crypt.h"
|
||||
|
||||
TEST(crypt, test) {
|
||||
// consistent with python crypt.crypt()
|
||||
EXPECT_STREQ("thXpd0YFlQG2w", crypt("hello", "there"));
|
||||
EXPECT_STREQ("$1$there$3B/lVCUlX8R18HDBuijby1", crypt("hello", "$1$there"));
|
||||
EXPECT_STREQ("$2aVOhEz8P7i6", crypt("hello", "$2$there"));
|
||||
EXPECT_STREQ("$5$there$.u5mdR0jvLs0jEf7qHTG98t8la1KVhEBH3eOFZ7ztL0",
|
||||
crypt("hello", "$5$there"));
|
||||
EXPECT_STREQ(
|
||||
"$6$there$y0TGuPcNSR23fFWCwYUj6VUfhnc9nlnkm6Y8waSLnANwCUcxK6esd7xm7.Jl."
|
||||
"jjr1/sPTaRK7igDNSxC.BhgX/",
|
||||
crypt("hello", "$6$there"));
|
||||
}
|
|
@ -44,6 +44,7 @@ TEST_LIBC_STDIO_DIRECTDEPS = \
|
|||
LIBC_X \
|
||||
LIBC_ZIPOS \
|
||||
THIRD_PARTY_GDTOA \
|
||||
THIRD_PARTY_MUSL \
|
||||
THIRD_PARTY_ZLIB \
|
||||
THIRD_PARTY_ZLIB_GZ
|
||||
|
||||
|
|
|
@ -18,7 +18,9 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/alg/alg.h"
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/bits/likely.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/rand/rand.h"
|
||||
#include "libc/str/internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
|
@ -27,6 +29,27 @@
|
|||
|
||||
#define MakeMemory(SL) memcpy(malloc(sizeof(SL) - 1), SL, sizeof(SL) - 1)
|
||||
|
||||
void *memmem_naive(const void *haystk, size_t haystklen, //
|
||||
const void *needle, size_t needlelen) {
|
||||
size_t i, j;
|
||||
if (!needlelen) return haystk;
|
||||
if (needlelen > haystklen) return 0;
|
||||
for (i = 0; i < haystklen; ++i) {
|
||||
for (j = 0;; ++j) {
|
||||
if (j == needlelen) return (/*unconst*/ char *)haystk + i;
|
||||
if (i + j == haystklen) break;
|
||||
if (((char *)haystk)[i + j] != ((char *)needle)[j]) break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(memmem, special) {
|
||||
EXPECT_EQ(0, memmem(0, 0, 0, 0));
|
||||
EXPECT_EQ(0, memmem(0, 0, "", 1));
|
||||
EXPECT_EQ("", memmem("", 1, 0, 0));
|
||||
}
|
||||
|
||||
TEST(memmem, test) {
|
||||
char *needle = MakeMemory("abcdefgh");
|
||||
char *haystk = MakeMemory("acccccccbbbbbbbbabcdefghdddddddd");
|
||||
|
@ -55,8 +78,8 @@ TEST(memmem, testStartOfMemory) {
|
|||
}
|
||||
|
||||
TEST(memmem, testEndOfMemory) {
|
||||
char *needle = MakeMemory("123");
|
||||
char *haystk = MakeMemory("abc123");
|
||||
char *needle = MakeMemory("123");
|
||||
EXPECT_EQ(&haystk[3], memmem(haystk, 6, needle, 3));
|
||||
free(haystk);
|
||||
free(needle);
|
||||
|
@ -79,8 +102,8 @@ TEST(memmem, testOneYes) {
|
|||
}
|
||||
|
||||
TEST(memmem, testCrossesSseRegister) {
|
||||
char *needle = MakeMemory("eeeeeeeeeeeeefffffffffffff");
|
||||
char *haystk = MakeMemory("eeeeeeeeeeeeeeeeffffffffffffffffrrrrrrrrrrrrrrrr");
|
||||
char *needle = MakeMemory("eeeeeeeeeeeeefffffffffffff");
|
||||
EXPECT_EQ(&haystk[3], memmem(haystk, 16 * 3, needle, 26));
|
||||
free(haystk);
|
||||
free(needle);
|
||||
|
@ -132,7 +155,46 @@ TEST(memmem, testWut) {
|
|||
ASSERT_STREQ("x", memmem("x", 1, "x", 1));
|
||||
}
|
||||
|
||||
TEST(memmem, fuzz) {
|
||||
int i, j, k, n, m;
|
||||
char a[128], b[128], *p, *q;
|
||||
for (i = 0; i < 10000; ++i) {
|
||||
rngset(a, sizeof(a), lemur64, -1);
|
||||
rngset(b, sizeof(b), lemur64, -1);
|
||||
p = a + lemur64() % sizeof(a) / 2;
|
||||
q = b + lemur64() % sizeof(b) / 2;
|
||||
n = lemur64() % sizeof(a) / 2;
|
||||
m = lemur64() % sizeof(b) / 2;
|
||||
ASSERT_EQ(memmem_naive(p, n, q, m), memmem(p, n, q, m));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* memmem naive l: 43,783c 14,142ns m: 31,285c 10,105ns
|
||||
* memmem l: 2,597c 839ns m: 2,612c 844ns
|
||||
* memmem l: 509c 164ns m: 599c 193ns
|
||||
*
|
||||
* strstr naive l: 103,057c 33,287ns m: 47,035c 15,192ns
|
||||
* strstr l: 3,186c 1,029ns m: 3,218c 1,039ns
|
||||
* strstr torture 1 l: 27c 9ns m: 61c 20ns
|
||||
* strstr torture 2 l: 2,322c 750ns m: 2,362c 763ns
|
||||
* strstr torture 4 l: 2,407c 777ns m: 2,448c 791ns
|
||||
* strstr torture 8 l: 2,803c 905ns m: 2,862c 924ns
|
||||
* strstr torture 16 l: 4,559c 1,473ns m: 3,614c 1,167ns
|
||||
* strstr torture 32 l: 5,324c 1,720ns m: 5,577c 1,801ns
|
||||
*
|
||||
* strcasestr naive l: 129,908c 41,959ns m: 155,420c 50,200ns
|
||||
* strcasestr l: 33,464c 10,809ns m: 31,636c 10,218ns
|
||||
* strcasestr tort 1 l: 38c 12ns m: 69c 22ns
|
||||
* strcasestr tort 2 l: 2,544c 822ns m: 2,580c 833ns
|
||||
* strcasestr tort 4 l: 2,745c 887ns m: 2,767c 894ns
|
||||
* strcasestr tort 8 l: 4,198c 1,356ns m: 4,216c 1,362ns
|
||||
* strcasestr tort 16 l: 7,402c 2,391ns m: 7,487c 2,418ns
|
||||
* strcasestr tort 32 l: 13,772c 4,448ns m: 12,945c 4,181ns
|
||||
*/
|
||||
BENCH(memmem, bench) {
|
||||
EZBENCH2("memmem naive", donothing,
|
||||
EXPROPRIATE(memmem_naive(kHyperion, kHyperionSize, "THE END", 7)));
|
||||
EZBENCH2("memmem", donothing,
|
||||
EXPROPRIATE(memmem(kHyperion, kHyperionSize, "THE END", 7)));
|
||||
EZBENCH2("memmem", donothing,
|
||||
|
|
|
@ -95,7 +95,20 @@ TEST(strstr, test) {
|
|||
ASSERT_STREQ("x", strstr("x", "x"));
|
||||
}
|
||||
|
||||
TEST(strstr, breakit) {
|
||||
char *p;
|
||||
p = gc(calloc(1, 32));
|
||||
p[0] = 'c';
|
||||
p[1] = 'c';
|
||||
p[10] = 'b';
|
||||
ASSERT_EQ(NULL, strstr(p, "b"));
|
||||
}
|
||||
|
||||
/*
|
||||
* memmem naive l: 43,783c 14,142ns m: 31,285c 10,105ns
|
||||
* memmem l: 2,597c 839ns m: 2,612c 844ns
|
||||
* memmem l: 509c 164ns m: 599c 193ns
|
||||
*
|
||||
* strstr naive l: 103,057c 33,287ns m: 47,035c 15,192ns
|
||||
* strstr l: 3,186c 1,029ns m: 3,218c 1,039ns
|
||||
* strstr torture 1 l: 27c 9ns m: 61c 20ns
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue