/*-*- 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)));
}
