Make C memory safe like Rust

This change enables Address Sanitizer systemically w/ `make MODE=dbg`.
Our version of Rust's `unsafe` keyword is named `noasan` which is used
for two functions that do aligned memory chunking, like `strcpy.c` and
we need to fix the tiny DEFLATE code, but that's it everything else is
fabulous you can have all the fischer price security blankets you need

Best of all is we're now able to use the ASAN data in Blinkenlights to
colorize the memory dumps. See the screenshot below of a test program:

  https://justine.lol/blinkenlights/asan.png

Which is operating on float arrays stored on the stack, with red areas
indicating poisoned memory, and the green areas indicate valid memory.
This commit is contained in:
Justine Tunney 2021-02-01 03:33:13 -08:00
parent fdc3fa9148
commit 1ff9ab95ac
153 changed files with 2545 additions and 2077 deletions

View file

@ -0,0 +1,29 @@
/*-*- 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/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)));
}

View file

@ -0,0 +1,29 @@
/*-*- 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));
}

View file

@ -19,7 +19,6 @@
#include "libc/limits.h"
#include "libc/math.h"
#include "libc/testlib/testlib.h"
#include "libc/tinymath/tinymath.h"
TEST(ilogb, yolo) {
EXPECT_EQ(0, ilogb(1));

View file

@ -0,0 +1,38 @@
/*-*- 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));
}

View file

@ -20,35 +20,31 @@
#include "libc/runtime/gc.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/tinymath/tinymath.h"
#include "libc/x/x.h"
TEST(powl, testLongDouble) {
/* .4248496805467504836322459796959084815827285786480897 */
EXPECT_STARTSWITH(".4248496805467504", gc(xdtoa(powl(0.7, 2.4))));
EXPECT_STARTSWITH(".4248496805467504", gc(xdtoa(tinymath_powl(0.7, 2.4))));
}
TEST(powl, testDouble) {
EXPECT_STARTSWITH(".4248496805467504", gc(xdtoa(pow(0.7, 2.4))));
EXPECT_STARTSWITH(".4248496805467504", gc(xdtoa(tinymath_pow(0.7, 2.4))));
}
TEST(powl, testFloat) {
EXPECT_STARTSWITH(".4248496", gc(xdtoa(powf(0.7f, 2.4f))));
EXPECT_STARTSWITH(".4248496", gc(xdtoa(tinymath_powf(0.7f, 2.4f))));
}
static long double do_powl(void) {
return CONCEAL("t", tinymath_powl(CONCEAL("t", 0.7), CONCEAL("t", 0.2)));
return CONCEAL("t", powl(CONCEAL("t", 0.7), CONCEAL("t", 0.2)));
}
static double do_pow(void) {
return CONCEAL("x", tinymath_pow(CONCEAL("x", 0.7), CONCEAL("x", 0.2)));
return CONCEAL("x", pow(CONCEAL("x", 0.7), CONCEAL("x", 0.2)));
}
static float do_powf(void) {
return CONCEAL("x", tinymath_powf(CONCEAL("x", 0.7f), CONCEAL("x", 0.2f)));
return CONCEAL("x", powf(CONCEAL("x", 0.7f), CONCEAL("x", 0.2f)));
}
BENCH(powl, bench) {

View file

@ -21,167 +21,163 @@
#include "libc/runtime/gc.h"
#include "libc/str/str.h"
#include "libc/testlib/testlib.h"
#include "libc/tinymath/tinymath.h"
#include "libc/x/x.h"
float tinymath_roundf$k8(float);
double tinymath_round$k8(double);
FIXTURE(intrin, disableHardwareExtensions) {
memset((/*unconst*/ void *)kCpuids, 0, sizeof(kCpuids));
}
TEST(round, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_round(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_round(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_round(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_round(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_round(-INFINITY))));
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(tinymath_roundl(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_roundl(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_roundl(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_roundl(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_roundl(-INFINITY))));
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(tinymath_round(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_round(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_round(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_round(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_round(.4))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_round(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_round(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_round(2.5))));
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(tinymath_roundf(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_roundf(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_roundf(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundf(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_roundf(.4))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_roundf(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_roundf(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_roundf(2.5))));
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(tinymath_roundl(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_roundl(-1.5))));
EXPECT_STREQ("-1", gc(xdtoa(tinymath_roundl(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundl(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_roundl(.4))));
EXPECT_STREQ("1", gc(xdtoa(tinymath_roundl(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_roundl(1.5))));
EXPECT_STREQ("3", gc(xdtoa(tinymath_roundl(2.5))));
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(tinymath_nearbyint(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_nearbyint(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyint(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyint(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyint(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyint(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyint(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyint(2.5))));
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(tinymath_nearbyintf(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_nearbyintf(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyintf(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyintf(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyintf(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyintf(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyintf(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyintf(2.5))));
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(tinymath_nearbyintl(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_nearbyintl(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyintl(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_nearbyintl(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyintl(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_nearbyintl(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyintl(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_nearbyintl(2.5))));
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(tinymath_rint(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_rint(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rint(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rint(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rint(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rint(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rint(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rint(2.5))));
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(rintf, test) {
EXPECT_STREQ("-2", gc(xdtoa(tinymath_rintf(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_rintf(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rintf(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rintf(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rintf(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rintf(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rintf(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rintf(2.5))));
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(tinymath_rintl(-2.5))));
EXPECT_STREQ("-2", gc(xdtoa(tinymath_rintl(-1.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rintl(-.5))));
EXPECT_STREQ("-0", gc(xdtoa(tinymath_rintl(-.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rintl(.4))));
EXPECT_STREQ("0", gc(xdtoa(tinymath_rintl(.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rintl(1.5))));
EXPECT_STREQ("2", gc(xdtoa(tinymath_rintl(2.5))));
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(tinymath_roundf(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_roundf(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_roundf(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_roundf(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_roundf(-INFINITY))));
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, tinymath_lroundf(-2.5));
EXPECT_EQ(-2, tinymath_lroundf(-1.5));
EXPECT_EQ(-1, tinymath_lroundf(-.5));
EXPECT_EQ(0, tinymath_lroundf(-.0));
EXPECT_EQ(1, tinymath_lroundf(.5));
EXPECT_EQ(2, tinymath_lroundf(1.5));
EXPECT_EQ(3, tinymath_lroundf(2.5));
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, tinymath_lround(-2.5));
EXPECT_EQ(-2, tinymath_lround(-1.5));
EXPECT_EQ(-1, tinymath_lround(-.5));
EXPECT_EQ(-0, tinymath_lround(-.4));
EXPECT_EQ(0, tinymath_lround(.4));
EXPECT_EQ(1, tinymath_lround(.5));
EXPECT_EQ(2, tinymath_lround(1.5));
EXPECT_EQ(3, tinymath_lround(2.5));
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, tinymath_lroundl(-2.5));
EXPECT_EQ(-2, tinymath_lroundl(-1.5));
EXPECT_EQ(-1, tinymath_lroundl(-.5));
EXPECT_EQ(-0, tinymath_lroundl(-.4));
EXPECT_EQ(0, tinymath_lroundl(.4));
EXPECT_EQ(1, tinymath_lroundl(.5));
EXPECT_EQ(2, tinymath_lroundl(1.5));
EXPECT_EQ(3, tinymath_lroundl(2.5));
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));
}

View file

@ -50,8 +50,8 @@ o/$(MODE)/test/libc/tinymath/%.com.dbg: \
@$(APELINK)
$(TEST_LIBC_TINYMATH_OBJS): \
DEFAULT_CCFLAGS += \
-fno-builtin
DEFAULT_CCFLAGS += \
-fno-builtin
.PHONY: o/$(MODE)/test/libc/tinymath
o/$(MODE)/test/libc/tinymath: \