/*-*- 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.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/

/* MODE=tiny makes these dependencies optional */
STATIC_YOINK("strnwidth");
STATIC_YOINK("strnwidth16");
STATIC_YOINK("wcsnwidth");

TEST(SUITE(sprintf), testStringLength) {
  ASSERT_STREQ("This", Format(FORMAT("%.4"), STRING("This is a test")));
  ASSERT_STREQ("test", Format(FORMAT("%.4"), STRING("test")));
  ASSERT_STREQ("123", Format(FORMAT("%.7"), STRING("123")));
  ASSERT_STREQ("", Format(FORMAT("%.7"), STRING("")));
  ASSERT_STREQ("1234ab", Format(FORMAT("%.4") FORMAT("%.2"), STRING("123456"),
                                STRING("abcdef")));
  ASSERT_STREQ(FORMAT(".2"), Format(FORMAT("%.4.2"), STRING("123456")));
  ASSERT_STREQ("123", Format(FORMAT("%.*"), 3, STRING("123456")));
}

TEST(SUITE(sprintf), testCharacterCounting) {
  ASSERT_STREQ("         â¥â¦â£â âºâ»â²", Format(FORMAT("%16"), STRING("â¥â¦â£â âºâ»â²")));
}

TEST(SUITE(snprintf), testTableFlip) {
  EXPECT_STREQ("Table flip          ", Format("%-20ls", L"Table flip"));
  EXPECT_STREQ("(â¯Â°â¡Â°)â¯ï¸µLâ»ââ»       ", Format("%-20ls", L"(â¯Â°â¡Â°)â¯ï¸µLâ»ââ»"));
  EXPECT_STREQ("(â¯Â°â¡Â°)â¯ï¸µuâ»ââ»       ", Format("%-20hs", u"(â¯Â°â¡Â°)â¯ï¸µuâ»ââ»"));
  EXPECT_STREQ("ã¡ãã¶å°è¿ã        ", Format("%-20ls", L"ã¡ãã¶å°è¿ã"));
  EXPECT_STREQ("       (â¯Â°â¡Â°)â¯ï¸µLâ»ââ»", Format("%20ls", L"(â¯Â°â¡Â°)â¯ï¸µLâ»ââ»"));
  EXPECT_STREQ("        ã¡ãã¶å°è¿ã", Format("%20ls", L"ã¡ãã¶å°è¿ã"));
}

TEST(SUITE(snprintf), testCombiningWidth) {
  EXPECT_STREQ("HÌ²EÌ²LÌ²LÌ²OÌ²     ",
               Format("%-10ls", L"H\u0332E\u0332L\u0332L\u0332O\u0332"));
  EXPECT_STREQ("     HÌ²EÌ²LÌ²LÌ²OÌ²",
               Format("%10hs", u"H\u0332E\u0332L\u0332L\u0332O\u0332"));
}

TEST(SUITE(snprintf), testQuoting) {
  EXPECT_STREQ("\\\"hiâ»\\'ââ»", Format("%'s", "\"hiâ»'ââ»"));
  EXPECT_STREQ(STRINGIFY("\"hiâ»\'ââ»"), Format("%`'s", "\"hiâ»'ââ»"));
  EXPECT_STREQ(STRINGIFY("\177\"hiâ»\'ââ»"), Format("%`'s", "\x7f\"hiâ»'ââ»"));
}

TEST(SUITE(snprintf), testBing_cString_stopsAtNulTerminator) {
  EXPECT_STREQ("â¥â¦â£â ", Format("%#s", "\3\4\5\6\0\3\4\5\6"));
}

TEST(SUITE(snprintf), testBing_precisionString_showsTrueBinary) {
  EXPECT_STREQ("â¥â¦â£â Â â¥â¦â£â ", Format("%#.*s", 9, "\3\4\5\6\0\3\4\5\6"));
}

TEST(SUITE(snprintf), testStringPrecision_showsTrueBinary) {
  EXPECT_STREQ("\3\4\0", Format("%.*s", 3, "\3\4\0"));
}

TEST(SUITE(snprintf), testPrecision_usesByteCount) {
  EXPECT_STREQ("ã¡ã", Format("%.*s", 6, "ã¡ãã¶å°è¿ã"));
}

TEST(SUITE(snprintf), testReprChar16) {
  EXPECT_STREQ("u'â¥'", Format("%`'hc", u'â¥'));
}

TEST(SUITE(snprintf), testReprChar32) {
  EXPECT_STREQ("L'â¥'", Format("%`'lc", L'â¥'));
}

TEST(SUITE(snprintf), testReprUtf8) {
  EXPECT_STREQ("\"â¥\"", Format("%`'s", u8"â¥"));
}

TEST(SUITE(snprintf), testReprUtf8Precision_countsBytes) {
  EXPECT_STREQ("\"â¥\"", Format("%`'.*s", 3, u8"â¥"));
}
