/*-*- 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/fmt/conv.h"
#include "libc/fmt/itoa.h"
#include "libc/limits.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"

TEST(FormatInt64, test) {
  char buf[21];
  EXPECT_EQ(1, FormatInt64(buf, 0) - buf);
  EXPECT_STREQ("0", buf);
  EXPECT_EQ(1, FormatInt64(buf, 1) - buf);
  EXPECT_STREQ("1", buf);
  EXPECT_EQ(2, FormatInt64(buf, -1) - buf);
  EXPECT_STREQ("-1", buf);
  EXPECT_EQ(19, FormatInt64(buf, INT64_MAX) - buf);
  EXPECT_STREQ("9223372036854775807", buf);
  EXPECT_EQ(20, FormatInt64(buf, INT64_MIN) - buf);
  EXPECT_STREQ("-9223372036854775808", buf);
}

TEST(FormatUint64, test) {
  char buf[21];
  EXPECT_EQ(1, FormatUint64(buf, 0) - buf);
  EXPECT_STREQ("0", buf);
  EXPECT_EQ(4, FormatUint64(buf, 1024) - buf);
  EXPECT_STREQ("1024", buf);
  EXPECT_EQ(20, FormatUint64(buf, UINT64_MAX) - buf);
  EXPECT_STREQ("18446744073709551615", buf);
  EXPECT_EQ(19, FormatUint64(buf, INT64_MIN) - buf);
  EXPECT_STREQ("9223372036854775808", buf);
}

TEST(int128toarray_radix10, test) {
  char buf[41];
  EXPECT_EQ(1, int128toarray_radix10(0, buf));
  EXPECT_STREQ("0", buf);
  EXPECT_EQ(39, int128toarray_radix10(INT128_MAX, buf));
  EXPECT_STREQ("170141183460469231731687303715884105727", buf);
  EXPECT_EQ(40, int128toarray_radix10(INT128_MIN, buf));
  EXPECT_STREQ("-170141183460469231731687303715884105728", buf);
}

TEST(uint128toarray_radix10, test) {
  char buf[40];
  EXPECT_EQ(1, uint128toarray_radix10(0, buf));
  EXPECT_STREQ("0", buf);
  EXPECT_EQ(39, uint128toarray_radix10(UINT128_MAX, buf));
  EXPECT_STREQ("340282366920938463463374607431768211455", buf);
  EXPECT_EQ(39, uint128toarray_radix10(INT128_MIN, buf));
  EXPECT_STREQ("170141183460469231731687303715884105728", buf);
}

BENCH(itoa64radix10, bench) {
  char b[21];
  EZBENCH2("itoa64radix10", donothing, FormatUint64(b, UINT64_MAX));
}
