path
stringlengths
14
112
content
stringlengths
0
6.32M
size
int64
0
6.32M
max_lines
int64
1
100k
repo_name
stringclasses
2 values
autogenerated
bool
1 class
cosmopolitan/test/libc/mem/critbit0_test.c
/*-*- 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/intrin/bits.h" #include "libc/mem/critbit0.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/testlib/testlib.h" struct Bog { unsigned i; unsigned n; const char *p[]; }; static dontdiscard struct Bog *NewBog(unsigned n) { struct Bog *res = malloc(sizeof(struct Bog) + sizeof(const char *) * n); res->i = 0; res->n = n; return res; } static void ClearBog(struct Bog *bog) { bog->i = 0; } static void FreeBog(struct Bog **bog) { free(*bog), *bog = NULL; } static const char *const elems[] = {"a", "aa", "aaz", "abz", "bba", "bbc", "bbd", NULL}; static void MakeTree(struct critbit0 *tree) { memset(tree, 0, sizeof(*tree)); for (unsigned i = 0; elems[i]; ++i) { ASSERT_EQ(true, critbit0_insert(tree, elems[i])); } } TEST(critbit0, testContains) { struct critbit0 tree[1]; MakeTree(tree); for (unsigned i = 0; elems[i]; ++i) { if (!critbit0_contains(tree, elems[i])) abort(); } critbit0_clear(tree); } static const char *const elems2[] = {"a", "aa", "b", "bb", "ab", "ba", "aba", "bab", NULL}; TEST(critbit0, testDelete) { struct critbit0 tree = {0}; for (unsigned i = 1; elems2[i]; ++i) { critbit0_clear(&tree); for (unsigned j = 0; j < i; ++j) critbit0_insert(&tree, elems2[j]); for (unsigned j = 0; j < i; ++j) { if (!critbit0_contains(&tree, elems2[j])) abort(); } for (unsigned j = 0; j < i; ++j) { if (1 != critbit0_delete(&tree, elems2[j])) abort(); } for (unsigned j = 0; j < i; ++j) { if (critbit0_contains(&tree, elems2[j])) abort(); } } critbit0_clear(&tree); } static intptr_t allprefixed_cb(const char *elem, void *arg) { struct Bog *bog = arg; ASSERT_LT(bog->i, bog->n); bog->p[bog->i++] = elem; return 0; } TEST(critbit0, testAllPrefixed) { struct critbit0 tree[1]; MakeTree(tree); struct Bog *a = NewBog(4); ASSERT_EQ(0, critbit0_allprefixed(tree, "a", allprefixed_cb, a)); ASSERT_EQ(4, a->i); ASSERT_STREQ("a", a->p[0]); ASSERT_STREQ("aa", a->p[1]); ASSERT_STREQ("aaz", a->p[2]); ASSERT_STREQ("abz", a->p[3]); ClearBog(a); ASSERT_EQ(0, critbit0_allprefixed(tree, "aa", allprefixed_cb, a)); ASSERT_EQ(2, a->i); ASSERT_STREQ("aa", a->p[0]); ASSERT_STREQ("aaz", a->p[1]); critbit0_clear(tree); FreeBog(&a); } static intptr_t allprefixed_cb_halt(const char *elem, void *arg) { struct Bog *bog = arg; ASSERT_LT(bog->i, bog->n); bog->p[bog->i++] = elem; return strcmp(elem, "aa") == 0 ? 123 : 0; } TEST(critbit0, testAllPrefixed_haltOnNonzero) { struct critbit0 tree[1]; MakeTree(tree); struct Bog *a = NewBog(4); ASSERT_EQ(123, critbit0_allprefixed(tree, "a", allprefixed_cb_halt, a)); ASSERT_EQ(2, a->i); ASSERT_STREQ("a", a->p[0]); ASSERT_STREQ("aa", a->p[1]); critbit0_clear(tree); FreeBog(&a); }
4,771
131
jart/cosmopolitan
false
cosmopolitan/test/libc/mem/djbsort_test.c
/*-*- 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/limits.h" #include "libc/log/log.h" #include "libc/macros.internal.h" #include "libc/mem/alg.h" #include "libc/mem/gc.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/nexgen32e.h" #include "libc/nexgen32e/x86feature.h" #include "libc/runtime/runtime.h" #include "libc/stdio/rand.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" void djbsort_avx2(int32_t *, long); size_t n; int32_t *a, *b, *c; void insertionsort(int32_t *a, size_t n) { int t; unsigned i, j; for (i = 1; i < n; ++i) { j = i; t = a[i]; while (j > 0 && t < a[j - 1]) { a[j] = a[j - 1]; --j; } a[j] = t; } } #ifdef __x86_64__ TEST(djbsort, test4) { static const int kA[] = {4, 3, 2, 1}; n = ARRAYLEN(kA); a = memcpy(_gc(malloc(n * 4)), kA, n * 4); b = memcpy(_gc(malloc(n * 4)), kA, n * 4); c = memcpy(_gc(malloc(n * 4)), kA, n * 4); insertionsort(a, n); djbsort_avx2(b, n); djbsort(c, n); ASSERT_EQ(0, memcmp(a, b, n * 4)); ASSERT_EQ(0, memcmp(a, c, n * 4)); } #endif /* __x86_64__ */ TEST(djbsort, test64) { static const int kA[64] = { -578159677, -506496753, 627992389, -1352456426, -632122174, -1439460768, -1910607416, INT_MAX, 828129452, 388318786, 1361377655, 178046, -250539006, -933303681, 553663398, 801377571, 1798551167, 1926219590, 1322300030, 2005832294, 190425814, 1896617905, -549032465, -930529122, 953163359, -1290004523, 447201234, 1351982281, 458539920, 791518325, -1086386708, 291355635, INT_MIN, -1891018285, 1780601656, 1324222158, 1182663010, -1223576562, -676035738, 1367175631, -1016599422, 1619595549, -783089669, -663931695, -1082674213, 1369114774, -1944970907, 196888289, 1400094001, -1170906601, 835635598, 1506409902, -1528765785, 1132926680, -351522751, -1737707221, -209740191, -1857759507, -353087096, 763588876, -1323943608, -1219421355, -582289873, 1062699814, }; n = ARRAYLEN(kA); a = memcpy(_gc(malloc(n * 4)), kA, n * 4); b = memcpy(_gc(malloc(n * 4)), kA, n * 4); c = memcpy(_gc(malloc(n * 4)), kA, n * 4); insertionsort(a, n); djbsort(c, n); ASSERT_EQ(0, memcmp(a, c, n * 4)); #ifdef __x86_64__ if (X86_HAVE(AVX2)) { djbsort_avx2(b, n); ASSERT_EQ(0, memcmp(a, b, n * 4)); } #endif /* __x86_64__ */ } static int CompareInt(const void *a, const void *b) { if (*(const int *)a < *(const int *)b) return -1; if (*(const int *)a > *(const int *)b) return +1; return 0; } BENCH(djbsort, bench) { n = 256; a = _gc(memalign(32, n * 4)); EZBENCH2("insertionsort[255]", rngset(a, n * 4, _rand64, -1), insertionsort(a, n)); EZBENCH2("djbsort[255]", rngset(a, n * 4, _rand64, -1), djbsort(a, n)); EZBENCH2("_intsort[255]", rngset(a, n * 4, _rand64, -1), _intsort(a, n)); EZBENCH2("qsort[255]", rngset(a, n * 4, _rand64, -1), qsort(a, n, sizeof(int), CompareInt)); }
4,865
115
jart/cosmopolitan
false
cosmopolitan/test/libc/mem/test.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ PKGS += TEST_LIBC_MEM TEST_LIBC_MEM_FILES := $(wildcard test/libc/mem/*) TEST_LIBC_MEM_SRCS = $(TEST_LIBC_MEM_SRCS_C) $(TEST_LIBC_MEM_SRCS_CC) TEST_LIBC_MEM_SRCS_C = $(filter %_test.c,$(TEST_LIBC_MEM_FILES)) TEST_LIBC_MEM_SRCS_CC = $(filter %_test.cc,$(TEST_LIBC_MEM_FILES)) TEST_LIBC_MEM_OBJS = \ $(TEST_LIBC_MEM_SRCS_C:%.c=o/$(MODE)/%.o) \ $(TEST_LIBC_MEM_SRCS_CC:%.cc=o/$(MODE)/%.o) TEST_LIBC_MEM_COMS = \ $(TEST_LIBC_MEM_SRCS_C:%.c=o/$(MODE)/%.com) \ $(TEST_LIBC_MEM_SRCS_CC:%.cc=o/$(MODE)/%.com) TEST_LIBC_MEM_BINS = \ $(TEST_LIBC_MEM_COMS) \ $(TEST_LIBC_MEM_COMS:%=%.dbg) TEST_LIBC_MEM_TESTS = \ $(TEST_LIBC_MEM_SRCS_C:%.c=o/$(MODE)/%.com.ok) \ $(TEST_LIBC_MEM_SRCS_CC:%.cc=o/$(MODE)/%.com.ok) TEST_LIBC_MEM_CHECKS = \ $(TEST_LIBC_MEM_SRCS_C:%.c=o/$(MODE)/%.com.runs) \ $(TEST_LIBC_MEM_SRCS_CC:%.cc=o/$(MODE)/%.com.runs) TEST_LIBC_MEM_DIRECTDEPS = \ LIBC_CALLS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_LOG \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_RUNTIME \ LIBC_SOCK \ LIBC_STDIO \ LIBC_STR \ LIBC_STUBS \ LIBC_SYSV \ LIBC_SYSV_CALLS \ LIBC_TESTLIB \ LIBC_THREAD \ LIBC_X \ LIBC_ZIPOS \ THIRD_PARTY_DLMALLOC \ THIRD_PARTY_LIBCXX TEST_LIBC_MEM_DEPS := \ $(call uniq,$(foreach x,$(TEST_LIBC_MEM_DIRECTDEPS),$($(x)))) o/$(MODE)/test/libc/mem/mem.pkg: \ $(TEST_LIBC_MEM_OBJS) \ $(foreach x,$(TEST_LIBC_MEM_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/test/libc/mem/%.com.dbg: \ $(TEST_LIBC_MEM_DEPS) \ o/$(MODE)/test/libc/mem/%.o \ o/$(MODE)/test/libc/mem/mem.pkg \ o/$(MODE)/test/libc/mem/prog/life.elf.zip.o \ o/$(MODE)/test/libc/mem/prog/sock.elf.zip.o \ $(LIBC_TESTMAIN) \ $(CRT) \ $(APE_NO_MODIFY_SELF) @$(APELINK) o/$(MODE)/test/libc/mem/prog/sock.o: \ test/libc/mem/prog/sock.c \ libc/errno.h \ libc/sock/sock.h \ libc/intrin/bswap.h \ libc/sysv/consts/af.h \ libc/runtime/symbolic.h \ libc/sysv/consts/sock.h ################################################################################ o/$(MODE)/test/libc/mem/prog/life.com.dbg: \ $(LIBC_RUNTIME) \ o/$(MODE)/test/libc/mem/prog/life.o \ $(CRT) \ $(APE) @$(APELINK) o/$(MODE)/test/libc/mem/prog/life.elf: \ o/$(MODE)/tool/build/assimilate.com \ o/$(MODE)/test/libc/mem/prog/life.com \ $(VM) @$(COMPILE) -wACP -T$@ \ build/bootstrap/cp.com \ o/$(MODE)/test/libc/mem/prog/life.com \ o/$(MODE)/test/libc/mem/prog/life.elf @$(COMPILE) -wAASSIMILATE -T$@ \ $(VM) \ o/$(MODE)/tool/build/assimilate.com -f \ o/$(MODE)/test/libc/mem/prog/life.elf o/$(MODE)/test/libc/mem/prog/life.elf.zip.o: private \ ZIPOBJ_FLAGS += \ -B ################################################################################ o/$(MODE)/test/libc/mem/prog/sock.com.dbg: \ $(LIBC_RUNTIME) \ $(LIBC_SOCK) \ o/$(MODE)/test/libc/mem/prog/sock.o \ $(CRT) \ $(APE) @$(APELINK) o/$(MODE)/test/libc/mem/prog/sock.elf: \ o/$(MODE)/tool/build/assimilate.com \ o/$(MODE)/test/libc/mem/prog/sock.com \ $(VM) @$(COMPILE) -wACP -T$@ \ build/bootstrap/cp.com \ o/$(MODE)/test/libc/mem/prog/sock.com \ o/$(MODE)/test/libc/mem/prog/sock.elf @$(COMPILE) -wAASSIMILATE -T$@ \ $(VM) \ o/$(MODE)/tool/build/assimilate.com -f \ o/$(MODE)/test/libc/mem/prog/sock.elf o/$(MODE)/test/libc/mem/prog/sock.elf.zip.o: private \ ZIPOBJ_FLAGS += \ -B ################################################################################ $(TEST_LIBC_MEM_OBJS): private \ DEFAULT_CCFLAGS += \ -fno-builtin .PHONY: o/$(MODE)/test/libc/mem o/$(MODE)/test/libc/mem: \ $(TEST_LIBC_MEM_BINS) \ $(TEST_LIBC_MEM_CHECKS)
4,024
142
jart/cosmopolitan
false
cosmopolitan/test/libc/mem/reverse_test.c
/*-*- 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/mem/reverse.internal.h" #include "libc/dce.h" #include "libc/macros.internal.h" #include "libc/testlib/testlib.h" TEST(reverse, test) { /* this test gets DCE'd :) */ int A[3] = {1, 2, 3}; reverse(A, ARRAYLEN(A)); EXPECT_EQ(3, A[0]); EXPECT_EQ(2, A[1]); EXPECT_EQ(1, A[2]); } TEST(reverse, testEmpty) { int A[3] = {1, 2, 3}; reverse(A, 0); EXPECT_EQ(1, A[0]); EXPECT_EQ(2, A[1]); EXPECT_EQ(3, A[2]); }
2,276
40
jart/cosmopolitan
false
cosmopolitan/test/libc/mem/qsort_test.c
/*-*- 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/intrin/bits.h" #include "libc/macros.internal.h" #include "libc/mem/alg.h" #include "libc/mem/gc.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/stdio/rand.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" int CompareInt(const void *a, const void *b) { const int *x = a; const int *y = b; if (*x < *y) return -1; if (*x > *y) return +1; return 0; } int CompareLong(const void *a, const void *b) { const long *x = a; const long *y = b; if (*x < *y) return -1; if (*x > *y) return +1; return 0; } TEST(qsort, test) { const int32_t A[][2] = {{4, 'a'}, {65, 'b'}, {2, 'c'}, {-31, 'd'}, {0, 'e'}, {99, 'f'}, {2, 'g'}, {83, 'h'}, {782, 'i'}, {1, 'j'}}; const int32_t B[][2] = {{-31, 'd'}, {0, 'e'}, {1, 'j'}, {2, 'c'}, {2, 'g'}, {4, 'a'}, {65, 'b'}, {83, 'h'}, {99, 'f'}, {782, 'i'}}; int32_t(*M)[2] = malloc(sizeof(A)); memcpy(M, B, sizeof(A)); qsort(M, ARRAYLEN(A), sizeof(*M), CompareInt); EXPECT_EQ(0, memcmp(M, B, sizeof(B))); free(M); } TEST(qsort, equivalence_random) { size_t i; size_t n = 1000; long *a = _gc(malloc(n * sizeof(long))); long *b = _gc(malloc(n * sizeof(long))); long *c = _gc(malloc(n * sizeof(long))); for (i = 0; i < n; ++i) a[i] = lemur64(); memcpy(b, a, n * sizeof(long)); memcpy(c, a, n * sizeof(long)); qsort(b, n, sizeof(long), CompareLong); heapsort(c, n, sizeof(long), CompareLong); ASSERT_EQ(0, memcmp(b, c, n * sizeof(long))); memcpy(c, a, n * sizeof(long)); mergesort(c, n, sizeof(long), CompareLong); ASSERT_EQ(0, memcmp(b, c, n * sizeof(long))); memcpy(c, a, n * sizeof(long)); smoothsort(c, n, sizeof(long), CompareLong); ASSERT_EQ(0, memcmp(b, c, n * sizeof(long))); memcpy(c, a, n * sizeof(long)); _longsort(c, n); ASSERT_EQ(0, memcmp(b, c, n * sizeof(long))); } TEST(qsort, equivalence_reverse) { size_t i; size_t n = 1000; long *a = _gc(malloc(n * sizeof(long))); long *b = _gc(malloc(n * sizeof(long))); long *c = _gc(malloc(n * sizeof(long))); for (i = 0; i < n; ++i) a[n - i - 1] = i; memcpy(b, a, n * sizeof(long)); memcpy(c, a, n * sizeof(long)); qsort(b, n, sizeof(long), CompareLong); heapsort(c, n, sizeof(long), CompareLong); ASSERT_EQ(0, memcmp(b, c, n * sizeof(long))); memcpy(c, a, n * sizeof(long)); mergesort(c, n, sizeof(long), CompareLong); ASSERT_EQ(0, memcmp(b, c, n * sizeof(long))); memcpy(c, a, n * sizeof(long)); smoothsort(c, n, sizeof(long), CompareLong); ASSERT_EQ(0, memcmp(b, c, n * sizeof(long))); memcpy(c, a, n * sizeof(long)); _longsort(c, n); ASSERT_EQ(0, memcmp(b, c, n * sizeof(long))); } BENCH(qsort, bench) { size_t i; size_t n = 1000; long *p1 = _gc(malloc(n * sizeof(long))); long *p2 = _gc(malloc(n * sizeof(long))); printf("\n"); for (i = 0; i < n; ++i) p1[i] = i + ((lemur64() % 3) - 1); EZBENCH2("qsort nearly", memcpy(p2, p1, n * sizeof(long)), qsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("heapsort nearly", memcpy(p2, p1, n * sizeof(long)), heapsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("mergesort nearly", memcpy(p2, p1, n * sizeof(long)), mergesort(p2, n, sizeof(long), CompareLong)); EZBENCH2("smoothsort nearly", memcpy(p2, p1, n * sizeof(long)), smoothsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("_longsort nearly", memcpy(p2, p1, n * sizeof(long)), _longsort(p2, n)); printf("\n"); for (i = 0; i < n; ++i) p1[i] = n - i; EZBENCH2("qsort reverse", memcpy(p2, p1, n * sizeof(long)), qsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("heapsort reverse", memcpy(p2, p1, n * sizeof(long)), heapsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("mergesort reverse", memcpy(p2, p1, n * sizeof(long)), mergesort(p2, n, sizeof(long), CompareLong)); EZBENCH2("smoothsort reverse", memcpy(p2, p1, n * sizeof(long)), smoothsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("_longsort reverse", memcpy(p2, p1, n * sizeof(long)), _longsort(p2, n)); printf("\n"); rngset(p1, n * sizeof(long), 0, 0); EZBENCH2("qsort random", memcpy(p2, p1, n * sizeof(long)), qsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("heapsort random", memcpy(p2, p1, n * sizeof(long)), heapsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("mergesort random", memcpy(p2, p1, n * sizeof(long)), mergesort(p2, n, sizeof(long), CompareLong)); EZBENCH2("smoothsort random", memcpy(p2, p1, n * sizeof(long)), smoothsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("_longsort random", memcpy(p2, p1, n * sizeof(long)), _longsort(p2, n)); printf("\n"); for (i = 0; i < n / 2; ++i) { p1[i] = i; p1[n - i - 1] = i; } EZBENCH2("qsort 2n", memcpy(p2, p1, n * sizeof(long)), qsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("heapsort 2n", memcpy(p2, p1, n * sizeof(long)), heapsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("mergesort 2n", memcpy(p2, p1, n * sizeof(long)), mergesort(p2, n, sizeof(long), CompareLong)); EZBENCH2("smoothsort 2n", memcpy(p2, p1, n * sizeof(long)), smoothsort(p2, n, sizeof(long), CompareLong)); EZBENCH2("_longsort 2n", memcpy(p2, p1, n * sizeof(long)), _longsort(p2, n)); }
7,351
167
jart/cosmopolitan
false
cosmopolitan/test/libc/mem/prog/sock.c
/*-*- 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/errno.h" #include "libc/sock/sock.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/sock.h" int main(int argc, char *argv[]) { int fd; if ((fd = socket(AF_INET, SOCK_STREAM, 0)) != -1) { return fd; } else { return 128 + errno; } }
2,112
32
jart/cosmopolitan
false
cosmopolitan/test/libc/mem/prog/life.c
/*-*- 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. │ ╚─────────────────────────────────────────────────────────────────────────────*/ int main(int argc, char *argv[]) { return 42; }
1,887
23
jart/cosmopolitan
false
cosmopolitan/test/libc/zipos/open_test.c
/*-*- 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/calls/calls.h" #include "libc/mem/gc.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" #include "libc/testlib/hyperion.h" #include "libc/testlib/testlib.h" #include "libc/thread/spawn.h" STATIC_YOINK("zip_uri_support"); STATIC_YOINK("libc/testlib/hyperion.txt"); STATIC_YOINK("inflate"); STATIC_YOINK("inflateInit2"); STATIC_YOINK("inflateEnd"); int Worker(void *arg, int tid) { int i, fd; char *data; for (i = 0; i < 20; ++i) { ASSERT_NE(-1, (fd = open("/zip/libc/testlib/hyperion.txt", O_RDONLY))); data = malloc(kHyperionSize); ASSERT_EQ(kHyperionSize, read(fd, data, kHyperionSize)); ASSERT_EQ(0, memcmp(data, kHyperion, kHyperionSize)); ASSERT_SYS(0, 0, close(fd)); free(data); } return 0; } TEST(zipos, test) { int i, n = 16; struct spawn *t = _gc(malloc(sizeof(struct spawn) * n)); for (i = 0; i < n; ++i) ASSERT_SYS(0, 0, _spawn(Worker, 0, t + i)); for (i = 0; i < n; ++i) EXPECT_SYS(0, 0, _join(t + i)); __print_maps(); }
2,900
56
jart/cosmopolitan
false
cosmopolitan/test/libc/zipos/test.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ PKGS += TEST_LIBC_ZIPOS TEST_LIBC_ZIPOS_SRCS := $(wildcard test/libc/zipos/*.c) TEST_LIBC_ZIPOS_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_ZIPOS_SRCS)) TEST_LIBC_ZIPOS_OBJS = \ $(TEST_LIBC_ZIPOS_SRCS:%.c=o/$(MODE)/%.o) TEST_LIBC_ZIPOS_COMS = \ $(TEST_LIBC_ZIPOS_SRCS:%.c=o/$(MODE)/%.com) TEST_LIBC_ZIPOS_BINS = \ $(TEST_LIBC_ZIPOS_COMS) \ $(TEST_LIBC_ZIPOS_COMS:%=%.dbg) TEST_LIBC_ZIPOS_TESTS = \ $(TEST_LIBC_ZIPOS_SRCS_TEST:%.c=o/$(MODE)/%.com.ok) TEST_LIBC_ZIPOS_CHECKS = \ $(TEST_LIBC_ZIPOS_SRCS_TEST:%.c=o/$(MODE)/%.com.runs) TEST_LIBC_ZIPOS_DIRECTDEPS = \ LIBC_CALLS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_RUNTIME \ LIBC_STR \ LIBC_STUBS \ LIBC_THREAD \ LIBC_SYSV \ LIBC_ZIPOS \ LIBC_TIME \ LIBC_TESTLIB \ THIRD_PARTY_ZLIB TEST_LIBC_ZIPOS_DEPS := \ $(call uniq,$(foreach x,$(TEST_LIBC_ZIPOS_DIRECTDEPS),$($(x)))) o/$(MODE)/test/libc/zipos/zipos.pkg: \ $(TEST_LIBC_ZIPOS_OBJS) \ $(foreach x,$(TEST_LIBC_ZIPOS_DIRECTDEPS),$($(x)_A).pkg) #o/$(MODE)/libc/testlib/hyperion.txt.zip.o: private ZIPOBJ_FLAGS += -0 o/$(MODE)/test/libc/zipos/%.com.dbg: \ $(TEST_LIBC_ZIPOS_DEPS) \ o/$(MODE)/test/libc/zipos/%.o \ o/$(MODE)/test/libc/zipos/zipos.pkg \ o/$(MODE)/libc/testlib/hyperion.txt.zip.o \ $(LIBC_TESTMAIN) \ $(CRT) \ $(APE_NO_MODIFY_SELF) @$(APELINK) .PHONY: o/$(MODE)/test/libc/zipos o/$(MODE)/test/libc/zipos: \ $(TEST_LIBC_ZIPOS_BINS) \ $(TEST_LIBC_ZIPOS_CHECKS)
1,776
64
jart/cosmopolitan
false
cosmopolitan/test/libc/x/xstrcat_test.c
/*-*- 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/mem/gc.internal.h" #include "libc/mem/mem.h" #include "libc/str/str.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" TEST(xstrcat, test) { EXPECT_STREQ("hi", gc(xstrcat("hi"))); EXPECT_STREQ("hithere", gc(xstrcat("hi", "there"))); EXPECT_STREQ("einszweidrei", gc(xstrcat("eins", "zwei", "drei"))); } TEST(xstrcat, pointerAbuse) { EXPECT_STREQ("hi there", gc(xstrcat("hi", ' ', "there"))); EXPECT_STREQ("hi there\n", gc(xstrcat("hi", ' ', "there", '\n'))); } int hard_static(void) { char *b, *p; p = b = malloc(16); p = stpcpy(p, "eins"); p = stpcpy(p, "zwei"); p = stpcpy(p, "drei"); free(b); return (intptr_t)b; } int hard_dynamic(void) { char *b, *p; p = b = malloc(16); p = stpcpy(p, VEIL("r", "eins")); p = stpcpy(p, VEIL("r", "zwei")); p = stpcpy(p, VEIL("r", "drei")); free(b); return (intptr_t)b; } BENCH(xstrcat, bench) { EZBENCH2("hard_static", donothing, EXPROPRIATE(hard_static())); EZBENCH2("hard_dynamic", donothing, EXPROPRIATE(hard_dynamic())); EZBENCH2("xstrcat", donothing, free(xstrcat("eins", "zwei", "drei"))); EZBENCH2("xasprintf", donothing, free(xasprintf("%s%s%s", "eins", "zwei", "drei"))); EZBENCH2("xstrcat2", donothing, free(xstrcat("einseinseins", "zweizweizwei", "dreidreidrei"))); EZBENCH2("xasprintf2", donothing, free(xasprintf("%s%s%s", "einseinseins", "zweizweizwei", "dreidreidrei"))); }
3,363
70
jart/cosmopolitan
false
cosmopolitan/test/libc/x/xfixpath_test.c
/*-*- 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/runtime/runtime.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(xfixpath, test) { setenv("PATH", "C:\\bin;C:\\usr\\bin;C:\\usr\\local\\bin", true); xfixpath(); ASSERT_STREQ("/C/bin:/C/usr/bin:/C/usr/local/bin", getenv("PATH")); }
2,104
28
jart/cosmopolitan
false
cosmopolitan/test/libc/x/utf16to32_test.c
/*-*- 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 2021 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/mem/mem.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/hyperion.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(utf16to32, testChinese) { EXPECT_STREQ( L"天地玄黄 宇宙洪荒 日月盈昃 辰宿列张 寒来暑往 秋收冬藏" L"闰馀成岁 律吕调阳 云腾致雨 露结为霜 金生丽水 玉出昆冈" L"剑号巨阙 珠称夜光 果珍李柰 菜重芥姜 海咸河淡 鳞潜羽翔" L"龙师火帝 鸟官人皇 始制文字 乃服衣裳 推位让国 有虞陶唐", gc(utf16to32(u"天地玄黄 宇宙洪荒 日月盈昃 辰宿列张 寒来暑往 秋收冬藏" u"闰馀成岁 律吕调阳 云腾致雨 露结为霜 金生丽水 玉出昆冈" u"剑号巨阙 珠称夜光 果珍李柰 菜重芥姜 海咸河淡 鳞潜羽翔" u"龙师火帝 鸟官人皇 始制文字 乃服衣裳 推位让国 有虞陶唐", -1, 0))); } TEST(utf16to32, testAstralPlanesGothic) { EXPECT_STREQ(L"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹𐌺𐌻𐌼𐌽𐌾𐌿𐍀𐍁𐍂𐍃𐍄𐍅𐍆𐍇𐍈𐍉𐍊" L"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹𐌺𐌻𐌼𐌽𐌾𐌿𐍀𐍁𐍂𐍃𐍄𐍅𐍆𐍇𐍈𐍉𐍊" L"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹𐌺𐌻𐌼𐌽𐌾𐌿𐍀𐍁𐍂𐍃𐍄𐍅𐍆𐍇𐍈𐍉𐍊" L"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹𐌺𐌻𐌼𐌽𐌾𐌿𐍀𐍁𐍂𐍃𐍄𐍅𐍆𐍇𐍈𐍉𐍊", gc(utf16to32(u"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹𐌺𐌻𐌼𐌽𐌾𐌿𐍀𐍁𐍂𐍃𐍄𐍅𐍆𐍇𐍈𐍉𐍊" u"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹𐌺𐌻𐌼𐌽𐌾𐌿𐍀𐍁𐍂𐍃𐍄𐍅𐍆𐍇𐍈𐍉𐍊" u"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹𐌺𐌻𐌼𐌽𐌾𐌿𐍀𐍁𐍂𐍃𐍄𐍅𐍆𐍇𐍈𐍉𐍊" u"𐌰𐌱𐌲𐌳𐌴𐌵𐌶𐌷𐌸𐌹𐌺𐌻𐌼𐌽𐌾𐌿𐍀𐍁𐍂𐍃𐍄𐍅𐍆𐍇𐍈𐍉𐍊", -1, 0))); } BENCH(utf16to8, bench) { size_t n; char16_t *h; h = gc(utf8to16(kHyperion, kHyperionSize, &n)); EZBENCH2("utf16to32", donothing, free(utf16to32(h, n, 0))); }
4,170
57
jart/cosmopolitan
false
cosmopolitan/test/libc/x/xasprintf_test.c
/*-*- 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/mem/gc.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" TEST(xasprintf, test) { EXPECT_STREQ("hi.there", _gc(xasprintf("%s.%s", "hi", "there"))); }
2,020
26
jart/cosmopolitan
false
cosmopolitan/test/libc/x/xjoinpaths_test.c
/*-*- 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/mem/gc.internal.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(xjoinpaths, test) { EXPECT_STREQ("", gc(xjoinpaths("", ""))); EXPECT_STREQ("b", gc(xjoinpaths("", "b"))); EXPECT_STREQ("a/b", gc(xjoinpaths("a", "b"))); EXPECT_STREQ("a/b", gc(xjoinpaths("a/", "b"))); EXPECT_STREQ("a/b/", gc(xjoinpaths("a", "b/"))); EXPECT_STREQ("/b", gc(xjoinpaths("a", "/b"))); EXPECT_STREQ("b", gc(xjoinpaths(".", "b"))); }
2,290
32
jart/cosmopolitan
false
cosmopolitan/test/libc/x/utf16to8_test.c
/*-*- 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 2021 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/mem/mem.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/hyperion.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(utf16to8, test) { EXPECT_STREQ("hello☻♥", gc(utf16to8(u"hello☻♥", -1, 0))); EXPECT_STREQ("hello☻♥hello☻♥h", gc(utf16to8(u"hello☻♥hello☻♥h", -1, 0))); EXPECT_STREQ("hello☻♥hello☻♥hi", gc(utf16to8(u"hello☻♥hello☻♥hi", -1, 0))); EXPECT_STREQ("hello☻♥hello☻♥hello☻♥hello☻♥hello☻♥", gc(utf16to8(u"hello☻♥hello☻♥hello☻♥hello☻♥hello☻♥", -1, 0))); EXPECT_STREQ("hello--hello--h", gc(utf16to8(u"hello--hello--h", -1, 0))); EXPECT_STREQ("hello--hello--hi", gc(utf16to8(u"hello--hello--hi", -1, 0))); EXPECT_STREQ("hello--hello--hello--hello--hello--", gc(utf16to8(u"hello--hello--hello--hello--hello--", -1, 0))); } BENCH(utf16to8, bench) { size_t n; char16_t *h; h = gc(utf8to16(kHyperion, kHyperionSize, &n)); EZBENCH2("utf16to8", donothing, free(utf16to8(h, n, 0))); }
2,922
44
jart/cosmopolitan
false
cosmopolitan/test/libc/x/xslurp_test.c
/*-*- 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/mem/gc.internal.h" #include "libc/str/str.h" #include "libc/testlib/hyperion.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" char testlib_enable_tmp_setup_teardown; TEST(xslurp, testEmptyWithNulTerminatedStringBehavior) { size_t got; const char *data; ASSERT_NE(-1, xbarf("xslurp.txt", "", -1)); ASSERT_NE(NULL, (data = gc(xslurp("xslurp.txt", &got)))); ASSERT_EQ(0, got); ASSERT_STREQ("", data); } TEST(xslurp, testHyperion) { size_t got; const char *data; ASSERT_NE(-1, xbarf("xslurp.txt", kHyperion, kHyperionSize)); ASSERT_NE(NULL, (data = gc(xslurp("xslurp.txt", &got)))); ASSERT_EQ(kHyperionSize, got); ASSERT_EQ(0, memcmp(data, kHyperion, kHyperionSize)); }
2,554
44
jart/cosmopolitan
false
cosmopolitan/test/libc/x/utf8to16_test.c
/*-*- 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 2021 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/mem/mem.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/hyperion.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(utf8to16, test) { EXPECT_STREQ(u"hello☻♥", gc(utf8to16("hello☻♥", -1, 0))); EXPECT_STREQ(u"hello☻♥hello☻♥h", gc(utf8to16("hello☻♥hello☻♥h", -1, 0))); EXPECT_STREQ(u"hello☻♥hello☻♥hi", gc(utf8to16("hello☻♥hello☻♥hi", -1, 0))); EXPECT_STREQ(u"hello☻♥hello☻♥hello☻♥hello☻♥hello☻♥", gc(utf8to16("hello☻♥hello☻♥hello☻♥hello☻♥hello☻♥", -1, 0))); EXPECT_STREQ(u"hello--hello--h", gc(utf8to16("hello--hello--h", -1, 0))); EXPECT_STREQ(u"hello--hello--hi", gc(utf8to16("hello--hello--hi", -1, 0))); EXPECT_STREQ(u"hello--hello--hello--hello--hello--", gc(utf8to16("hello--hello--hello--hello--hello--", -1, 0))); } BENCH(utf8to16, bench) { EZBENCH2("utf8to16", donothing, free(utf8to16(kHyperion, kHyperionSize, 0))); }
2,865
41
jart/cosmopolitan
false
cosmopolitan/test/libc/x/utf8to32_test.c
/*-*- 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 2021 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/mem/mem.h" #include "libc/mem/shuffle.internal.h" #include "libc/mem/gc.internal.h" #include "libc/stdio/rand.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/hyperion.h" #include "libc/testlib/testlib.h" #include "libc/testlib/viewables.h" #include "libc/x/x.h" TEST(utf8to32, test) { EXPECT_STREQ(L"", gc(utf8to32(0, 0, 0))); EXPECT_STREQ(L"", gc(utf8to32("", -1, 0))); EXPECT_STREQ(L"hello", gc(utf8to32("hello", -1, 0))); } TEST(utf8to32, poke) { wchar_t *p = gc(utf8to32("hi", 2, 0)); ASSERT_EQ('h', p[0]); ASSERT_EQ('i', p[1]); ASSERT_EQ(0, p[2]); } TEST(utf32to8, poke) { char *p = gc(utf32to8(L"hi", 2, 0)); ASSERT_EQ('h', p[0]); ASSERT_EQ('i', p[1]); ASSERT_EQ(0, p[2]); } TEST(utf8to32, testLargeAscii) { EXPECT_STREQ(L"hellohellohelloz", gc(utf8to32("hellohellohelloz", -1, 0))); EXPECT_STREQ(L"hellohellohellozhellohellohelloz", gc(utf8to32("hellohellohellozhellohellohelloz", -1, 0))); } TEST(utf32to8, testLargeAscii) { EXPECT_STREQ("hellohellohelloz", gc(utf32to8(L"hellohellohelloz", -1, 0))); EXPECT_STREQ("hellohellohellozhellohellohelloz", gc(utf32to8(L"hellohellohellozhellohellohelloz", -1, 0))); } TEST(utf8to32, testLargeThompsonPikeEncoded) { EXPECT_STREQ(L"hellohellohello𝑧hellohellohelloz", gc(utf8to32("hellohellohello𝑧hellohellohelloz", -1, 0))); EXPECT_STREQ(L"hellohellohelloh𝑧ellohellohelloz", gc(utf8to32("hellohellohelloh𝑧ellohellohelloz", -1, 0))); EXPECT_STREQ( L"𝑕𝑒𝑙𝑙𝑜𝑕𝑒𝑙𝑙𝑜𝑕𝑒𝑙𝑙𝑜𝑧", gc(utf8to32( "𝑕𝑒𝑙𝑙𝑜𝑕𝑒𝑙𝑙𝑜𝑕𝑒𝑙𝑙𝑜𝑧", -1, 0))); } TEST(utf32to8, testLargeThompsonPikeEncoded) { EXPECT_STREQ("hellohellohello𝑧hellohellohelloz", gc(utf32to8(L"hellohellohello𝑧hellohellohelloz", -1, 0))); EXPECT_STREQ("hellohellohelloh𝑧ellohellohelloz", gc(utf32to8(L"hellohellohelloh𝑧ellohellohelloz", -1, 0))); EXPECT_STREQ( "𝑕𝑒𝑙𝑙𝑜𝑕𝑒𝑙𝑙𝑜𝑕𝑒𝑙𝑙𝑜𝑧", gc(utf32to8( L"𝑕𝑒𝑙𝑙𝑜𝑕𝑒𝑙𝑙𝑜𝑕𝑒𝑙𝑙𝑜𝑧", -1, 0))); } char *GenerateBranchyUtf8Text(size_t *out_n) { char *p; size_t n; wchar_t *q = gc(utf8to32(kViewables, kViewablesSize, &n)); shuffle(lemur64, q, n); p = utf32to8(q, n, &n); if (out_n) *out_n = n; return p; } /* * utf8to32 l: 5,806c 1,875ns m: 5,863c 1,894ns * utf32to8 l: 104,671c 33,808ns m: 103,803c 33,528ns * utf8to32 [branchy] l: 746,846c 241,227ns m: 747,312c 241,377ns */ BENCH(utf8to32, bench) { EZBENCH2("utf8to32", donothing, free(utf8to32(kHyperion, kHyperionSize, 0))); size_t n; wchar_t *h = gc(utf8to32(kHyperion, kHyperionSize, &n)); EZBENCH2("utf32to8", donothing, free(utf32to8(h, n, 0))); char *p = gc(GenerateBranchyUtf8Text(&n)); EZBENCH2("utf8to32 [branchy]", donothing, free(utf8to32(p, n, 0))); }
4,914
108
jart/cosmopolitan
false
cosmopolitan/test/libc/x/test.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ PKGS += TEST_LIBC_X TEST_LIBC_X_SRCS := $(wildcard test/libc/x/*.c) TEST_LIBC_X_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_X_SRCS)) TEST_LIBC_X_OBJS = \ $(TEST_LIBC_X_SRCS:%.c=o/$(MODE)/%.o) TEST_LIBC_X_COMS = \ $(TEST_LIBC_X_SRCS:%.c=o/$(MODE)/%.com) TEST_LIBC_X_BINS = \ $(TEST_LIBC_X_COMS) \ $(TEST_LIBC_X_COMS:%=%.dbg) TEST_LIBC_X_TESTS = \ $(TEST_LIBC_X_SRCS_TEST:%.c=o/$(MODE)/%.com.ok) TEST_LIBC_X_CHECKS = \ $(TEST_LIBC_X_SRCS_TEST:%.c=o/$(MODE)/%.com.runs) TEST_LIBC_X_DIRECTDEPS = \ LIBC_CALLS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_RUNTIME \ LIBC_STDIO \ LIBC_STR \ LIBC_SOCK \ LIBC_STUBS \ LIBC_SYSV \ LIBC_THREAD \ LIBC_TESTLIB \ LIBC_X \ THIRD_PARTY_GDTOA TEST_LIBC_X_DEPS := \ $(call uniq,$(foreach x,$(TEST_LIBC_X_DIRECTDEPS),$($(x)))) o/$(MODE)/test/libc/x/x.pkg: \ $(TEST_LIBC_X_OBJS) \ $(foreach x,$(TEST_LIBC_X_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/test/libc/x/%.com.dbg: \ $(TEST_LIBC_X_DEPS) \ o/$(MODE)/test/libc/x/%.o \ o/$(MODE)/test/libc/x/x.pkg \ $(LIBC_TESTMAIN) \ $(CRT) \ $(APE_NO_MODIFY_SELF) @$(APELINK) .PHONY: o/$(MODE)/test/libc/x o/$(MODE)/test/libc/x: \ $(TEST_LIBC_X_BINS) \ $(TEST_LIBC_X_CHECKS)
1,487
62
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/socket_test.c
/*-*- 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/calls/calls.h" #include "libc/sock/sock.h" #include "libc/sock/struct/sockaddr.h" #include "libc/sock/struct/sockaddr6.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/ipproto.h" #include "libc/sysv/consts/limits.h" #include "libc/sysv/consts/sock.h" #include "libc/testlib/testlib.h" TEST(ipv4, test) { int ws, pid; char buf[16] = {0}; int64_t inoffset; uint32_t addrsize = sizeof(struct sockaddr_in); struct sockaddr_in addr = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(0x7f000001), }; ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); ASSERT_SYS(0, 0, bind(3, (struct sockaddr *)&addr, sizeof(addr))); ASSERT_SYS(0, 0, getsockname(3, (struct sockaddr *)&addr, &addrsize)); ASSERT_SYS(0, 0, listen(3, SOMAXCONN)); ASSERT_NE(-1, (pid = fork())); if (!pid) { ASSERT_SYS(0, 4, accept(3, (struct sockaddr *)&addr, &addrsize)); ASSERT_SYS(0, 5, send(4, "hello", 5, 0)); ASSERT_SYS(0, 0, close(4)); ASSERT_SYS(0, 0, close(3)); _Exit(0); } EXPECT_SYS(0, 0, close(3)); EXPECT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); EXPECT_SYS(0, 0, connect(3, (struct sockaddr *)&addr, sizeof(addr))); EXPECT_SYS(0, 5, read(3, buf, 16)); EXPECT_STREQ("hello", buf); EXPECT_SYS(0, 0, close(3)); EXPECT_NE(-1, wait(&ws)); ASSERT_TRUE(WIFEXITED(ws)); ASSERT_EQ(0, WEXITSTATUS(ws)); } TEST(ipv6, test) { int ws, pid; char buf[16] = {0}; int64_t inoffset; uint32_t addrsize = sizeof(struct sockaddr_in6); struct sockaddr_in6 addr = { .sin6_family = AF_INET6, .sin6_addr.s6_addr[15] = 1, }; ASSERT_SYS(0, 3, socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)); ASSERT_SYS(0, 0, bind(3, (struct sockaddr *)&addr, sizeof(addr))); ASSERT_SYS(0, 0, getsockname(3, (struct sockaddr *)&addr, &addrsize)); ASSERT_EQ(AF_INET6, addr.sin6_family); ASSERT_NE(0, addr.sin6_port); ASSERT_SYS(0, 0, listen(3, SOMAXCONN)); ASSERT_NE(-1, (pid = fork())); if (!pid) { ASSERT_SYS(0, 4, accept(3, (struct sockaddr *)&addr, &addrsize)); ASSERT_SYS(0, 5, send(4, "hello", 5, 0)); ASSERT_SYS(0, 0, close(4)); ASSERT_SYS(0, 0, close(3)); _Exit(0); } EXPECT_SYS(0, 0, close(3)); EXPECT_SYS(0, 3, socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)); EXPECT_SYS(0, 0, connect(3, (struct sockaddr *)&addr, sizeof(addr))); EXPECT_SYS(0, 5, read(3, buf, 16)); EXPECT_STREQ("hello", buf); EXPECT_SYS(0, 0, close(3)); EXPECT_NE(-1, wait(&ws)); ASSERT_TRUE(WIFEXITED(ws)); ASSERT_EQ(0, WEXITSTATUS(ws)); }
4,385
94
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/setsockopt_test.c
/*-*- 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/calls/calls.h" #include "libc/calls/struct/timeval.h" #include "libc/errno.h" #include "libc/sock/sock.h" #include "libc/sock/struct/sockaddr.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/ipproto.h" #include "libc/sysv/consts/so.h" #include "libc/sysv/consts/sock.h" #include "libc/sysv/consts/sol.h" #include "libc/testlib/testlib.h" void SetUpOnce(void) { ASSERT_SYS(0, 0, pledge("stdio inet", 0)); } TEST(setsockopt, SO_RCVTIMEO) { char buf[32]; struct timeval tv = {0, 10000}; struct sockaddr_in sa = {AF_INET, 0, {htonl(0x7f000001)}}; EXPECT_SYS(0, 3, socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)); EXPECT_SYS(0, 0, setsockopt(3, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv))); EXPECT_SYS(0, 0, bind(3, (struct sockaddr *)&sa, sizeof(struct sockaddr_in))); EXPECT_SYS(EAGAIN, -1, read(3, buf, sizeof(buf))); EXPECT_SYS(0, 0, close(3)); }
2,723
45
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/inet_pton_test.c
/*-*- 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/intrin/safemacros.internal.h" #include "libc/sock/sock.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/inaddr.h" #include "libc/testlib/testlib.h" TEST(inet_pton, testLocalhost) { uint8_t addr[4] = {255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &addr)); EXPECT_EQ(127, addr[0]); EXPECT_EQ(0, addr[1]); EXPECT_EQ(0, addr[2]); EXPECT_EQ(1, addr[3]); uint8_t addrv6[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "::1", &addrv6)); EXPECT_EQ(0, addrv6[0]); EXPECT_EQ(0, addrv6[1]); EXPECT_EQ(0, addrv6[2]); EXPECT_EQ(0, addrv6[3]); EXPECT_EQ(0, addrv6[4]); EXPECT_EQ(0, addrv6[5]); EXPECT_EQ(0, addrv6[6]); EXPECT_EQ(0, addrv6[7]); EXPECT_EQ(0, addrv6[8]); EXPECT_EQ(0, addrv6[9]); EXPECT_EQ(0, addrv6[10]); EXPECT_EQ(0, addrv6[11]); EXPECT_EQ(0, addrv6[12]); EXPECT_EQ(0, addrv6[13]); EXPECT_EQ(0, addrv6[14]); EXPECT_EQ(1, addrv6[15]); uint8_t addrv6_second[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "0:0:0:0:0:0:0:1", &addrv6_second)); EXPECT_EQ(0, addrv6_second[0]); EXPECT_EQ(0, addrv6_second[1]); EXPECT_EQ(0, addrv6_second[2]); EXPECT_EQ(0, addrv6_second[3]); EXPECT_EQ(0, addrv6_second[4]); EXPECT_EQ(0, addrv6_second[5]); EXPECT_EQ(0, addrv6_second[6]); EXPECT_EQ(0, addrv6_second[7]); EXPECT_EQ(0, addrv6_second[8]); EXPECT_EQ(0, addrv6_second[9]); EXPECT_EQ(0, addrv6_second[10]); EXPECT_EQ(0, addrv6_second[11]); EXPECT_EQ(0, addrv6_second[12]); EXPECT_EQ(0, addrv6_second[13]); EXPECT_EQ(0, addrv6_second[14]); EXPECT_EQ(1, addrv6_second[15]); } TEST(inet_pton, testAny) { uint8_t addr[4] = {255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET, "0.0.0.0", &addr)); EXPECT_EQ(0, addr[0]); EXPECT_EQ(0, addr[1]); EXPECT_EQ(0, addr[2]); EXPECT_EQ(0, addr[3]); uint8_t addrv6[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "::", &addrv6)); EXPECT_EQ(0, addrv6[0]); EXPECT_EQ(0, addrv6[1]); EXPECT_EQ(0, addrv6[2]); EXPECT_EQ(0, addrv6[3]); EXPECT_EQ(0, addrv6[4]); EXPECT_EQ(0, addrv6[5]); EXPECT_EQ(0, addrv6[6]); EXPECT_EQ(0, addrv6[7]); EXPECT_EQ(0, addrv6[8]); EXPECT_EQ(0, addrv6[9]); EXPECT_EQ(0, addrv6[10]); EXPECT_EQ(0, addrv6[11]); EXPECT_EQ(0, addrv6[12]); EXPECT_EQ(0, addrv6[13]); EXPECT_EQ(0, addrv6[14]); EXPECT_EQ(0, addrv6[15]); uint8_t addrv6_second[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "0:0:0:0:0:0:0:0", &addrv6_second)); EXPECT_EQ(0, addrv6_second[0]); EXPECT_EQ(0, addrv6_second[1]); EXPECT_EQ(0, addrv6_second[2]); EXPECT_EQ(0, addrv6_second[3]); EXPECT_EQ(0, addrv6_second[4]); EXPECT_EQ(0, addrv6_second[5]); EXPECT_EQ(0, addrv6_second[6]); EXPECT_EQ(0, addrv6_second[7]); EXPECT_EQ(0, addrv6_second[8]); EXPECT_EQ(0, addrv6_second[9]); EXPECT_EQ(0, addrv6_second[10]); EXPECT_EQ(0, addrv6_second[11]); EXPECT_EQ(0, addrv6_second[12]); EXPECT_EQ(0, addrv6_second[13]); EXPECT_EQ(0, addrv6_second[14]); EXPECT_EQ(0, addrv6_second[15]); } TEST(inet_pton, testShortAddress_doesntFillFullValue) { uint8_t addr[4] = {255, 255, 255, 255}; EXPECT_EQ(0, inet_pton(AF_INET, "127.0.0", &addr)); EXPECT_EQ(127, addr[0]); EXPECT_EQ(0, addr[1]); EXPECT_EQ(0, addr[2]); EXPECT_EQ(255, addr[3]); uint8_t addrv6[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:0", &addrv6)); } TEST(inet_pton, testOverflow_stopsParsing) { uint8_t addr[4] = {255, 255, 255, 255}; EXPECT_EQ(0, inet_pton(AF_INET, "0.300.0", &addr)); EXPECT_EQ(0, addr[0]); EXPECT_EQ(255, addr[1]); EXPECT_EQ(255, addr[2]); EXPECT_EQ(255, addr[3]); } TEST(inet_pton, testBadChar_stopsParsing) { uint8_t addr[4] = {255, 255, 255, 255}; EXPECT_EQ(0, inet_pton(AF_INET, "127-.0.0", &addr)); EXPECT_EQ(127, addr[0]); EXPECT_EQ(255, addr[1]); EXPECT_EQ(255, addr[2]); EXPECT_EQ(255, addr[3]); uint8_t addrv6[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0-0:0:0:0", &addrv6)); } TEST(inet_pton, testBadFamily_returnsNegAndChangesNothing) { uint8_t addr[4] = {255, 255, 255, 255}; EXPECT_EQ(-1, inet_pton(666, "127.0.0.1", &addr)); EXPECT_EQ(255, addr[0]); EXPECT_EQ(255, addr[1]); EXPECT_EQ(255, addr[2]); EXPECT_EQ(255, addr[3]); } TEST(inet_pton, testIPv6BadColon_fails) { uint8_t addrv6[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:0:0:", &addrv6)); uint8_t addrv6_second[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(0, inet_pton(AF_INET6, "0:0:0:0:0:0:0:0:0", &addrv6_second)); } TEST(inet_pton, testMappedIPv4inIPv6) { uint8_t addrv6[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "::FFFF:204.152.189.116", &addrv6)); EXPECT_EQ(0, addrv6[0]); EXPECT_EQ(0, addrv6[1]); EXPECT_EQ(0, addrv6[2]); EXPECT_EQ(0, addrv6[3]); EXPECT_EQ(0, addrv6[4]); EXPECT_EQ(0, addrv6[5]); EXPECT_EQ(0, addrv6[6]); EXPECT_EQ(0, addrv6[7]); EXPECT_EQ(0, addrv6[8]); EXPECT_EQ(0, addrv6[9]); EXPECT_EQ(0xFF, addrv6[10]); EXPECT_EQ(0xFF, addrv6[11]); EXPECT_EQ(0xCC, addrv6[12]); EXPECT_EQ(0x98, addrv6[13]); EXPECT_EQ(0xBD, addrv6[14]); EXPECT_EQ(0x74, addrv6[15]); uint8_t addrv6_second[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "1:2:3:4::5.6.7.8", &addrv6_second)); EXPECT_EQ(0, addrv6_second[0]); EXPECT_EQ(1, addrv6_second[1]); EXPECT_EQ(0, addrv6_second[2]); EXPECT_EQ(2, addrv6_second[3]); EXPECT_EQ(0, addrv6_second[4]); EXPECT_EQ(3, addrv6_second[5]); EXPECT_EQ(0, addrv6_second[6]); EXPECT_EQ(4, addrv6_second[7]); EXPECT_EQ(0, addrv6_second[8]); EXPECT_EQ(0, addrv6_second[9]); EXPECT_EQ(0, addrv6_second[10]); EXPECT_EQ(0, addrv6_second[11]); EXPECT_EQ(0x5, addrv6_second[12]); EXPECT_EQ(0x6, addrv6_second[13]); EXPECT_EQ(0x7, addrv6_second[14]); EXPECT_EQ(0x8, addrv6_second[15]); uint8_t addrv6_third[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "1:2:3:4::56.6.7.8", &addrv6_third)); EXPECT_EQ(0, addrv6_third[0]); EXPECT_EQ(1, addrv6_third[1]); EXPECT_EQ(0, addrv6_third[2]); EXPECT_EQ(2, addrv6_third[3]); EXPECT_EQ(0, addrv6_third[4]); EXPECT_EQ(3, addrv6_third[5]); EXPECT_EQ(0, addrv6_third[6]); EXPECT_EQ(4, addrv6_third[7]); EXPECT_EQ(0, addrv6_third[8]); EXPECT_EQ(0, addrv6_third[9]); EXPECT_EQ(0, addrv6_third[10]); EXPECT_EQ(0, addrv6_third[11]); EXPECT_EQ(56, addrv6_third[12]); EXPECT_EQ(0x6, addrv6_third[13]); EXPECT_EQ(0x7, addrv6_third[14]); EXPECT_EQ(0x8, addrv6_third[15]); uint8_t addrv6_forth[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "1:2:3:4:5:6:7.8.9.10", &addrv6_forth)); EXPECT_EQ(0, addrv6_forth[0]); EXPECT_EQ(1, addrv6_forth[1]); EXPECT_EQ(0, addrv6_forth[2]); EXPECT_EQ(2, addrv6_forth[3]); EXPECT_EQ(0, addrv6_forth[4]); EXPECT_EQ(3, addrv6_forth[5]); EXPECT_EQ(0, addrv6_forth[6]); EXPECT_EQ(4, addrv6_forth[7]); EXPECT_EQ(0, addrv6_forth[8]); EXPECT_EQ(5, addrv6_forth[9]); EXPECT_EQ(0, addrv6_forth[10]); EXPECT_EQ(6, addrv6_forth[11]); EXPECT_EQ(7, addrv6_forth[12]); EXPECT_EQ(8, addrv6_forth[13]); EXPECT_EQ(9, addrv6_forth[14]); EXPECT_EQ(10, addrv6_forth[15]); } TEST(inet_pton, testVariousIPv6) { uint8_t addrv6[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "1:0:0:0:0:0:0:8", &addrv6)); EXPECT_EQ(0, addrv6[0]); EXPECT_EQ(1, addrv6[1]); EXPECT_EQ(0, addrv6[2]); EXPECT_EQ(0, addrv6[3]); EXPECT_EQ(0, addrv6[4]); EXPECT_EQ(0, addrv6[5]); EXPECT_EQ(0, addrv6[6]); EXPECT_EQ(0, addrv6[7]); EXPECT_EQ(0, addrv6[8]); EXPECT_EQ(0, addrv6[9]); EXPECT_EQ(0, addrv6[10]); EXPECT_EQ(0, addrv6[11]); EXPECT_EQ(0, addrv6[12]); EXPECT_EQ(0, addrv6[13]); EXPECT_EQ(0, addrv6[14]); EXPECT_EQ(8, addrv6[15]); uint8_t addrv6_second[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "1::8", &addrv6_second)); EXPECT_EQ(0, addrv6_second[0]); EXPECT_EQ(1, addrv6_second[1]); EXPECT_EQ(0, addrv6_second[2]); EXPECT_EQ(0, addrv6_second[3]); EXPECT_EQ(0, addrv6_second[4]); EXPECT_EQ(0, addrv6_second[5]); EXPECT_EQ(0, addrv6_second[6]); EXPECT_EQ(0, addrv6_second[7]); EXPECT_EQ(0, addrv6_second[8]); EXPECT_EQ(0, addrv6_second[9]); EXPECT_EQ(0, addrv6_second[10]); EXPECT_EQ(0, addrv6_second[11]); EXPECT_EQ(0, addrv6_second[12]); EXPECT_EQ(0, addrv6_second[13]); EXPECT_EQ(0, addrv6_second[14]); EXPECT_EQ(8, addrv6_second[15]); uint8_t addrv6_third[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "1::1:2:3:4", &addrv6_third)); EXPECT_EQ(0, addrv6_third[0]); EXPECT_EQ(1, addrv6_third[1]); EXPECT_EQ(0, addrv6_third[2]); EXPECT_EQ(0, addrv6_third[3]); EXPECT_EQ(0, addrv6_third[4]); EXPECT_EQ(0, addrv6_third[5]); EXPECT_EQ(0, addrv6_third[6]); EXPECT_EQ(0, addrv6_third[7]); EXPECT_EQ(0, addrv6_third[8]); EXPECT_EQ(1, addrv6_third[9]); EXPECT_EQ(0, addrv6_third[10]); EXPECT_EQ(2, addrv6_third[11]); EXPECT_EQ(0, addrv6_third[12]); EXPECT_EQ(3, addrv6_third[13]); EXPECT_EQ(0, addrv6_third[14]); EXPECT_EQ(4, addrv6_third[15]); uint8_t addrv6_forth[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "1:2:3:4::1", &addrv6_forth)); EXPECT_EQ(0, addrv6_forth[0]); EXPECT_EQ(1, addrv6_forth[1]); EXPECT_EQ(0, addrv6_forth[2]); EXPECT_EQ(2, addrv6_forth[3]); EXPECT_EQ(0, addrv6_forth[4]); EXPECT_EQ(3, addrv6_forth[5]); EXPECT_EQ(0, addrv6_forth[6]); EXPECT_EQ(4, addrv6_forth[7]); EXPECT_EQ(0, addrv6_forth[8]); EXPECT_EQ(0, addrv6_forth[9]); EXPECT_EQ(0, addrv6_forth[10]); EXPECT_EQ(0, addrv6_forth[11]); EXPECT_EQ(0, addrv6_forth[12]); EXPECT_EQ(0, addrv6_forth[13]); EXPECT_EQ(0, addrv6_forth[14]); EXPECT_EQ(1, addrv6_forth[15]); uint8_t addrv6_fifth[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "1:2F3D:4::1", &addrv6_fifth)); EXPECT_EQ(0, addrv6_fifth[0]); EXPECT_EQ(1, addrv6_fifth[1]); EXPECT_EQ(0x2F, addrv6_fifth[2]); EXPECT_EQ(0x3D, addrv6_fifth[3]); EXPECT_EQ(0, addrv6_fifth[4]); EXPECT_EQ(4, addrv6_fifth[5]); EXPECT_EQ(0, addrv6_fifth[6]); EXPECT_EQ(0, addrv6_fifth[7]); EXPECT_EQ(0, addrv6_fifth[8]); EXPECT_EQ(0, addrv6_fifth[9]); EXPECT_EQ(0, addrv6_fifth[10]); EXPECT_EQ(0, addrv6_fifth[11]); EXPECT_EQ(0, addrv6_fifth[12]); EXPECT_EQ(0, addrv6_fifth[13]); EXPECT_EQ(0, addrv6_fifth[14]); EXPECT_EQ(1, addrv6_fifth[15]); uint8_t addrv6_sixth[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "AAAA:2:3:4::DDDD", &addrv6_sixth)); EXPECT_EQ(0xAA, addrv6_sixth[0]); EXPECT_EQ(0xAA, addrv6_sixth[1]); EXPECT_EQ(0, addrv6_sixth[2]); EXPECT_EQ(2, addrv6_sixth[3]); EXPECT_EQ(0, addrv6_sixth[4]); EXPECT_EQ(3, addrv6_sixth[5]); EXPECT_EQ(0, addrv6_sixth[6]); EXPECT_EQ(4, addrv6_sixth[7]); EXPECT_EQ(0, addrv6_sixth[8]); EXPECT_EQ(0, addrv6_sixth[9]); EXPECT_EQ(0, addrv6_sixth[10]); EXPECT_EQ(0, addrv6_sixth[11]); EXPECT_EQ(0, addrv6_sixth[12]); EXPECT_EQ(0, addrv6_sixth[13]); EXPECT_EQ(0xDD, addrv6_sixth[14]); EXPECT_EQ(0xDD, addrv6_sixth[15]); uint8_t addrv6_seventh[16] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; EXPECT_EQ(1, inet_pton(AF_INET6, "aaaa:2:3:4CDE::DDDD", &addrv6_seventh)); EXPECT_EQ(0xAA, addrv6_seventh[0]); EXPECT_EQ(0xAA, addrv6_seventh[1]); EXPECT_EQ(0, addrv6_seventh[2]); EXPECT_EQ(2, addrv6_seventh[3]); EXPECT_EQ(0, addrv6_seventh[4]); EXPECT_EQ(3, addrv6_seventh[5]); EXPECT_EQ(0x4C, addrv6_seventh[6]); EXPECT_EQ(0xDE, addrv6_seventh[7]); EXPECT_EQ(0, addrv6_seventh[8]); EXPECT_EQ(0, addrv6_seventh[9]); EXPECT_EQ(0, addrv6_seventh[10]); EXPECT_EQ(0, addrv6_seventh[11]); EXPECT_EQ(0, addrv6_seventh[12]); EXPECT_EQ(0, addrv6_seventh[13]); EXPECT_EQ(0xDD, addrv6_seventh[14]); EXPECT_EQ(0xDD, addrv6_seventh[15]); }
15,331
400
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/nointernet_test.c
/*-*- 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/calls/calls.h" #include "libc/calls/struct/stat.h" #include "libc/calls/syscall_support-sysv.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/log/log.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" #include "libc/sock/struct/msghdr.h" #include "libc/sock/struct/sockaddr.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/ipproto.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/sock.h" #include "libc/testlib/testlib.h" #ifdef __x86_64__ char testlib_enable_tmp_setup_teardown; void SetUpOnce(void) { if (nointernet() == -1) { ASSERT_TRUE(errno == EPERM || // already traced errno == ENOSYS); // non-linux or ancient linux if (errno == ENOSYS) { exit(0); } } } TEST(nointernet, testLocalhost_isAllowed) { struct sockaddr_in addr = {AF_INET, 0, {htonl(0x7f000001)}}; ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); ASSERT_SYS(0, 0, bind(3, (struct sockaddr *)&addr, sizeof(addr))); ASSERT_SYS(ECONNREFUSED, -1, connect(3, (struct sockaddr *)&addr, sizeof(addr))); ASSERT_SYS(0, 0, close(3)); } TEST(nointernet, testPublicInternet_isDenied) { struct sockaddr_in addr = {AF_INET, 0, {htonl(0x06060600)}}; ASSERT_SYS(EPERM, -1, socket(AF_BLUETOOTH, SOCK_STREAM, IPPROTO_TCP)); ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); ASSERT_SYS(ENOSYS, -1, bind(3, (struct sockaddr *)&addr, sizeof(addr))); ASSERT_SYS(ENOSYS, -1, connect(3, (struct sockaddr *)&addr, sizeof(addr))); ASSERT_SYS(0, 0, close(3)); } TEST(nointernet, sendmsgPrivateNetwork_doesntGetBlocked) { struct msghdr msg = { .msg_name = &(struct sockaddr_in){AF_INET, 31337, {htonl(0x0a000001)}}, .msg_namelen = sizeof(struct sockaddr_in), .msg_iov = &(struct iovec){"hi", 2}, .msg_iovlen = 1, }; ASSERT_SYS(EBADF, -1, sendmsg(-1, &msg, 0)); } TEST(nointernet, sendmsgPublicNetwork_raisesEnosys_whichPreemptsEbadf) { struct msghdr msg = { .msg_name = &(struct sockaddr_in){AF_INET, 31337, {htonl(0x06060600)}}, .msg_namelen = sizeof(struct sockaddr_in), .msg_iov = &(struct iovec){"hi", 2}, .msg_iovlen = 1, }; ASSERT_SYS(ENOSYS, -1, sendmsg(-1, &msg, 0)); ASSERT_SYS(0, 3, socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)); ASSERT_SYS(ENOSYS, -1, sendmsg(3, &msg, 0)); ASSERT_SYS(0, 0, close(3)); } #endif /* __x86_64__ */
4,264
90
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/unix_test.c
/*-*- 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/calls/calls.h" #include "libc/calls/internal.h" #include "libc/calls/struct/timeval.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/nexgen32e/vendor.internal.h" #include "libc/nt/version.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" #include "libc/sock/struct/sockaddr.h" #include "libc/str/str.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/so.h" #include "libc/sysv/consts/sock.h" #include "libc/sysv/consts/sol.h" #include "libc/testlib/testlib.h" #include "libc/time/time.h" char testlib_enable_tmp_setup_teardown; void SetUpOnce(void) { ASSERT_SYS(0, 0, pledge("stdio rpath cpath proc unix", 0)); } void DatagramServer(void) { char buf[256] = {0}; uint32_t len = sizeof(struct sockaddr_un); struct sockaddr_un addr = {AF_UNIX, "foo.sock"}; alarm(3); ASSERT_SYS(0, 3, socket(AF_UNIX, SOCK_DGRAM, 0)); ASSERT_SYS(0, 0, bind(3, (void *)&addr, len)); bzero(&addr, sizeof(addr)); ASSERT_SYS(0, 0, getsockname(3, (void *)&addr, &len)); ASSERT_EQ(11, len); ASSERT_STREQ("foo.sock", addr.sun_path); ASSERT_SYS(0, 5, read(3, buf, 256)); EXPECT_STREQ("hello", buf); ASSERT_SYS(0, 0, close(3)); } TEST(unix, datagram) { if (IsWindows()) return; // no unix datagram on windows :'( int ws; uint32_t len = sizeof(struct sockaddr_un); struct sockaddr_un addr = {AF_UNIX, "foo.sock"}; if (!fork()) { DatagramServer(); _Exit(0); } alarm(3); while (!fileexists(addr.sun_path)) usleep(10000); ASSERT_SYS(0, 3, socket(AF_UNIX, SOCK_DGRAM, 0)); ASSERT_SYS(0, 5, sendto(3, "hello", 5, 0, (struct sockaddr *)&addr, len)); ASSERT_SYS(0, 0, close(3)); ASSERT_NE(-1, wait(&ws)); EXPECT_TRUE(WIFEXITED(ws)); EXPECT_EQ(0, WEXITSTATUS(ws)); alarm(0); } void StreamServer(void) { char buf[256] = {0}; uint32_t len = sizeof(struct sockaddr_un); struct sockaddr_un addr = {AF_UNIX, "foo.sock"}; alarm(3); ASSERT_SYS(0, 3, socket(AF_UNIX, SOCK_STREAM, 0)); ASSERT_SYS(0, 0, bind(3, (void *)&addr, len)); bzero(&addr, sizeof(addr)); ASSERT_SYS(0, 0, getsockname(3, (void *)&addr, &len)); ASSERT_EQ(2 + 8 + 1, len); ASSERT_EQ(AF_UNIX, addr.sun_family); ASSERT_STREQ("foo.sock", addr.sun_path); ASSERT_SYS(0, 0, listen(3, 10)); bzero(&addr, sizeof(addr)); len = sizeof(addr); ASSERT_SYS(0, 4, accept(3, (struct sockaddr *)&addr, &len)); ASSERT_EQ(AF_UNIX, addr.sun_family); EXPECT_STREQ("", addr.sun_path); ASSERT_SYS(0, 5, read(4, buf, 256)); EXPECT_STREQ("hello", buf); ASSERT_SYS(0, 0, close(3)); } TEST(unix, stream) { if (IsWindows() && !IsAtLeastWindows10()) return; int ws; uint32_t len = sizeof(struct sockaddr_un); struct sockaddr_un addr = {AF_UNIX, "foo.sock"}; // TODO(jart): move this line down when kFdProcess is gone ASSERT_SYS(0, 3, socket(AF_UNIX, SOCK_STREAM, 0)); if (!fork()) { close(3); StreamServer(); _Exit(0); } alarm(3); while (!fileexists(addr.sun_path)) usleep(10000); ASSERT_SYS(0, 0, connect(3, (void *)&addr, len)); ASSERT_SYS(0, 5, write(3, "hello", 5)); ASSERT_SYS(0, 0, close(3)); ASSERT_NE(-1, wait(&ws)); EXPECT_TRUE(WIFEXITED(ws)); EXPECT_EQ(0, WEXITSTATUS(ws)); alarm(0); } TEST(unix, serverGoesDown_deletedSockFile) { // field of landmine if (IsWindows()) return; if (IsCygwin()) return; int ws, rc; char buf[8] = {0}; uint32_t len = sizeof(struct sockaddr_un); struct sockaddr_un addr = {AF_UNIX, "foo.sock"}; ASSERT_SYS(0, 3, socket(AF_UNIX, SOCK_DGRAM, 0)); ASSERT_SYS(0, 0, bind(3, (void *)&addr, len)); ASSERT_SYS(0, 4, socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)); ASSERT_SYS(0, 0, connect(4, (void *)&addr, len)); ASSERT_SYS(0, 5, write(4, "hello", 5)); ASSERT_SYS(0, 5, read(3, buf, 8)); ASSERT_SYS(0, 0, close(3)); ASSERT_EQ(-1, write(4, "hello", 5)); ASSERT_TRUE(errno == ECONNREFUSED || // Linux errno == ECONNRESET); // BSDs errno = 0; ASSERT_SYS(0, 0, unlink(addr.sun_path)); ASSERT_SYS(0, 3, socket(AF_UNIX, SOCK_DGRAM, 0)); ASSERT_SYS(0, 0, bind(3, (void *)&addr, len)); rc = write(4, "hello", 5); ASSERT_TRUE(rc == -1 && (errno == ECONNRESET || // errno == ENOTCONN || // errno == ECONNREFUSED || // errno == EDESTADDRREQ)); errno = 0; ASSERT_SYS(0, 0, close(4)); ASSERT_SYS(0, 4, socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)); ASSERT_SYS(0, 0, connect(4, (void *)&addr, len)); ASSERT_SYS(0, 5, write(4, "hello", 5)); ASSERT_SYS(0, 5, read(3, buf, 8)); ASSERT_SYS(0, 0, close(4)); ASSERT_SYS(0, 0, close(3)); } TEST(unix, serverGoesDown_usingSendTo_unlink) { // much easier if (IsWindows()) return; if (IsCygwin()) return; int ws, rc; char buf[8] = {0}; uint32_t len = sizeof(struct sockaddr_un); struct sockaddr_un addr = {AF_UNIX, "foo.sock"}; ASSERT_SYS(0, 3, socket(AF_UNIX, SOCK_DGRAM, 0)); ASSERT_SYS(0, 0, bind(3, (void *)&addr, len)); ASSERT_SYS(0, 4, socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0)); ASSERT_SYS(0, 5, sendto(4, "hello", 5, 0, (void *)&addr, len)); ASSERT_SYS(0, 5, read(3, buf, 8)); ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(IsWsl1() ? ENOTCONN : ECONNREFUSED, -1, sendto(4, "hello", 5, 0, (void *)&addr, len)); ASSERT_SYS(0, 0, unlink(addr.sun_path)); ASSERT_SYS(ENOENT, -1, sendto(4, "hello", 5, 0, (void *)&addr, len)); ASSERT_SYS(0, 3, socket(AF_UNIX, SOCK_DGRAM, 0)); ASSERT_SYS(0, 0, bind(3, (void *)&addr, len)); ASSERT_SYS(0, 5, sendto(4, "hello", 5, 0, (void *)&addr, len)); ASSERT_SYS(0, 5, read(3, buf, 8)); ASSERT_SYS(0, 0, close(4)); ASSERT_SYS(0, 0, close(3)); }
7,549
186
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/sendrecvmsg_test.c
/*-*- 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 2021 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/assert.h" #include "libc/calls/calls.h" #include "libc/calls/struct/iovec.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/mem/gc.internal.h" #include "libc/sock/sock.h" #include "libc/sock/struct/msghdr.h" #include "libc/sock/struct/sockaddr.h" #include "libc/str/str.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/ipproto.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/sock.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(sendrecvmsg, testPingPong) { int fd[2]; const char hello[] = "HELLO"; const char world[] = "WORLD"; struct msghdr msg; struct iovec data[2]; const uint32_t hwLen = strlen(hello) + strlen(world); memset(&msg, 0, sizeof(msg)); memset(&data[0], 0, sizeof(data)); data[0].iov_base = hello; data[0].iov_len = strlen(hello); data[1].iov_base = world; data[1].iov_len = strlen(world); /* Don't send the '\0' */ msg.msg_iov = &data[0]; msg.msg_iovlen = 2; ASSERT_NE(-1, socketpair(AF_UNIX, SOCK_STREAM, 0, fd)); ASSERT_EQ(hwLen, sendmsg(fd[0], &msg, 0)); data[0].iov_base = gc(xcalloc(20, 1)); data[0].iov_len = 20; msg.msg_iovlen = 1; ASSERT_EQ(hwLen, recvmsg(fd[1], &msg, 0)); EXPECT_STREQ("HELLOWORLD", msg.msg_iov[0].iov_base); ASSERT_NE(-1, close(fd[0])); ASSERT_NE(-1, close(fd[1])); }
3,175
68
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/sendfile_test.c
/*-*- 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/calls/calls.h" #include "libc/calls/struct/sigaction.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/kprintf.h" #include "libc/limits.h" #include "libc/mem/gc.internal.h" #include "libc/mem/mem.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/sock/sock.h" #include "libc/sock/struct/sockaddr.h" #include "libc/str/str.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/ipproto.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/shut.h" #include "libc/sysv/consts/sig.h" #include "libc/sysv/consts/sock.h" #include "libc/testlib/hyperion.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" char testlib_enable_tmp_setup_teardown; void SetUpOnce(void) { if (IsNetbsd()) exit(0); if (IsOpenbsd()) exit(0); ASSERT_SYS(0, 0, pledge("stdio rpath wpath cpath proc inet", 0)); } int64_t GetFileOffset(int fd) { int64_t pos; ASSERT_NE(-1, (pos = lseek(fd, 0, SEEK_CUR))); return pos; } TEST(sendfile, testSeeking) { char buf[1024]; int rc, ws, fds[2]; int64_t inoffset = 0; uint32_t addrsize = sizeof(struct sockaddr_in); struct sockaddr_in addr = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(0x7f000001), }; ASSERT_SYS(0, 3, creat("hyperion.txt", 0644)); ASSERT_SYS(0, 512, write(3, kHyperion, 512)); ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); ASSERT_SYS(0, 0, bind(3, (struct sockaddr *)&addr, sizeof(addr))); ASSERT_SYS(0, 0, getsockname(3, (struct sockaddr *)&addr, &addrsize)); ASSERT_SYS(0, 0, listen(3, 1)); if (!fork()) { ASSERT_SYS(0, 4, accept(3, (struct sockaddr *)&addr, &addrsize)); ASSERT_SYS(0, 5, open("hyperion.txt", O_RDONLY)); ASSERT_SYS(0, 12, sendfile(4, 5, &inoffset, 12)); ASSERT_EQ(0, GetFileOffset(5)); ASSERT_SYS(0, 8, read(5, buf, 8)); ASSERT_EQ(0, memcmp(buf, "The fall", 8)); ASSERT_EQ(8, GetFileOffset(5)); ASSERT_SYS(EBADF, -1, sendfile(5, 5, &inoffset, -1)); ASSERT_EQ(-1, sendfile(5, 5, &inoffset, -1)); ASSERT_TRUE(errno == ESPIPE || errno == EBADF); errno = 0; ASSERT_SYS(0, 500, sendfile(4, 5, &inoffset, -1)); ASSERT_EQ(8, GetFileOffset(5)); ASSERT_EQ(512, inoffset); inoffset = -1; ASSERT_SYS(EINVAL, -1, sendfile(4, 5, &inoffset, -1)); _Exit(0); } ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); ASSERT_SYS(0, 0, connect(3, (struct sockaddr *)&addr, sizeof(addr))); ASSERT_SYS(0, 12, read(3, buf, 12)); // needed due to windows ASSERT_SYS(0, 500, read(3, buf + 12, 700)); ASSERT_EQ(0, memcmp(buf, kHyperion, 512)); ASSERT_SYS(0, 0, close(3)); ASSERT_NE(-1, wait(&ws)); ASSERT_TRUE(WIFEXITED(ws)); ASSERT_EQ(0, WEXITSTATUS(ws)); } TEST(sendfile, testPositioning) { int ws, fds[2]; char buf[1024]; uint32_t addrsize = sizeof(struct sockaddr_in); struct sockaddr_in addr = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(0x7f000001), }; ASSERT_SYS(0, 3, creat("hyperion.txt", 0644)); ASSERT_SYS(0, 512, write(3, kHyperion, 512)); ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); ASSERT_SYS(0, 0, bind(3, (struct sockaddr *)&addr, sizeof(addr))); ASSERT_SYS(0, 0, getsockname(3, (struct sockaddr *)&addr, &addrsize)); ASSERT_SYS(0, 0, listen(3, 1)); if (!fork()) { signal(SIGPIPE, SIG_IGN); ASSERT_SYS(0, 4, accept(3, (struct sockaddr *)&addr, &addrsize)); ASSERT_SYS(0, 5, open("hyperion.txt", O_RDONLY)); ASSERT_SYS(0, 6, sendfile(4, 5, 0, 6)); ASSERT_EQ(6, GetFileOffset(5)); ASSERT_SYS(0, 6, sendfile(4, 5, 0, 6)); ASSERT_SYS(0, 0, shutdown(4, SHUT_WR)); ASSERT_EQ(-1, sendfile(4, 5, 0, 6)); ASSERT_TRUE(errno == EINVAL || errno == EPIPE); errno = 0; // XXX: WSL1 clobbers file offset on failure! if (!IsWsl1()) { ASSERT_EQ(12, GetFileOffset(5)); } _Exit(0); } ASSERT_SYS(0, 0, close(3)); ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)); ASSERT_SYS(0, 0, connect(3, (struct sockaddr *)&addr, sizeof(addr))); ASSERT_SYS(0, 6, read(3, buf, 6)); ASSERT_SYS(0, 6, read(3, buf + 6, 6)); ASSERT_SYS(0, 0, read(3, buf, 12)); ASSERT_EQ(0, memcmp(buf, kHyperion, 12)); ASSERT_SYS(0, 0, close(3)); ASSERT_NE(-1, wait(&ws)); ASSERT_TRUE(WIFEXITED(ws)); ASSERT_EQ(0, WEXITSTATUS(ws)); }
6,265
147
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/inet_ntop_test.c
/*-*- 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/errno.h" #include "libc/mem/mem.h" #include "libc/sock/sock.h" #include "libc/str/str.h" #include "libc/sysv/consts/af.h" #include "libc/testlib/testlib.h" TEST(inet_ntop, test) { char buf[16]; uint8_t localhost[4] = {127, 0, 0, 1}; EXPECT_STREQ("127.0.0.1", inet_ntop(AF_INET, localhost, buf, sizeof(buf))); } TEST(inet_ntop, testMax) { char buf[16]; uint8_t localhost[4] = {255, 255, 255, 255}; EXPECT_STREQ("255.255.255.255", inet_ntop(AF_INET, localhost, buf, sizeof(buf))); } TEST(inet_ntop, testBadFamily) { char buf[16] = "hi"; uint8_t localhost[4] = {127, 0, 0, 1}; ASSERT_EQ(NULL, inet_ntop(666, localhost, buf, sizeof(buf))); EXPECT_EQ(EAFNOSUPPORT, errno); ASSERT_STREQ("hi", buf); } TEST(inet_ntop, testNoSpace) { char *buf = memcpy(malloc(16), "hi", 3); uint8_t localhost[4] = {127, 0, 0, 1}; EXPECT_EQ(NULL, inet_ntop(AF_INET, localhost, buf, 1)); EXPECT_EQ(ENOSPC, errno); ASSERT_STREQ("hi", buf); ASSERT_EQ(NULL, inet_ntop(AF_INET, localhost, buf, 7)); ASSERT_STREQ("hi", buf); free(buf); } TEST(inet_ntop, ipv6_testMin_isJustColons) { char buf[46]; uint8_t ip[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; EXPECT_STREQ("::", inet_ntop(AF_INET6, ip, buf, sizeof(buf))); } TEST(inet_ntop, ipv6_testMax) { char buf[46]; uint8_t ip[16] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; EXPECT_STREQ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", inet_ntop(AF_INET6, ip, buf, sizeof(buf))); } TEST(inet_ntop, ipv6_loopback_isColonsThenJustOne) { char buf[46]; uint8_t ip[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; EXPECT_STREQ("::1", inet_ntop(AF_INET6, ip, buf, sizeof(buf))); } TEST(inet_ntop, ipv6_rfc4291example) { char buf[46]; uint8_t ip[16] = {0x20, 0x01, 0x0D, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x00, 0x20, 0x0C, 0x41, 0x7A}; EXPECT_STREQ("2001:db8::8:800:200c:417a", inet_ntop(AF_INET6, ip, buf, sizeof(buf))); } TEST(inet_ntop, ipv6_leading) { char buf[46]; uint8_t ip[16] = {0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; EXPECT_STREQ("1::", inet_ntop(AF_INET6, ip, buf, sizeof(buf))); } TEST(inet_ntop, ipv6_kindOfLeading) { char buf[46]; uint8_t ip[16] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; EXPECT_STREQ("100::", inet_ntop(AF_INET6, ip, buf, sizeof(buf))); }
4,575
101
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/socketpair_test.c
/*-*- 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 2021 Alison Winters │ │ │ │ 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/assert.h" #include "libc/calls/calls.h" #include "libc/dce.h" #include "libc/sock/sock.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/sock.h" #include "libc/testlib/testlib.h" void SetUpOnce(void) { ASSERT_SYS(0, 0, pledge("stdio tty", 0)); } TEST(socketpair, testAfUnixStream) { int fd[2]; const char ping[] = "ping"; const char pong[] = "pong"; char buf[32]; ASSERT_NE(-1, socketpair(AF_UNIX, SOCK_STREAM, 0, fd)); ASSERT_EQ(sizeof(ping), write(fd[0], ping, sizeof(ping))); ASSERT_EQ(sizeof(ping), read(fd[1], buf, sizeof(ping))); EXPECT_STREQ(ping, buf); ASSERT_EQ(sizeof(pong), write(fd[1], pong, sizeof(pong))); ASSERT_EQ(sizeof(pong), read(fd[0], buf, sizeof(pong))); EXPECT_STREQ(pong, buf); ASSERT_NE(-1, close(fd[0])); ASSERT_NE(-1, close(fd[1])); } TEST(socketpair, testAfUnixDgram) { int fd[2]; const char ping[] = "ping"; const char pong[] = "pong"; char buf[32]; ASSERT_NE(-1, socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)); ASSERT_EQ(sizeof(ping), write(fd[0], ping, sizeof(ping))); ASSERT_EQ(sizeof(ping), read(fd[1], buf, sizeof(buf))); EXPECT_STREQ(ping, buf); ASSERT_EQ(sizeof(pong), write(fd[1], pong, sizeof(pong))); ASSERT_EQ(sizeof(pong), read(fd[0], buf, sizeof(buf))); EXPECT_STREQ(pong, buf); ASSERT_NE(-1, close(fd[0])); ASSERT_NE(-1, close(fd[1])); } TEST(socketpair, testCloexec) { int fd[2]; const char ping[] = "ping"; const char pong[] = "pong"; char buf[32]; ASSERT_NE(-1, socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fd)); ASSERT_EQ(sizeof(ping), write(fd[0], ping, sizeof(ping))); ASSERT_EQ(sizeof(ping), read(fd[1], buf, sizeof(ping))); EXPECT_STREQ(ping, buf); ASSERT_EQ(sizeof(pong), write(fd[1], pong, sizeof(pong))); ASSERT_EQ(sizeof(pong), read(fd[0], buf, sizeof(pong))); EXPECT_STREQ(pong, buf); ASSERT_NE(-1, close(fd[0])); ASSERT_NE(-1, close(fd[1])); }
3,783
82
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/test.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ PKGS += TEST_LIBC_SOCK TEST_LIBC_SOCK_SRCS := $(wildcard test/libc/sock/*.c) TEST_LIBC_SOCK_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_SOCK_SRCS)) TEST_LIBC_SOCK_OBJS = \ $(TEST_LIBC_SOCK_SRCS:%.c=o/$(MODE)/%.o) TEST_LIBC_SOCK_COMS = \ $(TEST_LIBC_SOCK_SRCS:%.c=o/$(MODE)/%.com) TEST_LIBC_SOCK_BINS = \ $(TEST_LIBC_SOCK_COMS) \ $(TEST_LIBC_SOCK_COMS:%=%.dbg) TEST_LIBC_SOCK_TESTS = \ $(TEST_LIBC_SOCK_SRCS_TEST:%.c=o/$(MODE)/%.com.ok) TEST_LIBC_SOCK_CHECKS = \ $(TEST_LIBC_SOCK_SRCS_TEST:%.c=o/$(MODE)/%.com.runs) TEST_LIBC_SOCK_DIRECTDEPS = \ LIBC_CALLS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_RUNTIME \ LIBC_SOCK \ LIBC_STDIO \ LIBC_STR \ LIBC_STUBS \ LIBC_SYSV \ LIBC_LOG \ LIBC_SYSV_CALLS \ LIBC_TESTLIB \ LIBC_X \ TOOL_DECODE_LIB TEST_LIBC_SOCK_DEPS := \ $(call uniq,$(foreach x,$(TEST_LIBC_SOCK_DIRECTDEPS),$($(x)))) o/$(MODE)/test/libc/sock/sock.pkg: \ $(TEST_LIBC_SOCK_OBJS) \ $(foreach x,$(TEST_LIBC_SOCK_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/test/libc/sock/%.com.dbg: \ $(TEST_LIBC_SOCK_DEPS) \ o/$(MODE)/test/libc/sock/%.o \ o/$(MODE)/test/libc/sock/sock.pkg \ $(LIBC_TESTMAIN) \ $(CRT) \ $(APE_NO_MODIFY_SELF) @$(APELINK) o/$(MODE)/test/libc/sock/unix_test.com.runs: \ private .PLEDGE = stdio rpath wpath cpath fattr proc unix o/$(MODE)/test/libc/sock/setsockopt_test.com.runs \ o/$(MODE)/test/libc/sock/sendfile_test.com.runs \ o/$(MODE)/test/libc/sock/poll_test.com.runs \ o/$(MODE)/test/libc/sock/pollfd_test.com.runs: \ private .PLEDGE = stdio rpath wpath cpath fattr proc inet o/$(MODE)/test/libc/sock/sendrecvmsg_test.com.runs \ o/$(MODE)/test/libc/sock/nointernet_test.com.runs: \ private .PLEDGE = stdio rpath wpath cpath fattr proc inet recvfd sendfd o/$(MODE)/test/libc/sock/socket_test.com.runs: \ private .INTERNET = 1 # todo: ipv6 filtering o/$(MODE)/test/libc/sock/socket_test.com.runs: \ private .PLEDGE = stdio rpath wpath cpath fattr proc inet o/$(MODE)/test/libc/sock/recvmsg_test.com.runs: \ private .INTERNET = 1 # need to bind to 0.0.0.0 o/$(MODE)/test/libc/sock/recvmsg_test.com.runs: \ private .PLEDGE = stdio rpath wpath cpath fattr proc inet recvfd sendfd o/$(MODE)/test/libc/sock/shutdown_test.com.runs: \ private .PLEDGE = stdio rpath wpath cpath fattr proc inet $(TEST_LIBC_SOCK_OBJS): test/libc/sock/test.mk .PHONY: o/$(MODE)/test/libc/sock o/$(MODE)/test/libc/sock: \ $(TEST_LIBC_SOCK_BINS) \ $(TEST_LIBC_SOCK_CHECKS)
2,823
91
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/inet_ntoa_test.c
/*-*- 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 2021 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/sock/sock.h" #include "libc/sock/struct/sockaddr.h" #include "libc/testlib/testlib.h" TEST(inet_ntoa, test) { struct in_addr s; EXPECT_EQ(0x04030201, (s.s_addr = inet_addr("1.2.3.4"))); EXPECT_STREQ("1.2.3.4", inet_ntoa(s)); }
2,085
28
jart/cosmopolitan
false
cosmopolitan/test/libc/sock/select_test.c
/*-*- 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 2021 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/calls/calls.h" #include "libc/calls/pledge.h" #include "libc/calls/struct/sigaction.h" #include "libc/calls/struct/sigset.h" #include "libc/calls/struct/timeval.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/runtime/runtime.h" #include "libc/sock/select.h" #include "libc/sock/sock.h" #include "libc/sysv/consts/sig.h" #include "libc/testlib/testlib.h" #include "libc/time/time.h" bool gotsig; void SetUpOnce(void) { __pledge_mode = PLEDGE_PENALTY_KILL_PROCESS | PLEDGE_STDERR_LOGGING; ASSERT_SYS(0, 0, pledge("stdio", 0)); } void SetUp(void) { gotsig = false; } void OnSig(int sig) { gotsig = true; } TEST(select, allZero1) { sigset_t set, old; struct sigaction oldss; struct sigaction sa = {.sa_handler = OnSig}; ASSERT_SYS(0, 0, sigaction(SIGQUIT, &sa, &oldss)); ASSERT_SYS(0, 0, sigfillset(&set)); ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, &set, &old)); EXPECT_SYS(0, 0, kill(getpid(), SIGQUIT)); EXPECT_SYS(EINTR, -1, pselect(0, 0, 0, 0, 0, &old)); EXPECT_TRUE(gotsig); EXPECT_SYS(0, 0, sigprocmask(SIG_SETMASK, &old, 0)); EXPECT_SYS(0, 0, sigaction(SIGQUIT, &oldss, 0)); } TEST(select, pipe_hasInputFromSameProcess) { fd_set rfds; char buf[2]; int pipefds[2]; struct timeval tv = {.tv_usec = 100 * 1000}; EXPECT_SYS(0, 0, pipe(pipefds)); FD_ZERO(&rfds); FD_SET(pipefds[0], &rfds); EXPECT_SYS(0, 2, write(pipefds[1], "hi", 2)); EXPECT_SYS(0, 1, select(pipefds[0] + 1, &rfds, 0, 0, &tv)); EXPECT_TRUE(FD_ISSET(pipefds[0], &rfds)); EXPECT_SYS(0, 2, read(pipefds[0], buf, 2)); EXPECT_SYS(0, 0, select(pipefds[0] + 1, &rfds, 0, 0, &tv)); EXPECT_TRUE(!FD_ISSET(pipefds[0], &rfds)); EXPECT_SYS(0, 0, close(pipefds[0])); EXPECT_SYS(0, 0, close(pipefds[1])); } #if 0 // flaky TEST(select, testSleep) { int64_t e; long double n; struct timeval t = {0, 2000}; n = nowl(); EXPECT_SYS(0, 0, select(0, 0, 0, 0, &t)); e = (nowl() - n) * 1e6; EXPECT_GT(e, 1000); if (IsLinux()) { EXPECT_EQ(0, t.tv_sec); EXPECT_EQ(0, t.tv_usec); } } #endif
3,892
95
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/kcp437_test.c
/*-*- 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/str/tab.internal.h" #include "libc/str/unicode.h" #include "libc/testlib/testlib.h" TEST(kcp437, test) { long i; for (i = 0; i < 256; ++i) { EXPECT_EQ(1, wcwidth(kCp437[i]), "%lc", kCp437[i]); } }
2,059
29
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/ffs_test.c
/*-*- 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/nexgen32e/ffs.h" #include "libc/nexgen32e/nexgen32e.h" #include "libc/testlib/testlib.h" TEST(ffs, test) { EXPECT_EQ(__builtin_ffs(0), (ffs)(0)); EXPECT_EQ(__builtin_ffs(1), (ffs)(1)); EXPECT_EQ(__builtin_ffs(0x00100000), (ffs)(0x00100000)); EXPECT_EQ(__builtin_ffs(-1), (ffs)(-1)); } TEST(ffsl, test) { EXPECT_EQ(__builtin_ffsl(0), (ffsl)(0)); EXPECT_EQ(__builtin_ffsl(1), (ffsl)(1)); EXPECT_EQ(__builtin_ffsl(0x00100000), (ffsl)(0x00100000)); EXPECT_EQ(__builtin_ffsl(0x0010000000000000), (ffsl)(0x0010000000000000)); EXPECT_EQ(__builtin_ffsl(-1), (ffsl)(-1)); } TEST(ffsll, test) { EXPECT_EQ(__builtin_ffsll(0), (ffsll)(0)); EXPECT_EQ(__builtin_ffsll(1), (ffsll)(1)); EXPECT_EQ(__builtin_ffsll(0x00100000), (ffsll)(0x00100000)); EXPECT_EQ(__builtin_ffsll(0x0010000000000000), (ffsll)(0x0010000000000000)); EXPECT_EQ(__builtin_ffsll(-1), (ffsll)(-1)); }
2,738
45
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/stackrw_test.c
/*-*- 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/calls/struct/sigaction.h" #include "libc/calls/ucontext.h" #include "libc/dce.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/sa.h" #include "libc/sysv/consts/sig.h" #include "libc/testlib/testlib.h" /** * @fileoverview test non-executable stack is default */ jmp_buf jb; void EscapeSegfault(int sig, struct siginfo *si, void *vctx) { longjmp(jb, 666); } TEST(xstack, test) { if (IsXnu()) return; // TODO(jart): what's up with xnu in MODE=tiny? struct sigaction old[2]; struct sigaction sa = { .sa_sigaction = EscapeSegfault, .sa_flags = SA_SIGINFO, }; sigaction(SIGSEGV, &sa, old + 0); sigaction(SIGBUS, &sa, old + 1); char code[16] = { 0x55, // push %rbp 0xb8, 0007, 0x00, 0x00, 0x00, // mov $7,%eax 0x5d, // push %rbp 0xc3, // ret }; int (*func)(void) = (void *)code; int rc; if (!(rc = setjmp(jb))) { func(); abort(); } ASSERT_EQ(666, rc); sigaction(SIGBUS, old + 1, 0); sigaction(SIGSEGV, old + 0, 0); }
2,936
62
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/pcmpstr_test.c
/*-*- 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/nexgen32e/nexgen32e.h" #include "libc/testlib/testlib.h" // pcmpestrm /* TEST(pcmpstr, test) { */ /* EXPECT_EQ(0, pcmpstr()); */ /* EXPECT_STREQ("", pcmpstr()); */ /* } */
2,029
28
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/kbase36_test.c
/*-*- 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 2021 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/str/tab.internal.h" #include "libc/testlib/testlib.h" TEST(kBase36, test) { EXPECT_EQ(0, kBase36[0]); EXPECT_EQ(0, kBase36[255]); EXPECT_EQ(0, kBase36['!']); EXPECT_EQ(0 + 1, kBase36['0']); EXPECT_EQ(1 + 1, kBase36['1']); EXPECT_EQ(9 + 1, kBase36['9']); EXPECT_EQ(10 + 1, kBase36['a']); EXPECT_EQ(10 + 1, kBase36['A']); EXPECT_EQ(35 + 1, kBase36['z']); EXPECT_EQ(35 + 1, kBase36['Z']); EXPECT_EQ(0, kBase36['a' - 1]); EXPECT_EQ(0, kBase36['A' - 1]); EXPECT_EQ(0, kBase36['z' + 1]); EXPECT_EQ(0, kBase36['Z' + 1]); }
2,396
38
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/memrchr_test.c
/*-*- 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 2021 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/nexgen32e/nexgen32e.h" #include "libc/str/str.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" TEST(memrchr, test) { EXPECT_STREQ(".there", memrchr("yo.hi.there", '.', 11)); } BENCH(memrchr, bench) { EZBENCH2("memrchr", donothing, EXPROPRIATE(memrchr("yo.hi.there", '.', 11))); }
2,159
31
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/memset_test.c
/*-*- 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/mem/mem.h" #include "libc/nexgen32e/nexgen32e.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/testlib.h" void *_memset(void *, int, size_t) asm("memset"); TEST(memset, size0_doesNothing) { _memset(NULL, 0, 0); } TEST(memset, size1) { char *b = gc(malloc(1)); _memset(b, 7, 1); EXPECT_EQ(7, b[0]); } TEST(memset, size2) { char *b = gc(malloc(2)); _memset(b, 7, 2); EXPECT_EQ(7, b[0]); EXPECT_EQ(7, b[1]); } TEST(memset, size3) { char *b = gc(malloc(3)); _memset(b, 7, 3); EXPECT_EQ(7, b[0]); EXPECT_EQ(7, b[1]); EXPECT_EQ(7, b[2]); } TEST(memset, size4) { char *b = gc(malloc(4)); _memset(b, 7, 4); EXPECT_EQ(7, b[0]); EXPECT_EQ(7, b[1]); EXPECT_EQ(7, b[2]); EXPECT_EQ(7, b[3]); } TEST(memset, size5) { char *b = gc(malloc(5)); _memset(b, 7, 5); EXPECT_EQ(7, b[0]); EXPECT_EQ(7, b[1]); EXPECT_EQ(7, b[2]); EXPECT_EQ(7, b[3]); EXPECT_EQ(7, b[4]); } TEST(memset, wut) { char buf[128], *p, *q; _memset(buf, -1, sizeof(buf)); EXPECT_EQ(255, buf[8] & 255); }
2,882
75
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/memmove_test.c
/*-*- 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/intrin/bits.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" #include "libc/stdio/rand.h" #include "libc/str/str.h" #include "libc/testlib/testlib.h" #define N 65 #define S 1 long i, j, n; char *b0, *b1, *b2; dontinline char *PosixMemmove(char *dst, const char *src, size_t n) { char *tmp; tmp = malloc(n); memcpy(tmp, src, n); memcpy(dst, tmp, n); free(tmp); return dst; } TEST(MemMove, overlapping) { for (i = 0; i < N; i += S) { for (j = 0; j < N; j += S) { for (n = MIN(N - i, N - j) + 1; n--;) { b0 = rngset(malloc(N), N, _rand64, -1); b1 = memcpy(malloc(N), b0, N); b2 = memcpy(malloc(N), b0, N); ASSERT_EQ(b1 + j, memmove(b1 + j, b1 + i, n)); ASSERT_EQ(b2 + j, PosixMemmove(b2 + j, b2 + i, n)); ASSERT_EQ(0, memcmp(b1, b2, N), "j=%ld i=%ld n=%ld\n" "\t%#.*s data\n" "\t%#.*s memmove\n" "\t%#.*s posix", j, i, n, n, b0, n, b1, n, b2); free(b2); free(b1); free(b0); } } } } TEST(MemCpy, overlapping) { for (i = 0; i < N; i += S) { for (j = 0; j < N; j += S) { for (n = MIN(N - i, N - j) + 1; n--;) { if (j <= i) { b0 = rngset(malloc(N), N, _rand64, -1); b1 = memcpy(malloc(N), b0, N); b2 = memcpy(malloc(N), b0, N); ASSERT_EQ(b1 + j, memcpy(b1 + j, b1 + i, n)); ASSERT_EQ(b2 + j, PosixMemmove(b2 + j, b2 + i, n)); ASSERT_EQ(0, memcmp(b1, b2, N), "j=%ld i=%ld n=%ld\n" "\t%#.*s data\n" "\t%#.*s memmove\n" "\t%#.*s posix", j, i, n, n, b0, n, b1, n, b2); free(b2); free(b1); free(b0); } } } } } TEST(MemMove, overlappingDirect) { for (i = 0; i < N; i += S) { for (j = 0; j < N; j += S) { for (n = MIN(N - i, N - j) + 1; n--;) { b0 = rngset(malloc(N), N, _rand64, -1); b1 = memcpy(malloc(N), b0, N); b2 = memcpy(malloc(N), b0, N); ASSERT_EQ(b1 + j, (memmove)(b1 + j, b1 + i, n)); ASSERT_EQ(b2 + j, PosixMemmove(b2 + j, b2 + i, n)); ASSERT_EQ(0, memcmp(b1, b2, N), "j=%ld i=%ld n=%ld\n" "\t%#.*s data\n" "\t%#.*s memmove\n" "\t%#.*s posix", j, i, n, n, b0, n, b1, n, b2); free(b2); free(b1); free(b0); } } } }
4,396
111
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/lz4decode_test.c
/*-*- 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/intrin/safemacros.internal.h" #include "libc/calls/calls.h" #include "libc/log/check.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/kompressor.h" #include "libc/nexgen32e/lz4.h" #include "libc/runtime/ezmap.internal.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/testlib/testlib.h" TEST(lz4, decompress_emptyStringWithoutChecksum) { /* lz4 -9 --content-size --no-frame-crc /tmp/empty - | hexdump -C */ static char kLz4Data[] = {0x04, 0x22, 0x4d, 0x18, 0x60, 0x40, 0x82, 0x00, 0x00, 0x00, 0x00}; char *src = memcpy(malloc(sizeof(kLz4Data)), kLz4Data, sizeof(kLz4Data)); char *dst = malloc(1); *dst = 'z'; ASSERT_EQ(dst, lz4decode(dst, src)); ASSERT_EQ('z', *dst); free(dst); free(src); } TEST(lz4, decompress_oneLetterWithoutChecksum) { /* printf a >oneletter */ /* lz4 -9 --content-size --no-frame-crc oneletter /dev/stdout | hexdump -C */ static char kLz4Data[] = {0x04, 0x22, 0x4d, 0x18, 0x68, 0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x01, 0x00, 0x00, 0x80, 0x61, 0x00, 0x00, 0x00, 0x00}; char *src = memcpy(malloc(sizeof(kLz4Data)), kLz4Data, sizeof(kLz4Data)); char *dst = malloc(1); ASSERT_EQ(dst + 1, lz4decode(dst, src)); ASSERT_EQ('a', *dst); free(dst); free(src); } TEST(lz4, decompress_runLengthDecode) { /* printf aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >/tmp/a */ /* lz4 -9 --content-size --no-frame-crc /tmp/a - | hexdump -vC */ static char kLz4Data[] = { 0x04, 0x22, 0x4d, 0x18, 0x68, 0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x0b, 0x00, 0x00, 0x00, 0x1f, 0x61, 0x01, 0x00, 0x07, 0x50, 0x61, 0x61, 0x61, 0x61, 0x61, 0x00, 0x00, 0x00, 0x00}; char *src = memcpy(malloc(sizeof(kLz4Data)), kLz4Data, sizeof(kLz4Data)); const char *want = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; char *dst = malloc(strlen(want)); ASSERT_EQ(dst + strlen(want), lz4decode(dst, src)); ASSERT_STREQN(want, dst, strlen(want)); free(dst); free(src); } TEST(lz4, zoneFileGmt) { if (!fileexists("usr/share/zoneinfo.dict.lz4")) return; struct MappedFile dict, gmt; CHECK_NE(-1, MapFileRead("usr/share/zoneinfo.dict.lz4", &dict)); CHECK_NE(-1, MapFileRead("usr/share/zoneinfo/GMT.lz4", &gmt)); size_t mapsize, gmtsize; char *mapping, *gmtdata; lz4decode((gmtdata = lz4decode( (mapping = _mapanon( (mapsize = roundup( LZ4_FRAME_BLOCKCONTENTSIZE(lz4check(dict.addr)) + (gmtsize = LZ4_FRAME_BLOCKCONTENTSIZE( lz4check(gmt.addr))), FRAMESIZE)))), dict.addr)), gmt.addr); ASSERT_BINEQ( u"TZif2                  ☺   ☺           ☺   ♦      GMT   TZif2   " u"               ☺   ☺       ☺   ☺   ♦°              GMT   ◙GMT0◙", gmtdata); munmap(mapping, mapsize); UnmapFile(&dict); UnmapFile(&gmt); }
4,994
97
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/strcspn_test.c
/*-*- 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/str/str.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" TEST(strcspn, test) { EXPECT_EQ(0, strcspn("abcdefg", "ae")); EXPECT_EQ(0, strcspn("abcdefg", "aeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")); EXPECT_EQ(4, strcspn("abcdefg", "ze")); EXPECT_EQ(4, strcspn("abcdefg", "zeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")); EXPECT_EQ(5, strcspn("abcdfzg", "ze")); EXPECT_EQ(5, strcspn("abcdfzg", "zeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")); EXPECT_EQ(7, strcspn("abcdEfg", "ze")); EXPECT_EQ(7, strcspn("abcdEfg", "zeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")); EXPECT_EQ(31, strcspn("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOp", "abcdefghijklmnp")); EXPECT_EQ(31, strcspn("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOp", "abcdefghijklmnpabcdefghijklmnp")); } BENCH(strcspn, bench) { EZBENCH2("strcspn", donothing, EXPROPRIATE( strcspn("pABCDEFGHIJKLMNOPABCDEFGHIJKLMNO", "abcdefghijklmnp"))); EZBENCH2("strcspn", donothing, EXPROPRIATE( strcspn("ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOp", "abcdefghijklmnp"))); EZBENCH2( "strcspn", donothing, EXPROPRIATE(strcspn( "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOABCDEFGHIJKLMNOPABCDEFGHIJKLMNOp", "abcdefghijklmnp"))); }
3,111
50
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/cescapec_test.c
/*-*- 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/str/str.h" #include "libc/testlib/testlib.h" static const uint32_t kCescapecGolden[256] = { [0] = '\\' | '0' << 8 | '0' << 16 | '0' << 24, [1] = '\\' | '0' << 8 | '0' << 16 | '1' << 24, [2] = '\\' | '0' << 8 | '0' << 16 | '2' << 24, [3] = '\\' | '0' << 8 | '0' << 16 | '3' << 24, [4] = '\\' | '0' << 8 | '0' << 16 | '4' << 24, [5] = '\\' | '0' << 8 | '0' << 16 | '5' << 24, [6] = '\\' | '0' << 8 | '0' << 16 | '6' << 24, [7] = '\\' | 'a' << 8, [8] = '\\' | 'b' << 8, [9] = '\\' | 't' << 8, [10] = '\\' | 'n' << 8, [11] = '\\' | 'v' << 8, [12] = '\\' | 'f' << 8, [13] = '\\' | 'r' << 8, [14] = '\\' | '0' << 8 | '1' << 16 | '6' << 24, [15] = '\\' | '0' << 8 | '1' << 16 | '7' << 24, [16] = '\\' | '0' << 8 | '2' << 16 | '0' << 24, [17] = '\\' | '0' << 8 | '2' << 16 | '1' << 24, [18] = '\\' | '0' << 8 | '2' << 16 | '2' << 24, [19] = '\\' | '0' << 8 | '2' << 16 | '3' << 24, [20] = '\\' | '0' << 8 | '2' << 16 | '4' << 24, [21] = '\\' | '0' << 8 | '2' << 16 | '5' << 24, [22] = '\\' | '0' << 8 | '2' << 16 | '6' << 24, [23] = '\\' | '0' << 8 | '2' << 16 | '7' << 24, [24] = '\\' | '0' << 8 | '3' << 16 | '0' << 24, [25] = '\\' | '0' << 8 | '3' << 16 | '1' << 24, [26] = '\\' | '0' << 8 | '3' << 16 | '2' << 24, [27] = '\\' | '0' << 8 | '3' << 16 | '3' << 24, [28] = '\\' | '0' << 8 | '3' << 16 | '4' << 24, [29] = '\\' | '0' << 8 | '3' << 16 | '5' << 24, [30] = '\\' | '0' << 8 | '3' << 16 | '6' << 24, [31] = '\\' | '0' << 8 | '3' << 16 | '7' << 24, [32] = ' ', [33] = '!', [34] = '\\' | '"' << 8, [35] = '#', [36] = '$', [37] = '%', [38] = '&', [39] = '\\' | '\'' << 8, [40] = '(', [41] = ')', [42] = '*', [43] = '+', [44] = ',', [45] = '-', [46] = '.', [47] = '/', [48] = '0', [49] = '1', [50] = '2', [51] = '3', [52] = '4', [53] = '5', [54] = '6', [55] = '7', [56] = '8', [57] = '9', [58] = ':', [59] = ';', [60] = '<', [61] = '=', [62] = '>', [63] = '?', [64] = '@', [65] = 'A', [66] = 'B', [67] = 'C', [68] = 'D', [69] = 'E', [70] = 'F', [71] = 'G', [72] = 'H', [73] = 'I', [74] = 'J', [75] = 'K', [76] = 'L', [77] = 'M', [78] = 'N', [79] = 'O', [80] = 'P', [81] = 'Q', [82] = 'R', [83] = 'S', [84] = 'T', [85] = 'U', [86] = 'V', [87] = 'W', [88] = 'X', [89] = 'Y', [90] = 'Z', [91] = '[', [92] = '\\' | '\\' << 8, [93] = ']', [94] = '^', [95] = '_', [96] = '`', [97] = 'a', [98] = 'b', [99] = 'c', [100] = 'd', [101] = 'e', [102] = 'f', [103] = 'g', [104] = 'h', [105] = 'i', [106] = 'j', [107] = 'k', [108] = 'l', [109] = 'm', [110] = 'n', [111] = 'o', [112] = 'p', [113] = 'q', [114] = 'r', [115] = 's', [116] = 't', [117] = 'u', [118] = 'v', [119] = 'w', [120] = 'x', [121] = 'y', [122] = 'z', [123] = '{', [124] = '|', [125] = '}', [126] = '~', [127] = '\\' | '1' << 8 | '7' << 16 | '7' << 24, [128] = '\\' | '2' << 8 | '0' << 16 | '0' << 24, [129] = '\\' | '2' << 8 | '0' << 16 | '1' << 24, [130] = '\\' | '2' << 8 | '0' << 16 | '2' << 24, [131] = '\\' | '2' << 8 | '0' << 16 | '3' << 24, [132] = '\\' | '2' << 8 | '0' << 16 | '4' << 24, [133] = '\\' | '2' << 8 | '0' << 16 | '5' << 24, [134] = '\\' | '2' << 8 | '0' << 16 | '6' << 24, [135] = '\\' | '2' << 8 | '0' << 16 | '7' << 24, [136] = '\\' | '2' << 8 | '1' << 16 | '0' << 24, [137] = '\\' | '2' << 8 | '1' << 16 | '1' << 24, [138] = '\\' | '2' << 8 | '1' << 16 | '2' << 24, [139] = '\\' | '2' << 8 | '1' << 16 | '3' << 24, [140] = '\\' | '2' << 8 | '1' << 16 | '4' << 24, [141] = '\\' | '2' << 8 | '1' << 16 | '5' << 24, [142] = '\\' | '2' << 8 | '1' << 16 | '6' << 24, [143] = '\\' | '2' << 8 | '1' << 16 | '7' << 24, [144] = '\\' | '2' << 8 | '2' << 16 | '0' << 24, [145] = '\\' | '2' << 8 | '2' << 16 | '1' << 24, [146] = '\\' | '2' << 8 | '2' << 16 | '2' << 24, [147] = '\\' | '2' << 8 | '2' << 16 | '3' << 24, [148] = '\\' | '2' << 8 | '2' << 16 | '4' << 24, [149] = '\\' | '2' << 8 | '2' << 16 | '5' << 24, [150] = '\\' | '2' << 8 | '2' << 16 | '6' << 24, [151] = '\\' | '2' << 8 | '2' << 16 | '7' << 24, [152] = '\\' | '2' << 8 | '3' << 16 | '0' << 24, [153] = '\\' | '2' << 8 | '3' << 16 | '1' << 24, [154] = '\\' | '2' << 8 | '3' << 16 | '2' << 24, [155] = '\\' | '2' << 8 | '3' << 16 | '3' << 24, [156] = '\\' | '2' << 8 | '3' << 16 | '4' << 24, [157] = '\\' | '2' << 8 | '3' << 16 | '5' << 24, [158] = '\\' | '2' << 8 | '3' << 16 | '6' << 24, [159] = '\\' | '2' << 8 | '3' << 16 | '7' << 24, [160] = '\\' | '2' << 8 | '4' << 16 | '0' << 24, [161] = '\\' | '2' << 8 | '4' << 16 | '1' << 24, [162] = '\\' | '2' << 8 | '4' << 16 | '2' << 24, [163] = '\\' | '2' << 8 | '4' << 16 | '3' << 24, [164] = '\\' | '2' << 8 | '4' << 16 | '4' << 24, [165] = '\\' | '2' << 8 | '4' << 16 | '5' << 24, [166] = '\\' | '2' << 8 | '4' << 16 | '6' << 24, [167] = '\\' | '2' << 8 | '4' << 16 | '7' << 24, [168] = '\\' | '2' << 8 | '5' << 16 | '0' << 24, [169] = '\\' | '2' << 8 | '5' << 16 | '1' << 24, [170] = '\\' | '2' << 8 | '5' << 16 | '2' << 24, [171] = '\\' | '2' << 8 | '5' << 16 | '3' << 24, [172] = '\\' | '2' << 8 | '5' << 16 | '4' << 24, [173] = '\\' | '2' << 8 | '5' << 16 | '5' << 24, [174] = '\\' | '2' << 8 | '5' << 16 | '6' << 24, [175] = '\\' | '2' << 8 | '5' << 16 | '7' << 24, [176] = '\\' | '2' << 8 | '6' << 16 | '0' << 24, [177] = '\\' | '2' << 8 | '6' << 16 | '1' << 24, [178] = '\\' | '2' << 8 | '6' << 16 | '2' << 24, [179] = '\\' | '2' << 8 | '6' << 16 | '3' << 24, [180] = '\\' | '2' << 8 | '6' << 16 | '4' << 24, [181] = '\\' | '2' << 8 | '6' << 16 | '5' << 24, [182] = '\\' | '2' << 8 | '6' << 16 | '6' << 24, [183] = '\\' | '2' << 8 | '6' << 16 | '7' << 24, [184] = '\\' | '2' << 8 | '7' << 16 | '0' << 24, [185] = '\\' | '2' << 8 | '7' << 16 | '1' << 24, [186] = '\\' | '2' << 8 | '7' << 16 | '2' << 24, [187] = '\\' | '2' << 8 | '7' << 16 | '3' << 24, [188] = '\\' | '2' << 8 | '7' << 16 | '4' << 24, [189] = '\\' | '2' << 8 | '7' << 16 | '5' << 24, [190] = '\\' | '2' << 8 | '7' << 16 | '6' << 24, [191] = '\\' | '2' << 8 | '7' << 16 | '7' << 24, [192] = '\\' | '3' << 8 | '0' << 16 | '0' << 24, [193] = '\\' | '3' << 8 | '0' << 16 | '1' << 24, [194] = '\\' | '3' << 8 | '0' << 16 | '2' << 24, [195] = '\\' | '3' << 8 | '0' << 16 | '3' << 24, [196] = '\\' | '3' << 8 | '0' << 16 | '4' << 24, [197] = '\\' | '3' << 8 | '0' << 16 | '5' << 24, [198] = '\\' | '3' << 8 | '0' << 16 | '6' << 24, [199] = '\\' | '3' << 8 | '0' << 16 | '7' << 24, [200] = '\\' | '3' << 8 | '1' << 16 | '0' << 24, [201] = '\\' | '3' << 8 | '1' << 16 | '1' << 24, [202] = '\\' | '3' << 8 | '1' << 16 | '2' << 24, [203] = '\\' | '3' << 8 | '1' << 16 | '3' << 24, [204] = '\\' | '3' << 8 | '1' << 16 | '4' << 24, [205] = '\\' | '3' << 8 | '1' << 16 | '5' << 24, [206] = '\\' | '3' << 8 | '1' << 16 | '6' << 24, [207] = '\\' | '3' << 8 | '1' << 16 | '7' << 24, [208] = '\\' | '3' << 8 | '2' << 16 | '0' << 24, [209] = '\\' | '3' << 8 | '2' << 16 | '1' << 24, [210] = '\\' | '3' << 8 | '2' << 16 | '2' << 24, [211] = '\\' | '3' << 8 | '2' << 16 | '3' << 24, [212] = '\\' | '3' << 8 | '2' << 16 | '4' << 24, [213] = '\\' | '3' << 8 | '2' << 16 | '5' << 24, [214] = '\\' | '3' << 8 | '2' << 16 | '6' << 24, [215] = '\\' | '3' << 8 | '2' << 16 | '7' << 24, [216] = '\\' | '3' << 8 | '3' << 16 | '0' << 24, [217] = '\\' | '3' << 8 | '3' << 16 | '1' << 24, [218] = '\\' | '3' << 8 | '3' << 16 | '2' << 24, [219] = '\\' | '3' << 8 | '3' << 16 | '3' << 24, [220] = '\\' | '3' << 8 | '3' << 16 | '4' << 24, [221] = '\\' | '3' << 8 | '3' << 16 | '5' << 24, [222] = '\\' | '3' << 8 | '3' << 16 | '6' << 24, [223] = '\\' | '3' << 8 | '3' << 16 | '7' << 24, [224] = '\\' | '3' << 8 | '4' << 16 | '0' << 24, [225] = '\\' | '3' << 8 | '4' << 16 | '1' << 24, [226] = '\\' | '3' << 8 | '4' << 16 | '2' << 24, [227] = '\\' | '3' << 8 | '4' << 16 | '3' << 24, [228] = '\\' | '3' << 8 | '4' << 16 | '4' << 24, [229] = '\\' | '3' << 8 | '4' << 16 | '5' << 24, [230] = '\\' | '3' << 8 | '4' << 16 | '6' << 24, [231] = '\\' | '3' << 8 | '4' << 16 | '7' << 24, [232] = '\\' | '3' << 8 | '5' << 16 | '0' << 24, [233] = '\\' | '3' << 8 | '5' << 16 | '1' << 24, [234] = '\\' | '3' << 8 | '5' << 16 | '2' << 24, [235] = '\\' | '3' << 8 | '5' << 16 | '3' << 24, [236] = '\\' | '3' << 8 | '5' << 16 | '4' << 24, [237] = '\\' | '3' << 8 | '5' << 16 | '5' << 24, [238] = '\\' | '3' << 8 | '5' << 16 | '6' << 24, [239] = '\\' | '3' << 8 | '5' << 16 | '7' << 24, [240] = '\\' | '3' << 8 | '6' << 16 | '0' << 24, [241] = '\\' | '3' << 8 | '6' << 16 | '1' << 24, [242] = '\\' | '3' << 8 | '6' << 16 | '2' << 24, [243] = '\\' | '3' << 8 | '6' << 16 | '3' << 24, [244] = '\\' | '3' << 8 | '6' << 16 | '4' << 24, [245] = '\\' | '3' << 8 | '6' << 16 | '5' << 24, [246] = '\\' | '3' << 8 | '6' << 16 | '6' << 24, [247] = '\\' | '3' << 8 | '6' << 16 | '7' << 24, [248] = '\\' | '3' << 8 | '7' << 16 | '0' << 24, [249] = '\\' | '3' << 8 | '7' << 16 | '1' << 24, [250] = '\\' | '3' << 8 | '7' << 16 | '2' << 24, [251] = '\\' | '3' << 8 | '7' << 16 | '3' << 24, [252] = '\\' | '3' << 8 | '7' << 16 | '4' << 24, [253] = '\\' | '3' << 8 | '7' << 16 | '5' << 24, [254] = '\\' | '3' << 8 | '7' << 16 | '6' << 24, [255] = '\\' | '3' << 8 | '7' << 16 | '7' << 24, }; TEST(_cescapec, test) { int i; for (i = 0; i < 256; ++i) { ASSERT_EQ(kCescapecGolden[i], _cescapec(i)); } }
11,974
287
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/strsak32_test.c
/*-*- 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/nexgen32e/nexgen32e.h" #include "libc/mem/gc.internal.h" #include "libc/str/str.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/hyperion.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(strsak32, test) { EXPECT_EQ(0, wcslen(L"")); EXPECT_EQ(1, wcslen(L"1")); EXPECT_EQ(5, wcslen(L"hello")); } BENCH(strsak32, bench) { size_t wcslen_(const wchar_t *) asm("wcslen"); wchar_t *p = gc(utf8to32(kHyperion, kHyperionSize, 0)); EZBENCH_N("wcslen", kHyperionSize, wcslen_(p)); for (int i = 128; i >= 2; i /= 2) { p[i - 0] = 0; EZBENCH_N("wcslen", i - 0, wcslen_(p)); p[i - 1] = 0; EZBENCH_N("wcslen", i - 1, wcslen_(p)); } }
2,529
44
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/gclongjmp_test.c
/*-*- 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 2021 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/log/log.h" #include "libc/mem/gc.h" #include "libc/mem/gc.internal.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/gc.internal.h" #include "libc/nexgen32e/nexgen32e.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/thread/spawn.h" #include "libc/x/x.h" #ifdef __x86_64__ // TODO(jart): get garbage collector working properly on aarch64 #define GC(x) _defer(Free, x) char *x; char *y; char *z; jmp_buf jb; void Free(char *p) { strcpy(p, "FREE"); } void C(void) { x = GC(strdup("abcd")); if (0) PrintGarbage(); gclongjmp(jb, 1); abort(); } void B(void C(void)) { y = GC(strdup("HIHI")); C(); abort(); } void A(void C(void), void B(void(void))) { z = GC(strdup("yoyo")); B(C); abort(); } void (*Ap)(void(void), void(void(void))) = A; void (*Bp)(void(void)) = B; void (*Cp)(void) = C; TEST(gclongjmp, test) { if (0) PrintGarbage(); if (!setjmp(jb)) { Ap(Cp, Bp); abort(); } if (0) PrintGarbage(); EXPECT_STREQ("FREE", x); EXPECT_STREQ("FREE", y); EXPECT_STREQ("FREE", z); free(z); free(y); free(x); } void crawl(const char *path) { const char *dir; if (!strcmp(path, "/") || !strcmp(path, ".")) return; crawl(_gc(xdirname(path))); } int Worker(void *arg, int tid) { crawl("a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d"); return 0; } TEST(gc, torture) { int i, n = 32; struct spawn *t = gc(malloc(sizeof(struct spawn) * n)); for (i = 0; i < n; ++i) ASSERT_SYS(0, 0, _spawn(Worker, 0, t + i)); for (i = 0; i < n; ++i) EXPECT_SYS(0, 0, _join(t + i)); } void crawl2(jmp_buf jb, const char *path) { const char *dir; if (!strcmp(path, "/") || !strcmp(path, ".")) _gclongjmp(jb, 1); crawl2(jb, _gc(xdirname(path))); } int Worker2(void *arg, int tid) { jmp_buf jb; if (!setjmp(jb)) { crawl2(jb, "a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d/a/b/c/d"); } return 0; } TEST(_gclongjmp, torture) { int i, n = 32; struct spawn *t = gc(malloc(sizeof(struct spawn) * n)); for (i = 0; i < n; ++i) ASSERT_SYS(0, 0, _spawn(Worker2, 0, t + i)); for (i = 0; i < n; ++i) EXPECT_SYS(0, 0, _join(t + i)); } dontinline void F1(void) { /* 3x slower than F2() but sooo worth it */ gc(malloc(16)); } dontinline void F2(void) { void *volatile p; p = malloc(16); free(p); } void (*F1p)(void) = F1; void (*F2p)(void) = F2; BENCH(gc, bench) { EZBENCH2("gc(malloc(16))", donothing, F1p()); EZBENCH2("free(malloc(16))", donothing, F2p()); } #endif /* __x86_64__ */
4,436
143
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/test.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ PKGS += TEST_LIBC_NEXGEN32E TEST_LIBC_NEXGEN32E_SRCS := \ $(wildcard test/libc/nexgen32e/*.c) TEST_LIBC_NEXGEN32E_SRCS_TEST = \ $(filter %_test.c,$(TEST_LIBC_NEXGEN32E_SRCS)) TEST_LIBC_NEXGEN32E_OBJS = \ $(TEST_LIBC_NEXGEN32E_SRCS:%.c=o/$(MODE)/%.o) TEST_LIBC_NEXGEN32E_COMS = \ $(TEST_LIBC_NEXGEN32E_SRCS:%.c=o/$(MODE)/%.com) TEST_LIBC_NEXGEN32E_BINS = \ $(TEST_LIBC_NEXGEN32E_COMS) \ $(TEST_LIBC_NEXGEN32E_COMS:%=%.dbg) TEST_LIBC_NEXGEN32E_TESTS = \ $(TEST_LIBC_NEXGEN32E_SRCS_TEST:%.c=o/$(MODE)/%.com.ok) TEST_LIBC_NEXGEN32E_CHECKS = \ $(TEST_LIBC_NEXGEN32E_SRCS_TEST:%.c=o/$(MODE)/%.com.runs) TEST_LIBC_NEXGEN32E_DIRECTDEPS = \ LIBC_CALLS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_LOG \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_RUNTIME \ LIBC_STDIO \ LIBC_STR \ LIBC_STUBS \ LIBC_SYSV \ LIBC_THREAD \ LIBC_TESTLIB \ LIBC_X \ TOOL_VIZ_LIB \ THIRD_PARTY_XED TEST_LIBC_NEXGEN32E_DEPS := \ $(call uniq,$(foreach x,$(TEST_LIBC_NEXGEN32E_DIRECTDEPS),$($(x)))) o/$(MODE)/test/libc/nexgen32e/nexgen32e.pkg: \ $(TEST_LIBC_NEXGEN32E_OBJS) \ $(foreach x,$(TEST_LIBC_NEXGEN32E_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/test/libc/nexgen32e/%.com.dbg: \ $(TEST_LIBC_NEXGEN32E_DEPS) \ o/$(MODE)/test/libc/nexgen32e/%.o \ o/$(MODE)/test/libc/nexgen32e/nexgen32e.pkg \ $(LIBC_TESTMAIN) \ $(CRT) \ $(APE_NO_MODIFY_SELF) @$(APELINK) # we can't run this test on openbsd because rwx memory isn't allowed o/$(MODE)/test/libc/nexgen32e/stackrwx_test.com.ok: \ o/$(MODE)/tool/build/runit.com \ o/$(MODE)/tool/build/runitd.com \ o/$(MODE)/test/libc/nexgen32e/stackrwx_test.com @$(COMPILE) -wATEST -tT$@ $^ $(filter-out openbsd,$(HOSTS)) $(TEST_LIBC_NEXGEN32E_OBJS): private \ DEFAULT_CCFLAGS += \ -fno-builtin .PHONY: o/$(MODE)/test/libc/nexgen32e o/$(MODE)/test/libc/nexgen32e: \ $(TEST_LIBC_NEXGEN32E_BINS) \ $(TEST_LIBC_NEXGEN32E_CHECKS)
2,216
76
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/strcaseconv_test.c
/*-*- 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/mem/gc.internal.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/nexgen32e.h" #include "libc/stdio/rand.h" #include "libc/str/str.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" TEST(strtolower, testAligned) { EXPECT_STREQ("azcdabcdabcdabcd", strtolower(gc(strdup("AZCDabcdABCDabcd")))); EXPECT_STREQ("azcdabcdabcdabcdabcdabcdabcdabcd", strtolower(gc(strdup("AZCDabcdABCDabcdABCDabcdABCDabcd")))); } TEST(strtolower, testUnaligned) { EXPECT_STREQ("1", strtolower(gc(strdup("1")))); EXPECT_STREQ( "zcdabcdabcdabcdabcdabcdabcdabc", strtolower((char *)gc(strdup("AZCDabcdABCDabcdABCDabcdABCDabc")) + 1)); } TEST(strtoupper, testAligned) { EXPECT_STREQ("AZCDABCDABCDABCD", strtoupper(gc(strdup("AZCDabcdABCDabcd")))); EXPECT_STREQ("AZCDABCDABCDABCDABCDABCDABCDABCD", strtoupper(gc(strdup("AZCDabcdABCDabcdABCDabcdABCDabcd")))); } TEST(strtoupper, testUnaligned) { EXPECT_STREQ("1", strtoupper(gc(strdup("1")))); EXPECT_STREQ( "ZCDABCDABCDABCDABCDABCDABCDABC", strtoupper((char *)gc(strdup("AZCDabcdABCDabcdABCDabcdABCDabc")) + 1)); } BENCH(strtolower, bench) { size_t size = FRAMESIZE; char *data = gc(malloc(size)); EZBENCH2( "strtolower", { rngset(data, size, _rand64, -1); data[size - 1] = 0; }, strtolower(data)); }
3,216
64
jart/cosmopolitan
false
cosmopolitan/test/libc/nexgen32e/stackrwx_test.c
/*-*- 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/runtime/runtime.h" #include "libc/runtime/stack.h" #include "libc/str/str.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/prot.h" #include "libc/testlib/testlib.h" #if 0 // TODO(jart): fix MODE=tiny /** * @fileoverview tests executable stack */ STATIC_EXEC_STACK(); TEST(xstack, test) { char code[16] = { 0x55, // push %rbp 0xb8, 0007, 0x00, 0x00, 0x00, // mov $7,%eax 0x5d, // push %rbp 0xc3, // ret }; int (*func)(void) = (void *)code; ASSERT_EQ(7, func()); } #endif
2,453
46
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/round_test.c
/*-*- 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/nexgen32e/x86feature.h" #include "libc/str/str.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _round(double) asm("round"); float _roundf(float) asm("roundf"); long double _roundl(long double) asm("roundl"); long _lround(double) asm("lround"); long _lroundf(float) asm("lroundf"); long _lroundl(long double) asm("lroundl"); long _lrint(double) asm("lrint"); long _lrintf(float) asm("lrintf"); long _lrintl(long double) asm("lrintl"); TEST(round, testCornerCases) { EXPECT_STREQ("-0", gc(xdtoa(_round(-0.0)))); EXPECT_STREQ("NAN", gc(xdtoa(_round(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoa(_round(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_round(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(_round(-INFINITY)))); } TEST(roundl, testCornerCases) { EXPECT_STREQ("-0", gc(xdtoa(_roundl(-0.0)))); EXPECT_STREQ("NAN", gc(xdtoa(_roundl(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoa(_roundl(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_roundl(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(_roundl(-INFINITY)))); } TEST(round, test) { EXPECT_STREQ("-3", gc(xdtoa(_round(-2.5)))); EXPECT_STREQ("-2", gc(xdtoa(_round(-1.5)))); EXPECT_STREQ("-1", gc(xdtoa(_round(-.5)))); EXPECT_STREQ("-0", gc(xdtoa(_round(-.4)))); EXPECT_STREQ("0", gc(xdtoa(_round(.4)))); EXPECT_STREQ("1", gc(xdtoa(_round(.5)))); EXPECT_STREQ("2", gc(xdtoa(_round(1.5)))); EXPECT_STREQ("3", gc(xdtoa(_round(2.5)))); } TEST(roundf, test) { EXPECT_STREQ("-3", gc(xdtoa(_roundf(-2.5)))); EXPECT_STREQ("-2", gc(xdtoa(_roundf(-1.5)))); EXPECT_STREQ("-1", gc(xdtoa(_roundf(-.5)))); EXPECT_STREQ("-0", gc(xdtoa(_roundf(-.4)))); EXPECT_STREQ("0", gc(xdtoa(_roundf(.4)))); EXPECT_STREQ("1", gc(xdtoa(_roundf(.5)))); EXPECT_STREQ("2", gc(xdtoa(_roundf(1.5)))); EXPECT_STREQ("3", gc(xdtoa(_roundf(2.5)))); } TEST(roundl, test) { EXPECT_STREQ("-3", gc(xdtoa(_roundl(-2.5)))); EXPECT_STREQ("-2", gc(xdtoa(_roundl(-1.5)))); EXPECT_STREQ("-1", gc(xdtoa(_roundl(-.5)))); EXPECT_STREQ("-0", gc(xdtoa(_roundl(-.4)))); EXPECT_STREQ("0", gc(xdtoa(_roundl(.4)))); EXPECT_STREQ("1", gc(xdtoa(_roundl(.5)))); EXPECT_STREQ("2", gc(xdtoa(_roundl(1.5)))); EXPECT_STREQ("3", gc(xdtoa(_roundl(2.5)))); } TEST(nearbyint, test) { EXPECT_STREQ("-2", gc(xdtoa(nearbyint(-2.5)))); EXPECT_STREQ("-2", gc(xdtoa(nearbyint(-1.5)))); EXPECT_STREQ("-0", gc(xdtoa(nearbyint(-.5)))); EXPECT_STREQ("-0", gc(xdtoa(nearbyint(-.4)))); EXPECT_STREQ("0", gc(xdtoa(nearbyint(.4)))); EXPECT_STREQ("0", gc(xdtoa(nearbyint(.5)))); EXPECT_STREQ("2", gc(xdtoa(nearbyint(1.5)))); EXPECT_STREQ("2", gc(xdtoa(nearbyint(2.5)))); } TEST(nearbyintf, test) { EXPECT_STREQ("-2", gc(xdtoa(nearbyintf(-2.5)))); EXPECT_STREQ("-2", gc(xdtoa(nearbyintf(-1.5)))); EXPECT_STREQ("-0", gc(xdtoa(nearbyintf(-.5)))); EXPECT_STREQ("-0", gc(xdtoa(nearbyintf(-.4)))); EXPECT_STREQ("0", gc(xdtoa(nearbyintf(.4)))); EXPECT_STREQ("0", gc(xdtoa(nearbyintf(.5)))); EXPECT_STREQ("2", gc(xdtoa(nearbyintf(1.5)))); EXPECT_STREQ("2", gc(xdtoa(nearbyintf(2.5)))); } TEST(nearbyintl, test) { EXPECT_STREQ("-2", gc(xdtoa(nearbyintl(-2.5)))); EXPECT_STREQ("-2", gc(xdtoa(nearbyintl(-1.5)))); EXPECT_STREQ("-0", gc(xdtoa(nearbyintl(-.5)))); EXPECT_STREQ("-0", gc(xdtoa(nearbyintl(-.4)))); EXPECT_STREQ("0", gc(xdtoa(nearbyintl(.4)))); EXPECT_STREQ("0", gc(xdtoa(nearbyintl(.5)))); EXPECT_STREQ("2", gc(xdtoa(nearbyintl(1.5)))); EXPECT_STREQ("2", gc(xdtoa(nearbyintl(2.5)))); } TEST(rint, test) { EXPECT_STREQ("-2", gc(xdtoa(rint(-2.5)))); EXPECT_STREQ("-2", gc(xdtoa(rint(-1.5)))); EXPECT_STREQ("-0", gc(xdtoa(rint(-.5)))); EXPECT_STREQ("-0", gc(xdtoa(rint(-.4)))); EXPECT_STREQ("0", gc(xdtoa(rint(.4)))); EXPECT_STREQ("0", gc(xdtoa(rint(.5)))); EXPECT_STREQ("2", gc(xdtoa(rint(1.5)))); EXPECT_STREQ("2", gc(xdtoa(rint(2.5)))); } TEST(lrint, test) { EXPECT_EQ(-2, _lrint(-2.5)); EXPECT_EQ(-2, _lrint(-1.5)); EXPECT_EQ(-0, _lrint(-.5)); EXPECT_EQ(-0, _lrint(-.4)); EXPECT_EQ(0, _lrint(.4)); EXPECT_EQ(0, _lrint(.5)); EXPECT_EQ(2, _lrint(1.5)); EXPECT_EQ(2, _lrint(2.5)); } TEST(lrintf, test) { EXPECT_EQ(-2, _lrintf(-2.5)); EXPECT_EQ(-2, _lrintf(-1.5)); EXPECT_EQ(-0, _lrintf(-.5)); EXPECT_EQ(-0, _lrintf(-.4)); EXPECT_EQ(0, _lrintf(.4)); EXPECT_EQ(0, _lrintf(.5)); EXPECT_EQ(2, _lrintf(1.5)); EXPECT_EQ(2, _lrintf(2.5)); } TEST(rintf, test) { EXPECT_STREQ("-2", gc(xdtoa(rintf(-2.5)))); EXPECT_STREQ("-2", gc(xdtoa(rintf(-1.5)))); EXPECT_STREQ("-0", gc(xdtoa(rintf(-.5)))); EXPECT_STREQ("-0", gc(xdtoa(rintf(-.4)))); EXPECT_STREQ("0", gc(xdtoa(rintf(.4)))); EXPECT_STREQ("0", gc(xdtoa(rintf(.5)))); EXPECT_STREQ("2", gc(xdtoa(rintf(1.5)))); EXPECT_STREQ("2", gc(xdtoa(rintf(2.5)))); } TEST(rintl, test) { EXPECT_STREQ("-2", gc(xdtoa(rintl(-2.5)))); EXPECT_STREQ("-2", gc(xdtoa(rintl(-1.5)))); EXPECT_STREQ("-0", gc(xdtoa(rintl(-.5)))); EXPECT_STREQ("-0", gc(xdtoa(rintl(-.4)))); EXPECT_STREQ("0", gc(xdtoa(rintl(.4)))); EXPECT_STREQ("0", gc(xdtoa(rintl(.5)))); EXPECT_STREQ("2", gc(xdtoa(rintl(1.5)))); EXPECT_STREQ("2", gc(xdtoa(rintl(2.5)))); } TEST(roundf, testCornerCases) { EXPECT_STREQ("-0", gc(xdtoa(_roundf(-0.0)))); EXPECT_STREQ("NAN", gc(xdtoa(_roundf(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoa(_roundf(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_roundf(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(_roundf(-INFINITY)))); } TEST(lroundf, test) { EXPECT_EQ(-3, _lroundf(-2.5)); EXPECT_EQ(-2, _lroundf(-1.5)); EXPECT_EQ(-1, _lroundf(-.5)); EXPECT_EQ(0, _lroundf(-.0)); EXPECT_EQ(1, _lroundf(.5)); EXPECT_EQ(2, _lroundf(1.5)); EXPECT_EQ(3, _lroundf(2.5)); } TEST(lround, test) { EXPECT_EQ(-3, _lround(-2.5)); EXPECT_EQ(-2, _lround(-1.5)); EXPECT_EQ(-1, _lround(-.5)); EXPECT_EQ(-0, _lround(-.4)); EXPECT_EQ(0, _lround(.4)); EXPECT_EQ(1, _lround(.5)); EXPECT_EQ(2, _lround(1.5)); EXPECT_EQ(3, _lround(2.5)); } TEST(lroundl, test) { EXPECT_EQ(-3, _lroundl(-2.5)); EXPECT_EQ(-2, _lroundl(-1.5)); EXPECT_EQ(-1, _lroundl(-.5)); EXPECT_EQ(-0, _lroundl(-.4)); EXPECT_EQ(0, _lroundl(.4)); EXPECT_EQ(1, _lroundl(.5)); EXPECT_EQ(2, _lroundl(1.5)); EXPECT_EQ(3, _lroundl(2.5)); } BENCH(round, bench) { #ifdef __x86_64__ EZBENCH2("double+.5", donothing, EXPROPRIATE(VEIL("x", (double)(-3.5)) + .5)); EZBENCH2("float+.5f", donothing, EXPROPRIATE(VEIL("x", (float)(-3.5)) + .5)); EZBENCH2("ldbl+.5l", donothing, EXPROPRIATE(VEIL("t", (long double)(-3.5)) + .5)); #endif EZBENCH2("round", donothing, _round(.7)); /* ~4ns */ EZBENCH2("roundf", donothing, _roundf(.7)); /* ~3ns */ EZBENCH2("roundl", donothing, _roundl(.7)); /* ~8ns */ EZBENCH2("lrint", donothing, _lrint(.7)); /* ~1ns */ EZBENCH2("lrintf", donothing, _lrintf(.7)); /* ~1ns */ EZBENCH2("lrintl", donothing, _lrintl(.7)); /* ~78ns */ }
8,826
230
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/strtod_test.c
/*-*- 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 2021 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/fmt/fmt.h" #include "libc/macros.internal.h" #include "libc/mem/gc.h" #include "libc/mem/mem.h" #include "libc/runtime/fenv.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" #include "third_party/double-conversion/wrapper.h" #include "third_party/gdtoa/gdtoa.h" #define HEX(d) (union Pun){d}.i union Pun { double x; uint64_t i; }; static const struct { uint64_t i; const char *s; } V[] = { {0x2fa7b6d71d20b96c, "4e-079"}, // {0x09eb8d7e32be6396, "7e-261"}, // {0x3ae7361cb863de62, "6e-025"}, // {0x1eaf7e0db3799aa3, "7e-161"}, // {0x7c1cbb547777a285, "7e0289"}, // {0x507afcef51f0fb5f, "5e0079"}, // {0x508afcef51f0fb5f, "1e0080"}, // {0x0133339131c46f8b, "7e-303"}, // {0x5fa317e5ef3ab327, "5e0152"}, // {0x5a07a2ecc414a03f, "5e0125"}, // {0x5a27a2ecc414a03f, "2e0126"}, // {0x22d5570f59bd178c, "7e-141"}, // {0x18323ff06eea847a, "4e-192"}, // {0x49102498ea6df0c4, "9e0043"}, // {0x7ed754e31cd072da, "1e0303"}, // {0x2dde3cbc9907fdc8, "95e-089"}, // {0x689d1c26db7d0dae, "85e0194"}, // {0x77c0b7cb60c994da, "69e0267"}, // {0x3c665dde8e688ba6, "97e-019"}, // {0x49d033d7eca0adef, "37e0046"}, // {0x49e033d7eca0adef, "74e0046"}, // {0x2bc0ad836f269a17, "61e-099"}, // {0x151b39ae1909c31b, "53e-208"}, // {0x0fc27b2e4f210075, "93e-234"}, // {0x2c9a5db812948281, "79e-095"}, // {0x0772d36cf48e7abd, "87e-274"}, // {0x4585747ab143e353, "83e0025"}, // {0x38b698ccdc60015a, "17e-036"}, // {0x47246a3418629ef6, "53e0033"}, // {0x30ecd5bee57763e6, "51e-074"}, // {0x3bbdc03b8fd7016a, "63e-022"}, // {0x5e3ae03f245703e2, "839e0143"}, // {0x1abf14727744c63e, "749e-182"}, // {0x3b282782afe1869e, "999e-026"}, // {0x77b0b7cb60c994da, "345e0266"}, // {0x2b5ffc81bc29f02b, "914e-102"}, // {0x55b7221a79cdd1d9, "829e0102"}, // {0x5322d6b183fe4b55, "307e0090"}, // {0x6654374d8b87ac63, "859e0182"}, // {0x5216c309024bab4b, "283e0085"}, // {0x675526be9c22eb17, "589e0187"}, // {0x64fdcf7df8f573b7, "302e0176"}, // {0x650dcf7df8f573b7, "604e0176"}, // {0x0de03cea3586452e, "761e-244"}, // {0x7044d64d4079150c, "647e0230"}, // {0x64a7d93193f78fc6, "755e0174"}, // {0x30dcd5bee57763e6, "255e-075"}, // {0x4c159bd3ad46e346, "3391e0055"}, // {0x3d923d1b5eb1d778, "4147e-015"}, // {0x3b482782afe1869e, "3996e-026"}, // {0x3b382782afe1869e, "1998e-026"}, // {0x0335519ac5142aab, "3338e-296"}, // {0x0325519ac5142aab, "1669e-296"}, // {0x0772d2df246ecd2d, "8699e-276"}, // {0x73284e91f4aa0fdb, "5311e0243"}, // {0x2cd07c2d27a5b989, "7903e-096"}, // {0x11d19b8744033457, "7611e-226"}, // {0x4cb444b34a6fb3eb, "3257e0058"}, // {0x4cc444b34a6fb3eb, "6514e0058"}, // {0x77462644c61d41aa, "3571e0263"}, // {0x77562644c61d41aa, "7142e0263"}, // {0x72f3720e5d54d97c, "5311e0242"}, // {0x3384c98fce16152e, "1617e-063"}, // {0x4897d2950dc76da4, "51881e0037"}, // {0x285ef890f5de4c86, "31441e-118"}, // {0x5143e272a77478e8, "30179e0079"}, // {0x5153e272a77478e8, "60358e0079"}, // {0x3cc703856844bdbf, "63876e-020"}, // {0x3cb703856844bdbf, "31938e-020"}, // {0x3a42405b773fbdf3, "46073e-032"}, // {0x4b757eb8ad52a5c9, "32941e0051"}, // {0x49770105df3d47cb, "82081e0041"}, // {0x1440492a4a8a37fd, "38701e-215"}, // {0x4ab0c52fe6dc6a1b, "62745e0047"}, // {0x4ac0c52fe6dc6a1b, "12549e0048"}, // {0x1af099b393b84832, "64009e-183"}, // {0x77262644c61d41aa, "89275e0261"}, // {0x46232645e1ba93f0, "75859e0025"}, // {0x7c8272ed2307f56a, "57533e0287"}, // {0x70ad657059dc79aa, "584169e0229"}, // {0x29eb99d6240c1a28, "940189e-112"}, // {0x6a00fd07ed297f80, "416121e0197"}, // {0x6a10fd07ed297f80, "832242e0197"}, // {0x50e8a85eb277e645, "584738e0076"}, // {0x241b248728b9c117, "933587e-140"}, // {0x5a2dda592e398dd7, "252601e0121"}, // {0x79f9463b59b8f2bd, "358423e0274"}, // {0x14f25818c7294f27, "892771e-213"}, // {0x49670105df3d47cb, "410405e0040"}, // {0x0afbe2dd66200bef, "928609e-261"}, // {0x0c55a462d91c6ab3, "302276e-254"}, // {0x3c653a9985dbde6c, "920657e-023"}, // {0x3bf1f99e11ea0a24, "609019e-025"}, // {0x59f7e1e0f1c7a4ac, "252601e0120"}, // {0x34b00e7db3b3f242, "654839e-060"}, // {0x5c5e597c0b94b7ae, "8823691e0130"}, // {0x709d657059dc79aa, "2920845e0228"}, // {0x51fda232347e6032, "9210917e0080"}, // {0x026e58ffa48f4fce, "5800419e-303"}, // {0x0ee3ecf22ea07863, "6119898e-243"}, // {0x0ed3ecf22ea07863, "3059949e-243"}, // {0x6f90f73be1dff9ad, "2572231e0223"}, // {0x3cf8849dd33c95af, "5444097e-021"}, // {0x26f7e5902ce0e151, "5783893e-127"}, // {0x1295d4fe53afec65, "3865421e-225"}, // {0x61b4689b4a5fa201, "4590831e0156"}, // {0x61c4689b4a5fa201, "9181662e0156"}, // {0x3bbbe45a312d08a0, "5906361e-027"}, // {0x7225f0d408362a72, "7315057e0235"}, // {0x5762e51a84a3c6a0, "9088115e0106"}, // {0x5772e51a84a3c6a0, "1817623e0107"}, // {0x5c4e597c0b94b7ae, "44118455e0129"}, // {0x7e5512d5273e62e8, "35282041e0293"}, // {0x05129b01b6885d36, "31279898e-291"}, // {0x05029b01b6885d36, "15639949e-291"}, // {0x5f955bcf72fd10f9, "27966061e0145"}, // {0x5fa55bcf72fd10f9, "55932122e0145"}, // {0x36900683a21de855, "70176353e-053"}, // {0x3adf29ca0ff893b1, "40277543e-032"}, // {0x622193aff1f1c8e3, "50609263e0157"}, // {0x518b37c4b7928317, "66094077e0077"}, // {0x59406e98f5ec8f37, "84863171e0114"}, // {0x786526f061ca9053, "89396333e0264"}, // {0x016e07320602056c, "87575437e-309"}, // {0x3870bc7b7603a2ca, "78693511e-044"}, // {0x16d1470083f89d48, "90285923e-206"}, // {0x3b423a4ad20748a2, "30155207e-030"}, // {0x5acc569e968e0944, "245540327e0121"}, // {0x7d44997a298b2f2e, "263125459e0287"}, // {0x0c64472ba9550e86, "566446538e-257"}, // {0x0c54472ba9550e86, "283223269e-257"}, // {0x5b01b6231e18c5cb, "245540327e0122"}, // {0x5b11b6231e18c5cb, "491080654e0122"}, // {0x27a397d3c9745d2f, "971212611e-126"}, // {0x4c76ce94febdc7a5, "229058583e0052"}, // {0x49cc7ccf90c9f8ab, "325270231e0039"}, // {0x3a8880a3d515e849, "989648089e-035"}, // {0x7a720223f2b3a881, "653777767e0273"}, // {0x6d30bc60e6896717, "923091487e0209"}, // {0x7d89bfd8b3edfafa, "526250918e0288"}, // {0x018e07320602056c, "350301748e-309"}, // {0x17a14fe7daf8f3ae, "741111169e-203"}, // {0x0ff09355f8050c02, "667284113e-240"}, // {0x5abc569e968e0944, "1227701635e0120"}, // {0x1c38afe10a2a66aa, "9981396317e-182"}, // {0x041465b896c24520, "5232604057e-298"}, // {0x2fb0847822f765b2, "5572170023e-088"}, // {0x5b31b6231e18c5cb, "1964322616e0122"}, // {0x5b41b6231e18c5cb, "3928645232e0122"}, // {0x35f4614c3219891f, "8715380633e-058"}, // {0x279397d3c9745d2f, "4856063055e-127"}, // {0x223a06a1024b95e1, "8336960483e-153"}, // {0x21a01891fc4717fd, "1007046393e-155"}, // {0x1d695fd4c88d4b1b, "5378822089e-176"}, // {0x1a83db11ac608107, "5981342308e-190"}, // {0x3020b552d2edcdea, "7214782613e-086"}, // {0x5f70acde6a98eb4c, "5458466829e0142"}, // {0x2b5fc575867314ee, "9078555839e-109"}, // {0x526021f14ed7b3fa, "6418488827e0079"}, // {0x5081a151ddbd3c4a, "65325840981e0069"}, // {0x54a221bd871d2cf4, "49573485983e0089"}, // {0x51830e6c7d4e3480, "46275205733e0074"}, // {0x51930e6c7d4e3480, "92550411466e0074"}, // {0x1832c3e72d179607, "41129842097e-202"}, // {0x380960fe08d5847f, "93227267727e-049"}, // {0x688c49437fccfadb, "41297294357e0185"}, // {0x343a12666477886d, "41534892987e-067"}, // {0x6be0189a26df575f, "42333842451e0201"}, // {0x131155515fd37265, "78564021519e-227"}, // {0x35800a19a3ffd981, "53587107423e-061"}, // {0x18a32fa69a69bd6d, "53827010643e-200"}, // {0x6a4544e6daee2a18, "83356057653e0193"}, // {0x29a541ecdfd48694, "45256834646e-118"}, // {0x2b4fc575867314ee, "45392779195e-110"}, // {0x7e81deaf40ead9a0, "23934638219e0291"}, // {0x59e2d44edcc51304, "995779191233e0113"}, // {0x0b676688faee99bc, "997422852243e-265"}, // {0x28d925a0aabcdc68, "653532977297e-123"}, // {0x60f11894b202e9f4, "938885684947e0147"}, // {0x5c210c20303fe0f1, "619534293513e0124"}, // {0x39a5e66dc3d6bdb5, "539879452414e-042"}, // {0x782c1c352fc3c309, "742522891517e0259"}, // {0x3dbc06d366394441, "254901016865e-022"}, // {0x7c85fd7aa44d9477, "685763015669e0280"}, // {0x072aa65b58639e69, "384865004907e-285"}, // {0x5321958b36c5102b, "286556458711e0081"}, // {0x5331958b36c5102b, "573112917422e0081"}, // {0x234253ec0e161420, "769525178383e-150"}, // {0x6a3544e6daee2a18, "416780288265e0192"}, // {0x2b3fc575867314ee, "226963895975e-111"}, // {0x4f778b6516c2b478, "665592809339e0063"}, // {0x6f99ab8261990292, "3891901811465e0217"}, // {0x50e4177a9915fbf8, "4764593340755e0069"}, // {0x7a7173f76c63b792, "6336156586177e0269"}, // {0x56566fee05649a7a, "8233559360849e0095"}, // {0x2c538e6edd48f2a3, "3662265515198e-107"}, // {0x2c438e6edd48f2a3, "1831132757599e-107"}, // {0x1d726dae7bbeda75, "7812878489261e-179"}, // {0x60b28a61cf9483b7, "6363857920591e0145"}, // {0x53a51f508b287ae7, "8811915538555e0082"}, // {0x1a253db2fea1ea31, "9997878507563e-195"}, // {0x0634ee5d56b32957, "9224786422069e-291"}, // {0x058d3409dfbca26f, "6284426329974e-294"}, // {0x35c135972630774c, "9199302046091e-062"}, // {0x29423fa9e6fcf47e, "6070482281213e-122"}, // {0x0405acc2053064c2, "2780161250963e-301"}, // {0x5621f324d11d4862, "8233559360849e0094"}, // {0x6d94677812d3a606, "72027097041701e0206"}, // {0x6f79ab8261990292, "97297545286625e0215"}, // {0x3da5c6714def374c, "99021992302453e-025"}, // {0x3e373cdf8db7a7bc, "54104687080198e-022"}, // {0x5537f203339c9629, "33519685743233e0089"}, // {0x5547f203339c9629, "67039371486466e0089"}, // {0x1d626dae7bbeda75, "39064392446305e-180"}, // {0x78e072f3819c1321, "17796979903653e0261"}, // {0x4a9eebabe0957af3, "28921916763211e0038"}, // {0x6302920f96e7f9ef, "87605699161665e0155"}, // {0x34d9b2a5c4041e4b, "41921560615349e-067"}, // {0x4f7c7c5aea080a49, "80527976643809e0061"}, // {0x21cce77c2b3328fc, "72335858886654e-159"}, // {0x57f561def4a9ee32, "52656615219377e0102"}, // {0x33b8bf7e7fa6f02a, "15400733123779e-072"}, // {0x33a8bf7e7fa6f02a, "77003665618895e-073"}, // {0x3a42d73088f4050a, "475603213226859e-042"}, // {0x5b218a7f36172332, "972708181182949e0116"}, // {0x342eef5e1f90ac34, "246411729980464e-071"}, // {0x341eef5e1f90ac34, "123205864990232e-071"}, // {0x0e104273b18918b1, "609610927149051e-255"}, // {0x3a778cfcab31064d, "475603213226859e-041"}, // {0x508226c684c87261, "672574798934795e0065"}, // {0x509226c684c87261, "134514959786959e0066"}, // {0x2395f2df5e675a0f, "294897574603217e-151"}, // {0x4a7eebabe0957af3, "723047919080275e0036"}, // {0x30bddc7e975c5045, "660191429952702e-088"}, // {0x30addc7e975c5045, "330095714976351e-088"}, // {0x21fce77c2b3328fc, "578686871093232e-159"}, // {0x21dce77c2b3328fc, "144671717773308e-159"}, // {0x3398bf7e7fa6f02a, "385018328094475e-074"}, // {0x3077e3987916a69e, "330095714976351e-089"}, // {0x171a80a6e566428c, "2215901545757777e-212"}, // {0x09cacc46749dccfe, "1702061899637397e-276"}, // {0x6f53ae60753af6ca, "1864950924021923e0213"}, // {0x6f63ae60753af6ca, "3729901848043846e0213"}, // {0x20f8823a57adbef9, "7487252720986826e-165"}, // {0x20e8823a57adbef9, "3743626360493413e-165"}, // {0x5be5f6de9d5d6b5b, "4988915232824583e0119"}, // {0x7cae3c14d6916ce9, "3771476185376383e0277"}, // {0x2a81b96458445d07, "6182410494241627e-119"}, // {0x609dfc11fbf46087, "2572981889477453e0142"}, // {0x4dd280461b856ec5, "7793560217139653e0051"}, // {0x194fe601457dce4d, "9163942927285259e-202"}, // {0x63650aff653ffe8a, "6353227084707473e0155"}, // {0x176090684f5fe998, "4431803091515554e-211"}, // {0x6f0f7d6721f7f144, "9324754620109615e0211"}, // {0x79d90529a37b7e22, "8870461176410409e0263"}, // {0x612491daad0ba280, "90372559027740405e0143"}, // {0x61a011f2d73116f4, "18074511805548081e0146"}, // {0x496ec55666d8f9ec, "54897030182071313e0029"}, // {0x3ccb7738011e75fe, "76232626624829156e-032"}, // {0x2128823a57adbef9, "59898021767894608e-165"}, // {0x2118823a57adbef9, "29949010883947304e-165"}, // {0x4d83de005bd620df, "26153245263757307e0049"}, // {0x0d27c0747bd76fa1, "27176258005319167e-261"}, // {0x61d4166f8cfd5cb1, "18074511805548081e0147"}, // {0x2b759a2783ce70ab, "24691002732654881e-115"}, // {0x4f408ce499519ce3, "58483921078398283e0057"}, // {0x22692238f7987779, "64409240769861689e-159"}, // {0x11364981e39e66ca, "94080055902682397e-242"}, // {0x63550aff653ffe8a, "31766135423537365e0154"}, // {0x657a999ddec72aca, "68985865317742005e0164"}, // {0x658a999ddec72aca, "13797173063548401e0165"}, // {0x09522dc01ca1cb8c, "902042358290366539e-281"}, // {0x7c038fd93f1f5342, "238296178309629163e0272"}, // {0x72925ae62cb346d8, "783308178698887621e0226"}, // {0x499ec55666d8f9ec, "439176241456570504e0029"}, // {0x7e6adf51fa055e03, "899810892172646163e0283"}, // {0x14f307a67f1f69ff, "926145344610700019e-225"}, // {0x4d63de005bd620df, "653831131593932675e0047"}, // {0x4d73de005bd620df, "130766226318786535e0048"}, // {0x0693bfac6bc4767b, "557035730189854663e-294"}, // {0x0986b93023ca3e6f, "902042358290366539e-280"}, // {0x6d13bbb4bf05f087, "272104041512242479e0200"}, // {0x6d23bbb4bf05f087, "544208083024484958e0200"}, // {0x6b808ebc116f8a20, "680429695511221511e0192"}, // {0x7490db75cc001072, "308975121073410857e0236"}, // {0x53d7bff336d8ff06, "792644927852378159e0078"}, // {0x71f2cbac35f71140, "783308178698887621e0223"}, // {0x0f8ab223efcee35a, "8396094300569779681e-252"}, // {0x346b85c026a264e4, "3507665085003296281e-074"}, // {0x5336775b6caa5ae0, "7322325862592278999e0074"}, // {0x6f396397b06732a4, "6014546754280072926e0209"}, // {0x5cc3220dcd5899fd, "7120190517612959703e0120"}, // {0x34a1339818257f0f, "3507665085003296281e-073"}, // {0x168a9c42e5b6d89f, "4345544743100783551e-218"}, // {0x313146fe1075e1ef, "9778613303868468131e-090"}, // {0x32d3d969e3dbe723, "7539204280836061195e-082"}, // {0x19eaba3262ee707b, "7862637540082247119e-202"}, // {0x6d43bbb4bf05f087, "2176832332097939832e0200"}, // {0x5bbe71ec1ed0a4f9, "8643988913946659879e0115"}, // {0x6079c677be6f236e, "5529436763613147623e0138"}, // {0x1fed06692e6f5ef6, "6764958008109694533e-173"}, // {0x6cbf92bacb3cb40c, "6802601037806061975e0197"}, // {0x6ccf92bacb3cb40c, "1360520207561212395e0198"}, // {0x4dcd8f2cfc20d6e8, "62259110684423957791e0047"}, // {0x1526cec51a43f41a, "88800290202542652011e-226"}, // {0x162012954b6aabba, "41010852717673354694e-221"}, // {0x161012954b6aabba, "20505426358836677347e-221"}, // {0x4f7762068a24fd55, "66102447903809911604e0055"}, // {0x5cb3220dcd5899fd, "35600952588064798515e0119"}, // {0x6e78d92d2bcc7a81, "14371240869903838702e0205"}, // {0x4cf65d3e2acd616b, "57500690832492901689e0043"}, // {0x2dbdd54c40a2f25f, "23432630639573022093e-107"}, // {0x4e02797c1d948651, "62259110684423957791e0048"}, // {0x0475b22082529425, "35620497849450218807e-306"}, // {0x6d93bbb4bf05f087, "69658634627134074624e0200"}, // {0x37362d10462a26f4, "99440755792436956989e-062"}, // {0x54d945bfa911e32a, "55277197169490210673e0081"}, // {0x01f8c5f9551c2f9a, "36992084760177624177e-318"}, // {0x2cf01b8ef28251fc, "30888265282878466443e-111"}, // }; int oldround; void SetUp(void) { oldround = fegetround(); } void TearDown(void) { fesetround(oldround); } TEST(strtod, test) { int i; for (i = 0; i < ARRAYLEN(V); ++i) { EXPECT_EQ(V[i].i, HEX(strtod(V[i].s, 0)), "strtod(%`'s)", V[i].s); } } TEST(strtod, testNearest) { fesetround(FE_TONEAREST); EXPECT_STREQ("-1.79769313486231e+308", _gc(xasprintf("%.15g", strtod("-1.79769313486231e+308", NULL)))); } TEST(strtod, testDownward) { fesetround(FE_DOWNWARD); EXPECT_STREQ("-1.79769313486232e+308", _gc(xasprintf("%.15g", strtod("-1.79769313486231e+308", NULL)))); } TEST(strtod, testUpward) { fesetround(FE_UPWARD); EXPECT_STREQ("-1.7976931348623e+308", _gc(xasprintf("%.15g", strtod("-1.79769313486231e+308", NULL)))); } TEST(strtod, testTowardzero) { char *p; for (int i = 0; i < 9999; ++i) { fesetround(FE_TOWARDZERO); EXPECT_STREQ( "-1.7976931348623e+308", (p = xasprintf("%.15g", strtod("-1.79769313486231e+308", NULL)))); fesetround(FE_TONEAREST); free(p); } } BENCH(strtod, bench) { char buf[128]; EZBENCH2("strtod", donothing, strtod("-1.79769313486231e+308", 0)); EZBENCH2("StringToDouble", donothing, StringToDouble("-1.79769313486231e+308", 22, 0)); EZBENCH2("snprintf %g", donothing, snprintf(buf, 128, "%g", -1.79769313486231e+308)); EZBENCH2("DoubleToEcmascript", donothing, DoubleToEcmascript(buf, -1.79769313486231e+308)); }
22,626
419
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/floor_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(floor, test) { EXPECT_STREQ("3", gc(xdtoa(floor(3)))); EXPECT_STREQ("3", gc(xdtoa(floor(3.14)))); EXPECT_STREQ("-4", gc(xdtoa(floor(-3.14)))); EXPECT_STREQ("-0", gc(xdtoa(floor(-0.)))); EXPECT_STREQ("NAN", gc(xdtoa(floor(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoa(floor(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(floor(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(floor(-INFINITY)))); } TEST(floorf, test) { EXPECT_STREQ("3", gc(xdtoaf(floorf(3)))); EXPECT_STREQ("3", gc(xdtoaf(floorf(3.14)))); EXPECT_STREQ("-4", gc(xdtoaf(floorf(-3.14)))); EXPECT_STREQ("-0", gc(xdtoaf(floorf(-0.)))); EXPECT_STREQ("NAN", gc(xdtoaf(floorf(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoaf(floorf(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(floorf(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(floorf(-INFINITY)))); } TEST(floorl, test) { EXPECT_STREQ("3", gc(xdtoal(floorl(3)))); EXPECT_STREQ("3", gc(xdtoal(floorl(3.14)))); EXPECT_STREQ("-4", gc(xdtoal(floorl(-3.14)))); EXPECT_STREQ("-0", gc(xdtoal(floorl(-0.)))); EXPECT_STREQ("NAN", gc(xdtoal(floorl(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoal(floorl(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoal(floorl(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(floorl(-INFINITY)))); } BENCH(floorl, bench) { double _floor(double) asm("floor"); float _floorf(float) asm("floorf"); long double _floorl(long double) asm("floorl"); EZBENCH2("floor", donothing, _floor(.7)); /* ~1ns */ EZBENCH2("floorf", donothing, _floorf(.7)); /* ~3ns */ EZBENCH2("floorl", donothing, _floorl(.7)); /* ~9ns */ }
3,570
66
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/magicu_test.c
/*-*- 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 2023 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/tinymath/magicu.h" #include "libc/intrin/kprintf.h" #include "libc/limits.h" #include "libc/macros.internal.h" #include "libc/runtime/runtime.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/tinymath/magicu.h" #define T uint32_t #define TBIT (sizeof(T) * CHAR_BIT - 1) #define TMIN (((T) ~(T)0) > 1 ? (T)0 : (T)((uintmax_t)1 << TBIT)) #define TMAX (((T) ~(T)0) > 1 ? (T) ~(T)0 : (T)(((uintmax_t)1 << TBIT) - 1)) T V[] = {5, 4, 77, 4, 7, 0, 1, 2, 3, 4, -1, -2, -3, -4, TMIN, TMIN + 1, TMIN + 2, TMIN + 3, TMIN + 5, TMIN + 7, TMAX, TMAX - 1, TMAX - 2, TMAX - 77, TMAX - 3, TMAX - 5, TMAX - 7, TMAX - 50, TMIN / 2, TMAX / 2, TMAX / 2 - 3}; TEST(magicu, test) { int i, j; for (i = 0; i < ARRAYLEN(V); ++i) { if (!V[i]) continue; struct magicu d = __magicu_get(V[i]); for (j = 0; j < ARRAYLEN(V); ++j) { EXPECT_EQ(V[j] / V[i], __magicu_div(V[j], d)); } } } BENCH(magicu, bench) { struct magicu d = __magicu_get(UINT32_MAX); EZBENCH2("__magicu_get", donothing, __magicu_get(VEIL("r", UINT32_MAX))); EZBENCH2("__magicu_div", donothing, EXPROPRIATE(__magicu_div(VEIL("r", 77u), d))); EZBENCH2("/", donothing, EXPROPRIATE(VEIL("r", 77u) / VEIL("r", UINT32_MAX))); }
3,250
57
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/exp2l_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(exp2l, test) { EXPECT_STREQ("16", gc(xdtoal(exp2l(4)))); EXPECT_STREQ("16", gc(xdtoa(exp2(4)))); EXPECT_STREQ("16", gc(xdtoaf(exp2f(4)))); }
2,102
29
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/exp_test.c
/*-*- 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/math.h" #include "libc/mem/gc.h" #include "libc/stdio/stdio.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" double _exp(double) asm("exp"); float _expf(float) asm("expf"); long double _expl(long double) asm("expl"); TEST(expl, test) { EXPECT_STREQ("1", _gc(xdtoal(_expl(0)))); EXPECT_STREQ("1", _gc(xdtoal(_expl(-0.)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(_expl(INFINITY)))); EXPECT_STREQ("0", _gc(xdtoal(_expl(-INFINITY)))); EXPECT_STREQ("NAN", _gc(xdtoal(_expl(NAN)))); // EXPECT_STREQ("0", _gc(xdtoal(_expl(-132098844872390)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(_expl(132098844872390)))); } TEST(exp, test) { EXPECT_STREQ("1", _gc(xdtoa(_exp(0)))); EXPECT_STREQ("1", _gc(xdtoa(_exp(-0.)))); EXPECT_STREQ("INFINITY", _gc(xdtoa(_exp(INFINITY)))); EXPECT_STREQ("0", _gc(xdtoa(_exp(-INFINITY)))); EXPECT_STREQ("NAN", _gc(xdtoa(_exp(NAN)))); EXPECT_STREQ("0", _gc(xdtoa(_exp(-132098844872390)))); EXPECT_STREQ("INFINITY", _gc(xdtoa(_exp(132098844872390)))); } TEST(expf, test) { EXPECT_STREQ("1", _gc(xdtoaf(_expf(0)))); EXPECT_STREQ("1", _gc(xdtoaf(_expf(-0.)))); EXPECT_STREQ("INFINITY", _gc(xdtoaf(_expf(INFINITY)))); EXPECT_STREQ("0", _gc(xdtoaf(_expf(-INFINITY)))); EXPECT_STREQ("NAN", _gc(xdtoaf(_expf(NAN)))); EXPECT_STREQ("0", _gc(xdtoaf(_expf(-132098844872390)))); EXPECT_STREQ("INFINITY", _gc(xdtoaf(_expf(132098844872390)))); } TEST(exp, fun) { ASSERT_STREQ("7.389056", _gc(xasprintf("%f", _exp(2.0)))); ASSERT_STREQ("6.389056", _gc(xasprintf("%f", expm1(2.0)))); ASSERT_STREQ("6.389056", _gc(xasprintf("%f", _exp(2.0) - 1.0))); } BENCH(expl, bench) { EZBENCH2("exp", donothing, _exp(.7)); /* ~6ns */ EZBENCH2("expf", donothing, _expf(.7)); /* ~5ns */ EZBENCH2("expl", donothing, _expl(.7)); /* ~20ns */ }
3,709
72
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/fabs_test.c
/*-*- 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _fabs(double) asm("fabs"); float _fabsf(float) asm("fabsf"); long double _fabsl(long double) asm("fabsl"); TEST(fabsl, test) { EXPECT_STREQ("0", gc(xdtoal(_fabsl(-0.)))); EXPECT_STREQ("0", gc(xdtoal(_fabsl(-0.)))); EXPECT_STREQ("NAN", gc(xdtoal(_fabsl(NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_fabsl(INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_fabsl(-INFINITY)))); } TEST(fabs, test) { EXPECT_STREQ("0", gc(xdtoa(_fabs(-0.)))); EXPECT_STREQ("0", gc(xdtoa(_fabs(-0.)))); EXPECT_STREQ("NAN", gc(xdtoa(_fabs(NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_fabs(INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_fabs(-INFINITY)))); } TEST(fabsf, test) { EXPECT_STREQ("0", gc(xdtoaf(_fabsf(-0.)))); EXPECT_STREQ("0", gc(xdtoaf(_fabsf(-0.)))); EXPECT_STREQ("NAN", gc(xdtoaf(_fabsf(NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_fabsf(INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_fabsf(-INFINITY)))); } TEST(fabs, stuff) { EXPECT_LDBL_EQ(3.14, _fabs(3.14)); EXPECT_LDBL_EQ(3.14, _fabs(-3.14)); EXPECT_EQ(1, !!isnan(_fabs(NAN))); EXPECT_EQ(1, !!isnan(_fabs(-NAN))); EXPECT_EQ(0, !!signbit(_fabs(-NAN))); } BENCH(fabs, bench) { EZBENCH2("fabs", donothing, _fabs(.7)); /* ~6ns */ EZBENCH2("fabsf", donothing, _fabsf(.7)); /* ~5ns */ EZBENCH2("fabsl", donothing, _fabsl(.7)); /* ~28ns */ }
3,332
66
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/measureentropy_test.c
/*-*- 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 2021 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/math.h" #include "libc/stdio/rand.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/hyperion.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(MeasureEntropy, test) { MeasureEntropy(kMoby, 1000); EXPECT_EQ(00, lroundl(10 * MeasureEntropy(" ", 12))); EXPECT_EQ(16, lroundl(10 * MeasureEntropy("abcabcabcabc", 12))); EXPECT_EQ(36, lroundl(10 * MeasureEntropy("abcdefghijkl", 12))); EXPECT_EQ(49, lroundl(10 * MeasureEntropy(kMoby, 512))); } TEST(MeasureEntropy, testElfExecutable) { EXPECT_EQ(19, lroundl(10 * MeasureEntropy("\ \x7f\x45\x4c\x46\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x03\x00\x3e\x00\x01\x00\x00\x00\x18\xd7\x00\x00\x00\x00\x00\x00\ \x40\x00\x00\x00\x00\x00\x00\x00\xc8\xd0\x0c\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x40\x00\x38\x00\x09\x00\x40\x00\x16\x00\x15\x00\ \x06\x00\x00\x00\x04\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\ \x40\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\ \xf8\x01\x00\x00\x00\x00\x00\x00\xf8\x01\x00\x00\x00\x00\x00\x00\ \x08\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\ \x38\x02\x00\x00\x00\x00\x00\x00\x38\x02\x00\x00\x00\x00\x00\x00\ \x38\x02\x00\x00\x00\x00\x00\x00\x19\x00\x00\x00\x00\x00\x00\x00\ \x19\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\ \x01\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x90\xbc\x00\x00\x00\x00\x00\x00\x90\xbc\x00\x00\x00\x00\x00\x00\ \x00\x10\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\ \x00\xc0\x00\x00\x00\x00\x00\x00\x00\xc0\x00\x00\x00\x00\x00\x00\ \x00\xc0\x00\x00\x00\x00\x00\x00\x62\xb2\x09\x00\x00\x00\x00\x00\ \x62\xb2\x09\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\ \x01\x00\x00\x00\x04\x00\x00\x00\x00\x80\x0a\x00\x00\x00\x00\x00\ \x00\x80\x0a\x00\x00\x00\x00\x00\x00\x80\x0a\x00\x00\x00\x00\x00\ \xe4\x09\x02\x00\x00\x00\x00\x00\xe4\x09\x02\x00\x00\x00\x00\x00\ \x00\x10\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00\ \xc0\x95\x0c\x00\x00\x00\x00\x00\xc0\xa5\x0c\x00\x00\x00\x00\x00\ \xc0\xa5\x0c\x00\x00\x00\x00\x00\x64\x3a\x00\x00\x00\x00\x00\x00\ \xa8\x42\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\ \x02\x00\x00\x00\x06\x00\x00\x00\xa0\xc2\x0c\x00\x00\x00\x00\x00\ \xa0\xd2\x0c\x00\x00\x00\x00\x00\xa0\xd2\x0c\x00\x00\x00\x00\x00\ \x80\x01\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00\x00\x00\ \x08\x00\x00\x00\x00\x00\x00\x00\x51\xe5\x74\x64\x06\x00\x00\x00\ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00", 512))); } TEST(MeasureEntropy, testApeExecutable) { EXPECT_EQ(53, lroundl(10 * MeasureEntropy("\ \x7f\x45\x4c\x46\x02\x01\x01\x09\x00\x00\x00\x00\x00\x00\x00\x00\ \x02\x00\x3e\x00\x01\x00\x00\x00\x0a\x11\x40\x00\x00\x00\x00\x00\ \xe0\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x40\x00\x38\x00\x04\x00\x00\x00\x00\x00\x00\x00\ \xb2\x40\xeb\x00\xeb\x14\x90\x90\xeb\x06\x48\x83\xec\x08\x31\xd2\ \xbd\x00\x00\xeb\x05\xe9\xb0\x10\x00\x00\xfc\x0f\x1f\x87\x3e\xe0\ \xbf\x00\x70\x31\xc9\x8e\xc1\xfa\x8e\xd7\x89\xcc\xfb\x0e\x1f\xe8\ \x00\x00\x5e\x81\xee\x72\x00\xb8\x00\x02\x50\x50\x07\x31\xff\xb9\ \x00\x02\xf3\xa4\x0f\x1f\x87\xd2\xff\xea\x8e\x20\x00\x00\x8e\xd9\ \xb9\x00\x1b\xb8\x50\x00\x8e\xc0\x31\xc0\x31\xff\xf3\xaa\x80\xfa\ \x40\x74\x13\xe8\x15\x00\x07\xb0\x01\x31\xc9\x30\xf6\xbf\x40\x03\ \xe8\x48\x00\x4f\x75\xfa\xea\x20\x25\x00\x00\x53\x52\xb4\x08\xcd\ \x13\x72\x2c\x88\xcf\x80\xe7\x3f\x80\xe1\xc0\xd0\xc1\xd0\xc1\x86\ \xcd\x1e\x06\x1f\x31\xf6\x8e\xc6\xbe\x10\x15\x87\xf7\xa5\xa5\xa5\ \xa5\xa5\xa4\x1f\x93\xab\x91\xab\x92\xab\x58\xaa\x92\x5b\xc3\x5a\ \x80\xf2\x80\x31\xc0\xcd\x13\x72\xf7\xeb\xc1\x50\x51\x86\xcd\xd0\ \xc9\xd0\xc9\x08\xc1\x31\xdb\xb0\x01\xb4\x02\xcd\x13\x59\x58\x72\ \x1d\x8c\xc6\x83\xc6\x20\x8e\xc6\xfe\xc0\x3a\x06\x1c\x15\x76\x0d\ \xb0\x01\xfe\xc6\x3a\x36\x20\x15\x76\x03\x30\xf6\x41\xc3\x50\x31\ \xc0\xcd\x13\x58\xeb\xc5\x89\xfe\xac\x84\xc0\x74\x09\xbb\x07\x00\ \xb4\x0e\xcd\x10\xeb\xf2\xc3\x57\xbf\x78\x24\xe8\xe8\xff\x5f\xe8\ \xe4\xff\xbf\x80\x24\xe8\xde\xff\xf3\x90\xeb\xfc\xb9\x04\x00\xbe\ \x00\x04\xad\x85\xc0\x74\x0b\x51\x56\x97\xbe\x9e\x24\xe8\x05\x00\ \x5e\x59\xe2\xee\xc3\x89\xfa\x85\xd2\x74\x14\x52\x56\x31\xc9\xb1\ \x03\x01\xca\xac\x5e\x0c\x80\xee\x5a\xac\xee\x42\x49\x79\xfa\xc3\ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x55\xaa", 512))); } TEST(MeasureEntropy, testDevUrandom) { EXPECT_EQ(76, lroundl(10 * MeasureEntropy("\ \x67\xa6\x8f\x6f\x6f\x01\xa4\x60\xa5\x5a\x6a\xfa\x06\xfd\xbb\xbe\ \xe4\x73\x2f\xc4\x20\x25\xd9\xb2\x78\x7d\x9f\xaf\x5d\x03\x49\x01\ \x90\x94\x8a\xc0\x33\x78\xe0\x65\xd8\x9c\x19\x96\x25\x90\xc8\x6e\ \xf2\x65\xac\x72\xd9\x8e\x58\x05\x26\x22\xa3\x58\x41\x40\xee\x3c\ \x3a\xb0\x08\x7c\x70\x0e\x23\xdc\x52\x10\x21\x5c\xb7\x3d\x3a\xce\ \x7c\xd7\x2a\x8c\x35\x0c\x21\x2b\x03\x75\xbe\x0c\x49\x5b\xdb\x7a\ \x7e\xeb\x27\x03\x56\xca\x9f\xf8\x00\xb4\x4d\x5b\xd6\xde\x41\xd6\ \x86\x7b\x3c\x13\xd4\x2d\xd4\xe9\x79\x05\x48\xcc\xa5\x17\xf8\xb6\ \x74\xdf\x39\x70\x32\x64\x70\x10\xfb\x53\x06\x87\xef\xff\x9f\x4c\ \x07\xee\x09\x54\xb8\xda\x1a\x49\x9b\x12\x3a\xf9\xc3\x55\xbc\xa5\ \xad\x6c\x3d\x1f\x39\x84\xc6\xac\x45\x14\x4f\xa9\xfc\xd6\xfb\xce\ \x41\xf8\x78\x85\xe5\x72\x9c\x0c\xd3\x81\x9b\xa4\x72\xc9\xd5\xc8\ \xc2\x3c\xcc\x36\x58\xf6\x23\x2a\x2e\x9c\x38\xea\x73\x17\xf0\x72\ \x2d\x57\xf8\xc5\x62\x84\xb7\xce\x24\x7b\x46\x65\xc7\xf3\x78\x88\ \x77\x36\x93\x25\x5d\x78\xc9\xfb\x24\xbb\xec\x2f\x77\x4f\x82\xd8\ \x63\x23\xd1\x39\x54\x78\x14\x7d\x24\xc8\x1a\xed\x32\x2d\x7b\xdc\ \xf4\x92\xb1\xaf\x0d\x9b\xba\xb6\x72\xfb\xb2\x7f\x8f\xd5\x4c\x5e\ \x05\xa7\x05\x15\xc5\x51\xd0\x86\x0a\xce\x04\x84\x1e\xba\x44\xf3\ \xbc\x09\xa9\x4e\xc1\xc7\xbd\x7e\x45\x38\x04\xa3\x6c\xfa\x46\x57\ \x40\x93\xbf\xdd\x12\x05\x6c\xb0\xee\x08\x40\x74\xc9\xda\xe7\x30\ \xfa\x1f\x12\xc8\x31\x33\x53\xe4\x65\x2a\xe8\xbf\x2b\x3c\xd6\xcc\ \xec\x8f\x9a\x6f\xe1\xe0\xe6\x81\x0a\xf5\x46\x07\xeb\xcd\xba\xcb\ \xa1\xe5\x4a\x42\xd6\x20\xce\xb6\x16\xcf\x73\x30\x25\x17\xe3\x00\ \x2b\x58\x93\x86\x74\x57\x48\x8b\x2a\x35\x88\xc7\x84\x18\x53\x23\ \xba\xc3\x06\x0a\xd7\x09\xf2\xcb\xe1\xb1\x39\x07\xaf\x2d\xb2\xbc\ \x7d\x71\x91\x6f\x71\x53\x82\xed\x51\x96\xbf\x90\xb4\x4a\x9a\x20\ \x21\x8a\x72\xa3\xbc\xfc\xb9\xcd\x47\x5e\x38\x9c\xd2\xf9\xae\x7f\ \xb2\x1a\x2a\x81\x68\x31\x53\xb2\x11\xfa\x80\x71\x31\xdd\xde\x56\ \x9c\x5f\x3f\x50\xb5\x5f\x99\x5d\x36\x34\x23\x13\xa9\xf0\x04\x3d\ \xa0\xee\x1c\xe5\xe3\x8d\x60\x76\x62\x5a\xd2\xa3\xa2\x51\xea\x75\ \xab\x1f\x2f\xc4\x08\x35\x5d\xf3\x28\x5d\x59\x67\x88\xf0\x61\x6c\ \x8b\x5f\xaf\x90\xa9\x90\xfe\x36\x29\xcc\x02\xf7\x2f\xa7\x19\x0e", 512))); } BENCH(getrandom, bench) { EZBENCH2("MeasureEntropy", donothing, MeasureEntropy(kMoby, 512)); }
9,236
149
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/exp10_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _exp10(double) asm("exp10"); float _exp10f(float) asm("exp10f"); long double _exp10l(long double) asm("exp10l"); TEST(exp10l, test) { EXPECT_STREQ("1", gc(xdtoal(_exp10l(0)))); EXPECT_STREQ("1", gc(xdtoal(_exp10l(-0.)))); EXPECT_STREQ(".01", gc(xdtoal(_exp10l(-2.)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_exp10l(INFINITY)))); EXPECT_STREQ("0", gc(xdtoal(_exp10l(-INFINITY)))); EXPECT_STREQ("NAN", gc(xdtoal(_exp10l(NAN)))); EXPECT_STREQ("0", gc(xdtoal(_exp10l(-132098844872390)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_exp10l(132098844872390)))); } TEST(exp10, test) { EXPECT_STREQ("1", gc(xdtoa(_exp10(0)))); EXPECT_STREQ("1", gc(xdtoa(_exp10(-0.)))); EXPECT_STREQ(".01", gc(xdtoa(_exp10(-2.)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_exp10(INFINITY)))); EXPECT_STREQ("0", gc(xdtoa(_exp10(-INFINITY)))); EXPECT_STREQ("NAN", gc(xdtoa(_exp10(NAN)))); EXPECT_STREQ("0", gc(xdtoa(_exp10(-132098844872390)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_exp10(132098844872390)))); } TEST(exp10f, test) { EXPECT_STREQ("1", gc(xdtoaf(_exp10f(0)))); EXPECT_STREQ("1", gc(xdtoaf(_exp10f(-0.)))); EXPECT_STREQ(".01", gc(xdtoaf(_exp10f(-2.)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_exp10f(INFINITY)))); EXPECT_STREQ("0", gc(xdtoaf(_exp10f(-INFINITY)))); EXPECT_STREQ("NAN", gc(xdtoaf(_exp10f(NAN)))); EXPECT_STREQ("0", gc(xdtoaf(_exp10f(-132098844872390)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_exp10f(132098844872390)))); }
3,409
60
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/asin_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/stdio/rand.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" double asin_(double) asm("asin"); #define asin asin_ TEST(asin, test) { EXPECT_STREQ("0", _gc(xasprintf("%.15g", asin(0.)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", asin(-0.)))); EXPECT_STREQ("0.523598775598299", _gc(xasprintf("%.15g", asin(.5)))); EXPECT_STREQ("-0.523598775598299", _gc(xasprintf("%.15g", asin(-.5)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", asin(1.)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", asin(-1.)))); EXPECT_TRUE(isnan(asin(1.5))); EXPECT_TRUE(isnan(asin(-1.5))); EXPECT_TRUE(isnan(asin(NAN))); EXPECT_TRUE(isnan(asin(-NAN))); EXPECT_TRUE(isnan(asin(INFINITY))); EXPECT_TRUE(isnan(asin(-INFINITY))); EXPECT_STREQ("2.2250738585072e-308", _gc(xasprintf("%.15g", asin(__DBL_MIN__)))); EXPECT_TRUE(isnan(asin(__DBL_MAX__))); } BENCH(asinl, bench) { double _asin(double) asm("asin"); float _asinf(float) asm("asinf"); long double _asinl(long double) asm("asinl"); EZBENCH2("-asin", donothing, _asin(.7)); /* ~16ns */ EZBENCH2("-asinf", donothing, _asinf(.7)); /* ~12ns */ EZBENCH2("-asinl", donothing, _asinl(.7)); /* ~39ns */ }
3,156
55
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/tgamma_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" TEST(tgamma, test) { EXPECT_STREQ("inf", _gc(xasprintf("%.15g", tgamma(0.)))); EXPECT_STREQ("-inf", _gc(xasprintf("%.15g", tgamma(-0.)))); EXPECT_STREQ("9.51350769866873", _gc(xasprintf("%.15g", tgamma(.1)))); EXPECT_STREQ("-10.6862870211932", _gc(xasprintf("%.15g", tgamma(-.1)))); EXPECT_STREQ("1.77245385090552", _gc(xasprintf("%.15g", tgamma(.5)))); EXPECT_STREQ("-3.54490770181103", _gc(xasprintf("%.15g", tgamma(-.5)))); EXPECT_STREQ("1", _gc(xasprintf("%.15g", tgamma(1.)))); EXPECT_TRUE(isnan(tgamma(-1.))); EXPECT_STREQ("0.886226925452758", _gc(xasprintf("%.15g", tgamma(1.5)))); EXPECT_STREQ("2.36327180120735", _gc(xasprintf("%.15g", tgamma(-1.5)))); EXPECT_STREQ("1", _gc(xasprintf("%.15g", tgamma(2.)))); EXPECT_TRUE(isnan(tgamma(NAN))); EXPECT_TRUE(isnan(tgamma(-NAN))); EXPECT_STREQ("inf", _gc(xasprintf("%.15g", tgamma(INFINITY)))); EXPECT_TRUE(isnan(tgamma(-INFINITY))); EXPECT_STREQ("4.49423283715579e+307", _gc(xasprintf("%.15g", tgamma(__DBL_MIN__)))); EXPECT_STREQ("inf", _gc(xasprintf("%.15g", tgamma(__DBL_MAX__)))); EXPECT_STREQ("4.5035996273705e+15", _gc(xasprintf("%.14g", tgamma(-1.0000000000000002)))); EXPECT_STREQ("-47453133.3893415", _gc(xasprintf("%.15g", tgamma(-2.1073424255447e-08)))); }
3,255
48
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/exp2_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _exp2(double) asm("exp2"); float _exp2f(float) asm("exp2f"); long double _exp2l(long double) asm("exp2l"); TEST(exp2l, test) { EXPECT_STREQ("1", gc(xdtoal(_exp2l(0)))); EXPECT_STREQ("1", gc(xdtoal(_exp2l(-0.)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_exp2l(INFINITY)))); EXPECT_STREQ("0", gc(xdtoal(_exp2l(-INFINITY)))); EXPECT_STREQ("NAN", gc(xdtoal(_exp2l(NAN)))); EXPECT_STREQ("0", gc(xdtoal(_exp2l(-132098844872390)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_exp2l(132098844872390)))); } TEST(exp2, test) { EXPECT_STREQ("1", gc(xdtoa(_exp2(0)))); EXPECT_STREQ("1", gc(xdtoa(_exp2(-0.)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_exp2(INFINITY)))); EXPECT_STREQ("0", gc(xdtoa(_exp2(-INFINITY)))); EXPECT_STREQ("NAN", gc(xdtoa(_exp2(NAN)))); EXPECT_STREQ("0", gc(xdtoa(_exp2(-132098844872390)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_exp2(132098844872390)))); } TEST(exp2f, test) { EXPECT_STREQ("1", gc(xdtoaf(_exp2f(0)))); EXPECT_STREQ("1", gc(xdtoaf(_exp2f(-0.)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_exp2f(INFINITY)))); EXPECT_STREQ("0", gc(xdtoaf(_exp2f(-INFINITY)))); EXPECT_STREQ("NAN", gc(xdtoaf(_exp2f(NAN)))); EXPECT_STREQ("0", gc(xdtoaf(_exp2f(-132098844872390)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_exp2f(132098844872390)))); } BENCH(_exp2l, bench) { EZBENCH2("exp2", donothing, _exp2(.7)); /* ~6ns */ EZBENCH2("exp2f", donothing, _exp2f(.7)); /* ~5ns */ EZBENCH2("exp2l", donothing, _exp2l(.7)); /* ~30ns */ }
3,462
64
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/trunc_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _trunc(double) asm("trunc"); float _truncf(float) asm("truncf"); long double _truncl(long double) asm("truncl"); TEST(trunc, test) { EXPECT_STREQ("3", gc(xdtoa(_trunc(3)))); EXPECT_STREQ("3", gc(xdtoa(_trunc(3.14)))); EXPECT_STREQ("-3", gc(xdtoa(_trunc(-3.14)))); EXPECT_STREQ("-0", gc(xdtoa(_trunc(-0.)))); EXPECT_STREQ("NAN", gc(xdtoa(_trunc(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoa(_trunc(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_trunc(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(_trunc(-INFINITY)))); } TEST(truncf, test) { EXPECT_STREQ("3", gc(xdtoaf(_truncf(3)))); EXPECT_STREQ("3", gc(xdtoaf(_truncf(3.14)))); EXPECT_STREQ("-3", gc(xdtoaf(_truncf(-3.14)))); EXPECT_STREQ("-0", gc(xdtoaf(_truncf(-0.)))); EXPECT_STREQ("NAN", gc(xdtoaf(_truncf(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoaf(_truncf(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_truncf(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(_truncf(-INFINITY)))); } TEST(truncl, test) { EXPECT_STREQ("3", gc(xdtoal(_truncl(3)))); EXPECT_STREQ("3", gc(xdtoal(_truncl(3.14)))); EXPECT_STREQ("-3", gc(xdtoal(_truncl(-3.14)))); EXPECT_STREQ("-0", gc(xdtoal(_truncl(-0.)))); EXPECT_STREQ("NAN", gc(xdtoal(_truncl(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoal(_truncl(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_truncl(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(_truncl(-INFINITY)))); } BENCH(truncl, bench) { EZBENCH2("trunc", donothing, _trunc(.7)); // ~1ns EZBENCH2("truncf", donothing, _truncf(.7)); // ~1ns EZBENCH2("truncl", donothing, _truncl(.7)); // ~7ns }
3,583
67
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/ldexp_test.c
/*-*- 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/math.h" #include "libc/mem/gc.h" #include "libc/stdio/rand.h" #include "libc/stdio/stdio.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" double _ldexp(double, int) asm("ldexp"); float _ldexpf(float, int) asm("ldexpf"); long double _ldexpl(long double, int) asm("ldexpl"); int rando; void SetUp(void) { rando = rand() & 0xffff; } TEST(ldexpl, test) { EXPECT_EQ(rando, _ldexpl(rando, 0)); EXPECT_STREQ("NAN", _gc(xdtoal(_ldexpl(NAN, 0)))); EXPECT_STREQ("-NAN", _gc(xdtoal(_ldexpl(-NAN, 0)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(_ldexpl(INFINITY, 0)))); EXPECT_STREQ("-INFINITY", _gc(xdtoal(_ldexpl(-INFINITY, 0)))); EXPECT_STREQ("NAN", _gc(xdtoal(_ldexpl(NAN, 1)))); EXPECT_STREQ("-NAN", _gc(xdtoal(_ldexpl(-NAN, 1)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(_ldexpl(INFINITY, 1)))); EXPECT_STREQ("-INFINITY", _gc(xdtoal(_ldexpl(-INFINITY, 1)))); EXPECT_STREQ("16384", _gc(xdtoal(log2l(LDBL_MAX)))); EXPECT_STREQ(".00390625", _gc(xdtoal(_ldexpl(1, -8)))); EXPECT_STREQ("0", _gc(xdtoal(_ldexpl(0, -8)))); EXPECT_STREQ("0", _gc(xdtoal(_ldexpl(0, 8)))); EXPECT_STREQ("256", _gc(xdtoal(_ldexpl(1, 8)))); EXPECT_STREQ("512", _gc(xdtoal(_ldexpl(2, 8)))); EXPECT_STREQ("768", _gc(xdtoal(_ldexpl(3, 8)))); EXPECT_STREQ("6.997616471358197e+3461", _gc(xdtoal(_ldexpl(1, 11500)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(_ldexpl(1, 999999)))); // EXPECT_STREQ("0", _gc(xdtoal(_ldexpl(1, -999999)))); } TEST(ldexp, test) { EXPECT_EQ(rando, _ldexp(rando, 0)); EXPECT_STREQ("NAN", _gc(xdtoa(_ldexp(NAN, 0)))); EXPECT_STREQ("-NAN", _gc(xdtoa(_ldexp(-NAN, 0)))); EXPECT_STREQ("INFINITY", _gc(xdtoa(_ldexp(INFINITY, 0)))); EXPECT_STREQ("-INFINITY", _gc(xdtoa(_ldexp(-INFINITY, 0)))); EXPECT_STREQ("NAN", _gc(xdtoa(_ldexp(NAN, 1)))); EXPECT_STREQ("-NAN", _gc(xdtoa(_ldexp(-NAN, 1)))); EXPECT_STREQ("INFINITY", _gc(xdtoa(_ldexp(INFINITY, 1)))); EXPECT_STREQ("-INFINITY", _gc(xdtoa(_ldexp(-INFINITY, 1)))); EXPECT_STREQ("16384", _gc(xdtoa(log2l(LDBL_MAX)))); EXPECT_STREQ(".00390625", _gc(xdtoa(_ldexp(1, -8)))); EXPECT_STREQ("0", _gc(xdtoa(_ldexp(0, -8)))); EXPECT_STREQ("0", _gc(xdtoa(_ldexp(0, 8)))); EXPECT_STREQ("256", _gc(xdtoa(_ldexp(1, 8)))); EXPECT_STREQ("512", _gc(xdtoa(_ldexp(2, 8)))); EXPECT_STREQ("768", _gc(xdtoa(_ldexp(3, 8)))); EXPECT_STREQ("INFINITY", _gc(xdtoa(_ldexp(1, 999999)))); EXPECT_STREQ("0", _gc(xdtoa(_ldexp(1, -999999)))); } TEST(ldexpf, test) { EXPECT_EQ(rando, _ldexpf(rando, 0)); EXPECT_STREQ("NAN", _gc(xdtoaf(_ldexpf(NAN, 0)))); EXPECT_STREQ("-NAN", _gc(xdtoaf(_ldexpf(-NAN, 0)))); EXPECT_STREQ("INFINITY", _gc(xdtoaf(_ldexpf(INFINITY, 0)))); EXPECT_STREQ("-INFINITY", _gc(xdtoaf(_ldexpf(-INFINITY, 0)))); EXPECT_STREQ("NAN", _gc(xdtoaf(_ldexpf(NAN, 1)))); EXPECT_STREQ("-NAN", _gc(xdtoaf(_ldexpf(-NAN, 1)))); EXPECT_STREQ("INFINITY", _gc(xdtoaf(_ldexpf(INFINITY, 1)))); EXPECT_STREQ("-INFINITY", _gc(xdtoaf(_ldexpf(-INFINITY, 1)))); EXPECT_STREQ("16384", _gc(xdtoaf(log2l(LDBL_MAX)))); EXPECT_STREQ(".00390625", _gc(xdtoaf(_ldexpf(1, -8)))); EXPECT_STREQ("0", _gc(xdtoaf(_ldexpf(0, -8)))); EXPECT_STREQ("0", _gc(xdtoaf(_ldexpf(0, 8)))); EXPECT_STREQ("256", _gc(xdtoaf(_ldexpf(1, 8)))); EXPECT_STREQ("512", _gc(xdtoaf(_ldexpf(2, 8)))); EXPECT_STREQ("768", _gc(xdtoaf(_ldexpf(3, 8)))); EXPECT_STREQ("INFINITY", _gc(xdtoaf(_ldexpf(1, 999999)))); EXPECT_STREQ("0", _gc(xdtoaf(_ldexpf(1, -999999)))); } TEST(ldexp, stuff) { volatile int twopow = 5; volatile double pi = 3.14; ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", _ldexp(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", _ldexpf(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", scalb(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", scalbf(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", scalbn(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", scalbnf(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", scalbln(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2f", scalblnf(pi, twopow)))); #ifndef __aarch64__ // TODO: implement quad floating point in printf ASSERT_STREQ("100.48", _gc(xasprintf("%.2Lf", _ldexpl(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2Lf", scalbnl(pi, twopow)))); ASSERT_STREQ("100.48", _gc(xasprintf("%.2Lf", scalblnl(pi, twopow)))); #endif } BENCH(ldexpl, bench) { EZBENCH2("ldexp", donothing, _ldexp(.7, 3)); // ~2ns EZBENCH2("ldexpf", donothing, _ldexpf(.7, 3)); // ~2ns EZBENCH2("ldexpl", donothing, _ldexpl(.7, 3)); // ~8ns }
6,490
126
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/ceil_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(ceil, test) { EXPECT_STREQ("3", gc(xdtoa(ceil(3)))); EXPECT_STREQ("4", gc(xdtoa(ceil(3.14)))); EXPECT_STREQ("-3", gc(xdtoa(ceil(-3.14)))); EXPECT_STREQ("-0", gc(xdtoa(ceil(-0.)))); EXPECT_STREQ("NAN", gc(xdtoa(ceil(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoa(ceil(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(ceil(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(ceil(-INFINITY)))); } TEST(ceilf, test) { EXPECT_STREQ("3", gc(xdtoaf(ceilf(3)))); EXPECT_STREQ("4", gc(xdtoaf(ceilf(3.14)))); EXPECT_STREQ("-3", gc(xdtoaf(ceilf(-3.14)))); EXPECT_STREQ("-0", gc(xdtoaf(ceilf(-0.)))); EXPECT_STREQ("NAN", gc(xdtoaf(ceilf(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoaf(ceilf(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(ceilf(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(ceilf(-INFINITY)))); } TEST(ceill, test) { EXPECT_STREQ("3", gc(xdtoal(ceill(3)))); EXPECT_STREQ("4", gc(xdtoal(ceill(3.14)))); EXPECT_STREQ("-3", gc(xdtoal(ceill(-3.14)))); EXPECT_STREQ("-0", gc(xdtoal(ceill(-0.)))); EXPECT_STREQ("NAN", gc(xdtoal(ceill(NAN)))); EXPECT_STREQ("-NAN", gc(xdtoal(ceill(-NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoal(ceill(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(ceill(-INFINITY)))); } BENCH(ceill, bench) { double _ceil(double) asm("ceil"); float _ceilf(float) asm("ceilf"); long double _ceill(long double) asm("ceill"); EZBENCH2("ceil", donothing, _ceil(.7)); /* ~1ns */ EZBENCH2("ceilf", donothing, _ceilf(.7)); /* ~3ns */ EZBENCH2("ceill", donothing, _ceill(.7)); /* ~9ns */ }
3,530
66
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/log10_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _log10(double) asm("log10"); float _log10f(float) asm("log10f"); long double _log10l(long double) asm("log10l"); TEST(log10l, test) { EXPECT_STREQ("1", gc(xdtoal(_log10l(10)))); EXPECT_STREQ("NAN", gc(xdtoal(_log10l(NAN)))); EXPECT_STREQ("0", gc(xdtoal(_log10l(1)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_log10l(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(_log10l(0)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(_log10l(-0.)))); EXPECT_TRUE(isnan(_log10l(-1))); EXPECT_TRUE(isnan(_log10l(-2))); } TEST(log10, test) { EXPECT_STREQ(".301029995663981", gc(xdtoa(_log10(2)))); EXPECT_STREQ("2", gc(xdtoa(_log10(100)))); EXPECT_STREQ("NAN", gc(xdtoa(_log10(NAN)))); EXPECT_STREQ("0", gc(xdtoa(_log10(1)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_log10(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(_log10(0)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(_log10(-0.)))); EXPECT_TRUE(isnan(_log10(-1))); EXPECT_TRUE(isnan(_log10(-2))); } TEST(log10f, test) { EXPECT_STREQ("1", gc(xdtoaf(_log10f(10)))); EXPECT_STREQ("NAN", gc(xdtoaf(_log10f(NAN)))); EXPECT_STREQ("0", gc(xdtoaf(_log10f(1)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_log10f(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(_log10f(0)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(_log10f(-0.)))); EXPECT_TRUE(isnan(_log10f(-1))); EXPECT_TRUE(isnan(_log10f(-2))); } BENCH(log10, bench) { EZBENCH2("log10", donothing, _log10(.7)); // ~17ns EZBENCH2("log10f", donothing, _log10f(.7)); // ~15ns EZBENCH2("log10l", donothing, _log10l(.7)); // ~21ns }
3,544
68
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/sqrt_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _sqrt(double) asm("sqrt"); float _sqrtf(float) asm("sqrtf"); long double _sqrtl(long double) asm("sqrtl"); TEST(sqrtl, test) { EXPECT_STREQ("7", gc(xdtoal(_sqrtl(7 * 7)))); EXPECT_STREQ("NAN", gc(xdtoal(_sqrtl(NAN)))); EXPECT_STREQ("0", gc(xdtoal(_sqrtl(0)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_sqrtl(INFINITY)))); EXPECT_TRUE(isnan(_sqrtl(-1))); } TEST(sqrt, test) { EXPECT_STREQ("7", gc(xdtoa(_sqrt(7 * 7)))); EXPECT_STREQ("NAN", gc(xdtoa(_sqrt(NAN)))); EXPECT_STREQ("0", gc(xdtoa(_sqrt(0)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_sqrt(INFINITY)))); EXPECT_TRUE(isnan(_sqrt(-1))); } TEST(sqrtf, test) { EXPECT_STREQ("7", gc(xdtoaf(_sqrtf(7 * 7)))); EXPECT_STREQ("NAN", gc(xdtoaf(_sqrtf(NAN)))); EXPECT_STREQ("0", gc(xdtoaf(_sqrtf(0)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_sqrtf(INFINITY)))); EXPECT_TRUE(isnan(_sqrtf(-1))); } BENCH(_sqrt, bench) { EZBENCH2("sqrt", donothing, _sqrt(.7)); /* ~2ns */ EZBENCH2("sqrtf", donothing, _sqrtf(.7)); /* ~1ns */ EZBENCH2("sqrtl", donothing, _sqrtl(.7)); /* ~9ns */ }
3,043
58
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/ilogb_test.c
/*-*- 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/math.h" #include "libc/testlib/testlib.h" TEST(ilogb, test) { /* TODO(jart): this */ /* EXPECT_EQ(0x7fffffff, ilogb(INFINITY)); */ EXPECT_EQ(0, ilogb(1)); EXPECT_EQ(1, ilogb(2)); EXPECT_EQ(2, ilogb(4)); EXPECT_EQ(63, ilogb(1e19)); }
2,097
30
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/log1p_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(log1p, test) { EXPECT_STREQ("1", gc(xdtoa(log1p(M_E - 1)))); EXPECT_STREQ("2", gc(xdtoa(log1p(M_E * M_E - 1)))); EXPECT_STREQ("NAN", gc(xdtoa(log1p(NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(log1p(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(log1p(-1)))); EXPECT_TRUE(isnan(log1p(-2))); } TEST(log1pf, test) { EXPECT_STREQ("1", gc(xdtoaf(log1pf(M_E - 1)))); EXPECT_STREQ("NAN", gc(xdtoaf(log1pf(NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(log1pf(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(log1pf(-1)))); EXPECT_TRUE(isnan(log1pf(-2))); } TEST(log1pl, test) { EXPECT_STREQ("1", gc(xdtoal(log1pl(1.71828182845904523536L)))); EXPECT_STREQ("NAN", gc(xdtoal(log1pl(NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoal(log1pl(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(log1pl(-1)))); EXPECT_TRUE(isnan(log1pl(-2))); } BENCH(log1p, bench) { double _log1p(double) asm("log1p"); float _log1pf(float) asm("log1pf"); long double _log1pl(long double) asm("log1pl"); EZBENCH2("log1p", donothing, _log1p(.7)); /* ~17ns */ EZBENCH2("log1pf", donothing, _log1pf(.7)); /* ~17ns */ EZBENCH2("log1pl", donothing, _log1pl(.7)); /* ~28ns */ }
3,169
58
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/gamma_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(lgamma, test) { EXPECT_STREQ("NAN", gc(xdtoa(lgamma(NAN)))); EXPECT_STREQ("0", gc(xdtoa(lgamma(1)))); EXPECT_STREQ("0", gc(xdtoa(lgamma(2)))); EXPECT_STREQ("INFINITY", gc(xdtoa(lgamma(INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoa(lgamma(-INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoa(lgamma(-1)))); EXPECT_STREQ("INFINITY", gc(xdtoa(lgamma(-2)))); EXPECT_STREQ("1.26551212348465", gc(xdtoa(lgamma(-.5)))); }
2,383
34
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/sinh_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _sinh(double) asm("sinh"); float _sinhf(float) asm("sinhf"); long double _sinhl(long double) asm("sinhl"); TEST(sinh, test) { EXPECT_STREQ(".521095305493747", gc(xdtoa(_sinh(+.5)))); EXPECT_STREQ("-.521095305493747", gc(xdtoa(_sinh(-.5)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_sinh(30000)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(_sinh(-30000)))); EXPECT_STREQ("0", gc(xdtoa(_sinh(0)))); EXPECT_STREQ("-0", gc(xdtoa(_sinh(-0.)))); EXPECT_STREQ("NAN", gc(xdtoa(_sinh(NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_sinh(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(_sinh(-INFINITY)))); } TEST(sinhl, test) { EXPECT_STREQ(".5210953054937474", gc(xdtoal(_sinhl(+.5)))); EXPECT_STREQ("-.5210953054937474", gc(xdtoal(_sinhl(-.5)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_sinhl(30000)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(_sinhl(-30000)))); EXPECT_STREQ("0", gc(xdtoal(_sinhl(0)))); EXPECT_STREQ("-0", gc(xdtoal(_sinhl(-0.)))); EXPECT_STREQ("NAN", gc(xdtoal(_sinhl(NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_sinhl(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(_sinhl(-INFINITY)))); } TEST(sinhf, test) { EXPECT_STREQ(".521095", gc(xdtoaf(_sinhf(+.5)))); EXPECT_STREQ("-.521095", gc(xdtoaf(_sinhf(-.5)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_sinhf(30000)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(_sinhf(-30000)))); EXPECT_STREQ("0", gc(xdtoaf(_sinhf(0)))); EXPECT_STREQ("-0", gc(xdtoaf(_sinhf(-0.)))); EXPECT_STREQ("NAN", gc(xdtoaf(_sinhf(NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_sinhf(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(_sinhf(-INFINITY)))); } BENCH(sinh, bench) { EZBENCH2("sinh", donothing, _sinh(.7)); // ~24ns EZBENCH2("sinhf", donothing, _sinhf(.7)); // ~19ns EZBENCH2("sinhl", donothing, _sinhl(.7)); // ~15ns }
3,794
70
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/expm1_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" double _expm1(double) asm("expm1"); float _expm1f(float) asm("expm1f"); long double _expm1l(long double) asm("expm1l"); TEST(expm1, test) { EXPECT_STREQ("0", _gc(xdtoa(_expm1(0)))); EXPECT_STREQ("-0", _gc(xdtoa(_expm1(-0.)))); EXPECT_STREQ("NAN", _gc(xdtoa(_expm1(NAN)))); EXPECT_STREQ("-1", _gc(xdtoa(_expm1(-INFINITY)))); EXPECT_STREQ("INFINITY", _gc(xdtoa(_expm1(INFINITY)))); /* EXPECT_STREQ("-INFINITY", _gc(xdtoa(_expm1(-132098844872390)))); */ /* EXPECT_STREQ("INFINITY", _gc(xdtoa(_expm1(132098844872390)))); */ EXPECT_STREQ("0", _gc(xasprintf("%.15g", _expm1(0.)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _expm1(-0.)))); EXPECT_STREQ("0.648721270700128", _gc(xasprintf("%.15g", _expm1(.5)))); EXPECT_STREQ("-0.393469340287367", _gc(xasprintf("%.15g", _expm1(-.5)))); EXPECT_STREQ("1.71828182845905", _gc(xasprintf("%.15g", _expm1(1.)))); EXPECT_STREQ("-0.632120558828558", _gc(xasprintf("%.15g", _expm1(-1.)))); EXPECT_STREQ("3.48168907033806", _gc(xasprintf("%.15g", _expm1(1.5)))); EXPECT_STREQ("-0.77686983985157", _gc(xasprintf("%.15g", _expm1(-1.5)))); EXPECT_STREQ("6.38905609893065", _gc(xasprintf("%.15g", _expm1(2.)))); EXPECT_TRUE(isnan(_expm1(NAN))); EXPECT_TRUE(isnan(_expm1(-NAN))); EXPECT_STREQ("inf", _gc(xasprintf("%.15g", _expm1(INFINITY)))); EXPECT_STREQ("-1", _gc(xasprintf("%.15g", _expm1(-INFINITY)))); EXPECT_STREQ("2.2250738585072e-308", _gc(xasprintf("%.15g", _expm1(__DBL_MIN__)))); EXPECT_STREQ("inf", _gc(xasprintf("%.15g", _expm1(__DBL_MAX__)))); } TEST(expm1l, test) { EXPECT_STREQ("1.718281828459045", _gc(xdtoal(_expm1l(1)))); EXPECT_STREQ("1.718281828459045", _gc(xdtoal(expl(1) - 1))); EXPECT_STREQ("0", _gc(xdtoal(_expm1l(0)))); EXPECT_STREQ("-0", _gc(xdtoal(_expm1l(-0.)))); EXPECT_STREQ("NAN", _gc(xdtoal(_expm1l(NAN)))); EXPECT_STREQ("-1", _gc(xdtoal(_expm1l(-INFINITY)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(_expm1l(INFINITY)))); /* EXPECT_STREQ("-INFINITY", _gc(xdtoal(_expm1l(-132098844872390)))); */ /* EXPECT_STREQ("INFINITY", _gc(xdtoal(_expm1l(132098844872390)))); */ } TEST(expm1f, test) { EXPECT_STREQ("0", _gc(xdtoaf(_expm1f(0)))); EXPECT_STREQ("-0", _gc(xdtoaf(_expm1f(-0.)))); EXPECT_STREQ("NAN", _gc(xdtoaf(_expm1f(NAN)))); EXPECT_STREQ("-1", _gc(xdtoaf(_expm1f(-INFINITY)))); EXPECT_STREQ("INFINITY", _gc(xdtoaf(_expm1f(INFINITY)))); /* EXPECT_STREQ("-INFINITY", _gc(xdtoaf(_expm1f(-132098844872390)))); */ /* EXPECT_STREQ("INFINITY", _gc(xdtoaf(_expm1f(132098844872390)))); */ } BENCH(expm1, bench) { EZBENCH2("expm1", donothing, _expm1(.7)); // ~17ns EZBENCH2("expm1f", donothing, _expm1f(.7)); // ~13ns EZBENCH2("expm1l", donothing, _expm1l(.7)); // ~33ns }
4,737
83
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/csqrt_test.c
/*-*- 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/complex.h" #include "libc/mem/gc.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" TEST(csqrt, test) { complex double x = csqrt(-1); EXPECT_STREQ("0 1", _gc(xasprintf("%g %g", creal(x), cimag(x)))); }
2,074
28
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/cbrt_test.c
/*-*- 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 2021 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/calls/struct/timespec.h" #include "libc/math.h" #include "libc/mem/gc.h" #include "libc/stdio/stdio.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" double _cbrt(double) asm("cbrt"); float _cbrtf(float) asm("cbrtf"); long double _cbrtl(long double) asm("cbrtl"); TEST(cbrt, test) { EXPECT_STREQ("0", _gc(xasprintf("%.15g", _cbrt(0.)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _cbrt(-0.)))); EXPECT_STREQ("0.7937005259841", _gc(xasprintf("%.15g", _cbrt(.5)))); EXPECT_STREQ("-0.7937005259841", _gc(xasprintf("%.15g", _cbrt(-.5)))); EXPECT_STREQ("1", _gc(xasprintf("%.15g", _cbrt(1.)))); EXPECT_STREQ("-1", _gc(xasprintf("%.15g", _cbrt(-1.)))); EXPECT_STREQ("1.14471424255333", _gc(xasprintf("%.15g", _cbrt(1.5)))); EXPECT_STREQ("-1.14471424255333", _gc(xasprintf("%.15g", _cbrt(-1.5)))); EXPECT_STREQ("nan", _gc(xasprintf("%.15g", _cbrt(NAN)))); EXPECT_STREQ("-nan", _gc(xasprintf("%.15g", _cbrt(-NAN)))); EXPECT_STREQ("inf", _gc(xasprintf("%.15g", _cbrt(INFINITY)))); EXPECT_STREQ("-inf", _gc(xasprintf("%.15g", _cbrt(-INFINITY)))); EXPECT_STREQ("2.81264428523626e-103", _gc(xasprintf("%.15g", _cbrt(__DBL_MIN__)))); EXPECT_STREQ("5.64380309412236e+102", _gc(xasprintf("%.15g", _cbrt(__DBL_MAX__)))); } BENCH(cbrt, bench) { EZBENCH2("cbrt", donothing, _cbrt(.7)); // ~19ns EZBENCH2("cbrtf", donothing, _cbrtf(.7)); // ~15ns EZBENCH2("cbrtl", donothing, _cbrtl(.7)); // ~36ns }
3,355
55
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/complex_test.c
/*-*- 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/complex.h" #include "libc/fmt/fmt.h" #include "libc/testlib/testlib.h" TEST(complex, test) { char buf[128]; complex double z, z1; sprintf(buf, "I = %.1f%+.1fi", creal(_Complex_I), cimag(_Complex_I)); EXPECT_STREQ("I = 0.0+1.0i", buf); z1 = _Complex_I * _Complex_I; // imaginary unit squared sprintf(buf, "I * I = %.1f%+.1fi", creal(z1), cimag(z1)); EXPECT_STREQ("I * I = -1.0+0.0i", buf); z = 1.0 + 2.0 * _Complex_I; // usual way to form a complex number pre-C11 sprintf(buf, "z = %.1f%+.1fi", creal(z), cimag(z)); EXPECT_STREQ("z = 1.0+2.0i", buf); }
2,426
35
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/tan_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" double _tan(double) asm("tan"); float _tanf(float) asm("tanf"); long double _tanl(long double) asm("tanl"); TEST(tan, test) { EXPECT_STREQ("0", _gc(xasprintf("%.15g", _tan(0.)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _tan(-0.)))); EXPECT_STREQ("0.54630248984379", _gc(xasprintf("%.15g", _tan(.5)))); EXPECT_STREQ("-0.54630248984379", _gc(xasprintf("%.15g", _tan(-.5)))); EXPECT_STREQ("1.5574077246549", _gc(xasprintf("%.15g", _tan(1.)))); EXPECT_STREQ("-1.5574077246549", _gc(xasprintf("%.15g", _tan(-1.)))); EXPECT_STREQ("14.1014199471717", _gc(xasprintf("%.15g", _tan(1.5)))); EXPECT_STREQ("-14.1014199471717", _gc(xasprintf("%.15g", _tan(-1.5)))); EXPECT_STREQ("nan", _gc(xasprintf("%.15g", _tan(NAN)))); EXPECT_STREQ("-nan", _gc(xasprintf("%.15g", _tan(-NAN)))); EXPECT_TRUE(isnan(_tan(INFINITY))); EXPECT_TRUE(isnan(_tan(-INFINITY))); EXPECT_STREQ("2.2250738585072e-308", _gc(xasprintf("%.15g", _tan(__DBL_MIN__)))); EXPECT_STREQ("-0.0049620158744449", _gc(xasprintf("%.15g", _tan(__DBL_MAX__)))); } BENCH(tan, bench) { EZBENCH2("tan", donothing, _tan(.7)); // ~18ns EZBENCH2("tanf", donothing, _tanf(.7)); // ~6ns EZBENCH2("tanl", donothing, _tanl(.7)); // ~39ns }
3,231
53
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/cosh_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _cosh(double) asm("cosh"); float _coshf(float) asm("coshf"); long double _coshl(long double) asm("coshl"); TEST(coshl, test) { EXPECT_STREQ("1.127625965206381", gc(xdtoal(_coshl(+.5)))); EXPECT_STREQ("1.127625965206381", gc(xdtoal(_coshl(-.5)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_coshl(30000)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_coshl(-30000)))); EXPECT_STREQ("1", gc(xdtoal(_coshl(+0.)))); EXPECT_STREQ("1", gc(xdtoal(_coshl(-0.)))); EXPECT_TRUE(isnan(_coshl(NAN))); EXPECT_STREQ("INFINITY", gc(xdtoal(_coshl(INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_coshl(-INFINITY)))); } TEST(cosh, test) { EXPECT_STREQ("1.12762596520638", gc(xdtoa(_cosh(+.5)))); EXPECT_STREQ("1.12762596520638", gc(xdtoa(_cosh(-.5)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_cosh(30000)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_cosh(-30000)))); EXPECT_STREQ("1", gc(xdtoa(_cosh(+0.)))); EXPECT_STREQ("1", gc(xdtoa(_cosh(-0.)))); EXPECT_TRUE(isnan(_cosh(NAN))); EXPECT_STREQ("INFINITY", gc(xdtoa(_cosh(INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_cosh(-INFINITY)))); } TEST(coshf, test) { EXPECT_STREQ("1.12763", gc(xdtoaf(_coshf(+.5)))); EXPECT_STREQ("1.12763", gc(xdtoaf(_coshf(-.5)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_coshf(30000)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_coshf(-30000)))); EXPECT_STREQ("1", gc(xdtoaf(_coshf(+0.)))); EXPECT_STREQ("1", gc(xdtoaf(_coshf(-0.)))); EXPECT_TRUE(isnan(_coshf(NAN))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_coshf(INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_coshf(-INFINITY)))); } BENCH(cosh, bench) { EZBENCH2("cosh", donothing, _cosh(.7)); // ~6ns EZBENCH2("coshf", donothing, _coshf(.7)); // ~5ns EZBENCH2("coshl", donothing, _coshl(.7)); // ~25ns }
3,750
70
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/copysign_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(copysign, test) { EXPECT_STREQ("0", gc(xdtoa(copysign(0, +0)))); EXPECT_STREQ("-0", gc(xdtoa(copysign(0, -0.)))); EXPECT_STREQ("0", gc(xdtoa(copysign(0, +1)))); EXPECT_STREQ("-0", gc(xdtoa(copysign(-0., -1)))); EXPECT_STREQ("2", gc(xdtoa(copysign(2, +1)))); EXPECT_STREQ("-2", gc(xdtoa(copysign(-2, -1)))); EXPECT_STREQ("NAN", gc(xdtoa(copysign(NAN, +1)))); EXPECT_STREQ("-NAN", gc(xdtoa(copysign(NAN, -1)))); EXPECT_STREQ("INFINITY", gc(xdtoa(copysign(INFINITY, +1)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(copysign(INFINITY, -1)))); EXPECT_STREQ("NAN", gc(xdtoa(copysign(-NAN, +1)))); EXPECT_STREQ("-NAN", gc(xdtoa(copysign(-NAN, -1)))); EXPECT_STREQ("INFINITY", gc(xdtoa(copysign(-INFINITY, +1)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(copysign(-INFINITY, -1)))); } TEST(copysignl, test) { EXPECT_STREQ("0", gc(xdtoal(copysignl(0, +0)))); EXPECT_STREQ("-0", gc(xdtoal(copysignl(0, -0.)))); EXPECT_STREQ("0", gc(xdtoal(copysignl(0, +1)))); EXPECT_STREQ("-0", gc(xdtoal(copysignl(-0., -1)))); EXPECT_STREQ("2", gc(xdtoal(copysignl(2, +1)))); EXPECT_STREQ("-2", gc(xdtoal(copysignl(-2, -1)))); EXPECT_STREQ("NAN", gc(xdtoal(copysignl(NAN, +1)))); EXPECT_STREQ("-NAN", gc(xdtoal(copysignl(NAN, -1)))); EXPECT_STREQ("INFINITY", gc(xdtoal(copysignl(INFINITY, +1)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(copysignl(INFINITY, -1)))); EXPECT_STREQ("NAN", gc(xdtoal(copysignl(-NAN, +1)))); EXPECT_STREQ("-NAN", gc(xdtoal(copysignl(-NAN, -1)))); EXPECT_STREQ("INFINITY", gc(xdtoal(copysignl(-INFINITY, +1)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(copysignl(-INFINITY, -1)))); } TEST(copysignf, test) { EXPECT_STREQ("0", gc(xdtoaf(copysignf(0, +0)))); EXPECT_STREQ("-0", gc(xdtoaf(copysignf(0, -0.)))); EXPECT_STREQ("0", gc(xdtoaf(copysignf(0, +1)))); EXPECT_STREQ("-0", gc(xdtoaf(copysignf(-0., -1)))); EXPECT_STREQ("2", gc(xdtoaf(copysignf(2, +1)))); EXPECT_STREQ("-2", gc(xdtoaf(copysignf(-2, -1)))); EXPECT_STREQ("NAN", gc(xdtoaf(copysignf(NAN, +1)))); EXPECT_STREQ("-NAN", gc(xdtoaf(copysignf(NAN, -1)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(copysignf(INFINITY, +1)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(copysignf(INFINITY, -1)))); EXPECT_STREQ("NAN", gc(xdtoaf(copysignf(-NAN, +1)))); EXPECT_STREQ("-NAN", gc(xdtoaf(copysignf(-NAN, -1)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(copysignf(-INFINITY, +1)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(copysignf(-INFINITY, -1)))); }
4,404
74
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/pow10_test.c
/*-*- 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/math.h" #include "libc/testlib/testlib.h" TEST(pow10, testLdbl) { EXPECT_LDBL_EQ(1, pow10l(0)); EXPECT_LDBL_EQ(10, pow10l(1)); EXPECT_LDBL_EQ(100, pow10l(2)); } TEST(pow10, testDouble) { EXPECT_DOUBLE_EQ(1, pow10(0)); EXPECT_DOUBLE_EQ(10, pow10(1)); EXPECT_DOUBLE_EQ(100, pow10(2)); } TEST(pow10, testFloat) { EXPECT_FLOAT_EQ(1, pow10f(0)); EXPECT_FLOAT_EQ(10, pow10f(1)); EXPECT_FLOAT_EQ(100, pow10f(2)); }
2,280
39
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/acosh_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _acosh(double) asm("acosh"); float _acoshf(float) asm("acoshf"); long double _acoshl(long double) asm("acoshl"); TEST(acosh, test) { EXPECT_STREQ(".962423650119207", gc(xdtoa(_acosh(1.5)))); EXPECT_STREQ("0", gc(xdtoa(_acosh(1)))); EXPECT_TRUE(isnan(_acosh(NAN))); EXPECT_TRUE(isnan(_acosh(.5))); EXPECT_TRUE(isnan(_acosh(-.5))); EXPECT_TRUE(isnan(_acosh(-1.5))); EXPECT_STREQ("INFINITY", gc(xdtoa(_acosh(INFINITY)))); } TEST(acoshf, test) { EXPECT_STREQ(".962424", gc(xdtoaf(_acoshf(1.5)))); EXPECT_STREQ("0", gc(xdtoaf(_acoshf(1)))); EXPECT_TRUE(isnan(_acoshf(NAN))); EXPECT_TRUE(isnan(_acoshf(.5))); EXPECT_TRUE(isnan(_acoshf(-.5))); EXPECT_TRUE(isnan(_acoshf(-1.5))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_acoshf(INFINITY)))); } TEST(acoshl, test) { volatile long double x = 16; EXPECT_STREQ("4", gc(xdtoal(sqrtl(x)))); EXPECT_STREQ(".9624236501192069", gc(xdtoal(_acoshl(1.5)))); EXPECT_STREQ("0", gc(xdtoal(_acoshl(1)))); EXPECT_TRUE(isnan(_acoshl(NAN))); EXPECT_TRUE(isnan(_acoshl(.5))); EXPECT_TRUE(isnan(_acoshl(-.5))); EXPECT_TRUE(isnan(_acoshl(-1.5))); EXPECT_STREQ("INFINITY", gc(xdtoal(_acoshl(INFINITY)))); }
3,127
59
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/fsum_test.c
/*-*- 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 2021 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/macros.internal.h" #include "libc/math.h" #include "libc/mem/gc.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" #define N 100000 float F[N]; double D[N]; void SetUp(void) { int i; for (i = 0; i < N / 2; ++i) { D[i * 2 + 0] = 1000000000.1; D[i * 2 + 1] = 1.1; } for (i = 0; i < N / 2; ++i) { F[i * 2 + 0] = 1000.1; F[i * 2 + 1] = 1.1; } } TEST(fsum, test) { EXPECT_STREQ("500000000.6", _gc(xasprintf("%.15g", fsum(D, N) / N))); } TEST(fsumf, test) { EXPECT_STREQ("500.6", _gc(xasprintf("%.7g", fsumf(F, N) / N))); } BENCH(fsum, bench) { EZBENCH2("fsum", donothing, fsum(D, N)); EZBENCH2("fsumf", donothing, fsumf(F, N)); }
2,566
55
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/atan2l_test.c
/*-*- 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" TEST(atan2l, test) { volatile double a = -.9816175436063843; volatile double b = -.1908585813741899; EXPECT_STREQ("-2.95", _gc(xasprintf("%.2f", atan2f(b, a)))); EXPECT_STREQ("-2.95", _gc(xasprintf("%.2f", atan2(b, a)))); #ifndef __aarch64__ // TODO: implement quad floating point into printf EXPECT_STREQ("-2.95", _gc(xasprintf("%.2Lf", atan2l(b, a)))); #endif } TEST(atan2, testSpecialCases) { ASSERT_STREQ("NAN", _gc(xdtoa(atan2(NAN, 0)))); ASSERT_STREQ("NAN", _gc(xdtoa(atan2(0, NAN)))); ASSERT_STREQ("0", _gc(xdtoa(atan2(+0., +0.)))); ASSERT_STREQ("0", _gc(xdtoa(atan2(+0., +1.)))); ASSERT_STREQ("0", _gc(xdtoa(atan2(+0., +2.)))); ASSERT_STREQ("0", _gc(xdtoa(atan2(1, INFINITY)))); ASSERT_STREQ("3.141592653589793", _gc(xdtoal(atan2(+0., -0.)))); ASSERT_STREQ("3.141592653589793", _gc(xdtoal(atan2(+0., -1.)))); ASSERT_STREQ("3.141592653589793", _gc(xdtoal(atan2(+0., -2.)))); ASSERT_STREQ("-1.570796326794897", _gc(xdtoal(atan2(-1., -0.)))); ASSERT_STREQ("-1.570796326794897", _gc(xdtoal(atan2(-1., +0.)))); ASSERT_STREQ("-1.570796326794897", _gc(xdtoal(atan2(-2., -0.)))); ASSERT_STREQ("-1.570796326794897", _gc(xdtoal(atan2(-2., +0.)))); ASSERT_STREQ("1.570796326794897", _gc(xdtoal(atan2(+1., -0.)))); ASSERT_STREQ("1.570796326794897", _gc(xdtoal(atan2(+1., +0.)))); ASSERT_STREQ("1.570796326794897", _gc(xdtoal(atan2(+2., -0.)))); ASSERT_STREQ("1.570796326794897", _gc(xdtoal(atan2(+2., +0.)))); ASSERT_STREQ("1.570796326794897", _gc(xdtoal(atan2(INFINITY, 1)))); ASSERT_STREQ("1.570796326794897", _gc(xdtoal(atan2(INFINITY, -1)))); ASSERT_STREQ("3.141592653589793", _gc(xdtoal(atan2(1, -INFINITY)))); ASSERT_STREQ("2.356194490192345", _gc(xdtoal(atan2(INFINITY, -INFINITY)))); ASSERT_STREQ(".7853981633974483", _gc(xdtoal(atan2(INFINITY, INFINITY)))); }
3,793
60
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/asinh_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" double _asinh(double) asm("asinh"); float _asinhf(float) asm("asinhf"); long double _asinhl(long double) asm("asinhl"); TEST(asinh, test) { EXPECT_STREQ(".481211825059603", _gc(xdtoa(_asinh(+.5)))); EXPECT_STREQ("-.481211825059603", _gc(xdtoa(_asinh(-.5)))); EXPECT_STREQ("0", _gc(xdtoa(_asinh(0)))); EXPECT_STREQ("NAN", _gc(xdtoa(_asinh(NAN)))); EXPECT_STREQ("INFINITY", _gc(xdtoa(_asinh(INFINITY)))); EXPECT_STREQ("-2.1073424255447e-08", _gc(xasprintf("%.15g", _asinh(-2.1073424255447e-08)))); } TEST(asinhf, test) { EXPECT_STREQ(".481212", _gc(xdtoaf(_asinhf(+.5)))); EXPECT_STREQ("-.481212", _gc(xdtoaf(_asinhf(-.5)))); EXPECT_STREQ("0", _gc(xdtoaf(_asinhf(0)))); EXPECT_STREQ("NAN", _gc(xdtoaf(_asinhf(NAN)))); EXPECT_STREQ("INFINITY", _gc(xdtoaf(_asinhf(INFINITY)))); } TEST(asinhl, test) { EXPECT_STREQ(".4812118250596034", _gc(xdtoal(_asinhl(+.5)))); EXPECT_STREQ("-.4812118250596034", _gc(xdtoal(_asinhl(-.5)))); EXPECT_STREQ("0", _gc(xdtoal(_asinhl(0)))); EXPECT_STREQ("NAN", _gc(xdtoal(_asinhl(NAN)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(_asinhl(INFINITY)))); } BENCH(asinh, bench) { EZBENCH2("asinh", donothing, _asinh(.7)); // ~26ns EZBENCH2("asinhf", donothing, _asinhf(.7)); // ~17ns EZBENCH2("asinhl", donothing, _asinhl(.7)); // ~48ns }
3,322
61
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/atan2_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" double _atan2(double, double) asm("atan2"); float _atan2f(float, float) asm("atan2f"); long double _atan2l(long double, long double) asm("atan2l"); TEST(atan2, yx) { EXPECT_STREQ("-0.321750554396642", _gc(xasprintf("%.15g", atan(-.5 / 1.5)))); EXPECT_STREQ("-0.321750554396642", _gc(xasprintf("%.15g", _atan2(-.5, 1.5)))); } TEST(atan2, test) { EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(0., 0.)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(0., -0.)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(0., .5)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(0., -.5)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(0., 1.)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(0., -1.)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(0., 1.5)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(0., -1.5)))); EXPECT_TRUE(isnan(_atan2(0., NAN))); EXPECT_TRUE(isnan(_atan2(0., -NAN))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(0., INFINITY)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(0., -INFINITY)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(0., __DBL_MIN__)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(0., __DBL_MAX__)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-0., 0.)))); EXPECT_STREQ("-3.14159265358979", _gc(xasprintf("%.15g", _atan2(-0., -0.)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-0., .5)))); EXPECT_STREQ("-3.14159265358979", _gc(xasprintf("%.15g", _atan2(-0., -.5)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-0., 1.)))); EXPECT_STREQ("-3.14159265358979", _gc(xasprintf("%.15g", _atan2(-0., -1.)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-0., 1.5)))); EXPECT_STREQ("-3.14159265358979", _gc(xasprintf("%.15g", _atan2(-0., -1.5)))); EXPECT_TRUE(isnan(_atan2(-0., NAN))); EXPECT_TRUE(isnan(_atan2(-0., -NAN))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-0., INFINITY)))); EXPECT_STREQ("-3.14159265358979", _gc(xasprintf("%.15g", _atan2(-0., -INFINITY)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-0., __DBL_MIN__)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-0., __DBL_MAX__)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(.5, 0.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(.5, -0.)))); EXPECT_STREQ("0.785398163397448", _gc(xasprintf("%.15g", _atan2(.5, .5)))); EXPECT_STREQ("2.35619449019234", _gc(xasprintf("%.15g", _atan2(.5, -.5)))); EXPECT_STREQ("0.463647609000806", _gc(xasprintf("%.15g", _atan2(.5, 1.)))); EXPECT_STREQ("2.67794504458899", _gc(xasprintf("%.15g", _atan2(.5, -1.)))); EXPECT_STREQ("0.321750554396642", _gc(xasprintf("%.15g", _atan2(.5, 1.5)))); EXPECT_STREQ("2.81984209919315", _gc(xasprintf("%.15g", _atan2(.5, -1.5)))); EXPECT_TRUE(isnan(_atan2(.5, NAN))); EXPECT_TRUE(isnan(_atan2(.5, -NAN))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(.5, INFINITY)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(.5, -INFINITY)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(.5, __DBL_MIN__)))); EXPECT_STREQ("2.781342323134e-309", _gc(xasprintf("%.15g", _atan2(.5, __DBL_MAX__)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-.5, 0.)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-.5, -0.)))); EXPECT_STREQ("-0.785398163397448", _gc(xasprintf("%.15g", _atan2(-.5, .5)))); EXPECT_STREQ("-2.35619449019234", _gc(xasprintf("%.15g", _atan2(-.5, -.5)))); EXPECT_STREQ("-0.463647609000806", _gc(xasprintf("%.15g", _atan2(-.5, 1.)))); EXPECT_STREQ("-2.67794504458899", _gc(xasprintf("%.15g", _atan2(-.5, -1.)))); EXPECT_STREQ("-0.321750554396642", _gc(xasprintf("%.15g", _atan2(-.5, 1.5)))); EXPECT_STREQ("-2.81984209919315", _gc(xasprintf("%.15g", _atan2(-.5, -1.5)))); EXPECT_TRUE(isnan(_atan2(-.5, NAN))); EXPECT_TRUE(isnan(_atan2(-.5, -NAN))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-.5, INFINITY)))); EXPECT_STREQ("-3.14159265358979", _gc(xasprintf("%.15g", _atan2(-.5, -INFINITY)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-.5, __DBL_MIN__)))); EXPECT_STREQ("-2.781342323134e-309", _gc(xasprintf("%.15g", _atan2(-.5, __DBL_MAX__)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(1., 0.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(1., -0.)))); EXPECT_STREQ("1.10714871779409", _gc(xasprintf("%.15g", _atan2(1., .5)))); EXPECT_STREQ("2.0344439357957", _gc(xasprintf("%.15g", _atan2(1., -.5)))); EXPECT_STREQ("0.785398163397448", _gc(xasprintf("%.15g", _atan2(1., 1.)))); EXPECT_STREQ("2.35619449019234", _gc(xasprintf("%.15g", _atan2(1., -1.)))); EXPECT_STREQ("0.588002603547568", _gc(xasprintf("%.15g", _atan2(1., 1.5)))); EXPECT_STREQ("2.55359005004223", _gc(xasprintf("%.15g", _atan2(1., -1.5)))); EXPECT_TRUE(isnan(_atan2(1., NAN))); EXPECT_TRUE(isnan(_atan2(1., -NAN))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(1., INFINITY)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(1., -INFINITY)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(1., __DBL_MIN__)))); EXPECT_STREQ("5.562684646268e-309", _gc(xasprintf("%.15g", _atan2(1., __DBL_MAX__)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-1., 0.)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-1., -0.)))); EXPECT_STREQ("-1.10714871779409", _gc(xasprintf("%.15g", _atan2(-1., .5)))); EXPECT_STREQ("-2.0344439357957", _gc(xasprintf("%.15g", _atan2(-1., -.5)))); EXPECT_STREQ("-0.785398163397448", _gc(xasprintf("%.15g", _atan2(-1., 1.)))); EXPECT_STREQ("-2.35619449019234", _gc(xasprintf("%.15g", _atan2(-1., -1.)))); EXPECT_STREQ("-0.588002603547568", _gc(xasprintf("%.15g", _atan2(-1., 1.5)))); EXPECT_STREQ("-2.55359005004223", _gc(xasprintf("%.15g", _atan2(-1., -1.5)))); EXPECT_TRUE(isnan(_atan2(-1., NAN))); EXPECT_TRUE(isnan(_atan2(-1., -NAN))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-1., INFINITY)))); EXPECT_STREQ("-3.14159265358979", _gc(xasprintf("%.15g", _atan2(-1., -INFINITY)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-1., __DBL_MIN__)))); EXPECT_STREQ("-5.562684646268e-309", _gc(xasprintf("%.15g", _atan2(-1., __DBL_MAX__)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(1.5, 0.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(1.5, -0.)))); EXPECT_STREQ("1.24904577239825", _gc(xasprintf("%.15g", _atan2(1.5, .5)))); EXPECT_STREQ("1.89254688119154", _gc(xasprintf("%.15g", _atan2(1.5, -.5)))); EXPECT_STREQ("0.982793723247329", _gc(xasprintf("%.15g", _atan2(1.5, 1.)))); EXPECT_STREQ("2.15879893034246", _gc(xasprintf("%.15g", _atan2(1.5, -1.)))); EXPECT_STREQ("0.785398163397448", _gc(xasprintf("%.15g", _atan2(1.5, 1.5)))); EXPECT_STREQ("2.35619449019234", _gc(xasprintf("%.15g", _atan2(1.5, -1.5)))); EXPECT_TRUE(isnan(_atan2(1.5, NAN))); EXPECT_TRUE(isnan(_atan2(1.5, -NAN))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(1.5, INFINITY)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(1.5, -INFINITY)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(1.5, __DBL_MIN__)))); EXPECT_STREQ("8.34402696940201e-309", _gc(xasprintf("%.15g", _atan2(1.5, __DBL_MAX__)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-1.5, 0.)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-1.5, -0.)))); EXPECT_STREQ("-1.24904577239825", _gc(xasprintf("%.15g", _atan2(-1.5, .5)))); EXPECT_STREQ("-1.89254688119154", _gc(xasprintf("%.15g", _atan2(-1.5, -.5)))); EXPECT_STREQ("-0.982793723247329", _gc(xasprintf("%.15g", _atan2(-1.5, 1.)))); EXPECT_STREQ("-2.15879893034246", _gc(xasprintf("%.15g", _atan2(-1.5, -1.)))); EXPECT_STREQ("-0.785398163397448", _gc(xasprintf("%.15g", _atan2(-1.5, 1.5)))); EXPECT_STREQ("-2.35619449019234", _gc(xasprintf("%.15g", _atan2(-1.5, -1.5)))); EXPECT_TRUE(isnan(_atan2(-1.5, NAN))); EXPECT_TRUE(isnan(_atan2(-1.5, -NAN))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-1.5, INFINITY)))); EXPECT_STREQ("-3.14159265358979", _gc(xasprintf("%.15g", _atan2(-1.5, -INFINITY)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-1.5, __DBL_MIN__)))); EXPECT_STREQ("-8.34402696940201e-309", _gc(xasprintf("%.15g", _atan2(-1.5, __DBL_MAX__)))); EXPECT_TRUE(isnan(_atan2(NAN, 0.))); EXPECT_TRUE(isnan(_atan2(NAN, -0.))); EXPECT_TRUE(isnan(_atan2(NAN, .5))); EXPECT_TRUE(isnan(_atan2(NAN, -.5))); EXPECT_TRUE(isnan(_atan2(NAN, 1.))); EXPECT_TRUE(isnan(_atan2(NAN, -1.))); EXPECT_TRUE(isnan(_atan2(NAN, 1.5))); EXPECT_TRUE(isnan(_atan2(NAN, -1.5))); EXPECT_TRUE(isnan(_atan2(NAN, NAN))); EXPECT_TRUE(isnan(_atan2(NAN, -NAN))); EXPECT_TRUE(isnan(_atan2(NAN, INFINITY))); EXPECT_TRUE(isnan(_atan2(NAN, -INFINITY))); EXPECT_TRUE(isnan(_atan2(NAN, __DBL_MIN__))); EXPECT_TRUE(isnan(_atan2(NAN, __DBL_MAX__))); EXPECT_TRUE(isnan(_atan2(-NAN, 0.))); EXPECT_TRUE(isnan(_atan2(-NAN, -0.))); EXPECT_TRUE(isnan(_atan2(-NAN, .5))); EXPECT_TRUE(isnan(_atan2(-NAN, -.5))); EXPECT_TRUE(isnan(_atan2(-NAN, 1.))); EXPECT_TRUE(isnan(_atan2(-NAN, -1.))); EXPECT_TRUE(isnan(_atan2(-NAN, 1.5))); EXPECT_TRUE(isnan(_atan2(-NAN, -1.5))); EXPECT_TRUE(isnan(_atan2(-NAN, NAN))); EXPECT_TRUE(isnan(_atan2(-NAN, -NAN))); EXPECT_TRUE(isnan(_atan2(-NAN, INFINITY))); EXPECT_TRUE(isnan(_atan2(-NAN, -INFINITY))); EXPECT_TRUE(isnan(_atan2(-NAN, __DBL_MIN__))); EXPECT_TRUE(isnan(_atan2(-NAN, __DBL_MAX__))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(INFINITY, 0.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(INFINITY, -0.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(INFINITY, .5)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(INFINITY, -.5)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(INFINITY, 1.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(INFINITY, -1.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(INFINITY, 1.5)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(INFINITY, -1.5)))); EXPECT_TRUE(isnan(_atan2(INFINITY, NAN))); EXPECT_TRUE(isnan(_atan2(INFINITY, -NAN))); EXPECT_STREQ("0.785398163397448", _gc(xasprintf("%.15g", _atan2(INFINITY, INFINITY)))); EXPECT_STREQ("2.35619449019234", _gc(xasprintf("%.15g", _atan2(INFINITY, -INFINITY)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(INFINITY, __DBL_MIN__)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(INFINITY, __DBL_MAX__)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-INFINITY, 0.)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-INFINITY, -0.)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-INFINITY, .5)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-INFINITY, -.5)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-INFINITY, 1.)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-INFINITY, -1.)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-INFINITY, 1.5)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-INFINITY, -1.5)))); EXPECT_TRUE(isnan(_atan2(-INFINITY, NAN))); EXPECT_TRUE(isnan(_atan2(-INFINITY, -NAN))); EXPECT_STREQ("-0.785398163397448", _gc(xasprintf("%.15g", _atan2(-INFINITY, INFINITY)))); EXPECT_STREQ("-2.35619449019234", _gc(xasprintf("%.15g", _atan2(-INFINITY, -INFINITY)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-INFINITY, __DBL_MIN__)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", _atan2(-INFINITY, __DBL_MAX__)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, 0.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, -0.)))); EXPECT_STREQ("4.4501477170144e-308", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, .5)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, -.5)))); EXPECT_STREQ("2.2250738585072e-308", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, 1.)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, -1.)))); EXPECT_STREQ("1.48338257233813e-308", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, 1.5)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, -1.5)))); EXPECT_TRUE(isnan(_atan2(__DBL_MIN__, NAN))); EXPECT_TRUE(isnan(_atan2(__DBL_MIN__, -NAN))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, INFINITY)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, -INFINITY)))); EXPECT_STREQ("0.785398163397448", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, __DBL_MIN__)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(__DBL_MIN__, __DBL_MAX__)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, 0.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, -0.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, .5)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, -.5)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, 1.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, -1.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, 1.5)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, -1.5)))); EXPECT_TRUE(isnan(_atan2(__DBL_MAX__, NAN))); EXPECT_TRUE(isnan(_atan2(__DBL_MAX__, -NAN))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, INFINITY)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, -INFINITY)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, __DBL_MIN__)))); EXPECT_STREQ("0.785398163397448", _gc(xasprintf("%.15g", _atan2(__DBL_MAX__, __DBL_MAX__)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-0.000000000000001, INFINITY)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _atan2(-1, INFINITY)))); EXPECT_STREQ( "-0", _gc(xasprintf("%.15g", _atan2(-1.7976931348623157e308, INFINITY)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _atan2(1.4142135623731, 0)))); } BENCH(atan2, bench) { volatile float f = 3; volatile double d = 3; volatile long double l = 3; EZBENCH2("atan2", donothing, _atan2(d, d)); // ~19ns EZBENCH2("atan2f", donothing, _atan2f(f, f)); // ~12ns EZBENCH2("atan2l", donothing, _atan2l(l, l)); // ~31ns }
17,889
315
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/atan_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" TEST(atan, test) { EXPECT_STREQ("0", _gc(xasprintf("%.15g", atan(0.)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", atan(-0.)))); EXPECT_STREQ("0.463647609000806", _gc(xasprintf("%.15g", atan(.5)))); EXPECT_STREQ("-0.463647609000806", _gc(xasprintf("%.15g", atan(-.5)))); EXPECT_STREQ("0.785398163397448", _gc(xasprintf("%.15g", atan(1.)))); EXPECT_STREQ("-0.785398163397448", _gc(xasprintf("%.15g", atan(-1.)))); EXPECT_STREQ("0.982793723247329", _gc(xasprintf("%.15g", atan(1.5)))); EXPECT_STREQ("-0.982793723247329", _gc(xasprintf("%.15g", atan(-1.5)))); EXPECT_STREQ("nan", _gc(xasprintf("%.15g", atan(NAN)))); EXPECT_STREQ("-nan", _gc(xasprintf("%.15g", atan(-NAN)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", atan(INFINITY)))); EXPECT_STREQ("-1.5707963267949", _gc(xasprintf("%.15g", atan(-INFINITY)))); EXPECT_STREQ("2.2250738585072e-308", _gc(xasprintf("%.15g", atan(__DBL_MIN__)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", atan(__DBL_MAX__)))); } BENCH(atanl, bench) { double _atan(double) asm("atan"); float _atanf(float) asm("atanf"); long double _atanl(long double) asm("atanl"); EZBENCH2("-atan", donothing, _atan(.7)); /* ~18ns */ EZBENCH2("-atanf", donothing, _atanf(.7)); /* ~12ns */ EZBENCH2("-atanl", donothing, _atanl(.7)); /* ~34ns */ }
3,326
51
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/cos_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" double _cos(double) asm("cos"); float _cosf(float) asm("cosf"); long double _cosl(long double) asm("cosl"); TEST(cos, test) { EXPECT_STREQ("1", _gc(xasprintf("%.15g", _cos(0.)))); EXPECT_STREQ("1", _gc(xasprintf("%.15g", _cos(-0.)))); EXPECT_STREQ("0.995004165278026", _gc(xasprintf("%.15g", _cos(.1)))); EXPECT_STREQ("0.995004165278026", _gc(xasprintf("%.15g", _cos(-.1)))); EXPECT_STREQ("0.877582561890373", _gc(xasprintf("%.15g", _cos(.5)))); EXPECT_STREQ("0.877582561890373", _gc(xasprintf("%.15g", _cos(-.5)))); EXPECT_STREQ("0.54030230586814", _gc(xasprintf("%.15g", _cos(1.)))); EXPECT_STREQ("0.54030230586814", _gc(xasprintf("%.15g", _cos(-1.)))); EXPECT_STREQ("0.0707372016677029", _gc(xasprintf("%.15g", _cos(1.5)))); EXPECT_STREQ("0.0707372016677029", _gc(xasprintf("%.15g", _cos(-1.5)))); EXPECT_STREQ("-0.416146836547142", _gc(xasprintf("%.15g", _cos(2.)))); EXPECT_TRUE(isnan(_cos(NAN))); EXPECT_TRUE(isnan(_cos(-NAN))); EXPECT_TRUE(isnan(_cos(INFINITY))); EXPECT_TRUE(isnan(_cos(-INFINITY))); EXPECT_STREQ("1", _gc(xasprintf("%.15g", _cos(__DBL_MIN__)))); EXPECT_STREQ("-0.99998768942656", _gc(xasprintf("%.15g", _cos(__DBL_MAX__)))); EXPECT_STREQ("0.54030230586814", _gc(xasprintf("%.15g", _cos(-1.0000000000000002)))); EXPECT_STREQ("1", _gc(xasprintf("%.15g", _cos(-2.1073424255447e-08)))); } BENCH(cos, bench) { EZBENCH2("cos", donothing, _cos(.7)); // ~6ns EZBENCH2("cosf", donothing, _cosf(.7)); // ~5ns EZBENCH2("cosl", donothing, _cosl(.7)); // ~25ns }
3,526
57
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/atanh_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" double _atanh(double) asm("atanh"); float _atanhf(float) asm("atanhf"); long double _atanhl(long double) asm("atanhl"); TEST(atanh, test) { EXPECT_STREQ("0", _gc(xdtoa(_atanh(0)))); EXPECT_STREQ("-0", _gc(xdtoa(_atanh(-0.)))); EXPECT_STREQ(".549306144334055", _gc(xdtoa(_atanh(.5)))); EXPECT_STREQ("-.549306144334055", _gc(xdtoa(_atanh(-.5)))); EXPECT_STREQ("INFINITY", _gc(xdtoa(_atanh(+1)))); EXPECT_STREQ("-INFINITY", _gc(xdtoa(_atanh(-1)))); EXPECT_TRUE(isnan(_atanh(+1.1))); EXPECT_TRUE(isnan(_atanh(-1.1))); EXPECT_STREQ("-2.1073424255447e-08", _gc(xasprintf("%.15g", _atanh(-2.1073424255447e-08)))); } TEST(atanhl, test) { EXPECT_STREQ("0", _gc(xdtoal(_atanhl(0)))); EXPECT_STREQ("-0", _gc(xdtoal(_atanhl(-0.)))); EXPECT_STREQ(".5493061443340548", _gc(xdtoal(_atanhl(.5)))); EXPECT_STREQ("-.5493061443340548", _gc(xdtoal(_atanhl(-.5)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(_atanhl(+1)))); EXPECT_STREQ("-INFINITY", _gc(xdtoal(_atanhl(-1)))); EXPECT_TRUE(isnan(_atanhl(+1.1))); EXPECT_TRUE(isnan(_atanhl(-1.1))); } TEST(atanhf, test) { EXPECT_STREQ("0", _gc(xdtoaf(_atanhf(0)))); EXPECT_STREQ("-0", _gc(xdtoaf(_atanhf(-0.)))); EXPECT_STREQ(".549306", _gc(xdtoaf(_atanhf(.5)))); EXPECT_STREQ("-.549306", _gc(xdtoaf(_atanhf(-.5)))); EXPECT_STREQ("INFINITY", _gc(xdtoaf(_atanhf(+1)))); EXPECT_STREQ("-INFINITY", _gc(xdtoaf(_atanhf(-1)))); EXPECT_TRUE(isnan(_atanhf(+1.1))); EXPECT_TRUE(isnan(_atanhf(-1.1))); } BENCH(_atanh, bench) { volatile double a = .5; EZBENCH2("atanhf", donothing, EXPROPRIATE(_atanhf(a))); EZBENCH2("atanh", donothing, EXPROPRIATE(_atanh(a))); EZBENCH2("atanhl", donothing, EXPROPRIATE(_atanhl(a))); }
3,712
71
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/powl_test.c
/*-*- 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/calls/struct/sigaction.h" #include "libc/calls/struct/siginfo.h" #include "libc/calls/ucontext.h" #include "libc/errno.h" #include "libc/fmt/fmt.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "libc/mem/gc.h" #include "libc/runtime/pc.internal.h" #include "libc/stdio/rand.h" #include "libc/stdio/stdio.h" #include "libc/sysv/consts/sa.h" #include "libc/sysv/consts/sig.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" int rando; char buf[128]; void SetUp(void) { rando = rand() & 0xffff; } static double POW(double x, double y) { return powl(x, y); } static char *fmtd(double x) { sprintf(buf, "%.15g", x); return buf; } static char *fmtf(float x) { sprintf(buf, "%.6g", x); return buf; } TEST(powl, test) { EXPECT_STREQ("27", _gc(xdtoal(powl(3, 3)))); EXPECT_STREQ("-27", _gc(xdtoal(powl(-3, 3)))); EXPECT_STREQ("1e+4932", _gc(xdtoal(powl(10, 4932)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(10, 4933)))); // EXPECT_STREQ("0", _gc(xdtoal(powl(10, -5000)))); EXPECT_STREQ("1.063382396627933e+37", _gc(xdtoal(powl(2, 123)))); EXPECT_STARTSWITH(".4248496805467504", _gc(xdtoal(powl(.7, 2.4)))); EXPECT_STREQ("1", _gc(xdtoal(powl(1, NAN)))); EXPECT_STREQ("1", _gc(xdtoal(powl(1, rando)))); EXPECT_STREQ("1", _gc(xdtoal(powl(NAN, 0)))); EXPECT_STREQ("1", _gc(xdtoal(powl(rando, 0)))); EXPECT_STREQ("0", _gc(xdtoal(powl(0, 1)))); EXPECT_STREQ("0", _gc(xdtoal(powl(0, 2)))); EXPECT_STREQ("0", _gc(xdtoal(powl(0, 2.1)))); EXPECT_STREQ("1", _gc(xdtoal(powl(-1, INFINITY)))); EXPECT_STREQ("1", _gc(xdtoal(powl(-1, -INFINITY)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(1. / MAX(2, rando), -INFINITY)))); EXPECT_STREQ("0", _gc(xdtoal(powl(1.1, -INFINITY)))); EXPECT_STREQ("0", _gc(xdtoal(powl(MAX(2, rando), -INFINITY)))); EXPECT_STREQ("0", _gc(xdtoal(powl(1. / MAX(2, rando), INFINITY)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(MAX(2, rando), INFINITY)))); EXPECT_STREQ("-0", _gc(xdtoal(powl(-INFINITY, -1)))); EXPECT_STREQ("0", _gc(xdtoal(powl(-INFINITY, -1.1)))); EXPECT_STREQ("0", _gc(xdtoal(powl(-INFINITY, -2)))); EXPECT_STREQ("0", _gc(xdtoal(powl(-INFINITY, -2.1)))); EXPECT_STREQ("-0", _gc(xdtoal(powl(-INFINITY, -3)))); EXPECT_STREQ("0", _gc(xdtoal(powl(-INFINITY, -3.1)))); EXPECT_STREQ("-INFINITY", _gc(xdtoal(powl(-INFINITY, 1)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(-INFINITY, 1.1)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(-INFINITY, 2)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(-INFINITY, 2.1)))); EXPECT_STREQ("-INFINITY", _gc(xdtoal(powl(-INFINITY, 3)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(-INFINITY, 3.1)))); EXPECT_STREQ("0", _gc(xdtoal(powl(INFINITY, -1)))); EXPECT_STREQ("0", _gc(xdtoal(powl(INFINITY, -.1)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(INFINITY, 1)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(INFINITY, .1)))); EXPECT_STREQ("1", _gc(xdtoal(powl(INFINITY, 0)))); EXPECT_STREQ("1", _gc(xdtoal(powl(INFINITY, -0.)))); EXPECT_STREQ("1", _gc(xdtoal(powl(0, 0)))); EXPECT_STREQ("1", _gc(xdtoal(powl(0, -0.)))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(0, -(MAX(rando, 1) | 1))))); EXPECT_STREQ("-INFINITY", _gc(xdtoal(powl(-0., -(MAX(rando, 1) | 1))))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(0, -(rando & -2))))); EXPECT_STREQ("INFINITY", _gc(xdtoal(powl(-0., -(rando & -2))))); EXPECT_TRUE(isnan(powl(-3, 1. / MAX(rando, 2)))); EXPECT_TRUE(isnan(powl(-3, -(1. / MAX(rando, 2))))); EXPECT_STREQ("-.3333333333333333", _gc(xdtoal(powl(-3, -1)))); EXPECT_STREQ(".1111111111111111", _gc(xdtoal(powl(-3, -2)))); EXPECT_STREQ("-0", _gc(xdtoal(powl(-0., MAX(1, rando) | 1)))); EXPECT_STREQ("0", _gc(xdtoal(powl(-0., MAX(1, rando) & ~1)))); } TEST(pow, test) { EXPECT_STREQ("27", fmtd(pow(3, 3))); EXPECT_STREQ("-27", fmtd(pow(-3, 3))); EXPECT_STREQ("1e+308", fmtd(pow(10, 308))); EXPECT_STREQ("inf", fmtd(pow(10, 309))); EXPECT_STREQ("0", fmtd(pow(10, -5000))); EXPECT_STREQ("1.06338239662793e+37", fmtd(pow(2, 123))); EXPECT_STARTSWITH("0.42484968054675", fmtd(pow(.7, 2.4))); EXPECT_STREQ("1", fmtd(pow(1, NAN))); EXPECT_STREQ("1", fmtd(pow(1, rando))); EXPECT_STREQ("1", fmtd(pow(NAN, 0))); EXPECT_STREQ("1", fmtd(pow(rando, 0))); EXPECT_STREQ("0", fmtd(pow(0, 1))); EXPECT_STREQ("0", fmtd(pow(0, 2))); EXPECT_STREQ("0", fmtd(pow(0, 2.1))); EXPECT_STREQ("1", fmtd(pow(-1, INFINITY))); EXPECT_STREQ("1", fmtd(pow(-1, -INFINITY))); EXPECT_STREQ("inf", fmtd(pow(1. / MAX(2, rando), -INFINITY))); EXPECT_STREQ("0", fmtd(pow(1.1, -INFINITY))); EXPECT_STREQ("0", fmtd(pow(MAX(2, rando), -INFINITY))); EXPECT_STREQ("0", fmtd(pow(1. / MAX(2, rando), INFINITY))); EXPECT_STREQ("inf", fmtd(pow(MAX(2, rando), INFINITY))); EXPECT_STREQ("-0", fmtd(pow(-INFINITY, -1))); EXPECT_STREQ("0", fmtd(pow(-INFINITY, -1.1))); EXPECT_STREQ("0", fmtd(pow(-INFINITY, -2))); EXPECT_STREQ("0", fmtd(pow(-INFINITY, -2.1))); EXPECT_STREQ("-0", fmtd(pow(-INFINITY, -3))); EXPECT_STREQ("0", fmtd(pow(-INFINITY, -3.1))); EXPECT_STREQ("-inf", fmtd(pow(-INFINITY, 1))); EXPECT_STREQ("inf", fmtd(pow(-INFINITY, 1.1))); EXPECT_STREQ("inf", fmtd(pow(-INFINITY, 2))); EXPECT_STREQ("inf", fmtd(pow(-INFINITY, 2.1))); EXPECT_STREQ("-inf", fmtd(pow(-INFINITY, 3))); EXPECT_STREQ("inf", fmtd(pow(-INFINITY, 3.1))); EXPECT_STREQ("0", fmtd(pow(INFINITY, -1))); EXPECT_STREQ("0", fmtd(pow(INFINITY, -.1))); EXPECT_STREQ("inf", fmtd(pow(INFINITY, 1))); EXPECT_STREQ("inf", fmtd(pow(INFINITY, .1))); EXPECT_STREQ("1", fmtd(pow(INFINITY, 0))); EXPECT_STREQ("1", fmtd(pow(INFINITY, -0.))); EXPECT_STREQ("1", fmtd(pow(0, 0))); EXPECT_STREQ("1", fmtd(pow(0, -0.))); EXPECT_STREQ("inf", fmtd(pow(0, -(MAX(rando, 1) | 1)))); EXPECT_STREQ("-inf", fmtd(pow(-0., -(MAX(rando, 1) | 1)))); EXPECT_STREQ("inf", fmtd(pow(0, -(rando & -2)))); EXPECT_STREQ("inf", fmtd(pow(-0., -(rando & -2)))); EXPECT_STREQ("-0.333333333333333", fmtd(pow(-3, -1))); EXPECT_STREQ("0.111111111111111", fmtd(pow(-3, -2))); EXPECT_STREQ("-0", _gc(xdtoa(pow(-0., MAX(1, rando) | 1)))); EXPECT_STREQ("0", _gc(xdtoa(pow(-0., MAX(1, rando) & ~1)))); } TEST(powf, test) { EXPECT_STREQ("27", fmtf(powf(3, 3))); EXPECT_STREQ("-27", fmtf(powf(-3, 3))); EXPECT_STREQ("1e+38", fmtf(powf(10, 38))); EXPECT_STREQ("inf", fmtf(powf(10, 39))); EXPECT_STREQ("0", fmtf(powf(10, -5000))); EXPECT_STREQ("1.06338e+37", fmtf(powf(2, 123))); EXPECT_STARTSWITH("0.42485", fmtf(powf(.7, 2.4))); EXPECT_STREQ("1", fmtf(powf(1, NAN))); EXPECT_STREQ("1", fmtf(powf(1, rando))); EXPECT_STREQ("1", fmtf(powf(NAN, 0))); EXPECT_STREQ("1", fmtf(powf(rando, 0))); EXPECT_STREQ("0", fmtf(powf(0, 1))); EXPECT_STREQ("0", fmtf(powf(0, 2))); EXPECT_STREQ("0", fmtf(powf(0, 2.1))); EXPECT_STREQ("1", fmtf(powf(-1, INFINITY))); EXPECT_STREQ("1", fmtf(powf(-1, -INFINITY))); EXPECT_STREQ("inf", fmtf(powf(1. / MAX(2, rando), -INFINITY))); EXPECT_STREQ("0", fmtf(powf(1.1, -INFINITY))); EXPECT_STREQ("0", fmtf(powf(MAX(2, rando), -INFINITY))); EXPECT_STREQ("0", fmtf(powf(1. / MAX(2, rando), INFINITY))); EXPECT_STREQ("inf", fmtf(powf(MAX(2, rando), INFINITY))); EXPECT_STREQ("-0", fmtf(powf(-INFINITY, -1))); EXPECT_STREQ("0", fmtf(powf(-INFINITY, -1.1))); EXPECT_STREQ("0", fmtf(powf(-INFINITY, -2))); EXPECT_STREQ("0", fmtf(powf(-INFINITY, -2.1))); EXPECT_STREQ("-0", fmtf(powf(-INFINITY, -3))); EXPECT_STREQ("0", fmtf(powf(-INFINITY, -3.1))); EXPECT_STREQ("-inf", fmtf(powf(-INFINITY, 1))); EXPECT_STREQ("inf", fmtf(powf(-INFINITY, 1.1))); EXPECT_STREQ("inf", fmtf(powf(-INFINITY, 2))); EXPECT_STREQ("inf", fmtf(powf(-INFINITY, 2.1))); EXPECT_STREQ("-inf", fmtf(powf(-INFINITY, 3))); EXPECT_STREQ("inf", fmtf(powf(-INFINITY, 3.1))); EXPECT_STREQ("0", fmtf(powf(INFINITY, -1))); EXPECT_STREQ("0", fmtf(powf(INFINITY, -.1))); EXPECT_STREQ("inf", fmtf(powf(INFINITY, 1))); EXPECT_STREQ("inf", fmtf(powf(INFINITY, .1))); EXPECT_STREQ("1", fmtf(powf(INFINITY, 0))); EXPECT_STREQ("1", fmtf(powf(INFINITY, -0.))); EXPECT_STREQ("1", fmtf(powf(0, 0))); EXPECT_STREQ("1", fmtf(powf(0, -0.))); EXPECT_STREQ("inf", fmtf(powf(0, -(MAX(rando, 1) | 1)))); EXPECT_STREQ("-inf", fmtf(powf(-0., -(MAX(rando, 1) | 1)))); EXPECT_STREQ("inf", fmtf(powf(0, -(rando & -2)))); EXPECT_STREQ("inf", fmtf(powf(-0., -(rando & -2)))); EXPECT_STREQ("-0.333333", fmtf(powf(-3, -1))); EXPECT_STREQ("0.111111", fmtf(powf(-3, -2))); EXPECT_STREQ("-0", _gc(xdtoaf(powf(-0., MAX(1, rando) | 1)))); EXPECT_STREQ("0", _gc(xdtoaf(powf(-0., MAX(1, rando) & ~1)))); } #if 0 TEST(powl, errors) { EXPECT_STREQ("1", fmtd(POW(0., 0.))); EXPECT_STREQ("1", fmtd(POW(0., -0.))); EXPECT_STREQ("0", fmtd(POW(0., .5))); errno = 0; EXPECT_STREQ("inf", fmtd(POW(0., -.5))); EXPECT_EQ(ERANGE, errno); EXPECT_STREQ("0", fmtd(POW(0., 1.))); EXPECT_STREQ("inf", fmtd(POW(0., -1.))); EXPECT_STREQ("0", fmtd(POW(0., 1.5))); errno = 0; EXPECT_STREQ("inf", fmtd(POW(0., -1.5))); EXPECT_EQ(ERANGE, errno); EXPECT_STREQ("0", fmtd(POW(0., 2.))); errno = 0; EXPECT_STREQ("inf", fmtd(POW(0., -2.))); EXPECT_EQ(ERANGE, errno); EXPECT_TRUE(isnan(POW(0., NAN))); EXPECT_TRUE(isnan(POW(0., -NAN))); EXPECT_STREQ("0", fmtd(POW(0., INFINITY))); EXPECT_STREQ("inf", fmtd(POW(0., -INFINITY))); EXPECT_STREQ("0", fmtd(POW(0., __DBL_MIN__))); EXPECT_STREQ("0", fmtd(POW(0., __DBL_MAX__))); EXPECT_STREQ("1", fmtd(POW(-0., 0.))); EXPECT_STREQ("1", fmtd(POW(-0., -0.))); EXPECT_STREQ("0", fmtd(POW(-0., .5))); errno = 0; EXPECT_STREQ("inf", fmtd(POW(-0., -.5))); EXPECT_EQ(ERANGE, errno); EXPECT_STREQ("-0", fmtd(POW(-0., 1.))); EXPECT_STREQ("-inf", fmtd(POW(-0., -1.))); EXPECT_STREQ("0", fmtd(POW(-0., 1.5))); errno = 0; EXPECT_STREQ("inf", fmtd(POW(-0., -1.5))); EXPECT_EQ(ERANGE, errno); EXPECT_STREQ("0", fmtd(POW(-0., 2.))); errno = 0; EXPECT_STREQ("inf", fmtd(POW(-0., -2.))); EXPECT_EQ(ERANGE, errno); EXPECT_TRUE(isnan(POW(-0., NAN))); EXPECT_TRUE(isnan(POW(-0., -NAN))); EXPECT_STREQ("0", fmtd(POW(-0., INFINITY))); EXPECT_STREQ("inf", fmtd(POW(-0., -INFINITY))); EXPECT_STREQ("0", fmtd(POW(-0., __DBL_MIN__))); EXPECT_STREQ("0", fmtd(POW(-0., __DBL_MAX__))); EXPECT_STREQ("1", fmtd(POW(.5, 0.))); EXPECT_STREQ("1", fmtd(POW(.5, -0.))); EXPECT_STREQ("0.707106781186548", fmtd(POW(.5, .5))); EXPECT_STREQ("1.4142135623731", fmtd(POW(.5, -.5))); EXPECT_STREQ("0.5", fmtd(POW(.5, 1.))); EXPECT_STREQ("2", fmtd(POW(.5, -1.))); EXPECT_STREQ("0.353553390593274", fmtd(POW(.5, 1.5))); EXPECT_STREQ("2.82842712474619", fmtd(POW(.5, -1.5))); EXPECT_STREQ("0.25", fmtd(POW(.5, 2.))); EXPECT_STREQ("4", fmtd(POW(.5, -2.))); EXPECT_TRUE(isnan(POW(.5, NAN))); EXPECT_TRUE(isnan(POW(.5, -NAN))); EXPECT_STREQ("0", fmtd(POW(.5, INFINITY))); EXPECT_STREQ("inf", fmtd(POW(.5, -INFINITY))); EXPECT_STREQ("1", fmtd(POW(.5, __DBL_MIN__))); errno = 0; EXPECT_STREQ("0", fmtd(POW(.5, __DBL_MAX__))); /* EXPECT_EQ(ERANGE, errno); */ EXPECT_STREQ("1", fmtd(POW(-.5, 0.))); EXPECT_STREQ("1", fmtd(POW(-.5, -0.))); errno = 0; EXPECT_TRUE(isnan(POW(-.5, .5))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_TRUE(isnan(POW(-.5, -.5))); EXPECT_EQ(EDOM, errno); EXPECT_STREQ("-0.5", fmtd(POW(-.5, 1.))); EXPECT_STREQ("-2", fmtd(POW(-.5, -1.))); errno = 0; EXPECT_TRUE(isnan(POW(-.5, 1.5))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_TRUE(isnan(POW(-.5, -1.5))); EXPECT_EQ(EDOM, errno); EXPECT_STREQ("0.25", fmtd(POW(-.5, 2.))); EXPECT_STREQ("4", fmtd(POW(-.5, -2.))); EXPECT_TRUE(isnan(POW(-.5, NAN))); EXPECT_TRUE(isnan(POW(-.5, -NAN))); EXPECT_STREQ("0", fmtd(POW(-.5, INFINITY))); EXPECT_STREQ("inf", fmtd(POW(-.5, -INFINITY))); errno = 0; EXPECT_TRUE(isnan(POW(-.5, __DBL_MIN__))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_STREQ("0", fmtd(POW(-.5, __DBL_MAX__))); /* EXPECT_EQ(ERANGE, errno); */ EXPECT_STREQ("1", fmtd(POW(1., 0.))); EXPECT_STREQ("1", fmtd(POW(1., -0.))); EXPECT_STREQ("1", fmtd(POW(1., .5))); EXPECT_STREQ("1", fmtd(POW(1., -.5))); EXPECT_STREQ("1", fmtd(POW(1., 1.))); EXPECT_STREQ("1", fmtd(POW(1., -1.))); EXPECT_STREQ("1", fmtd(POW(1., 1.5))); EXPECT_STREQ("1", fmtd(POW(1., -1.5))); EXPECT_STREQ("1", fmtd(POW(1., 2.))); EXPECT_STREQ("1", fmtd(POW(1., -2.))); EXPECT_STREQ("1", fmtd(POW(1., NAN))); EXPECT_STREQ("1", fmtd(POW(1., -NAN))); EXPECT_STREQ("1", fmtd(POW(1., INFINITY))); EXPECT_STREQ("1", fmtd(POW(1., -INFINITY))); EXPECT_STREQ("1", fmtd(POW(1., __DBL_MIN__))); EXPECT_STREQ("1", fmtd(POW(1., __DBL_MAX__))); EXPECT_STREQ("1", fmtd(POW(-1., 0.))); EXPECT_STREQ("1", fmtd(POW(-1., -0.))); errno = 0; EXPECT_TRUE(isnan(POW(-1., .5))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_TRUE(isnan(POW(-1., -.5))); EXPECT_EQ(EDOM, errno); EXPECT_STREQ("-1", fmtd(POW(-1., 1.))); EXPECT_STREQ("-1", fmtd(POW(-1., -1.))); errno = 0; EXPECT_TRUE(isnan(POW(-1., 1.5))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_TRUE(isnan(POW(-1., -1.5))); EXPECT_EQ(EDOM, errno); EXPECT_STREQ("1", fmtd(POW(-1., 2.0))); EXPECT_STREQ("1", fmtd(POW(-1., -2.0))); EXPECT_TRUE(isnan(POW(-1., NAN))); EXPECT_TRUE(isnan(POW(-1., -NAN))); EXPECT_STREQ("1", fmtd(POW(-1., INFINITY))); EXPECT_STREQ("1", fmtd(POW(-1., -INFINITY))); errno = 0; EXPECT_TRUE(isnan(POW(-1., __DBL_MIN__))); EXPECT_EQ(EDOM, errno); EXPECT_STREQ("1", fmtd(POW(-1., __DBL_MAX__))); EXPECT_STREQ("1", fmtd(POW(1.5, 0.))); EXPECT_STREQ("1", fmtd(POW(1.5, -0.))); EXPECT_STREQ("1.22474487139159", fmtd(POW(1.5, .5))); EXPECT_STREQ("0.816496580927726", fmtd(POW(1.5, -.5))); EXPECT_STREQ("1.5", fmtd(POW(1.5, 1.))); EXPECT_STREQ("0.666666666666667", fmtd(POW(1.5, -1.))); EXPECT_STREQ("1.83711730708738", fmtd(POW(1.5, 1.5))); EXPECT_STREQ("0.544331053951817", fmtd(POW(1.5, -1.5))); EXPECT_STREQ("2.25", fmtd(POW(1.5, 2.0))); EXPECT_STREQ("0.444444444444444", fmtd(POW(1.5, -2.0))); EXPECT_TRUE(isnan(POW(1.5, NAN))); EXPECT_TRUE(isnan(POW(1.5, -NAN))); EXPECT_STREQ("inf", fmtd(POW(1.5, INFINITY))); EXPECT_STREQ("0", fmtd(POW(1.5, -INFINITY))); EXPECT_STREQ("1", fmtd(POW(1.5, __DBL_MIN__))); errno = 0; EXPECT_STREQ("inf", fmtd(POW(1.5, __DBL_MAX__))); /* EXPECT_EQ(ERANGE, errno); */ EXPECT_STREQ("1", fmtd(POW(-1.5, 0.))); EXPECT_STREQ("1", fmtd(POW(-1.5, -0.))); errno = 0; EXPECT_TRUE(isnan(POW(-1.5, .5))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_TRUE(isnan(POW(-1.5, -.5))); EXPECT_EQ(EDOM, errno); EXPECT_STREQ("-1.5", fmtd(POW(-1.5, 1.))); EXPECT_STREQ("-0.666666666666667", fmtd(POW(-1.5, -1.))); errno = 0; EXPECT_TRUE(isnan(POW(-1.5, 1.5))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_TRUE(isnan(POW(-1.5, -1.5))); EXPECT_EQ(EDOM, errno); EXPECT_STREQ("2.25", fmtd(POW(-1.5, 2.0))); EXPECT_STREQ("0.444444444444444", fmtd(POW(-1.5, -2.0))); EXPECT_TRUE(isnan(POW(-1.5, NAN))); EXPECT_TRUE(isnan(POW(-1.5, -NAN))); EXPECT_STREQ("inf", fmtd(POW(-1.5, INFINITY))); EXPECT_STREQ("0", fmtd(POW(-1.5, -INFINITY))); errno = 0; EXPECT_TRUE(isnan(POW(-1.5, __DBL_MIN__))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_STREQ("inf", fmtd(POW(-1.5, __DBL_MAX__))); /* EXPECT_EQ(ERANGE, errno); */ EXPECT_STREQ("1", fmtd(POW(+2.0, 0.))); EXPECT_STREQ("1", fmtd(POW(+2.0, -0.))); EXPECT_STREQ("1.4142135623731", fmtd(POW(+2.0, .5))); EXPECT_STREQ("0.707106781186548", fmtd(POW(+2.0, -.5))); EXPECT_STREQ("2", fmtd(POW(+2.0, 1.))); EXPECT_STREQ("0.5", fmtd(POW(+2.0, -1.))); EXPECT_STREQ("2.82842712474619", fmtd(POW(+2.0, 1.5))); EXPECT_STREQ("0.353553390593274", fmtd(POW(+2.0, -1.5))); EXPECT_STREQ("4", fmtd(POW(+2.0, 2.0))); EXPECT_STREQ("0.25", fmtd(POW(+2.0, -2.0))); EXPECT_TRUE(isnan(POW(+2.0, NAN))); EXPECT_TRUE(isnan(POW(+2.0, -NAN))); EXPECT_STREQ("inf", fmtd(POW(+2.0, INFINITY))); EXPECT_STREQ("0", fmtd(POW(+2.0, -INFINITY))); EXPECT_STREQ("1", fmtd(POW(+2.0, __DBL_MIN__))); errno = 0; EXPECT_STREQ("inf", fmtd(POW(+2.0, __DBL_MAX__))); /* EXPECT_EQ(ERANGE, errno); */ EXPECT_STREQ("1", fmtd(POW(-2.0, 0.))); EXPECT_STREQ("1", fmtd(POW(-2.0, -0.))); errno = 0; EXPECT_TRUE(isnan(POW(-2.0, .5))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_TRUE(isnan(POW(-2.0, -.5))); EXPECT_EQ(EDOM, errno); EXPECT_STREQ("-2", fmtd(POW(-2.0, 1.))); EXPECT_STREQ("-0.5", fmtd(POW(-2.0, -1.))); errno = 0; EXPECT_TRUE(isnan(POW(-2.0, 1.5))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_TRUE(isnan(POW(-2.0, -1.5))); EXPECT_EQ(EDOM, errno); EXPECT_STREQ("4", fmtd(POW(-2.0, 2.0))); EXPECT_STREQ("0.25", fmtd(POW(-2.0, -2.0))); EXPECT_TRUE(isnan(POW(-2.0, NAN))); EXPECT_TRUE(isnan(POW(-2.0, -NAN))); EXPECT_STREQ("inf", fmtd(POW(-2.0, INFINITY))); EXPECT_STREQ("0", fmtd(POW(-2.0, -INFINITY))); errno = 0; EXPECT_TRUE(isnan(POW(-2.0, __DBL_MIN__))); EXPECT_EQ(EDOM, errno); errno = 0; EXPECT_STREQ("inf", fmtd(POW(-2.0, __DBL_MAX__))); /* EXPECT_EQ(ERANGE, errno); */ EXPECT_STREQ("1", fmtd(POW(NAN, 0.))); EXPECT_STREQ("1", fmtd(POW(NAN, -0.))); EXPECT_TRUE(isnan(POW(NAN, .5))); EXPECT_TRUE(isnan(POW(NAN, -.5))); EXPECT_TRUE(isnan(POW(NAN, 1.))); EXPECT_TRUE(isnan(POW(NAN, -1.))); EXPECT_TRUE(isnan(POW(NAN, 1.5))); EXPECT_TRUE(isnan(POW(NAN, -1.5))); EXPECT_TRUE(isnan(POW(NAN, 2.0))); EXPECT_TRUE(isnan(POW(NAN, -2.0))); EXPECT_TRUE(isnan(POW(NAN, NAN))); EXPECT_TRUE(isnan(POW(NAN, -NAN))); EXPECT_TRUE(isnan(POW(NAN, INFINITY))); EXPECT_TRUE(isnan(POW(NAN, -INFINITY))); EXPECT_TRUE(isnan(POW(NAN, __DBL_MIN__))); EXPECT_TRUE(isnan(POW(NAN, __DBL_MAX__))); EXPECT_STREQ("1", fmtd(POW(-NAN, 0.))); EXPECT_STREQ("1", fmtd(POW(-NAN, -0.))); EXPECT_TRUE(isnan(POW(-NAN, .5))); EXPECT_TRUE(isnan(POW(-NAN, -.5))); EXPECT_TRUE(isnan(POW(-NAN, 1.))); EXPECT_TRUE(isnan(POW(-NAN, -1.))); EXPECT_TRUE(isnan(POW(-NAN, 1.5))); EXPECT_TRUE(isnan(POW(-NAN, -1.5))); EXPECT_TRUE(isnan(POW(-NAN, 2.0))); EXPECT_TRUE(isnan(POW(-NAN, -2.0))); EXPECT_TRUE(isnan(POW(-NAN, NAN))); EXPECT_TRUE(isnan(POW(-NAN, -NAN))); EXPECT_TRUE(isnan(POW(-NAN, INFINITY))); EXPECT_TRUE(isnan(POW(-NAN, -INFINITY))); EXPECT_TRUE(isnan(POW(-NAN, __DBL_MIN__))); EXPECT_TRUE(isnan(POW(-NAN, __DBL_MAX__))); EXPECT_STREQ("1", fmtd(POW(INFINITY, 0.))); EXPECT_STREQ("1", fmtd(POW(INFINITY, -0.))); EXPECT_STREQ("inf", fmtd(POW(INFINITY, .5))); EXPECT_STREQ("0", fmtd(POW(INFINITY, -.5))); EXPECT_STREQ("inf", fmtd(POW(INFINITY, 1.))); EXPECT_STREQ("0", fmtd(POW(INFINITY, -1.))); EXPECT_STREQ("inf", fmtd(POW(INFINITY, 1.5))); EXPECT_STREQ("0", fmtd(POW(INFINITY, -1.5))); EXPECT_STREQ("inf", fmtd(POW(INFINITY, 2.0))); EXPECT_STREQ("0", fmtd(POW(INFINITY, -2.0))); EXPECT_TRUE(isnan(POW(INFINITY, NAN))); EXPECT_TRUE(isnan(POW(INFINITY, -NAN))); EXPECT_STREQ("inf", fmtd(POW(INFINITY, INFINITY))); EXPECT_STREQ("0", fmtd(POW(INFINITY, -INFINITY))); EXPECT_STREQ("inf", fmtd(POW(INFINITY, __DBL_MIN__))); EXPECT_STREQ("inf", fmtd(POW(INFINITY, __DBL_MAX__))); EXPECT_STREQ("1", fmtd(POW(-INFINITY, 0.))); EXPECT_STREQ("1", fmtd(POW(-INFINITY, -0.))); EXPECT_STREQ("inf", fmtd(POW(-INFINITY, .5))); EXPECT_STREQ("0", fmtd(POW(-INFINITY, -.5))); EXPECT_STREQ("-inf", fmtd(POW(-INFINITY, 1.))); EXPECT_STREQ("-0", fmtd(POW(-INFINITY, -1.))); EXPECT_STREQ("inf", fmtd(POW(-INFINITY, 1.5))); EXPECT_STREQ("0", fmtd(POW(-INFINITY, -1.5))); EXPECT_STREQ("inf", fmtd(POW(-INFINITY, 2.0))); EXPECT_STREQ("0", fmtd(POW(-INFINITY, -2.0))); EXPECT_TRUE(isnan(POW(-INFINITY, NAN))); EXPECT_TRUE(isnan(POW(-INFINITY, -NAN))); EXPECT_STREQ("inf", fmtd(POW(-INFINITY, INFINITY))); EXPECT_STREQ("0", fmtd(POW(-INFINITY, -INFINITY))); EXPECT_STREQ("inf", fmtd(POW(-INFINITY, __DBL_MIN__))); EXPECT_STREQ("inf", fmtd(POW(-INFINITY, __DBL_MAX__))); EXPECT_STREQ("1", fmtd(POW(__DBL_MIN__, 0.))); EXPECT_STREQ("1", fmtd(POW(__DBL_MIN__, -0.))); EXPECT_STREQ("1.49166814624004e-154", fmtd(POW(__DBL_MIN__, .5))); EXPECT_STREQ("6.7039039649713e+153", fmtd(POW(__DBL_MIN__, -.5))); EXPECT_STREQ("2.2250738585072e-308", fmtd(POW(__DBL_MIN__, 1.))); EXPECT_STREQ("4.49423283715579e+307", fmtd(POW(__DBL_MIN__, -1.))); errno = 0; EXPECT_STREQ("0", fmtd(POW(__DBL_MIN__, 1.5))); /* EXPECT_EQ(ERANGE, errno); */ errno = 0; EXPECT_STREQ("inf", fmtd(POW(__DBL_MIN__, -1.5))); /* EXPECT_EQ(ERANGE, errno); */ EXPECT_STREQ("0", fmtd(POW(__DBL_MIN__, 2.0))); EXPECT_STREQ("inf", fmtd(POW(__DBL_MIN__, -2.0))); EXPECT_TRUE(isnan(POW(__DBL_MIN__, NAN))); EXPECT_TRUE(isnan(POW(__DBL_MIN__, -NAN))); EXPECT_STREQ("0", fmtd(POW(__DBL_MIN__, INFINITY))); EXPECT_STREQ("inf", fmtd(POW(__DBL_MIN__, -INFINITY))); EXPECT_STREQ("1", fmtd(POW(__DBL_MIN__, __DBL_MIN__))); /* errno = 0; */ /* wut */ /* EXPECT_STREQ("0", fmtd(POW(__DBL_MIN__, __DBL_MAX__))); */ /* EXPECT_EQ(ERANGE, errno); */ EXPECT_STREQ("1", fmtd(POW(__DBL_MAX__, 0.))); EXPECT_STREQ("1", fmtd(POW(__DBL_MAX__, -0.))); EXPECT_STREQ("1.34078079299426e+154", fmtd(POW(__DBL_MAX__, .5))); EXPECT_STREQ("7.45834073120021e-155", fmtd(POW(__DBL_MAX__, -.5))); EXPECT_STREQ("1.79769313486232e+308", fmtd(POW(__DBL_MAX__, 1.))); EXPECT_STREQ("5.562684646268e-309", fmtd(POW(__DBL_MAX__, -1.))); errno = 0; EXPECT_STREQ("inf", fmtd(POW(__DBL_MAX__, 1.5))); /* EXPECT_EQ(ERANGE, errno); */ errno = 0; EXPECT_STREQ("0", fmtd(POW(__DBL_MAX__, -1.5))); /* EXPECT_EQ(ERANGE, errno); */ EXPECT_STREQ("inf", fmtd(POW(__DBL_MAX__, 2.0))); errno = 0; EXPECT_STREQ("0", fmtd(POW(__DBL_MAX__, -2.0))); /* EXPECT_EQ(ERANGE, errno); */ EXPECT_TRUE(isnan(POW(__DBL_MAX__, NAN))); EXPECT_TRUE(isnan(POW(__DBL_MAX__, -NAN))); EXPECT_STREQ("inf", fmtd(POW(__DBL_MAX__, INFINITY))); EXPECT_STREQ("0", fmtd(POW(__DBL_MAX__, -INFINITY))); EXPECT_STREQ("1", fmtd(POW(__DBL_MAX__, __DBL_MIN__))); errno = 0; EXPECT_STREQ("inf", fmtd(POW(__DBL_MAX__, __DBL_MAX__))); /* EXPECT_EQ(ERANGE, errno); */ EXPECT_STREQ("1", _gc(xasprintf("%.15g", POW(0., 0)))); EXPECT_STREQ("1", _gc(xasprintf("%.15g", POW(-0., 0)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", POW(-0., 1)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", POW(-0., 11)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", POW(-0., 111)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", POW(-0., 2)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", POW(-0., 22)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", POW(-0., 222)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", POW(-0., 2.5)))); } #endif BENCH(powl, bench) { double _pow(double, double) asm("pow"); float _powf(float, float) asm("powf"); long double _powl(long double, long double) asm("powl"); EZBENCH2("pow", donothing, _pow(.7, .2)); /* ~18ns */ EZBENCH2("powf", donothing, _powf(.7, .2)); /* ~16ns */ EZBENCH2("powl", donothing, _powl(.7, .2)); /* ~56ns */ }
24,878
572
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/acos_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/stdio/rand.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" double _acos(double) asm("acos"); float _acosf(float) asm("acosf"); long double _acosl(long double) asm("acosl"); TEST(acos, test) { EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _acos(0.)))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _acos(-0.)))); EXPECT_STREQ("1.0471975511966", _gc(xasprintf("%.15g", _acos(.5)))); EXPECT_STREQ("2.0943951023932", _gc(xasprintf("%.15g", _acos(-.5)))); EXPECT_STREQ("0", _gc(xasprintf("%.15g", _acos(1.)))); EXPECT_STREQ("3.14159265358979", _gc(xasprintf("%.15g", _acos(-1.)))); EXPECT_TRUE(isnan(_acos(1.5))); EXPECT_TRUE(isnan(_acos(-1.5))); EXPECT_TRUE(isnan(_acos(2.))); EXPECT_TRUE(isnan(_acos(NAN))); EXPECT_TRUE(isnan(_acos(-NAN))); EXPECT_TRUE(isnan(_acos(INFINITY))); EXPECT_TRUE(isnan(_acos(-INFINITY))); EXPECT_STREQ("1.5707963267949", _gc(xasprintf("%.15g", _acos(__DBL_MIN__)))); EXPECT_TRUE(isnan(_acos(__DBL_MAX__))); } BENCH(acos, bench) { EZBENCH2("acos", donothing, _acos(.7)); /* ~13ns */ EZBENCH2("acosf", donothing, _acosf(.7)); /* ~10ns */ EZBENCH2("acosl", donothing, _acosl(.7)); /* ~26ns */ }
3,128
53
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/sincos_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" TEST(sincos, test) { double sine, cosine; sincos(.1, &sine, &cosine); EXPECT_STREQ("0.0998334166468282", _gc(xasprintf("%.15g", sine))); EXPECT_STREQ("0.995004165278026", _gc(xasprintf("%.15g", cosine))); } TEST(sincosf, test) { float sine, cosine; sincosf(.1, &sine, &cosine); EXPECT_STREQ("0.0998334", _gc(xasprintf("%.6g", sine))); EXPECT_STREQ("0.995004", _gc(xasprintf("%.6g", cosine))); } TEST(sincosl, test) { long double sine, cosine; sincosl(.1, &sine, &cosine); #ifndef __aarch64__ // TODO(jart): get quad floats working with printf EXPECT_STREQ("0.0998334166468282", _gc(xasprintf("%.15Lg", sine))); EXPECT_STREQ("0.995004165278026", _gc(xasprintf("%.15Lg", cosine))); #endif } #define NUM .123 BENCH(sincos, bench) { double _sin(double) asm("sin"); float _sinf(float) asm("sinf"); long double _sinl(long double) asm("sinl"); double _cos(double) asm("cos"); float _cosf(float) asm("cosf"); long double _cosl(long double) asm("cosl"); double _sincos(double, double*, double*) asm("sincos"); float _sincosf(float, float*, float*) asm("sincosf"); long double _sincosl(long double, long double*, long double*) asm("sincosl"); volatile float sinef, cosinef; volatile double sine, cosine; volatile long double sinel, cosinel; EZBENCH2("sin+cos", donothing, (_sin(NUM), _cos(NUM))); EZBENCH2("sincos", donothing, _sincos(NUM, &sine, &cosine)); EZBENCH2("sinf+cosf", donothing, (_sinf(NUM), _cosf(NUM))); EZBENCH2("sincosf", donothing, _sincosf(NUM, &sinef, &cosinef)); EZBENCH2("sinl+cosl", donothing, (_sinl(NUM), _cosl(NUM))); EZBENCH2("sincosl", donothing, _sincosl(NUM, &sinel, &cosinel)); }
3,649
71
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/sin_test.c
/*-*- 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/math.h" #include "libc/mem/gc.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" double _sin(double) asm("sin"); float _sinf(float) asm("sinf"); long double _sinl(long double) asm("sinl"); TEST(sinl, test) { EXPECT_STREQ("NAN", _gc(xdtoal(_sinl(NAN)))); EXPECT_TRUE(isnan(_sinl(+INFINITY))); EXPECT_TRUE(isnan(_sinl(-INFINITY))); EXPECT_STREQ(".479425538604203", _gc(xdtoal(_sinl(.5)))); EXPECT_STREQ("-.479425538604203", _gc(xdtoal(_sinl(-.5)))); EXPECT_STREQ(".8414709794048734", _gc(xdtoal(_sinl(.99999999)))); /* EXPECT_STREQ("-.998836772397", _gc(xdtoal(_sinl(555555555555)))); */ /* EXPECT_STREQ("1", _gc(xdtoal(_sinl(5.319372648326541e+255L)))); */ } TEST(sin, test) { EXPECT_STREQ("0", _gc(xasprintf("%.15g", _sin(0.)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _sin(-0.)))); EXPECT_STREQ("0.0998334166468282", _gc(xasprintf("%.15g", _sin(.1)))); EXPECT_STREQ("-0.0998334166468282", _gc(xasprintf("%.15g", _sin(-.1)))); EXPECT_STREQ("0.479425538604203", _gc(xasprintf("%.15g", _sin(.5)))); EXPECT_STREQ("-0.479425538604203", _gc(xasprintf("%.15g", _sin(-.5)))); EXPECT_STREQ("0.841470984807897", _gc(xasprintf("%.15g", _sin(1.)))); EXPECT_STREQ("-0.841470984807897", _gc(xasprintf("%.15g", _sin(-1.)))); EXPECT_STREQ("0.997494986604054", _gc(xasprintf("%.15g", _sin(1.5)))); EXPECT_STREQ("-0.997494986604054", _gc(xasprintf("%.15g", _sin(-1.5)))); EXPECT_STREQ("0.909297426825682", _gc(xasprintf("%.15g", _sin(2.)))); EXPECT_TRUE(isnan(_sin(NAN))); EXPECT_TRUE(isnan(_sin(-NAN))); EXPECT_TRUE(isnan(_sin(INFINITY))); EXPECT_TRUE(isnan(_sin(-INFINITY))); EXPECT_STREQ("2.2250738585072e-308", _gc(xasprintf("%.15g", _sin(__DBL_MIN__)))); EXPECT_STREQ("0.00496195478918406", _gc(xasprintf("%.15g", _sin(__DBL_MAX__)))); EXPECT_STREQ("-0.841470984807897", _gc(xasprintf("%.15g", _sin(-1.0000000000000002)))); EXPECT_STREQ("-2.1073424255447e-08", _gc(xasprintf("%.15g", _sin(-2.1073424255447e-08)))); } TEST(sinf, test) { EXPECT_TRUE(isnan(_sinf(NAN))); EXPECT_TRUE(isnan(_sinf(+INFINITY))); EXPECT_TRUE(isnan(_sinf(-INFINITY))); EXPECT_STREQ("NAN", _gc(xdtoaf(_sinf(NAN)))); EXPECT_STARTSWITH(".479426", _gc(xdtoaf(_sinf(.5f)))); EXPECT_STARTSWITH("-.479426", _gc(xdtoaf(_sinf(-.5f)))); EXPECT_STARTSWITH(".873283", _gc(xdtoaf(_sinf(555)))); } BENCH(_sin, bench) { EZBENCH2("sin", donothing, _sin(.7)); /* ~5ns */ EZBENCH2("sinf", donothing, _sinf(.7)); /* ~5ns */ EZBENCH2("sinl", donothing, _sinl(.7)); /* ~28ns */ }
4,491
82
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/log_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(log, test) { EXPECT_STREQ("1", gc(xdtoa(log(M_E)))); EXPECT_STREQ("2", gc(xdtoa(log(M_E * M_E)))); EXPECT_STREQ("NAN", gc(xdtoa(log(NAN)))); EXPECT_STREQ("0", gc(xdtoa(log(1)))); EXPECT_STREQ("INFINITY", gc(xdtoa(log(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(log(0)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(log(-0.)))); EXPECT_TRUE(isnan(log(-1))); EXPECT_TRUE(isnan(log(-2))); } TEST(logf, test) { EXPECT_STREQ("1", gc(xdtoaf(logf(M_E)))); EXPECT_STREQ("NAN", gc(xdtoaf(logf(NAN)))); EXPECT_STREQ("0", gc(xdtoaf(logf(1)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(logf(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(logf(0)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(logf(-0.)))); EXPECT_TRUE(isnan(logf(-1))); EXPECT_TRUE(isnan(logf(-2))); } TEST(logl, test) { EXPECT_STREQ("1", gc(xdtoal(logl(2.71828182845904523536L)))); EXPECT_STREQ("NAN", gc(xdtoal(logl(NAN)))); EXPECT_STREQ("0", gc(xdtoal(logl(1)))); EXPECT_STREQ("INFINITY", gc(xdtoal(logl(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(logl(0)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(logl(-0.)))); EXPECT_TRUE(isnan(logl(-1))); EXPECT_TRUE(isnan(logl(-2))); } BENCH(logl, bench) { double _log(double) asm("log"); float _logf(float) asm("logf"); long double _logl(long double) asm("logl"); EZBENCH2("log", donothing, _log(.7)); // ~8ns EZBENCH2("logf", donothing, _logf(.7)); // ~6ns EZBENCH2("logl", donothing, _logl(.7)); // ~21ns }
3,458
67
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/erf_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(erf, test) { EXPECT_STREQ("NAN", gc(xdtoa(erf(NAN)))); EXPECT_STREQ("0", gc(xdtoa(erf(0)))); EXPECT_STREQ("1", gc(xdtoa(erf(INFINITY)))); EXPECT_STREQ(".999977909503001", gc(xdtoa(erf(3)))); } TEST(erfc, test) { EXPECT_STREQ("NAN", gc(xdtoa(erfc(NAN)))); EXPECT_STREQ("1", gc(xdtoa(erfc(0)))); EXPECT_STREQ("1", gc(xdtoa(erfc(-0.)))); EXPECT_STREQ("0", gc(xdtoa(erfc(INFINITY)))); EXPECT_STREQ("2", gc(xdtoa(erfc(-INFINITY)))); EXPECT_STREQ("2.20904969985854e-05", gc(xdtoa(erfc(3)))); }
2,464
39
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/tanh_test.c
/*-*- 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 2021 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/fmt/conv.h" #include "libc/intrin/kprintf.h" #include "libc/macros.internal.h" #include "libc/math.h" #include "libc/mem/gc.h" #include "libc/runtime/runtime.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "libc/x/xasprintf.h" double _tanh(double) asm("tanh"); float _tanhf(float) asm("tanhf"); long double _tanhl(long double) asm("tanhl"); TEST(tanh, test) { EXPECT_STREQ("0", _gc(xasprintf("%.15g", _tanh(0.)))); EXPECT_STREQ("-0", _gc(xasprintf("%.15g", _tanh(-0.)))); EXPECT_STREQ("0.0996679946249558", _gc(xasprintf("%.15g", _tanh(.1)))); EXPECT_STREQ("-0.0996679946249558", _gc(xasprintf("%.15g", _tanh(-.1)))); EXPECT_STREQ("0.46211715726001", _gc(xasprintf("%.15g", _tanh(.5)))); EXPECT_STREQ("-0.46211715726001", _gc(xasprintf("%.15g", _tanh(-.5)))); EXPECT_STREQ("0.761594155955765", _gc(xasprintf("%.15g", _tanh(1.)))); EXPECT_STREQ("-0.761594155955765", _gc(xasprintf("%.15g", _tanh(-1.)))); EXPECT_STREQ("0.905148253644866", _gc(xasprintf("%.15g", _tanh(1.5)))); EXPECT_STREQ("-0.905148253644866", _gc(xasprintf("%.15g", _tanh(-1.5)))); EXPECT_STREQ("0.964027580075817", _gc(xasprintf("%.15g", _tanh(2.)))); EXPECT_TRUE(isnan(_tanh(NAN))); EXPECT_TRUE(isnan(_tanh(-NAN))); EXPECT_STREQ("1", _gc(xasprintf("%.15g", _tanh(INFINITY)))); EXPECT_STREQ("-1", _gc(xasprintf("%.15g", _tanh(-INFINITY)))); EXPECT_STREQ("2.2250738585072e-308", _gc(xasprintf("%.15g", _tanh(__DBL_MIN__)))); EXPECT_STREQ("1", _gc(xasprintf("%.15g", _tanh(__DBL_MAX__)))); EXPECT_STREQ("-0.761594155955765", _gc(xasprintf("%.15g", _tanh(-1.0000000000000002)))); EXPECT_STREQ("-2.1073424255447e-08", _gc(xasprintf("%.15g", _tanh(-2.1073424255447e-08)))); } TEST(tanhf, test) { EXPECT_STREQ(".099668", _gc(xdtoaf(_tanhf(+.1)))); EXPECT_STREQ("-.099668", _gc(xdtoaf(_tanhf(-.1)))); EXPECT_STREQ("0", _gc(xdtoaf(_tanhf(0)))); EXPECT_STREQ("-0", _gc(xdtoaf(_tanhf(-0.)))); EXPECT_TRUE(isnan(_tanhf(NAN))); EXPECT_STREQ("1", _gc(xdtoaf(_tanhf(INFINITY)))); EXPECT_STREQ("-1", _gc(xdtoaf(_tanhf(-INFINITY)))); } TEST(tanhl, test) { EXPECT_STREQ(".09966799462495582", _gc(xdtoal(_tanhl(+.1)))); EXPECT_STREQ("-.09966799462495582", _gc(xdtoal(_tanhl(-.1)))); EXPECT_STREQ("0", _gc(xdtoal(_tanhl(0)))); EXPECT_STREQ("-0", _gc(xdtoal(_tanhl(-0.)))); EXPECT_TRUE(isnan(_tanhl(NAN))); EXPECT_STREQ("1", _gc(xdtoal(_tanhl(INFINITY)))); EXPECT_STREQ("-1", _gc(xdtoal(_tanhl(-INFINITY)))); } BENCH(tanhl, bench) { EZBENCH2("-tanh", donothing, _tanh(.7)); // ~27ns EZBENCH2("-tanhf", donothing, _tanhf(.7)); // ~15ns EZBENCH2("-tanhl", donothing, _tanhl(.7)); // ~42ns } static inline float i2f(uint32_t i) { union { uint32_t i; float f; } u = {i}; return u.f; } static inline uint32_t f2i(float f) { union { float f; uint32_t i; } u = {f}; return u.i; } #if 0 TEST(tanhf, brute) { long i; int lim = 100; uint32_t x, y; for (i = 0; i <= 0x100000000; i += 7) { x = f2i(tanhf(i2f(i))); y = f2i(tanhf2(i2f(i))); if (abs(x - y) > 2) { kprintf("bah %#lx %s %d\n", i, _gc(xdtoaf(i2f(i))), abs(x - y)); kprintf(" %-12s %#x\n", _gc(xdtoaf(i2f(x))), x); kprintf(" %-12s %#x\n", _gc(xdtoaf(i2f(y))), y); if (!--lim) break; } } if (lim != 100) { exit(1); } } #endif
5,243
121
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/logb_test.c
/*-*- 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/limits.h" #include "libc/math.h" #include "libc/stdio/rand.h" #include "libc/stdio/stdio.h" #include "libc/testlib/testlib.h" TEST(ilogb, yolo) { EXPECT_EQ(0, ilogb(1)); EXPECT_EQ(1, ilogb(2)); EXPECT_EQ(2, ilogb(4)); EXPECT_EQ(63, ilogb(1e19)); EXPECT_EQ(0, ilogbf(1)); EXPECT_EQ(1, ilogbf(2)); EXPECT_EQ(2, ilogbf(4)); EXPECT_EQ(63, ilogb(1e19)); EXPECT_EQ(0, ilogbl(1)); EXPECT_EQ(1, ilogbl(2)); EXPECT_EQ(2, ilogbl(4)); EXPECT_EQ(63, ilogbl(1e19)); } TEST(logb, yolo) { EXPECT_EQ(0, (int)logb(1)); EXPECT_EQ(1, (int)logb(2)); EXPECT_EQ(2, (int)logb(4)); EXPECT_EQ(63, (int)logb(1e19)); EXPECT_EQ(0, (int)logbf(1)); EXPECT_EQ(1, (int)logbf(2)); EXPECT_EQ(2, (int)logbf(4)); EXPECT_EQ(63, (int)logb(1e19)); EXPECT_EQ(0, (int)logbl(1)); EXPECT_EQ(1, (int)logbl(2)); EXPECT_EQ(2, (int)logbl(4)); EXPECT_EQ(63, (int)logbl(1e19)); } TEST(ilogb, NANandZero) { EXPECT_EQ(FP_ILOGB0, ilogb(0.0)); EXPECT_EQ(FP_ILOGBNAN, ilogb(NAN)); EXPECT_EQ(FP_ILOGB0, (int)ilogb(0)); EXPECT_EQ(FP_ILOGBNAN, (int)ilogb(NAN)); EXPECT_EQ(FP_ILOGB0, ilogbf(0)); EXPECT_EQ(FP_ILOGBNAN, ilogbf(NAN)); EXPECT_EQ(FP_ILOGB0, ilogbl(0)); EXPECT_EQ(FP_ILOGBNAN, ilogbl(NAN)); }
3,065
65
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/test.mk
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ #───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘ PKGS += TEST_LIBC_TINYMATH TEST_LIBC_TINYMATH_SRCS := $(wildcard test/libc/tinymath/*.c) TEST_LIBC_TINYMATH_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_TINYMATH_SRCS)) TEST_LIBC_TINYMATH_OBJS = \ $(TEST_LIBC_TINYMATH_SRCS:%.c=o/$(MODE)/%.o) TEST_LIBC_TINYMATH_COMS = \ $(TEST_LIBC_TINYMATH_SRCS:%.c=o/$(MODE)/%.com) TEST_LIBC_TINYMATH_BINS = \ $(TEST_LIBC_TINYMATH_COMS) \ $(TEST_LIBC_TINYMATH_COMS:%=%.dbg) TEST_LIBC_TINYMATH_TESTS = \ $(TEST_LIBC_TINYMATH_SRCS_TEST:%.c=o/$(MODE)/%.com.ok) TEST_LIBC_TINYMATH_CHECKS = \ $(TEST_LIBC_TINYMATH_SRCS_TEST:%.c=o/$(MODE)/%.com.runs) TEST_LIBC_TINYMATH_DIRECTDEPS = \ LIBC_CALLS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_MEM \ LIBC_NEXGEN32E \ LIBC_STDIO \ LIBC_RUNTIME \ LIBC_STR \ LIBC_STUBS \ LIBC_SYSV \ LIBC_TESTLIB \ LIBC_TINYMATH \ LIBC_X \ THIRD_PARTY_COMPILER_RT \ THIRD_PARTY_GDTOA \ THIRD_PARTY_COMPILER_RT \ THIRD_PARTY_DOUBLECONVERSION TEST_LIBC_TINYMATH_DEPS := \ $(call uniq,$(foreach x,$(TEST_LIBC_TINYMATH_DIRECTDEPS),$($(x)))) o/$(MODE)/test/libc/tinymath/tinymath.pkg: \ $(TEST_LIBC_TINYMATH_OBJS) \ $(foreach x,$(TEST_LIBC_TINYMATH_DIRECTDEPS),$($(x)_A).pkg) o/$(MODE)/test/libc/tinymath/%.com.dbg: \ $(TEST_LIBC_TINYMATH_DEPS) \ o/$(MODE)/test/libc/tinymath/%.o \ o/$(MODE)/test/libc/tinymath/tinymath.pkg \ $(LIBC_TESTMAIN) \ $(CRT) \ $(APE_NO_MODIFY_SELF) @$(APELINK) $(TEST_LIBC_TINYMATH_OBJS): private \ OVERRIDE_CFLAGS += \ -fno-builtin .PHONY: o/$(MODE)/test/libc/tinymath o/$(MODE)/test/libc/tinymath: \ $(TEST_LIBC_TINYMATH_BINS) \ $(TEST_LIBC_TINYMATH_CHECKS)
1,925
68
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/hypot_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" double _hypot(double, double) asm("hypot"); float _hypotf(float, float) asm("hypotf"); long double _hypotl(long double, long double) asm("hypotl"); TEST(hypot, test) { EXPECT_STREQ("0", gc(xdtoa(_hypot(0, 0)))); EXPECT_STREQ("3", gc(xdtoa(_hypot(3, 0)))); EXPECT_STREQ("3", gc(xdtoa(_hypot(0, 3)))); EXPECT_STREQ("5", gc(xdtoa(_hypot(3, 4)))); EXPECT_STREQ("5", gc(xdtoa(_hypot(4, 3)))); EXPECT_STREQ("5", gc(xdtoa(_hypot(3, -4)))); EXPECT_STREQ("5", gc(xdtoa(_hypot(-4, 3)))); EXPECT_STREQ("5", gc(xdtoa(_hypot(-3, 4)))); EXPECT_STREQ("5", gc(xdtoa(_hypot(4, -3)))); EXPECT_STREQ("5", gc(xdtoa(_hypot(-3, -4)))); EXPECT_STREQ("5", gc(xdtoa(_hypot(-4, -3)))); EXPECT_STREQ("1.4142135623731", gc(xdtoa(_hypot(1, 1)))); EXPECT_STREQ("1.4142135623731", gc(xdtoa(_hypot(1, -1)))); EXPECT_STREQ("1.4142135623731", gc(xdtoa(_hypot(-1, 1)))); EXPECT_STREQ("1.41421362601271", gc(xdtoa(_hypot(1.0000001, .99999999)))); EXPECT_STREQ("1.41421362601271", gc(xdtoa(_hypot(.99999999, 1.0000001)))); EXPECT_STREQ("1.4142135623731e+154", gc(xdtoa(_hypot(1e154, 1e154)))); EXPECT_STREQ("NAN", gc(xdtoa(_hypot(0, NAN)))); EXPECT_STREQ("NAN", gc(xdtoa(_hypot(NAN, 0)))); EXPECT_STREQ("NAN", gc(xdtoa(_hypot(NAN, NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_hypot(INFINITY, 0)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_hypot(0, INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_hypot(INFINITY, NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_hypot(NAN, INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoa(_hypot(INFINITY, INFINITY)))); } TEST(hypotf, test) { EXPECT_STREQ("0", gc(xdtoa(_hypotf(0, 0)))); EXPECT_STREQ("3", gc(xdtoa(_hypotf(3, 0)))); EXPECT_STREQ("3", gc(xdtoa(_hypotf(0, 3)))); EXPECT_STREQ("5", gc(xdtoa(_hypotf(3, 4)))); EXPECT_STREQ("5", gc(xdtoa(_hypotf(4, 3)))); EXPECT_STREQ("5", gc(xdtoa(_hypotf(3, -4)))); EXPECT_STREQ("5", gc(xdtoa(_hypotf(-4, 3)))); EXPECT_STREQ("5", gc(xdtoa(_hypotf(-3, 4)))); EXPECT_STREQ("5", gc(xdtoa(_hypotf(4, -3)))); EXPECT_STREQ("5", gc(xdtoa(_hypotf(-3, -4)))); EXPECT_STREQ("5", gc(xdtoa(_hypotf(-4, -3)))); EXPECT_STREQ("1.41421", gc(xdtoaf(_hypotf(1, 1)))); EXPECT_STREQ("1.41421", gc(xdtoaf(_hypotf(1, -1)))); EXPECT_STREQ("1.41421", gc(xdtoaf(_hypotf(-1, 1)))); EXPECT_STREQ("1.41421", gc(xdtoaf(_hypotf(1.000001, 0.999999)))); EXPECT_STREQ("1.41421", gc(xdtoaf(_hypotf(0.999999, 1.000001)))); EXPECT_STREQ("1.41421e+38", gc(xdtoaf(_hypotf(1e38, 1e38)))); EXPECT_STREQ("NAN", gc(xdtoaf(_hypotf(0, NAN)))); EXPECT_STREQ("NAN", gc(xdtoaf(_hypotf(NAN, 0)))); EXPECT_STREQ("NAN", gc(xdtoaf(_hypotf(NAN, NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_hypotf(INFINITY, 0)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_hypotf(0, INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_hypotf(INFINITY, NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_hypotf(NAN, INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(_hypotf(INFINITY, INFINITY)))); } TEST(hypotll, test) { EXPECT_STREQ("0", gc(xdtoal(_hypotl(0, 0)))); EXPECT_STREQ("3", gc(xdtoal(_hypotl(3, 0)))); EXPECT_STREQ("3", gc(xdtoal(_hypotl(0, 3)))); EXPECT_STREQ("5", gc(xdtoal(_hypotl(3, 4)))); EXPECT_STREQ("5", gc(xdtoal(_hypotl(4, 3)))); EXPECT_STREQ("5", gc(xdtoal(_hypotl(3, -4)))); EXPECT_STREQ("5", gc(xdtoal(_hypotl(-4, 3)))); EXPECT_STREQ("5", gc(xdtoal(_hypotl(-3, 4)))); EXPECT_STREQ("5", gc(xdtoal(_hypotl(4, -3)))); EXPECT_STREQ("5", gc(xdtoal(_hypotl(-3, -4)))); EXPECT_STREQ("5", gc(xdtoal(_hypotl(-4, -3)))); EXPECT_STREQ("1.414213562373095", gc(xdtoal(_hypotl(1, 1)))); EXPECT_STREQ("1.414213562373095", gc(xdtoal(_hypotl(1, -1)))); EXPECT_STREQ("1.414213562373095", gc(xdtoal(_hypotl(-1, 1)))); EXPECT_STREQ("1.414213626012708", gc(xdtoal(_hypotl(1.0000001, .99999999)))); EXPECT_STREQ("1.414213626012708", gc(xdtoal(_hypotl(.99999999, 1.0000001)))); EXPECT_STREQ("1.414213562373095e+4931", gc(xdtoal(_hypotl(1e4931L, 1e4931L)))); EXPECT_STREQ("NAN", gc(xdtoal(_hypotl(0, NAN)))); EXPECT_STREQ("NAN", gc(xdtoal(_hypotl(NAN, 0)))); EXPECT_STREQ("NAN", gc(xdtoal(_hypotl(NAN, NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_hypotl(INFINITY, 0)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_hypotl(0, INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_hypotl(INFINITY, NAN)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_hypotl(NAN, INFINITY)))); EXPECT_STREQ("INFINITY", gc(xdtoal(_hypotl(INFINITY, INFINITY)))); } /* _hypot (musl) l: 53𝑐 17𝑛𝑠 m: 85𝑐 27𝑛𝑠 _hypot l: 39𝑐 13𝑛𝑠 m: 66𝑐 21𝑛𝑠 _hypotf l: 25𝑐 8𝑛𝑠 m: 55𝑐 18𝑛𝑠 _hypotl l: 43𝑐 14𝑛𝑠 m: 74𝑐 24𝑛𝑠 */ BENCH(_hypot, bench) { volatile double a = 2; volatile double b = 3; EZBENCH2("hypotf", donothing, EXPROPRIATE(_hypotf(a, b))); EZBENCH2("hypot", donothing, EXPROPRIATE(_hypot(a, b))); EZBENCH2("hypotl", donothing, EXPROPRIATE(_hypotl(a, b))); }
7,060
128
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/log2_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" TEST(log2l, test) { EXPECT_STREQ("1", gc(xdtoal(log2l(2)))); EXPECT_STREQ("NAN", gc(xdtoal(log2l(NAN)))); EXPECT_STREQ("0", gc(xdtoal(log2l(1)))); EXPECT_STREQ("INFINITY", gc(xdtoal(log2l(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(log2l(0)))); EXPECT_STREQ("-INFINITY", gc(xdtoal(log2l(-0.)))); EXPECT_TRUE(isnan(log2l(-1))); EXPECT_TRUE(isnan(log2l(-2))); } TEST(log2, test) { EXPECT_STREQ("1", gc(xdtoa(log2(2)))); EXPECT_STREQ("2", gc(xdtoa(log2(2 * 2)))); EXPECT_STREQ("NAN", gc(xdtoa(log2(NAN)))); EXPECT_STREQ("0", gc(xdtoa(log2(1)))); EXPECT_STREQ("INFINITY", gc(xdtoa(log2(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(log2(0)))); EXPECT_STREQ("-INFINITY", gc(xdtoa(log2(-0.)))); EXPECT_TRUE(isnan(log2(-1))); EXPECT_TRUE(isnan(log2(-2))); } TEST(log2f, test) { EXPECT_STREQ("1", gc(xdtoaf(log2f(2)))); EXPECT_STREQ("NAN", gc(xdtoaf(log2f(NAN)))); EXPECT_STREQ("0", gc(xdtoaf(log2f(1)))); EXPECT_STREQ("INFINITY", gc(xdtoaf(log2f(INFINITY)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(log2f(0)))); EXPECT_STREQ("-INFINITY", gc(xdtoaf(log2f(-0.)))); EXPECT_TRUE(isnan(log2f(-1))); EXPECT_TRUE(isnan(log2f(-2))); } BENCH(log2, bench) { double _log2(double) asm("log2"); float _log2f(float) asm("log2f"); long double _log2l(long double) asm("log2l"); EZBENCH2("log2", donothing, _log2(.7)); // ~8ns EZBENCH2("log2f", donothing, _log2f(.7)); // ~6ns EZBENCH2("log2l", donothing, _log2l(.7)); // ~21ns }
3,468
67
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/remainder_test.c
/*-*- 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 2023 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/assert.h" #include "libc/fmt/conv.h" #include "libc/intrin/kprintf.h" #include "libc/math.h" #include "libc/mem/gc.h" #include "libc/runtime/runtime.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" float remainderf2(float, float); double _remainder(double, double) asm("remainder"); float _remainderf(float, float) asm("remainderf"); long double _remainderl(long double, long double) asm("remainderl"); TEST(remainder, test) { EXPECT_TRUE(isnan(_remainder(7, NAN))); EXPECT_TRUE(isnan(_remainder(NAN, 7))); EXPECT_TRUE(isnan(_remainder(INFINITY, 7))); EXPECT_TRUE(isnan(_remainder(7, 0))); EXPECT_STREQ("-1", _gc(xdtoa(_remainder(7, 4)))); } TEST(remainderf, test) { EXPECT_TRUE(isnan(_remainderf(7, NAN))); EXPECT_TRUE(isnan(_remainderf(NAN, 7))); EXPECT_TRUE(isnan(_remainderf(INFINITY, 7))); EXPECT_TRUE(isnan(_remainderf(7, 0))); EXPECT_STREQ("-1", _gc(xdtoaf(_remainderf(7, 4)))); } TEST(remainderl, test) { EXPECT_TRUE(isnan(_remainderl(7, NAN))); EXPECT_TRUE(isnan(_remainderl(NAN, 7))); EXPECT_TRUE(isnan(_remainderl(INFINITY, 7))); EXPECT_TRUE(isnan(_remainderl(7, 0))); EXPECT_STREQ("-1", _gc(xdtoal(_remainderl(7, 4)))); } static inline float i2f(uint32_t i) { union { uint32_t i; float f; } u = {i}; return u.f; } static inline uint32_t f2i(float f) { union { float f; uint32_t i; } u = {f}; return u.i; } BENCH(remainder, bench) { EZBENCH2("remainder", donothing, _remainder(7, 3)); // ~20ns EZBENCH2("remainderf", donothing, _remainderf(7, 3)); // ~20ns EZBENCH2("remainderl", donothing, _remainderl(7, 3)); // ~169ns } #if 0 TEST(remainderf, brute) { long i, j; float a, b; int lim = 100; uint32_t x, y; for (i = 0; i <= 0x100000000; i += 1048573) { for (j = 0; j <= 0x100000000; j += 1048573) { a = remainderf(i2f(i), i2f(j)); b = remainderf2(i2f(i), i2f(j)); if (isunordered(a, b)) { assert(isnan(a) == isnan(b)); assert(isfinite(a) == isfinite(b)); continue; } x = f2i(a); y = f2i(b); if (abs(x - y) > 1) { kprintf("bah %#lx %s %#lx %d\n", i, _gc(xdtoaf(i2f(i))), j, _gc(xdtoaf(i2f(j))), abs(x - y)); kprintf(" %-12s %#x\n", _gc(xdtoaf(i2f(x))), x); kprintf(" %-12s %#x\n", _gc(xdtoaf(i2f(y))), y); if (!--lim) break; } } kprintf("%ld\r", i); } if (lim != 100) { exit(1); } } #endif
4,327
112
jart/cosmopolitan
false
cosmopolitan/test/libc/tinymath/fmod_test.c
/*-*- 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 2021 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/math.h" #include "libc/mem/gc.internal.h" #include "libc/stdio/rand.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" int rando; void SetUp(void) { rando = rand() & 0xffff; } TEST(fmodl, test) { EXPECT_STREQ("0", gc(xdtoal(fmodl(0, rando)))); EXPECT_STREQ("NAN", gc(xdtoal(fmodl(1, NAN)))); EXPECT_STREQ("NAN", gc(xdtoal(fmodl(NAN, 1)))); EXPECT_TRUE(isnan(fmodl(INFINITY, 1))); EXPECT_TRUE(isnan(fmodl(1, 0))); EXPECT_STREQ("8", gc(xdtoal(fmodl(8, 32)))); EXPECT_STREQ("8e+100", gc(xdtoal(fmodl(8e100, 32e100)))); EXPECT_STREQ("5.319372648326541e+255", gc(xdtoal(ldexpl(6381956970095103, 797)))); EXPECT_STREQ(".09287409360354737", gc(xdtoal(fmodl(ldexpl(6381956970095103, 797), M_2_PI)))); } TEST(fmod, test) { EXPECT_STREQ("0", gc(xdtoa(fmod(0, rando)))); EXPECT_STREQ("NAN", gc(xdtoa(fmod(1, NAN)))); EXPECT_STREQ("NAN", gc(xdtoa(fmod(NAN, 1)))); EXPECT_TRUE(isnan(fmod(INFINITY, 1))); EXPECT_TRUE(isnan(fmod(1, 0))); EXPECT_STREQ("8", gc(xdtoa(fmod(8, 32)))); EXPECT_STREQ("8e+100", gc(xdtoa(fmod(8e100, 32e100)))); EXPECT_STREQ("5.31937264832654e+255", gc(xdtoa(ldexp(6381956970095103, 797)))); EXPECT_STREQ(".0928740936035474", gc(xdtoa(fmod(ldexp(6381956970095103, 797), M_2_PI)))); } TEST(fmodf, test) { EXPECT_STREQ("0", gc(xdtoaf(fmodf(0, rando)))); EXPECT_STREQ("NAN", gc(xdtoaf(fmodf(1, NAN)))); EXPECT_STREQ("NAN", gc(xdtoaf(fmodf(NAN, 1)))); EXPECT_TRUE(isnan(fmodf(INFINITY, 1))); EXPECT_TRUE(isnan(fmodf(1, 0))); EXPECT_STREQ("8", gc(xdtoaf(fmodf(8, 32)))); EXPECT_STREQ("8e+20", gc(xdtoaf(fmodf(8e20, 32e20)))); } BENCH(fmodf, bench) { EZBENCH2("fmodf eze", donothing, fmodf(8, 32)); EZBENCH2("fmodf big", donothing, fmodf(5.319372648326541e+255, M_2_PI)); } BENCH(fmod, bench) { // fmod-tiny.S goes slow in the average case, fast for big case EZBENCH2("fmod eze", donothing, fmod(8, 32)); // fmod.c goes fast for the average case very slow for big case EZBENCH2("fmod big", donothing, fmod(5.319372648326541e+255, M_2_PI)); } BENCH(fmodl, bench) { EZBENCH2("fmodl eze", donothing, fmodl(8, 32)); EZBENCH2("fmodl big", donothing, fmodl(5.319372648326541e+255, M_2_PI)); }
4,120
86
jart/cosmopolitan
false
cosmopolitan/test/libc/log/backtrace.c
/*-*- 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/fmt/conv.h" #include "libc/limits.h" #include "libc/log/log.h" #include "libc/mem/mem.h" #include "libc/runtime/internal.h" #include "libc/runtime/symbols.internal.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #ifdef __x86_64__ int StackOverflow(int f(), int n) { if (n < INT_MAX) { return f(f, n + 1) - 1; } else { return INT_MAX; } } void FpuCrash(void) { typedef char xmm_t __attribute__((__vector_size__(16))); xmm_t v = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; volatile int x = 0; asm volatile("fldpi"); asm volatile("mov\t%0,%%r15" : /* no outputs */ : "g"(0x3133731337)); asm volatile("movaps\t%0,%%xmm15" : /* no outputs */ : "x"(v)); fputc(7 / x, stdout); } char bss[10]; void BssOverrunCrash(int n) { int i; for (i = 0; i < n; ++i) { bss[i] = i; } } char data[10] = "abcdeabcde"; void DataOverrunCrash(int n) { int i; for (i = 0; i < n; ++i) { data[i] = i; } } const char rodata[10] = "abcdeabcde"; int RodataOverrunCrash(int i) { return rodata[i]; } char *StackOverrunCrash(int n) { int i; char stack[10]; bzero(stack, sizeof(stack)); for (i = 0; i < n; ++i) { stack[i] = i; } return strdup(stack); } char *MemoryLeakCrash(void) { char *p = strdup("doge"); CheckForMemoryLeaks(); return p; } int NpeCrash(char *p) { asm("nop"); // xxx: due to backtrace addr-1 thing return *p; } void (*pFpuCrash)(void) = FpuCrash; void (*pBssOverrunCrash)(int) = BssOverrunCrash; void (*pDataOverrunCrash)(int) = DataOverrunCrash; int (*pRodataOverrunCrash)(int) = RodataOverrunCrash; char *(*pStackOverrunCrash)(int) = StackOverrunCrash; char *(*pMemoryLeakCrash)(void) = MemoryLeakCrash; int (*pNpeCrash)(char *) = NpeCrash; int (*pStackOverflow)(int (*)(), int) = StackOverflow; int main(int argc, char *argv[]) { ShowCrashReports(); if (argc > 1) { switch (atoi(argv[1])) { case 0: break; case 1: pFpuCrash(); exit(0); case 2: pBssOverrunCrash(10 + 1); exit(0); case 3: exit(pRodataOverrunCrash(10 + 1)); case 4: pDataOverrunCrash(10 + 1); exit(0); case 5: exit((intptr_t)pStackOverrunCrash(10 + 10000)); case 6: exit((intptr_t)pMemoryLeakCrash()); case 7: exit(pNpeCrash(0)); case 8: exit(pNpeCrash(0)); case 9: exit(pStackOverflow(pStackOverflow, 0)); default: fputs("error: unrecognized argument\n", stderr); exit(1); } } else { fputs("error: too few args\n", stderr); exit(1); } } #endif /* __x86_64__ */
4,524
137
jart/cosmopolitan
false
cosmopolitan/test/libc/log/appendresourcereport_test.c
/*-*- 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/calls/calls.h" #include "libc/calls/struct/rusage.h" #include "libc/log/appendresourcereport.internal.h" #include "libc/log/log.h" #include "libc/mem/mem.h" #include "libc/str/str.h" #include "libc/sysv/consts/rusage.h" #include "libc/testlib/testlib.h" TEST(AppendResourceReport, testEmpty_doesNothing) { char *b = 0; struct rusage ru; bzero(&ru, sizeof(ru)); AppendResourceReport(&b, &ru, " / "); EXPECT_EQ(NULL, b); } TEST(AppendResourceReport, testMemory_balloons) { char *b = 0; struct rusage ru; bzero(&ru, sizeof(ru)); ru.ru_maxrss = 1; AppendResourceReport(&b, &ru, ""); EXPECT_STREQ("ballooned to 1kb in size", b); free(b); } #if 0 TEST(AppendResourceReport, impure) { char *b = 0; struct rusage ru; EXPECT_EQ(0, getrusage(RUSAGE_SELF, &ru)); AppendResourceReport(&b, &ru, "\n"); EXPECT_STREQ("ballooned to 1kb in size", b); free(b); } #endif
2,742
56
jart/cosmopolitan
false
cosmopolitan/test/libc/log/backtrace_test.c
/*-*- 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 2021 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/assert.h" #include "libc/calls/calls.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/fmt/conv.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/kprintf.h" #include "libc/limits.h" #include "libc/log/libfatal.internal.h" #include "libc/log/log.h" #include "libc/mem/copyfd.internal.h" #include "libc/mem/gc.h" #include "libc/mem/mem.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" #include "libc/stdio/append.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/consts/sig.h" #include "libc/testlib/testlib.h" #include "libc/x/xasprintf.h" #include "net/http/escape.h" #ifdef __x86_64__ STATIC_YOINK("backtrace.com"); STATIC_YOINK("backtrace.com.dbg"); char testlib_enable_tmp_setup_teardown_once; void SetUpOnce(void) { ASSERT_NE(-1, mkdir("bin", 0755)); testlib_extract("/zip/backtrace.com", "bin/backtrace.com", 0755); testlib_extract("/zip/backtrace.com.dbg", "bin/backtrace.com.dbg", 0755); } static bool OutputHasSymbol(const char *output, const char *s) { return strstr(output, s) || (!FindDebugBinary() && strstr(output, "NULL")); } // UNFREED MEMORY // o/dbg/test/libc/log/backtrace_test.com // max allocated space 655,360 // total allocated space 80 // total free space 327,600 // releasable space 0 // mmaped space 65,360 // non-mmapped space 327,680 // // 100080040020 64 bytes 5 used // 421871 strdup // 416529 MemoryLeakCrash // 41666d SetUp // 45428c testlib_runtestcases // // 00007fff0000-000080010000 rw-pa-F 2x shadow of 000000000000 // 000080070000-0000800a0000 rw-pa-F 3x shadow of 0000003c0000 // 02008fff0000-020090020000 rw-pa-F 3x shadow of 10007ffc0000 // 020090060000-020090080000 rw-pa-F 2x shadow of 100080340000 // 0e007fff0000-0e0080010000 rw-pa-F 2x shadow of 6ffffffc0000 // 100006560000-100006580000 rw-pa-F 2x shadow of 7ffc32b40000 // 100080000000-100080050000 rw-pa-- 5x automap w/ 50 frame hole // 100080370000-100080390000 rw-pa-- 2x automap w/ 1 frame hole // 1000803a0000-1000803b0000 rw-pa-- 1x automap // 6ffffffe0000-700000000000 rw-paSF 2x stack // # 24 frames mapped w/ 51 frames gapped TEST(ShowCrashReports, testMemoryLeakCrash) { size_t got; ssize_t rc; int ws, pid, fds[2]; char *output, buf[512]; if (!IsAsan()) { /* TODO(jart): How can we make this work without ASAN? */ return; } ASSERT_NE(-1, pipe2(fds, O_CLOEXEC)); ASSERT_NE(-1, (pid = vfork())); if (!pid) { dup2(fds[1], 1); dup2(fds[1], 2); execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "6", 0}); _Exit(127); } close(fds[1]); output = 0; appends(&output, ""); for (;;) { rc = read(fds[0], buf, sizeof(buf)); if (rc == -1) { ASSERT_EQ(EINTR, errno); continue; } if ((got = rc)) { appendd(&output, buf, got); } else { break; } } close(fds[0]); ASSERT_NE(-1, wait(&ws)); EXPECT_TRUE(WIFEXITED(ws)); EXPECT_EQ(78, WEXITSTATUS(ws)); if (!strstr(output, "UNFREED MEMORY")) { fprintf(stderr, "ERROR: crash report didn't report leak\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } if (IsAsan()) { if (!OutputHasSymbol(output, "strdup") || !OutputHasSymbol(output, "MemoryLeakCrash")) { fprintf(stderr, "ERROR: crash report didn't backtrace allocation\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } } free(output); } // clang-format off // asan error: stack overrun 1-byte store at 0x6fffffffff0a shadow 0x0e007fff7fe1 // x // uuuuuuuuuuuuuuuuuuuuuuuuuuuuuu..........oooooooooooooooooooooo.................. // |-15 |-15 |-15 |0 |2 |-13 |-13 |0 |0 // ╡A    ΦCE     └eA              ☺☻♥♦♣♠•◘○○0000003fffffffff◙CapAmb:○0000↑ÿ_╟ⁿ⌂  ÿ  // 000000400000-000000462000 .text 000000462000-00000046a000 .data // 00007fff0000-00008000ffff // 000080070000-00008009ffff // 02008fff0000-02009001ffff // 020090060000-02009007ffff // 0e007ffb0000-0e008000ffff ←shadow // 100018eb0000-100018ecffff // 100080000000-10008009ffff // 100080360000-10008037ffff // 100080390000-10008039ffff // 6fffffe00000-6fffffffffff ←address // 0x0000000000407c06: __die at libc/log/die.c:37 // 0x000000000040b1c1: __asan_report_store at libc/intrin/asan.c:1104 // 0x0000000000443302: __asan_report_store1 at libc/intrin/somanyasan.S:118 // 0x000000000041669a: StackOverrunCrash at test/libc/log/backtrace_test.c:76 // 0x00000000004167e7: SetUp at test/libc/log/backtrace_test.c:105 // 0x0000000000452d4b: testlib_runtestcases at libc/testlib/testrunner.c:98 // 0x000000000044c740: testlib_runalltests at libc/testlib/runner.c:37 // 0x00000000004026db: main at libc/testlib/testmain.c:155 // 0x000000000040324f: cosmo at libc/runtime/cosmo.S:64 // 0x000000000040219b: _start at libc/crt/crt.S:67 // clang-format on TEST(ShowCrashReports, testStackOverrunCrash) { if (!IsAsan()) return; size_t got; ssize_t rc; int ws, pid, fds[2]; char *output, buf[512]; ASSERT_NE(-1, pipe2(fds, O_CLOEXEC)); ASSERT_NE(-1, (pid = vfork())); if (!pid) { dup2(fds[1], 1); dup2(fds[1], 2); execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "5", 0}); _Exit(127); } close(fds[1]); output = 0; appends(&output, ""); for (;;) { rc = read(fds[0], buf, sizeof(buf)); if (rc == -1) { ASSERT_EQ(EINTR, errno); continue; } if ((got = rc)) { appendd(&output, buf, got); } else { break; } } close(fds[0]); ASSERT_NE(-1, wait(&ws)); EXPECT_TRUE(WIFEXITED(ws)); EXPECT_EQ(77, WEXITSTATUS(ws)); /* NULL is stopgap until we can copy symbol tablces into binary */ if (!OutputHasSymbol(output, "StackOverrunCrash")) { fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } if (strstr(output, "'int' index 10 into 'char [10]' out of bounds")) { // ubsan nailed it } else { // asan nailed it if (!strstr(output, "☺☻♥♦♣♠•◘○")) { fprintf(stderr, "ERROR: crash report didn't have memory diagram\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } if (!strstr(output, "stack overrun")) { fprintf(stderr, "ERROR: crash report misclassified stack overrun\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } } free(output); } // error: Uncaught SIGFPE (FPE_INTDIV) on nightmare pid 11724 // /home/jart/cosmo/o/dbg/test/libc/log/backtrace_test.com.tmp.11721 // ENOTTY[25] // Linux nightmare SMP Thu, 12 Aug 2021 06:16:45 UTC // // 0x0000000000414659: FpuCrash at test/libc/log/backtrace_test.c:35 // 0x000000000045003b: testlib_runtestcases at libc/testlib/testrunner.c:98 // 0x000000000044b770: testlib_runalltests at libc/testlib/runner.c:37 // 0x000000000040278e: main at libc/testlib/testmain.c:86 // 0x0000000000403210: cosmo at libc/runtime/cosmo.S:65 // 0x0000000000402247: _start at libc/crt/crt.S:67 // // RAX 0000000000000007 RBX 00006fffffffff10 RDI 00007ffe0745fde1 ST(0) 0.0 // RCX 0000000000000000 RDX 0000000000000000 RSI 0000000000489900 ST(1) 0.0 // RBP 00006fffffffff70 RSP 00006fffffffff10 RIP 000000000041465a ST(2) 0.0 // R8 0000000000000001 R9 00006ffffffffcc0 R10 00006ffffffffe60 ST(3) 0.0 // R11 000000000000000d R12 00000dffffffffe2 R13 00006fffffffff10 ST(4) 0.0 // R14 0000000000000003 R15 000000000049b700 VF PF ZF IF // // XMM0 00000000000000000000000000000000 XMM8 00000000000000000000000000000000 // XMM1 000000008000000400000000004160ea XMM9 00000000000000000000000000000000 // XMM2 00000000000000000000000000000000 XMM10 00000000000000000000000000000000 // XMM3 00000000000000000000000000000000 XMM11 00000000000000000000000000000000 // XMM4 00000000000000000000000000000000 XMM12 00000000000000000000000000000000 // XMM5 00000000000000000000000000000000 XMM13 00000000000000000000000000000000 // XMM6 00000000000000000000000000000000 XMM14 00000000000000000000000000000000 // XMM7 00000000000000000000000000000000 XMM15 00000000000000000000000000000000 // // mm->i == 4; // mm->p[ 0]=={0x00008007,0x00008008,-1,3,50}; /* 2 */ // /* 234,881,012 */ // mm->p[ 1]=={0x0e007ffd,0x0e007fff,-1,3,50}; /* 3 */ // /* 33,538,280 */ // mm->p[ 2]=={0x100040e8,0x100040e8,-1,3,50}; /* 1 */ // /* 1,610,596,103 */ // mm->p[ 3]=={0x6ffffff0,0x6fffffff,12884901888,306,0}; /* 16 */ // /* 22 frames mapped w/ 1,879,015,395 frames gapped */ // // 00400000-0045b000 r-xp 00000000 08:03 4587526 // /home/jart/cosmo/o/dbg/test/libc/log/backtrace_test.com.tmp.11721 // 0045b000-00461000 rw-p 0005b000 08:03 4587526 // /home/jart/cosmo/o/dbg/test/libc/log/backtrace_test.com.tmp.11721 // 00461000-004a0000 rw-p 00000000 00:00 0 // 80070000-80090000 rw-p 00000000 00:00 0 // e007ffd0000-e0080000000 rw-p 00000000 00:00 0 // 100040e80000-100040e90000 rw-p 00000000 00:00 0 // 6ffffff00000-700000000000 rw-p 00000000 00:00 0 // 7ffe0743f000-7ffe07460000 rw-p 00000000 00:00 0 [stack] // 7ffe075a8000-7ffe075ab000 r--p 00000000 00:00 0 [vvar] // 7ffe075ab000-7ffe075ac000 r-xp 00000000 00:00 0 [vdso] // // /home/jart/cosmo/o/dbg/test/libc/log/backtrace_test.com.tmp.11721 1 TEST(ShowCrashReports, testDivideByZero) { size_t got; ssize_t rc; int ws, pid, fds[2]; char *output, buf[512]; ASSERT_NE(-1, pipe2(fds, O_CLOEXEC)); ASSERT_NE(-1, (pid = vfork())); if (!pid) { dup2(fds[1], 1); dup2(fds[1], 2); execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "1", 0}); _Exit(127); } close(fds[1]); output = 0; appends(&output, ""); for (;;) { rc = read(fds[0], buf, sizeof(buf)); if (rc == -1) { ASSERT_EQ(EINTR, errno); continue; } if ((got = rc)) { appendd(&output, buf, got); } else { break; } } close(fds[0]); ASSERT_NE(-1, wait(&ws)); EXPECT_TRUE(WIFEXITED(ws)); assert(128 + SIGFPE == WEXITSTATUS(ws) || 77 == WEXITSTATUS(ws)); /* NULL is stopgap until we can copy symbol tables into binary */ #ifdef __FNO_OMIT_FRAME_POINTER__ if (!OutputHasSymbol(output, "FpuCrash")) { fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } #endif if (strstr(output, "divrem overflow")) { // UBSAN handled it } else { // ShowCrashReports() handled it if (!strstr(output, _gc(xasprintf("%d", pid)))) { fprintf(stderr, "ERROR: crash report didn't have pid\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } if (!strstr(output, "SIGFPE")) { fprintf(stderr, "ERROR: crash report didn't have signal name\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } // XXX: WSL doesn't save and restore x87 registers to ucontext_t if (!IsWsl1()) { if (!strstr(output, "3.141")) { fprintf(stderr, "ERROR: crash report didn't have fpu register\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } } if (!strstr(output, "0f0e0d0c0b0a09080706050403020100")) { fprintf(stderr, "ERROR: crash report didn't have sse register\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } if (!strstr(output, "3133731337")) { fprintf(stderr, "ERROR: crash report didn't have general register\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } } free(output); } TEST(ShowCrashReports, testStackOverflow) { if (IsXnu()) return; // TODO(jart): fix me if (IsWindows()) return; // TODO(jart): fix me if (IsFreebsd()) return; // TODO(jart): fix me if (IsOpenbsd()) return; // TODO(jart): fix me size_t got; ssize_t rc; int ws, pid, fds[2]; char *output, buf[512]; ASSERT_NE(-1, pipe2(fds, O_CLOEXEC)); ASSERT_NE(-1, (pid = vfork())); if (!pid) { dup2(fds[1], 1); dup2(fds[1], 2); execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "9", 0}); _Exit(127); } close(fds[1]); output = 0; appends(&output, ""); for (;;) { rc = read(fds[0], buf, sizeof(buf)); if (rc == -1) { ASSERT_EQ(EINTR, errno); continue; } if ((got = rc)) { appendd(&output, buf, got); } else { break; } } close(fds[0]); ASSERT_NE(-1, wait(&ws)); EXPECT_TRUE(WIFEXITED(ws)); // kprintf("exit status %d\n", WEXITSTATUS(ws)); assert(128 + SIGSEGV == WEXITSTATUS(ws) || 77 == WEXITSTATUS(ws)); /* NULL is stopgap until we can copy symbol tablces into binary */ #ifdef __FNO_OMIT_FRAME_POINTER__ if (!OutputHasSymbol(output, "StackOverflow")) { fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } #endif // ShowCrashReports() handled it if (!strstr(output, _gc(xasprintf("%d", pid)))) { fprintf(stderr, "ERROR: crash report didn't have pid\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } if (!strstr(output, "SIGSEGV")) { fprintf(stderr, "ERROR: crash report didn't have signal name\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } if (!IsTiny()) { if (!strstr(output, "Stack Overflow")) { fprintf(stderr, "ERROR: crash report didn't have 'Stack Overflow'\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } } else { if (!strstr(output, "SEGV_MAPERR")) { fprintf(stderr, "ERROR: crash report didn't have 'SEGV_MAPERR'\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } } free(output); } // clang-format off // // test/libc/log/backtrace_test.c:59: ubsan error: 'int' index 10 into 'char [10]' out of bounds // 0x000000000040a352: __die at libc/log/die.c:40 // 0x0000000000489bc8: __ubsan_abort at libc/intrin/ubsan.c:196 // 0x0000000000489e1c: __ubsan_handle_out_of_bounds at libc/intrin/ubsan.c:242 // 0x0000000000423666: BssOverrunCrash at test/libc/log/backtrace_test.c:59 // 0x0000000000423e0a: SetUp at test/libc/log/backtrace_test.c:115 // 0x000000000049350b: testlib_runtestcases at libc/testlib/testrunner.c:98 // 0x000000000048ab50: testlib_runalltests at libc/testlib/runner.c:37 // 0x00000000004028d0: main at libc/testlib/testmain.c:94 // 0x0000000000403977: cosmo at libc/runtime/cosmo.S:69 // 0x00000000004021ae: _start at libc/crt/crt.S:78 // // asan error: global redzone 1-byte store at 0x00000048cf2a shadow 0x0000800899e5 // x // ........................................OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO // |0 |0 |0 |0 |2 |-6 |-6 |-6 |-6 //                                ☺☻♥♦♣♠•◘○                                         // 000000400000-000000460000 .text // 000000460000-000000468000 .data // 00007fff0000-00008000ffff // 000080070000-00008009ffff ←shadow // 02008fff0000-02009001ffff // 020090060000-02009007ffff // 0e007ffb0000-0e008000ffff // 1000286b0000-1000286cffff // 100080000000-10008009ffff // 100080350000-10008036ffff // 100080380000-10008038ffff // 6fffffe00000-6fffffffffff // 0x0000000000407af6: __die at libc/log/die.c:36 // 0x0000000000444f13: __asan_die at libc/intrin/asan.c:318 // 0x0000000000445bc8: __asan_report at libc/intrin/asan.c:667 // 0x0000000000445e41: __asan_report_memory_fault at libc/intrin/asan.c:672 // 0x0000000000446312: __asan_report_store at libc/intrin/asan.c:1008 // 0x0000000000444442: __asan_report_store1 at libc/intrin/somanyasan.S:118 // 0x0000000000416216: BssOverrunCrash at test/libc/log/backtrace_test.c:52 // 0x000000000041642a: SetUp at test/libc/log/backtrace_test.c:73 // 0x00000000004513eb: testlib_runtestcases at libc/testlib/testrunner.c:98 // 0x000000000044bbe0: testlib_runalltests at libc/testlib/runner.c:37 // 0x00000000004026db: main at libc/testlib/testmain.c:155 // 0x000000000040323f: cosmo at libc/runtime/cosmo.S:64 // 0x000000000040219b: _start at libc/crt/crt.S:67 // // clang-format on TEST(ShowCrashReports, testBssOverrunCrash) { if (!IsAsan()) return; size_t got; ssize_t rc; int ws, pid, fds[2]; char *output, buf[512]; ASSERT_NE(-1, pipe2(fds, O_CLOEXEC)); ASSERT_NE(-1, (pid = vfork())); if (!pid) { dup2(fds[1], 1); dup2(fds[1], 2); execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "2", 0}); _Exit(127); } close(fds[1]); output = 0; appends(&output, ""); for (;;) { rc = read(fds[0], buf, sizeof(buf)); if (rc == -1) { ASSERT_EQ(EINTR, errno); continue; } if ((got = rc)) { appendd(&output, buf, got); } else { break; } } close(fds[0]); ASSERT_NE(-1, wait(&ws)); EXPECT_TRUE(WIFEXITED(ws)); EXPECT_EQ(77, WEXITSTATUS(ws)); /* NULL is stopgap until we can copy symbol tablces into binary */ #ifdef __FNO_OMIT_FRAME_POINTER__ if (!OutputHasSymbol(output, "BssOverrunCrash")) { fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } #endif if (!strstr(output, "'int' index 10 into 'char [10]' out of bounds") && (!strstr(output, "☺☻♥♦♣♠•◘○") || !strstr(output, "global redzone"))) { fprintf(stderr, "ERROR: crash report didn't have memory diagram\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } free(output); } // clang-format off // asan error: null pointer dereference 1-byte load at 0x000000000000 shadow 0x00007fff8000 // x // MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅∅ // |-17 |-17 |-17 |-17 |-17 |-1 |-1 |-1 |-1 |-1 // ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ // 000000400000-000000464000 .text // 000000464000-00000046d000 .data // 00007fff0000-00008000ffff ←shadow // 000080070000-00008009ffff // 02008fff0000-02009001ffff // 020090060000-02009007ffff // 0e007fff0000-0e008000ffff // 10000d3e0000-10000d3fffff // 100080000000-10008009ffff // 100080370000-10008038ffff // 1000803a0000-1000803affff // 6ffffffe0000-6fffffffffff // 0x0000000000407c84: __die at libc/log/die.c:37 // 0x000000000040b1ee: __asan_report_load at libc/intrin/asan.c:1083 // 0x000000000041639e: NpeCrash at test/libc/log/backtrace_test.c:87 // 0x0000000000416733: SetUp at test/libc/log/backtrace_test.c:120 // 0x00000000004541fb: testlib_runtestcases at libc/testlib/testrunner.c:98 // 0x000000000044d000: testlib_runalltests at libc/testlib/runner.c:37 // 0x00000000004026db: main at libc/testlib/testmain.c:94 // 0x000000000040327f: cosmo at libc/runtime/cosmo.S:64 // 0x000000000040219b: _start at libc/crt/crt.S:67 // clang-format on TEST(ShowCrashReports, testNpeCrash) { if (!IsAsan()) return; size_t got; ssize_t rc; int ws, pid, fds[2]; char *output, buf[512]; ASSERT_NE(-1, pipe2(fds, O_CLOEXEC)); ASSERT_NE(-1, (pid = vfork())); if (!pid) { dup2(fds[1], 1); dup2(fds[1], 2); execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "7", 0}); _Exit(127); } close(fds[1]); output = 0; appends(&output, ""); for (;;) { rc = read(fds[0], buf, sizeof(buf)); if (rc == -1) { ASSERT_EQ(EINTR, errno); continue; } if ((got = rc)) { appendd(&output, buf, got); } else { break; } } close(fds[0]); ASSERT_NE(-1, wait(&ws)); EXPECT_TRUE(WIFEXITED(ws)); EXPECT_EQ(77, WEXITSTATUS(ws)); /* NULL is stopgap until we can copy symbol tables into binary */ if (!strstr(output, "null pointer")) { fprintf(stderr, "ERROR: crash report didn't diagnose the problem\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } #ifdef __FNO_OMIT_FRAME_POINTER__ if (!OutputHasSymbol(output, "NpeCrash")) { fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } #endif if (strstr(output, "null pointer access")) { // ubsan nailed it } else { // asan nailed it if (!strstr(output, "∅∅∅∅")) { fprintf(stderr, "ERROR: crash report didn't have shadow diagram\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } } free(output); } TEST(ShowCrashReports, testDataOverrunCrash) { if (!IsAsan()) return; size_t got; ssize_t rc; int ws, pid, fds[2]; char *output, buf[512]; ASSERT_NE(-1, pipe2(fds, O_CLOEXEC)); ASSERT_NE(-1, (pid = vfork())); if (!pid) { dup2(fds[1], 1); dup2(fds[1], 2); execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "4", 0}); _Exit(127); } close(fds[1]); output = 0; appends(&output, ""); for (;;) { rc = read(fds[0], buf, sizeof(buf)); if (rc == -1) { ASSERT_EQ(EINTR, errno); continue; } if ((got = rc)) { appendd(&output, buf, got); } else { break; } } close(fds[0]); ASSERT_NE(-1, wait(&ws)); EXPECT_TRUE(WIFEXITED(ws)); EXPECT_EQ(77, WEXITSTATUS(ws)); /* NULL is stopgap until we can copy symbol tablces into binary */ #ifdef __FNO_OMIT_FRAME_POINTER__ if (!OutputHasSymbol(output, "DataOverrunCrash")) { fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } #endif if (!strstr(output, "'int' index 10 into 'char [10]' out of bounds") && (!strstr(output, "☺☻♥♦♣♠•◘○") || !strstr(output, "global redzone"))) { fprintf(stderr, "ERROR: crash report didn't have memory diagram\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } free(output); } TEST(ShowCrashReports, testNpeCrashAfterFinalize) { /* * this test makes sure we're not doing things like depending on * environment variables after __cxa_finalize is called in cases * where putenv() is used */ size_t got; ssize_t rc; int ws, pid, fds[2]; char *output, buf[512]; ASSERT_NE(-1, pipe2(fds, O_CLOEXEC)); ASSERT_NE(-1, (pid = vfork())); if (!pid) { dup2(fds[1], 1); dup2(fds[1], 2); execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "8", 0}); _Exit(127); } close(fds[1]); output = 0; appends(&output, ""); for (;;) { rc = read(fds[0], buf, sizeof(buf)); if (rc == -1) { ASSERT_EQ(EINTR, errno); continue; } if ((got = rc)) { appendd(&output, buf, got); } else { break; } } close(fds[0]); ASSERT_NE(-1, wait(&ws)); EXPECT_TRUE(WIFEXITED(ws)); EXPECT_EQ(0, WTERMSIG(ws)); EXPECT_EQ(IsAsan() ? 77 : 128 + SIGSEGV, WEXITSTATUS(ws)); /* NULL is stopgap until we can copy symbol tables into binary */ if (!strstr(output, IsAsan() ? "null pointer" : "Uncaught SIGSEGV (SEGV_")) { fprintf(stderr, "ERROR: crash report didn't diagnose the problem\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } #ifdef __FNO_OMIT_FRAME_POINTER__ if (!OutputHasSymbol(output, "NpeCrash")) { fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n", _gc(IndentLines(output, -1, 0, 4))); __die(); } #endif free(output); } #endif /* __x86_64__ */
25,676
712
jart/cosmopolitan
false