/*-*- 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/rand/rand.h"
#include "libc/runtime/gc.internal.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_STREQ("-NAN", gc(xdtoal(fmodl(INFINITY, 1))));
  EXPECT_STREQ("-NAN", gc(xdtoal(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_STREQ("-NAN", gc(xdtoa(fmod(INFINITY, 1))));
  EXPECT_STREQ("-NAN", gc(xdtoa(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_STREQ("-NAN", gc(xdtoaf(fmodf(INFINITY, 1))));
  EXPECT_STREQ("-NAN", gc(xdtoaf(fmodf(1, 0))));
  EXPECT_STREQ("8", gc(xdtoaf(fmodf(8, 32))));
  EXPECT_STREQ("8e+20", gc(xdtoaf(fmodf(8e20, 32e20))));
}

BENCH(fmod, bench) {
  EZBENCH2("fmod eze", donothing, fmodl(8, 32));
  EZBENCH2("fmod big", donothing, fmodl(5.319372648326541e+255, M_2_PI));
}
