Import C++ Standard Template Library

You can now use the hardest fastest and most dangerous language there is
with Cosmopolitan. So far about 75% of LLVM libcxx has been added. A few
breaking changes needed to be made to help this go smoothly.

- Rename nothrow to dontthrow
- Rename nodiscard to dontdiscard
- Add some libm functions, e.g. lgamma, nan, etc.
- Change intmax_t from int128 to int64 like everything else
- Introduce %jjd formatting directive for int128_t
- Introduce strtoi128(), strtou128(), etc.
- Rename bsrmax() to bsr128()

Some of the templates that should be working currently are std::vector,
std::string, std::map, std::set, std::deque, etc.
This commit is contained in:
Justine Tunney 2022-03-22 05:51:41 -07:00
parent 5022f9e920
commit 868af3f950
286 changed files with 123987 additions and 507 deletions

View file

@ -306,24 +306,24 @@ TEST(strtoimax, testEndPtr) {
ASSERT_EQ(1, e - p);
}
TEST(strtoimax, testLimits) {
TEST(strtoi128, testLimits) {
EXPECT_EQ(
((uintmax_t)0xffffffffffffffff) << 64 | (uintmax_t)0xffffffffffffffff,
strtoimax("-1", NULL, 0));
((uint128_t)0xffffffffffffffff) << 64 | (uint128_t)0xffffffffffffffff,
strtoi128("-1", NULL, 0));
EXPECT_EQ(
((uintmax_t)0x7fffffffffffffff) << 64 | (uintmax_t)0xffffffffffffffff,
strtoimax("0x7fffffffffffffffffffffffffffffff", NULL, 0));
((uint128_t)0x7fffffffffffffff) << 64 | (uint128_t)0xffffffffffffffff,
strtoi128("0x7fffffffffffffffffffffffffffffff", NULL, 0));
}
TEST(strtoimax, testOutsideLimit) {
TEST(strtoi128, testOutsideLimit) {
errno = 0;
EXPECT_EQ(
((uintmax_t)0x7fffffffffffffff) << 64 | (uintmax_t)0xffffffffffffffff,
strtoimax("0x80000000000000000000000000000000", NULL, 0));
((uint128_t)0x7fffffffffffffff) << 64 | (uint128_t)0xffffffffffffffff,
strtoi128("0x80000000000000000000000000000000", NULL, 0));
EXPECT_EQ(ERANGE, errno);
errno = 0;
EXPECT_EQ(((uintmax_t)0x8000000000000000) << 64 | 0x0000000000000000,
strtoimax("-0x80000000000000000000000000000001", NULL, 0));
EXPECT_EQ(((uint128_t)0x8000000000000000) << 64 | 0x0000000000000000,
strtoi128("-0x80000000000000000000000000000001", NULL, 0));
EXPECT_EQ(ERANGE, errno);
}
@ -335,6 +335,7 @@ TEST(strtoul, neghex) {
TEST(strtoumax, testZero) {
EXPECT_EQ(UINTMAX_MIN, strtoumax("0", NULL, 0));
EXPECT_EQ(UINT128_MIN, strtou128("0", NULL, 0));
}
TEST(strtoumax, testDecimal) {
EXPECT_EQ(123, strtoumax("123", NULL, 0));
@ -353,16 +354,16 @@ TEST(strtoumax, testBinary) {
EXPECT_EQ(42, strtoumax("0b101010", NULL, 2));
}
TEST(strtoumax, testMaximum) {
EXPECT_EQ(UINTMAX_MAX,
strtoumax("340282366920938463463374607431768211455", NULL, 0));
EXPECT_EQ(UINTMAX_MAX,
strtoumax("0xffffffffffffffffffffffffffffffff", NULL, 0));
TEST(strtou128, test128imum) {
EXPECT_EQ(UINT128_MAX,
strtou128("340282366920938463463374607431768211455", NULL, 0));
EXPECT_EQ(UINT128_MAX,
strtou128("0xffffffffffffffffffffffffffffffff", NULL, 0));
}
TEST(strtoumax, testTwosBane) {
EXPECT_EQ(((uintmax_t)0x8000000000000000) << 64 | 0x0000000000000000,
strtoumax("0x80000000000000000000000000000000", NULL, 0));
TEST(strtou128, testTwosBane) {
EXPECT_EQ(((uint128_t)0x8000000000000000) << 64 | 0x0000000000000000,
strtou128("0x80000000000000000000000000000000", NULL, 0));
}
TEST(wcstol, test) {
@ -566,4 +567,12 @@ BENCH(atoi, bench) {
EXPROPRIATE(wcstoimax(VEIL("r", L"100000000"), 0, 10)));
EZBENCH2("wcstoumax 10⁸", donothing,
EXPROPRIATE(wcstoimax(VEIL("r", L"100000000"), 0, 10)));
EZBENCH2("strtoi128 10⁸", donothing,
EXPROPRIATE(strtoi128(VEIL("r", "100000000"), 0, 10)));
EZBENCH2("strtou128 10⁸", donothing,
EXPROPRIATE(strtoi128(VEIL("r", "100000000"), 0, 10)));
EZBENCH2("wcstoi128 10⁸", donothing,
EXPROPRIATE(wcstoi128(VEIL("r", L"100000000"), 0, 10)));
EZBENCH2("wcstou128 10⁸", donothing,
EXPROPRIATE(wcstoi128(VEIL("r", L"100000000"), 0, 10)));
}

View file

@ -572,7 +572,7 @@ TEST(xasprintf, hugeNtoa) {
ASSERT_STREQ(
"0b1111111111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111",
gc(xasprintf("%#jb", UINT128_MAX)));
gc(xasprintf("%#jjb", UINT128_MAX)));
}
TEST(xasprintf, twosBane) {
@ -591,20 +591,19 @@ TEST(snprintf, testFixedWidthString_wontOverrunInput) {
free(buf);
}
/* TODO(jart): why is this weird in TINY mode? */
/* TEST(snprintf, testFixedWidthStringIsNull_wontOverrunBuffer) { */
/* int N = 3; */
/* char *buf = malloc(N + 1); */
/* EXPECT_EQ(3, snprintf(buf, N + 1, "%.*s", pushpop(N), pushpop(NULL))); */
/* EXPECT_STREQ("(nu", buf); */
/* EXPECT_EQ(3, snprintf(buf, N + 1, "%#.*s", pushpop(N), pushpop(NULL))); */
/* EXPECT_STREQ("(nu", buf); */
/* EXPECT_EQ(3, snprintf(buf, N + 1, "%`'.*s", pushpop(N), pushpop(NULL))); */
/* EXPECT_STREQ("NUL", buf); */
/* EXPECT_EQ(3, snprintf(buf, N + 1, "%`#.*s", pushpop(N), pushpop(NULL))); */
/* EXPECT_STREQ("NUL", buf); */
/* free(buf); */
/* } */
TEST(snprintf, testFixedWidthStringIsNull_wontOverrunBuffer) {
int N = 3;
char *buf = malloc(N + 1);
EXPECT_EQ(3, snprintf(buf, N + 1, "%.*s", pushpop(N), pushpop(NULL)));
EXPECT_STREQ("(nu", buf);
EXPECT_EQ(3, snprintf(buf, N + 1, "%#.*s", pushpop(N), pushpop(NULL)));
EXPECT_STREQ("(nu", buf);
EXPECT_EQ(3, snprintf(buf, N + 1, "%`'.*s", pushpop(N), pushpop(NULL)));
EXPECT_STREQ("NUL", buf);
EXPECT_EQ(3, snprintf(buf, N + 1, "%`#.*s", pushpop(N), pushpop(NULL)));
EXPECT_STREQ("NUL", buf);
free(buf);
}
TEST(snprintf, twosBaneWithTypePromotion) {
int16_t x = 0x8000;
@ -646,14 +645,10 @@ BENCH(palandprintf, bench) {
EZBENCH2("INT_MIN %d", donothing, Format("%d", VEIL("r", INT_MIN)));
EZBENCH2("INT_MIN %,d", donothing, Format("%,d", VEIL("r", INT_MIN)));
EZBENCH2("INT_MIN %ld", donothing, Format("%ld", (long)VEIL("r", INT_MIN)));
EZBENCH2("INT_MIN %jd", donothing,
Format("%jd", (intmax_t)VEIL("r", INT_MIN)));
EZBENCH2("LONG_MIN %lx", donothing, Format("%lx", VEIL("r", LONG_MIN)));
EZBENCH2("LONG_MIN %ld", donothing, Format("%ld", VEIL("r", LONG_MIN)));
EZBENCH2("LONG_MIN %jd", donothing,
Format("%jd", (intmax_t)VEIL("r", LONG_MIN)));
EZBENCH2("LONG_MIN %jx", donothing,
Format("%jx", (intmax_t)VEIL("r", LONG_MIN)));
EZBENCH2("INT128_MIN %jjd", donothing, Format("%jjd", INT128_MIN));
EZBENCH2("INT128_MIN %jjx", donothing, Format("%jjx", INT128_MIN));
EZBENCH2("int64toarray 23", donothing, int64toarray_radix10(23, buffer));
EZBENCH2("int64toarray min", donothing,
int64toarray_radix10(INT_MIN, buffer));

View file

@ -26,7 +26,7 @@
#define sscanf1(STR, FMT) \
({ \
errno = 0; \
intmax_t x = 0; \
int128_t x = 0; \
EXPECT_EQ(1, sscanf(STR, FMT, &x)); \
x; \
})
@ -50,11 +50,11 @@ TEST(sscanf, testHex) {
EXPECT_EQ(0x123, sscanf1("123", "%x"));
EXPECT_EQ(0x123, sscanf1("0x123", "%x"));
EXPECT_EQ(0x123, sscanf1("0123", "%x"));
EXPECT_EQ(INTMAX_MAX,
sscanf1("170141183460469231731687303715884105727", "%jd"));
EXPECT_EQ(INTMAX_MIN,
sscanf1("-170141183460469231731687303715884105728", "%jd"));
EXPECT_EQ(UINTMAX_MAX, sscanf1("0xffffffffffffffffffffffffffffffff", "%jx"));
EXPECT_EQ(INT128_MAX,
sscanf1("170141183460469231731687303715884105727", "%jjd"));
EXPECT_EQ(INT128_MIN,
sscanf1("-170141183460469231731687303715884105728", "%jjd"));
EXPECT_EQ(UINT128_MAX, sscanf1("0xffffffffffffffffffffffffffffffff", "%jjx"));
}
TEST(sscanf, testOctal) {