microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
test/static_test/test_ustring.cc
67lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #include <cstring> |
| 5 | #include "gtest/gtest.h" |
| 6 | #include "ustring.h" |
| 7 | |
| 8 | void convert_test(const char* const_str) { |
| 9 | std::string string(const_str); |
| 10 | const std::string const_string(const_str); |
| 11 | |
| 12 | auto str = std::shared_ptr<char>(strdup(const_str)); |
| 13 | ustring char_construct(str.get()); |
| 14 | EXPECT_EQ(const_string, std::string(char_construct)); |
| 15 | |
| 16 | ustring const_char_construct(const_str); |
| 17 | EXPECT_EQ(const_string, std::string(const_char_construct)); |
| 18 | |
| 19 | ustring string_construct(string); |
| 20 | EXPECT_EQ(const_string, std::string(string_construct)); |
| 21 | |
| 22 | ustring const_string_construct(const_string); |
| 23 | EXPECT_EQ(const_string, std::string(const_string_construct)); |
| 24 | |
| 25 | std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> cvt; |
| 26 | std::u32string u32_str(cvt.from_bytes(const_string)); |
| 27 | const std::u32string u32_const_str(cvt.from_bytes(const_string)); |
| 28 | |
| 29 | ustring u32string_construct(u32_str); |
| 30 | EXPECT_EQ(const_string, std::string(u32string_construct)); |
| 31 | |
| 32 | ustring u32string_const_construct(u32_const_str); |
| 33 | EXPECT_EQ(const_string, std::string(u32string_const_construct)); |
| 34 | |
| 35 | ustring u32string_move_construct(std::move(u32_const_str)); |
| 36 | EXPECT_EQ(const_string, std::string(u32string_move_construct)); |
| 37 | |
| 38 | ustring u32string_const_move_construct(std::move(u32_const_str)); |
| 39 | EXPECT_EQ(const_string, std::string(u32string_const_move_construct)); |
| 40 | } |
| 41 | |
| 42 | TEST(ustring, construct_and_convert) { |
| 43 | convert_test("English Test"); |
| 44 | convert_test("Test de français"); |
| 45 | convert_test("中文测试"); |
| 46 | convert_test("日本語テスト"); |
| 47 | convert_test("🧐 Test"); |
| 48 | } |
| 49 | |
| 50 | TEST(ustring, operater) { |
| 51 | ustring test("一些汉字"); |
| 52 | |
| 53 | EXPECT_EQ(test.size(), 4); |
| 54 | EXPECT_EQ(test[0], U'一'); |
| 55 | EXPECT_EQ(test[1], U'些'); |
| 56 | EXPECT_EQ(test[2], U'汉'); |
| 57 | EXPECT_EQ(test[3], U'字'); |
| 58 | |
| 59 | EXPECT_EQ(test.find(U"一"), 0); |
| 60 | |
| 61 | EXPECT_EQ(test.find_first_not_of(U"一"), 1); |
| 62 | EXPECT_EQ(test.find_last_not_of(U"一"), 3); |
| 63 | |
| 64 | test.append(U"用来测试"); |
| 65 | EXPECT_EQ(test.at(4), U'用'); |
| 66 | } |
| 67 | |
| 68 | |