/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2022 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see .
*/
#include "common/Pcsx2Defs.h"
#include "common/StringUtil.h"
#include
#include
#include
TEST(StringUtil, ToChars)
{
ASSERT_EQ(StringUtil::ToChars(false), "false");
ASSERT_EQ(StringUtil::ToChars(true), "true");
ASSERT_EQ(StringUtil::ToChars(0), "0");
ASSERT_EQ(StringUtil::ToChars(-1337), "-1337");
ASSERT_EQ(StringUtil::ToChars(1337), "1337");
ASSERT_EQ(StringUtil::ToChars(1337u), "1337");
ASSERT_EQ(StringUtil::ToChars(13.37f), "13.37");
ASSERT_EQ(StringUtil::ToChars(255, 16), "ff");
}
TEST(StringUtil, FromChars)
{
ASSERT_EQ(StringUtil::FromChars("false").value_or(true), false);
ASSERT_EQ(StringUtil::FromChars("true").value_or(false), true);
ASSERT_EQ(StringUtil::FromChars("0").value_or(-1), 0);
ASSERT_EQ(StringUtil::FromChars("-1337").value_or(0), -1337);
ASSERT_EQ(StringUtil::FromChars("1337").value_or(0), 1337);
ASSERT_EQ(StringUtil::FromChars("1337").value_or(0), 1337);
ASSERT_TRUE(std::abs(StringUtil::FromChars("13.37").value_or(0.0f) - 13.37) < 0.01f);
ASSERT_EQ(StringUtil::FromChars("ff", 16).value_or(0), 255);
}
TEST(StringUtil, FromCharsWithEndPtr)
{
using namespace std::literals;
std::string_view endptr;
ASSERT_EQ(StringUtil::FromChars("123x456", 16, &endptr), std::optional(0x123));
ASSERT_EQ(endptr, "x456"sv);
ASSERT_EQ(StringUtil::FromChars("0x1234", 16, &endptr), std::optional(0u));
ASSERT_EQ(endptr, "x1234"sv);
ASSERT_EQ(StringUtil::FromChars("1234", 16, &endptr), std::optional(0x1234u));
ASSERT_TRUE(endptr.empty());
ASSERT_EQ(StringUtil::FromChars("abcdefg", 16, &endptr), std::optional(0xabcdef));
ASSERT_EQ(endptr, "g"sv);
ASSERT_EQ(StringUtil::FromChars("123abc", 10, &endptr), std::optional(123));
ASSERT_EQ(endptr, "abc"sv);
ASSERT_EQ(StringUtil::FromChars("1.0g", &endptr), std::optional(1.0f));
ASSERT_EQ(endptr, "g"sv);
ASSERT_EQ(StringUtil::FromChars("2x", &endptr), std::optional(2.0f));
ASSERT_EQ(endptr, "x"sv);
ASSERT_EQ(StringUtil::FromChars(".1p", &endptr), std::optional(0.1f));
ASSERT_EQ(endptr, "p"sv);
ASSERT_EQ(StringUtil::FromChars("1", &endptr), std::optional(1.0f));
ASSERT_TRUE(endptr.empty());
}
#if 0
// NOTE: These tests are disabled, because they require the da_DK locale to actually be present.
// Which probably isn't going to be the case on the CI.
TEST(StringUtil, ToCharsIsLocaleIndependent)
{
const auto old_locale = std::locale();
std::locale::global(std::locale::classic());
std::string classic_result(StringUtil::ToChars(13.37f));
std::locale::global(std::locale("da_DK"));
std::string dk_result(StringUtil::ToChars(13.37f));
std::locale::global(old_locale);
ASSERT_EQ(classic_result, dk_result);
}
TEST(StringUtil, FromCharsIsLocaleIndependent)
{
const auto old_locale = std::locale();
std::locale::global(std::locale::classic());
const float classic_result = StringUtil::FromChars("13.37").value_or(0.0f);
std::locale::global(std::locale("da_DK"));
const float dk_result = StringUtil::FromChars("13.37").value_or(0.0f);
std::locale::global(old_locale);
ASSERT_EQ(classic_result, dk_result);
}
#endif