chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
paddle_test(stringprintf_test SRCS printf_test.cc)
|
||||
paddle_test(to_string_test SRCS to_string_test.cc)
|
||||
paddle_test(split_test SRCS split_test.cc)
|
||||
paddle_test(string_helper_test SRCS string_helper_test.cc DEPS string_helper)
|
||||
|
||||
if(WITH_ONNXRUNTIME AND WIN32)
|
||||
# Copy onnxruntime for some c++ test in Windows, since the test will
|
||||
# be build only in CI, so suppose the generator in Windows is Ninja.
|
||||
copy_onnx(stringprintf_test)
|
||||
copy_onnx(to_string_test)
|
||||
copy_onnx(split_test)
|
||||
copy_onnx(string_helper_test)
|
||||
endif()
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/utils/string/printf.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
TEST(StringPrintf, StringPrintf) {
|
||||
std::string weekday = "Wednesday";
|
||||
const char* month = "July";
|
||||
size_t day = 27;
|
||||
int hour = 14;
|
||||
int min = 44;
|
||||
EXPECT_EQ(std::string("Wednesday, July 27, 14:44"),
|
||||
paddle::string::Sprintf(
|
||||
"%s, %s %d, %.2d:%.2d", weekday, month, day, hour, min));
|
||||
EXPECT_EQ(std::string(""), paddle::string::Sprintf());
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/utils/string/split.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
TEST(StringSplit, StringSplit) {
|
||||
std::string to_split = "0,1,2,3,4,5";
|
||||
int i = 0;
|
||||
for (auto const& s : paddle::string::Split(to_split, ',')) {
|
||||
EXPECT_EQ(atoi(s.c_str()), i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "paddle/utils/string/string_helper.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
TEST(StringHelper, EndsWith) {
|
||||
std::string input("hello world");
|
||||
std::string test1("world");
|
||||
std::string test2("helloworld");
|
||||
std::string test3("hello world hello world");
|
||||
|
||||
EXPECT_TRUE(paddle::string::ends_with(input, test1));
|
||||
EXPECT_TRUE(paddle::string::ends_with(input, input));
|
||||
|
||||
EXPECT_FALSE(paddle::string::ends_with(input, test2));
|
||||
EXPECT_FALSE(paddle::string::ends_with(input, test3));
|
||||
}
|
||||
|
||||
TEST(StringHelper, FormatStringAppend) {
|
||||
std::string str("hello");
|
||||
char fmt[] = "%d"; // NOLINT
|
||||
|
||||
paddle::string::format_string_append(str, fmt, 10);
|
||||
EXPECT_EQ(str, "hello10");
|
||||
}
|
||||
|
||||
TEST(StringHelper, JoinStrings) {
|
||||
std::vector<std::string> v;
|
||||
v.emplace_back("hello");
|
||||
v.emplace_back("world");
|
||||
|
||||
std::string result = paddle::string::join_strings(v, ' ');
|
||||
EXPECT_EQ(result, "hello world");
|
||||
|
||||
result = paddle::string::join_strings(v, '\n');
|
||||
EXPECT_EQ(result, "hello\nworld");
|
||||
|
||||
result = paddle::string::join_strings(v, ',');
|
||||
EXPECT_EQ(result, "hello,world");
|
||||
|
||||
result = paddle::string::join_strings(v, " new ");
|
||||
EXPECT_EQ(result, "hello new world");
|
||||
}
|
||||
|
||||
TEST(StringHelper, JoinStringsWithConversion) {
|
||||
std::vector<int> v = {2, 3};
|
||||
auto result =
|
||||
paddle::string::join_strings(v, ",", [](int x) { return x * x; });
|
||||
EXPECT_EQ(result, "4,9");
|
||||
}
|
||||
|
||||
TEST(StringHelper, SplitString) {
|
||||
std::string line = "hello world my world";
|
||||
std::vector<paddle::string::str_ptr> vals;
|
||||
int num = 0;
|
||||
num =
|
||||
paddle::string::split_string_ptr(line.c_str(), line.length(), ' ', &vals);
|
||||
EXPECT_EQ(num, 4);
|
||||
|
||||
num = paddle::string::split_string_ptr(
|
||||
line.c_str(), line.length(), ' ', &vals, 3);
|
||||
EXPECT_EQ(num, 3);
|
||||
|
||||
num = paddle::string::split_string_ptr(
|
||||
line.c_str(), line.length(), ' ', &vals, 10);
|
||||
EXPECT_EQ(num, 4);
|
||||
|
||||
num = paddle::string::split_string_ptr(line.c_str(), -1, ' ', &vals, 3);
|
||||
EXPECT_EQ(num, 0);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include "paddle/utils/string/to_string.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
constexpr char kOutputString[] = "User Defined Output"; // NOLINT
|
||||
class UserDefinedClass {
|
||||
public:
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& s, const UserDefinedClass& ins) {
|
||||
s << kOutputString;
|
||||
return s;
|
||||
}
|
||||
|
||||
TEST(to_string, normal) {
|
||||
using paddle::string::to_string;
|
||||
ASSERT_EQ("10", to_string(10));
|
||||
ASSERT_EQ("abc", to_string("abc"));
|
||||
ASSERT_EQ("1.2", to_string(1.2));
|
||||
}
|
||||
|
||||
TEST(to_string, user_defined) {
|
||||
UserDefinedClass instance;
|
||||
ASSERT_EQ(kOutputString, paddle::string::to_string(instance));
|
||||
}
|
||||
Reference in New Issue
Block a user