microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
test/static_test/test_utils.cc
65lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #include "gtest/gtest.h" |
| 5 | #include "re2/re2.h" |
| 6 | #include "nlohmann/json.hpp" |
| 7 | #include "string_utils.h" |
| 8 | |
| 9 | |
| 10 | TEST(utils, make_string) { |
| 11 | std::string res = MakeString("a", "b", 0); |
| 12 | EXPECT_EQ(res, "ab0"); |
| 13 | } |
| 14 | |
| 15 | TEST(utils, re2_basic) { |
| 16 | re2::StringPiece piece("1234"); |
| 17 | re2::RE2 reg("[0-9]*"); |
| 18 | } |
| 19 | |
| 20 | TEST(utils, json) { |
| 21 | nlohmann::json j; |
| 22 | j.push_back("foo"); |
| 23 | EXPECT_EQ(j.size(), 1); |
| 24 | } |
| 25 | |
| 26 | TEST(utils, split_string) { |
| 27 | auto result = SplitString("a b c d e f", " "); |
| 28 | EXPECT_EQ(result.size(), 6); |
| 29 | |
| 30 | // contain a space |
| 31 | result = SplitString("ab cd ef gh", " "); |
| 32 | EXPECT_EQ(result.size(), 5); |
| 33 | |
| 34 | // contain a space |
| 35 | result = SplitString("ab cd ef gh", " ", true); |
| 36 | EXPECT_EQ(result.size(), 4); |
| 37 | |
| 38 | result = SplitString("abcd", " "); |
| 39 | EXPECT_EQ(result.size(), 1); |
| 40 | EXPECT_EQ(result[0], "abcd"); |
| 41 | |
| 42 | result = SplitString("eabc\nasbd", "\n"); |
| 43 | EXPECT_EQ(result.size(), 2); |
| 44 | EXPECT_EQ(result[0], "eabc"); |
| 45 | |
| 46 | result = SplitString("a\nb\n", "\n"); |
| 47 | EXPECT_EQ(result.size(), 2); |
| 48 | EXPECT_EQ(result[1], "b"); |
| 49 | |
| 50 | // two seps |
| 51 | result = SplitString("ea,bc\nas,bd", ",\n"); |
| 52 | EXPECT_EQ(result.size(), 4); |
| 53 | EXPECT_EQ(result[1], "bc"); |
| 54 | } |
| 55 | |
| 56 | TEST(utils, utf8) { |
| 57 | std::vector<std::string> srcs = {"abc", "ABCé", "中文"}; |
| 58 | std::vector<std::string> lowered = {"abc", "abcé", "中文"}; |
| 59 | for (size_t i = 0; i < srcs.size(); ++i) { |
| 60 | std::string lower; |
| 61 | lower = srcs[i]; |
| 62 | std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower); |
| 63 | EXPECT_EQ(lowered[i], lower); |
| 64 | } |
| 65 | } |