mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-30 00:08:30 +00:00
Fold conv package into fmt
Both packages had nearly identical dependency requirements, so merging them should help reduce the complexity of the build graph.
This commit is contained in:
parent
978b7858f9
commit
1fc91f3580
203 changed files with 352 additions and 563 deletions
56
test/libc/fmt/basename_test.c
Normal file
56
test/libc/fmt/basename_test.c
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*-*- 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/bits/bits.h"
|
||||
#include "libc/fmt/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"));
|
||||
}
|
||||
|
||||
TEST(basename, testTrailingSlash_isIgnored) {
|
||||
/* should be "foo" but basename() doesn't allocate memory */
|
||||
EXPECT_STREQ("foo/", basename("foo/"));
|
||||
EXPECT_STREQ("foo//", basename("foo//"));
|
||||
}
|
||||
|
||||
TEST(basename, testOnlySlashes_oneSlashOnlyVasily) {
|
||||
EXPECT_STREQ("/", basename("///"));
|
||||
}
|
||||
|
||||
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("..")));
|
||||
}
|
67
test/libc/fmt/itoa64radix10_test.c
Normal file
67
test/libc/fmt/itoa64radix10_test.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
/*-*- 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/fmt/conv.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(int64toarray_radix10, test) {
|
||||
char buf[21];
|
||||
EXPECT_EQ(1, int64toarray_radix10(0, buf));
|
||||
EXPECT_STREQ("0", buf);
|
||||
EXPECT_EQ(1, int64toarray_radix10(1, buf));
|
||||
EXPECT_STREQ("1", buf);
|
||||
EXPECT_EQ(2, int64toarray_radix10(-1, buf));
|
||||
EXPECT_STREQ("-1", buf);
|
||||
EXPECT_EQ(19, int64toarray_radix10(INT64_MAX, buf));
|
||||
EXPECT_STREQ("9223372036854775807", buf);
|
||||
EXPECT_EQ(20, int64toarray_radix10(INT64_MIN, buf));
|
||||
EXPECT_STREQ("-9223372036854775808", buf);
|
||||
}
|
||||
|
||||
TEST(uint64toarray_radix10, test) {
|
||||
char buf[21];
|
||||
EXPECT_EQ(1, uint64toarray_radix10(0, buf));
|
||||
EXPECT_STREQ("0", buf);
|
||||
EXPECT_EQ(20, uint64toarray_radix10(UINT64_MAX, buf));
|
||||
EXPECT_STREQ("18446744073709551615", buf);
|
||||
EXPECT_EQ(19, uint64toarray_radix10(INT64_MIN, buf));
|
||||
EXPECT_STREQ("9223372036854775808", buf);
|
||||
}
|
||||
|
||||
TEST(int128toarray_radix10, test) {
|
||||
char buf[41];
|
||||
EXPECT_EQ(1, int128toarray_radix10(0, buf));
|
||||
EXPECT_STREQ("0", buf);
|
||||
EXPECT_EQ(39, int128toarray_radix10(INT128_MAX, buf));
|
||||
EXPECT_STREQ("170141183460469231731687303715884105727", buf);
|
||||
EXPECT_EQ(40, int128toarray_radix10(INT128_MIN, buf));
|
||||
EXPECT_STREQ("-170141183460469231731687303715884105728", buf);
|
||||
}
|
||||
|
||||
TEST(uint128toarray_radix10, test) {
|
||||
char buf[40];
|
||||
EXPECT_EQ(1, uint128toarray_radix10(0, buf));
|
||||
EXPECT_STREQ("0", buf);
|
||||
EXPECT_EQ(39, uint128toarray_radix10(UINT128_MAX, buf));
|
||||
EXPECT_STREQ("340282366920938463463374607431768211455", buf);
|
||||
EXPECT_EQ(39, uint128toarray_radix10(INT128_MIN, buf));
|
||||
EXPECT_STREQ("170141183460469231731687303715884105728", buf);
|
||||
}
|
37
test/libc/fmt/itoa64radix16_test.c
Normal file
37
test/libc/fmt/itoa64radix16_test.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*-*- 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/fmt/itoa.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(itoa64radix16, test) {
|
||||
char buf[21];
|
||||
EXPECT_EQ(5, uint64toarray_radix16(0x31337, buf));
|
||||
EXPECT_STREQ("31337", buf);
|
||||
EXPECT_EQ(2, uint64toarray_radix16(0x13, buf));
|
||||
EXPECT_STREQ("13", buf);
|
||||
EXPECT_EQ(3, uint64toarray_radix16(0x113, buf));
|
||||
EXPECT_STREQ("113", buf);
|
||||
}
|
||||
|
||||
TEST(itoa64fixed16, test) {
|
||||
char buf[21];
|
||||
EXPECT_EQ(8, uint64toarray_fixed16(0x31337, buf, 32));
|
||||
EXPECT_STREQ("00031337", buf);
|
||||
}
|
36
test/libc/fmt/llog10_test.c
Normal file
36
test/libc/fmt/llog10_test.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*-*- 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/bits/bits.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(llog10, test) {
|
||||
EXPECT_EQ(0, llog10(1));
|
||||
EXPECT_EQ(0, llog10(2));
|
||||
EXPECT_EQ(0, llog10(9));
|
||||
EXPECT_EQ(1, llog10(10));
|
||||
EXPECT_EQ(1, llog10(11));
|
||||
EXPECT_EQ(1, llog10(99));
|
||||
EXPECT_EQ(2, llog10(100));
|
||||
EXPECT_EQ(2, llog10(101));
|
||||
EXPECT_EQ(9, llog10(INT_MAX));
|
||||
EXPECT_EQ(12, llog10(1000000000000));
|
||||
}
|
|
@ -27,9 +27,9 @@
|
|||
#include "libc/bits/progn.internal.h"
|
||||
#include "libc/bits/pushpop.h"
|
||||
#include "libc/bits/safemacros.internal.h"
|
||||
#include "libc/conv/itoa.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/math.h"
|
||||
#include "libc/mem/mem.h"
|
||||
|
|
52
test/libc/fmt/strtoimax_test.c
Normal file
52
test/libc/fmt/strtoimax_test.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*-*- 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/bits/bits.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(strtoimax, testZero) {
|
||||
EXPECT_EQ(0, strtoimax("0", NULL, 0));
|
||||
}
|
||||
|
||||
TEST(strtoimax, testDecimal) {
|
||||
EXPECT_EQ(-123, strtoimax("-123", NULL, 0));
|
||||
}
|
||||
|
||||
TEST(strtoimax, testHex) {
|
||||
EXPECT_EQ(-255, strtoimax("-0xff", NULL, 0));
|
||||
}
|
||||
|
||||
TEST(strtoimax, testOctal) {
|
||||
EXPECT_EQ(-123, strtoimax("-0173", NULL, 0));
|
||||
}
|
||||
|
||||
TEST(strtoimax, testLimits) {
|
||||
EXPECT_EQ(
|
||||
((uintmax_t)0xffffffffffffffff) << 64 | (uintmax_t)0xffffffffffffffff,
|
||||
strtoimax("-1", NULL, 0));
|
||||
EXPECT_EQ(
|
||||
((uintmax_t)0x7fffffffffffffff) << 64 | (uintmax_t)0xffffffffffffffff,
|
||||
strtoimax("0x7fffffffffffffffffffffffffffffff", NULL, 0));
|
||||
}
|
||||
|
||||
TEST(strtoimax, testTwosBane) {
|
||||
EXPECT_EQ(((uintmax_t)0x8000000000000000) << 64 | 0x0000000000000000,
|
||||
strtoimax("0x80000000000000000000000000000000", NULL, 0));
|
||||
}
|
43
test/libc/fmt/strtoumax_test.c
Normal file
43
test/libc/fmt/strtoumax_test.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*-*- 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/bits/bits.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(strtoumax, testZero) {
|
||||
EXPECT_EQ(UINTMAX_MIN, strtoumax("0", NULL, 0));
|
||||
}
|
||||
TEST(strtoumax, testDecimal) {
|
||||
EXPECT_EQ(123, strtoumax("123", NULL, 0));
|
||||
}
|
||||
TEST(strtoumax, testHex) {
|
||||
EXPECT_EQ(255, strtoumax("0xff", NULL, 0));
|
||||
}
|
||||
TEST(strtoumax, testOctal) {
|
||||
EXPECT_EQ(123, strtoumax("0173", NULL, 0));
|
||||
}
|
||||
|
||||
TEST(strtoumax, testMaximum) {
|
||||
EXPECT_EQ(UINTMAX_MAX,
|
||||
strtoumax("340282366920938463463374607431768211455", NULL, 0));
|
||||
EXPECT_EQ(UINTMAX_MAX,
|
||||
strtoumax("0xffffffffffffffffffffffffffffffff", NULL, 0));
|
||||
}
|
|
@ -21,7 +21,6 @@ TEST_LIBC_FMT_CHECKS = \
|
|||
TEST_LIBC_FMT_DIRECTDEPS = \
|
||||
LIBC_ALG \
|
||||
LIBC_CALLS_HEFTY \
|
||||
LIBC_CONV \
|
||||
LIBC_FMT \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
|
@ -55,6 +54,10 @@ o/$(MODE)/test/libc/fmt/%.com.dbg: \
|
|||
|
||||
$(TEST_LIBC_FMT_OBJS): test/libc/fmt/test.mk
|
||||
|
||||
$(TEST_LIBC_FMT_OBJS): \
|
||||
DEFAULT_CCFLAGS += \
|
||||
-fno-builtin
|
||||
|
||||
.PHONY: o/$(MODE)/test/libc/fmt
|
||||
o/$(MODE)/test/libc/fmt: \
|
||||
$(TEST_LIBC_FMT_BINS) \
|
||||
|
|
33
test/libc/fmt/timevaltofiletime_test.c
Normal file
33
test/libc/fmt/timevaltofiletime_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 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/calls/struct/timeval.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/nt/struct/filetime.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
TEST(TimeValToFileTime, roundTrip) {
|
||||
struct timeval tv1, tv2;
|
||||
tv1.tv_sec = 31337;
|
||||
tv1.tv_usec = 1337;
|
||||
FileTimeToTimeVal(&tv2, TimeValToFileTime(&tv1));
|
||||
EXPECT_EQ(31337, tv2.tv_sec);
|
||||
EXPECT_EQ(1337, tv2.tv_usec);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue