stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
string.cpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023-2024 Amebis
4*/
5
6#include "pch.hpp"
7
8using namespace std;
9#ifdef _WIN32
10using namespace Microsoft::VisualStudio::CppUnitTestFramework;
11#endif
12
13namespace UnitTests
14{
15 TEST_CLASS(string)
16 {
17 public:
18 TEST_METHOD(sprintf)
19 {
20 Assert::AreEqual(L"This is a test.", stdex::sprintf(L"This is %ls.", stdex::locale_default, L"a test").c_str());
21 Assert::AreEqual<size_t>(15, stdex::sprintf(L"This is %ls.", stdex::locale_default, L"a test").size());
22 Assert::AreEqual("This is a test.", stdex::sprintf("This is %s.", stdex::locale_default, "a test").c_str());
23 Assert::AreEqual<size_t>(15, stdex::sprintf("This is %s.", stdex::locale_default, "a test").size());
24
25 // swprintf functions return EILSEQ when %ls inserts contain emoji on Mac. 😢
26 Assert::AreEqual(L"This is a tést.", stdex::sprintf(L"This is %ls.", stdex::locale_default, L"a tést").c_str());
27 Assert::AreEqual("This is a 🐔Test🐮.", stdex::sprintf("This is %s.", stdex::locale_default, "a 🐔Test🐮").c_str());
28
29 wstring wstr;
30 std::string str;
31 for (size_t i = 0; i < 2000; i++) {
32 wstr += L"tést\r\n";
33 str += "🐔Test🐮\r\n";
34 }
35 Assert::AreEqual(wstr.c_str(), stdex::sprintf(L"%ls", stdex::locale_default, wstr.data()).c_str());
36 Assert::AreEqual(wstr.size(), stdex::sprintf(L"%ls", stdex::locale_default, wstr.data()).size());
37 Assert::AreEqual(str.c_str(), stdex::sprintf("%s", stdex::locale_utf8, str.data()).c_str());
38 Assert::AreEqual(str.size(), stdex::sprintf("%s", stdex::locale_utf8, str.data()).size());
39 }
40 };
41}