C++ 17 support: add tests, remove duplicate utf8::append()

This commit is contained in:
Nemanja Trifunovic 2021-04-25 11:54:47 -04:00
parent 50361edbfa
commit a7d530de5f
3 changed files with 89 additions and 5 deletions

View file

@ -29,16 +29,12 @@ DEALINGS IN THE SOFTWARE.
#define UTF8_FOR_CPP_7e906c01_03a3_4daf_b420_ea7ea952b3c9
#include "checked.h"
#include "cpp11.h"
#include <string>
namespace utf8
{
inline void append(char32_t cp, std::string& s)
{
append(uint32_t(cp), std::back_inserter(s));
}
inline std::string utf16to8(std::u16string_view s)
{
std::string result;

View file

@ -1,5 +1,6 @@
add_executable(negative ${PROJECT_SOURCE_DIR}/tests/negative.cpp)
add_executable(cpp11 ${PROJECT_SOURCE_DIR}/tests/test_cpp11.cpp)
add_executable(cpp17 ${PROJECT_SOURCE_DIR}/tests/test_cpp17.cpp)
add_executable(apitests
${PROJECT_SOURCE_DIR}/tests/test_checked_api.cpp
${PROJECT_SOURCE_DIR}/tests/test_unchecked_api.cpp
@ -14,6 +15,7 @@ add_executable(noexceptionstests
target_link_libraries(negative PRIVATE utf8::cpp)
target_link_libraries(cpp11 PRIVATE utf8::cpp)
target_link_libraries(cpp17 PRIVATE utf8::cpp)
target_link_libraries(apitests PRIVATE utf8::cpp)
target_link_libraries(noexceptionstests PRIVATE utf8::cpp)
@ -31,8 +33,15 @@ set_target_properties(cpp11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO)
set_target_properties(cpp17
PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO)
add_test(negative_test negative ${PROJECT_SOURCE_DIR}/tests/test_data/utf8_invalid.txt)
add_test(cpp11_test cpp11)
add_test(cpp17_test cpp17)
add_test(api_test apitests)
add_test(noexceptions_test noexceptionstests)

79
tests/test_cpp17.cpp Normal file
View file

@ -0,0 +1,79 @@
#include "../extern/ftest/ftest.h"
#include "utf8.h"
#include <string>
using namespace utf8;
using namespace std;
#if __cplusplus >= 201703L // C++ 17 or later
TEST(CPP17APITests, test_utf16to8)
{
u16string utf16string = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e};
u16string_view utf16stringview(u16string);
string u = utf16to8(utf16string);
EXPECT_EQ (u.size(), 10);
}
TEST(CPP17APITests, test_utf8to16)
{
string_view utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e";
u16string utf16result = utf8to16(utf8_with_surrogates);
EXPECT_EQ (utf16result.size(), 4);
EXPECT_EQ (utf16result[2], 0xd834);
EXPECT_EQ (utf16result[3], 0xdd1e);
}
TEST(CPP17APITests, test_utf32to8)
{
u32string utf32string = {0x448, 0x65E5, 0x10346};
u32string_view utf32stringview(utf32string);
string utf8result = utf32to8(utf32string);
EXPECT_EQ (utf8result.size(), 9);
}
TEST(CPP17APITests, test_utf8to32)
{
string_view twochars = "\xe6\x97\xa5\xd1\x88";
u32string utf32result = utf8to32(twochars);
EXPECT_EQ (utf32result.size(), 2);
}
TEST(CPP17APITests, test_find_invalid)
{
string_view utf_invalid = "\xe6\x97\xa5\xd1\x88\xfa";
auto invalid = find_invalid(utf_invalid);
EXPECT_EQ (invalid, 5);
}
TEST(CPP17APITests, test_is_valid)
{
string_view utf_invalid = "\xe6\x97\xa5\xd1\x88\xfa";
bool bvalid = is_valid(utf_invalid);
EXPECT_FALSE (bvalid);
string_view utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e";
bvalid = is_valid(utf8_with_surrogates);
EXPECT_TRUE (bvalid);
}
TEST(CPP17APITests, test_replace_invalid)
{
string_view invalid_sequence = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z";
string replace_invalid_result = replace_invalid(invalid_sequence, '?');
bool bvalid = is_valid(replace_invalid_result);
EXPECT_TRUE (bvalid);
const string fixed_invalid_sequence = "a????z";
EXPECT_EQ(fixed_invalid_sequence, replace_invalid_result);
}
TEST(CPP17APITests, test_starts_with_bom)
{
string byte_order_mark = {char(0xef), char(0xbb), char(0xbf)};
string_view byte_order_mark_view(byte_order_mark);
bool bbom = starts_with_bom(byte_order_mark_view);
EXPECT_TRUE (bbom);
string_view threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88";
bool no_bbom = starts_with_bom(threechars);
EXPECT_FALSE (no_bbom);
}
#endif // C++ 11 or later