mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-05 04:02:28 +00:00
Initial import
This commit is contained in:
commit
c91b3c5006
14915 changed files with 590219 additions and 0 deletions
30
test/libc/math/exp_test.c
Normal file
30
test/libc/math/exp_test.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/math.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
TEST(exp, test) {
|
||||
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)));
|
||||
}
|
39
test/libc/math/pow10_test.c
Normal file
39
test/libc/math/pow10_test.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/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));
|
||||
}
|
52
test/libc/math/powl_test.c
Normal file
52
test/libc/math/powl_test.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/math.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
long double x, y, z;
|
||||
|
||||
COMBO(x, pos1) { x = +1.3L; }
|
||||
COMBO(x, pos2) { x = +2.3L; }
|
||||
COMBO(y, pos1) { y = +1.3L; }
|
||||
COMBO(y, pos2) { y = +2.3L; }
|
||||
COMBO(z, pi) { z = M_PI; }
|
||||
COMBO(z, e) { z = M_E; }
|
||||
|
||||
TEST(powl, lolgebra) {
|
||||
/* EXPECT_LDBL_EQ(1, powl(x, 0)); */
|
||||
/* EXPECT_LDBL_EQ(x, powl(x, 1)); */
|
||||
/* EXPECT_LDBL_EQ(powl(x, -1), 1.0L / x); */
|
||||
/* EXPECT_LDBL_EQ(powl(x, 0.5), sqrtl(x)); */
|
||||
/* EXPECT_LDBL_EQ(powl(x, y) * powl(x, z), powl(x, y + z)); */
|
||||
/* EXPECT_LDBL_EQ(powl(x, y) * powl(z, y), powl(x * z, y)); */
|
||||
/* EXPECT_LDBL_EQ(powl(x, y) / x, powl(x, y - 1)); */
|
||||
/* EXPECT_LDBL_EQ(x / powl(y, z), x * powl(y, -z)); */
|
||||
/* EXPECT_LDBL_EQ(powi(x, 0), 1); */
|
||||
/* EXPECT_LDBL_EQ(powi(x, 1), x); */
|
||||
/* EXPECT_LDBL_EQ(powi(x, -1), 1 / x); */
|
||||
/* EXPECT_LDBL_EQ(powi(1, x), 1); */
|
||||
/* EXPECT_LDBL_EQ(powi(x, y) * powi(z, y), powi(x * z, y)); */
|
||||
}
|
||||
|
||||
TEST(powl, testConstants) {
|
||||
EXPECT_LDBL_EQ(16, powl(2, 4));
|
||||
/* TODO(jart): We need a better AlmostEqual() */
|
||||
/* EXPECT_LDBL_EQ(3.347061799072891e+62, powl(1337.31337, 20)); */
|
||||
}
|
77
test/libc/math/round_test.c
Normal file
77
test/libc/math/round_test.c
Normal file
|
@ -0,0 +1,77 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/math.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
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("1", gc(xdtoa(round(.5))));
|
||||
EXPECT_STREQ("2", gc(xdtoa(round(1.5))));
|
||||
EXPECT_STREQ("3", gc(xdtoa(round(2.5))));
|
||||
}
|
||||
|
||||
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(lround, test) {
|
||||
EXPECT_EQ(-3, lround(-2.5));
|
||||
EXPECT_EQ(-2, lround(-1.5));
|
||||
EXPECT_EQ(-1, lround(-.5));
|
||||
EXPECT_EQ(0, lround(-.0));
|
||||
EXPECT_EQ(1, lround(.5));
|
||||
EXPECT_EQ(2, lround(1.5));
|
||||
EXPECT_EQ(3, lround(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("1", gc(xdtoa(roundf(.5))));
|
||||
EXPECT_STREQ("2", gc(xdtoa(roundf(1.5))));
|
||||
EXPECT_STREQ("3", gc(xdtoa(roundf(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));
|
||||
}
|
56
test/libc/math/test.mk
Normal file
56
test/libc/math/test.mk
Normal file
|
@ -0,0 +1,56 @@
|
|||
#-*-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_MATH
|
||||
|
||||
TEST_LIBC_MATH_SRCS := $(wildcard test/libc/math/*.c)
|
||||
TEST_LIBC_MATH_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_MATH_SRCS))
|
||||
TEST_LIBC_MATH_COMS = $(TEST_LIBC_MATH_OBJS:%.o=%.com)
|
||||
|
||||
TEST_LIBC_MATH_OBJS = \
|
||||
$(TEST_LIBC_MATH_SRCS:%=o/$(MODE)/%.zip.o) \
|
||||
$(TEST_LIBC_MATH_SRCS:%.c=o/$(MODE)/%.o)
|
||||
|
||||
TEST_LIBC_MATH_BINS = \
|
||||
$(TEST_LIBC_MATH_COMS) \
|
||||
$(TEST_LIBC_MATH_COMS:%=%.dbg)
|
||||
|
||||
TEST_LIBC_MATH_TESTS = $(TEST_LIBC_MATH_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
|
||||
|
||||
TEST_LIBC_MATH_CHECKS = \
|
||||
$(TEST_LIBC_MATH_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
|
||||
|
||||
TEST_LIBC_MATH_DIRECTDEPS = \
|
||||
LIBC_CALLS_HEFTY \
|
||||
LIBC_FMT \
|
||||
LIBC_TINYMATH \
|
||||
LIBC_MEM \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_STUBS \
|
||||
LIBC_TESTLIB \
|
||||
LIBC_X
|
||||
|
||||
TEST_LIBC_MATH_DEPS := \
|
||||
$(call uniq,$(foreach x,$(TEST_LIBC_MATH_DIRECTDEPS),$($(x))))
|
||||
|
||||
o/$(MODE)/test/libc/math/math.pkg: \
|
||||
$(TEST_LIBC_MATH_OBJS) \
|
||||
$(foreach x,$(TEST_LIBC_MATH_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
o/$(MODE)/test/libc/math/%.com.dbg: \
|
||||
$(TEST_LIBC_MATH_DEPS) \
|
||||
o/$(MODE)/test/libc/math/%.o \
|
||||
o/$(MODE)/test/libc/math/math.pkg \
|
||||
$(LIBC_TESTMAIN) \
|
||||
$(CRT) \
|
||||
$(APE)
|
||||
@$(APELINK)
|
||||
|
||||
$(TEST_LIBC_MATH_OBJS): \
|
||||
DEFAULT_CCFLAGS += \
|
||||
-fno-builtin
|
||||
|
||||
.PHONY: o/$(MODE)/test/libc/math
|
||||
o/$(MODE)/test/libc/math: \
|
||||
$(TEST_LIBC_MATH_BINS) \
|
||||
$(TEST_LIBC_MATH_CHECKS)
|
Loading…
Add table
Add a link
Reference in a new issue