chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:40:42 +08:00
commit e25996e7db
15472 changed files with 3536181 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
# if(NOT WITH_GFLAGS)
# paddle_test(flags_native_test SRCS flags_native_test.cc)
# endif()
add_subdirectory(string)
paddle_test(array_ref_test SRCS array_ref_test.cc)
paddle_test(small_vector_test SRCS small_vector_test.cc)
paddle_test(variant_test SRCS variant_test.cc)
paddle_test(span_test SRCS span_test.cc)
paddle_test(exception_test SRCS exception_test.cc)
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(array_ref_test)
copy_onnx(small_vector_test)
copy_onnx(variant_test)
copy_onnx(span_test)
endif()
+270
View File
@@ -0,0 +1,270 @@
// Copyright (c) 2022 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/array_ref.h"
#include <array>
#include <cstdlib>
#include <ctime>
#include "glog/logging.h"
#include "gtest/gtest.h"
#include "paddle/common/enforce.h"
TEST(array_ref, array_ref) {
paddle::array_ref<int> a;
PADDLE_ENFORCE_EQ(a.size(),
size_t(0),
common::errors::InvalidArgument(
"Array's size is invalid, expected %d but received %d.",
size_t(0),
a.size()));
PADDLE_ENFORCE_EQ(a.data(),
static_cast<int*>(nullptr),
common::errors::InvalidArgument(
"Array's data is invalid, expected %d but received %d.",
static_cast<int*>(nullptr),
a.data()));
paddle::array_ref<int> b(paddle::none);
PADDLE_ENFORCE_EQ(b.size(),
size_t(0),
common::errors::InvalidArgument(
"Array's size is invalid, expected %d but received %d.",
size_t(0),
b.size()));
PADDLE_ENFORCE_EQ(b.data(),
static_cast<int*>(nullptr),
common::errors::InvalidArgument(
"Array's data is invalid, expected %d but received %d.",
static_cast<int*>(nullptr),
b.data()));
int v = 1;
paddle::array_ref<int> c(v);
PADDLE_ENFORCE_EQ(
c.size(),
size_t(1),
common::errors::InvalidArgument(
"Array's size is invalid, expected %d(size_t(1)) but received %d.",
size_t(1),
c.size()));
PADDLE_ENFORCE_EQ(
c.data(),
&v,
common::errors::InvalidArgument(
"Array's data is invalid, expected %d(&v) but received %d.",
&v,
c.data()));
PADDLE_ENFORCE_EQ(c.equals(paddle::make_array_ref(v)),
true,
common::errors::InvalidArgument(
"The output of paddle::make_array_ref(v) is wrong."));
std::array<int, 5> v1 = {1, 2, 3, 4, 5};
paddle::array_ref<int> d(v1.data(), 5);
PADDLE_ENFORCE_EQ(
d.size(),
size_t(5),
common::errors::InvalidArgument(
"Array's size is invalid, expected %d(size_t(5)) but received %d.",
size_t(5),
d.size()));
PADDLE_ENFORCE_EQ(d.data(),
v1.data(),
common::errors::InvalidArgument(
"Array's data is invalid, expected %d but received %d.",
v1.data(),
d.data()));
PADDLE_ENFORCE_EQ(
d.equals(paddle::make_array_ref(v1.data(), 5)),
true,
common::errors::InvalidArgument(
"The output of paddle::make_array_ref(v1.data(), 5) is wrong."));
paddle::array_ref<int> e(&v1[0], &v1[4]);
PADDLE_ENFORCE_EQ(
e.size(),
size_t(4),
common::errors::InvalidArgument(
"Array's size is invalid, expected %d(size_t(4)) but received %d.",
size_t(4),
e.size()));
PADDLE_ENFORCE_EQ(
e.data(),
v1.data(),
common::errors::InvalidArgument(
"Array's data is invalid, expected %d(v1.data()) but received %d.",
v1.data(),
e.data()));
PADDLE_ENFORCE_EQ(
e.equals(paddle::make_array_ref(&v1[0], &v1[4])),
true,
common::errors::InvalidArgument(
"The output of paddle::make_array_ref(&v1[0], &v1[4]) is wrong."));
paddle::small_vector<int, 3> small_vector{1, 2, 3};
paddle::array_ref<int> f(small_vector);
PADDLE_ENFORCE_EQ(
f.size(),
size_t(3),
common::errors::InvalidArgument(
"Array's size is invalid, expected %d(size_t(3)) but received %d.",
size_t(3),
f.size()));
PADDLE_ENFORCE_EQ(f.data(),
small_vector.data(),
common::errors::InvalidArgument(
"Array's data is invalid, expected %d but received %d.",
small_vector.data(),
f.data()));
PADDLE_ENFORCE_EQ(
f.equals(paddle::make_array_ref(small_vector)),
true,
common::errors::InvalidArgument(
"The output of paddle::make_array_ref(small_vector) is wrong."));
std::vector<int> vector{1, 2, 3};
paddle::array_ref<int> g(vector);
PADDLE_ENFORCE_EQ(
g.size(),
size_t(3),
common::errors::InvalidArgument(
"Array's size is invalid, expected %d(size_t(3)) but received %d.",
size_t(3),
g.size()));
PADDLE_ENFORCE_EQ(g.data(),
vector.data(),
common::errors::InvalidArgument(
"Array's data is invalid, expected %d but received %d.",
vector.data(),
g.data()));
PADDLE_ENFORCE_EQ(
g.equals(paddle::make_array_ref(vector)),
true,
common::errors::InvalidArgument(
"The output of paddle::make_array_ref(vector) is wrong."));
std::initializer_list<int> list = {1, 2, 3};
paddle::array_ref<int> h(list);
PADDLE_ENFORCE_EQ(
h.size(),
size_t(3),
common::errors::InvalidArgument(
"Array's size is invalid, expected %d(size_t(3)) but received %d.",
size_t(3),
h.size()));
PADDLE_ENFORCE_EQ(h.data(),
list.begin(),
common::errors::InvalidArgument(
"Array's data is invalid, expected %d but received %d.",
list.begin(),
h.data()));
paddle::array_ref<int> i(h);
PADDLE_ENFORCE_EQ(
i.size(),
size_t(3),
common::errors::InvalidArgument(
"Array's size is invalid, expected %d(size_t(3)) but received %d.",
size_t(3),
i.size()));
PADDLE_ENFORCE_EQ(i.data(),
list.begin(),
common::errors::InvalidArgument(
"Array's data is invalid, expected %d but received %d.",
list.begin(),
i.data()));
PADDLE_ENFORCE_EQ(
i.equals(h),
true,
common::errors::InvalidArgument("Array i(h) is not equal with h"));
PADDLE_ENFORCE_EQ(i.equals(paddle::make_array_ref(h)),
true,
common::errors::InvalidArgument(
"i(h) is not equal with paddle::make_array_ref(h)"));
auto slice = i.slice(1, 2);
PADDLE_ENFORCE_EQ(
slice.size(),
size_t(2),
common::errors::InvalidArgument(
"Slice's size is invalid, expected %d(size_t(2)) but received %d.",
size_t(2),
slice.size()));
PADDLE_ENFORCE_EQ(
slice[0],
2,
common::errors::InvalidArgument(
"slice[0]'s value is invalid, expected 2 but received %d.",
slice[0]));
PADDLE_ENFORCE_EQ(
slice[1],
3,
common::errors::InvalidArgument(
"slice[1]'s value is invalid, expected 3 but received %d.",
slice[1]));
auto drop = i.drop_front(2);
PADDLE_ENFORCE_EQ(
drop.size(),
size_t(1),
common::errors::InvalidArgument(
"Drop's size is invalid, expected %d(size_t(1)) but received %d.",
size_t(1),
drop.size()));
PADDLE_ENFORCE_EQ(
drop[0],
3,
common::errors::InvalidArgument(
"drop[0]'s value is invalid, expected 3 but received %d.", drop[0]));
static paddle::array_ref<int> nums = {1, 2, 3, 4, 5, 6, 7, 8};
auto front = nums.take_front(3);
PADDLE_ENFORCE_EQ(
front.size(),
size_t(3),
common::errors::InvalidArgument(
"Front Array's size is invalid, expected %d but received %d.",
size_t(3),
front.size()));
for (size_t i = 0; i < 3; ++i) {
PADDLE_ENFORCE_EQ(
front[i],
nums[i],
common::errors::InvalidArgument(
"front[%d]'s value is invalid, expected %d but received %d.",
i,
nums[i],
front[i]));
}
auto back = nums.take_back(3);
PADDLE_ENFORCE_EQ(
back.size(),
size_t(3),
common::errors::InvalidArgument(
"Back Array's size is invalid, expected %d but received %d.",
size_t(3),
back.size()));
for (size_t i = 0; i < 3; ++i) {
PADDLE_ENFORCE_EQ(
back[i],
nums[i + 5],
common::errors::InvalidArgument(
"back[%d]'s value is invalid, expected %d but received %d.",
i,
nums[i + 5],
back[i]));
}
}
+60
View File
@@ -0,0 +1,60 @@
// Copyright (c) 2026 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/common/exception.h"
#include <optional>
#include <string>
#include "gtest/gtest.h"
TEST(error_message_test, optional_with_value) {
std::optional<int> value = 42;
std::string msg = common::ErrorMessage("value=", value).to_string();
EXPECT_EQ(msg, "value=42");
}
TEST(error_message_test, optional_without_value) {
std::optional<int> value = std::nullopt;
std::string msg = common::ErrorMessage("value=", value).to_string();
EXPECT_EQ(msg, "value=nullopt");
}
TEST(pd_check_test, condition_true_no_throw) {
EXPECT_NO_THROW(PD_CHECK(true, "should not throw"));
}
TEST(pd_check_test, condition_false_throws) {
EXPECT_THROW(PD_CHECK(false, "bad value"), ::common::PD_Exception);
}
TEST(pd_check_test, exception_message_contains_custom_msg) {
try {
PD_CHECK(1 == 2, "expected 1==2 but got mismatch");
FAIL() << "expected PD_Exception to be thrown";
} catch (const ::common::PD_Exception& e) {
EXPECT_NE(std::string(e.what()).find("expected 1==2 but got mismatch"),
std::string::npos);
}
}
TEST(pd_check_test, condition_false_with_optional_in_message) {
std::optional<int> opt = std::nullopt;
try {
PD_CHECK(opt.has_value(), "opt is ", opt);
FAIL() << "expected PD_Exception to be thrown";
} catch (const ::common::PD_Exception& e) {
EXPECT_NE(std::string(e.what()).find("opt is nullopt"), std::string::npos);
}
}
+54
View File
@@ -0,0 +1,54 @@
// Copyright (c) 2026 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.
#pragma once
#include <exception>
#include <string>
#include "gtest/gtest.h"
namespace test {
namespace utils {
template <typename ExceptionType = std::exception, typename Fn>
inline void ExpectThrowContains(Fn&& fn,
const std::string& expected_substr,
const std::string& context = "") {
try {
fn();
if (context.empty()) {
FAIL() << "Expected exception containing: " << expected_substr;
} else {
FAIL() << "Expected exception containing: " << expected_substr
<< ", context: " << context;
}
} catch (const ExceptionType& e) {
if (context.empty()) {
EXPECT_NE(std::string(e.what()).find(expected_substr), std::string::npos)
<< "actual error: " << e.what();
} else {
EXPECT_NE(std::string(e.what()).find(expected_substr), std::string::npos)
<< "context: " << context << ", actual error: " << e.what();
}
} catch (...) {
if (context.empty()) {
FAIL() << "Expected the specified exception type";
} else {
FAIL() << "Expected the specified exception type, context: " << context;
}
}
}
} // namespace utils
} // namespace test
+110
View File
@@ -0,0 +1,110 @@
// Copyright (c) 2023 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/common/flags.h"
#include <cstdlib>
#include "gtest/gtest.h"
PD_DEFINE_int32(paddle_test_int32, 1, "test int32 flag");
PD_DEFINE_uint32(paddle_test_uint32, 2, "test uint32 flag");
PD_DEFINE_string(paddle_test_string, "raw", "test string flag");
using namespace paddle::flags; // NOLINT
void SplitCommandlineArg(const std::string& commandline,
int* argc,
char*** argv) {
static std::vector<std::string> args;
args.clear();
for (size_t start_pos = 0, end_pos = 0;
start_pos < commandline.size() && end_pos != std::string::npos;
start_pos = end_pos + 1) {
end_pos = commandline.find(' ', start_pos);
args.push_back(commandline.substr(start_pos, end_pos - start_pos));
}
args.push_back(""); // test empty argument
*argc = static_cast<int>(args.size());
*argv = new char*[*argc];
for (size_t i = 0; i < args.size(); i++) {
(*argv)[i] = const_cast<char*>(args[i].c_str());
}
}
TEST(flags_native_test, ParseCommandLineFlags) {
uint32_t test_int32 = 2;
ASSERT_EQ(FLAGS_paddle_test_int32, 1);
ASSERT_EQ(FLAGS_paddle_test_uint32, test_int32);
ASSERT_EQ(FLAGS_paddle_test_string, "raw");
// Construct commandline arguments input
std::string commandline =
"test --paddle_test_int32=3 --paddle_test_uint32=\"4\" "
"--paddle_test_string \"modified string\"";
int argc = 0;
char** argv = nullptr;
SplitCommandlineArg(commandline, &argc, &argv);
// Parse commandline flags and check
ParseCommandLineFlags(&argc, &argv);
delete argv;
test_int32 = 4;
ASSERT_EQ(FLAGS_paddle_test_int32, 3);
ASSERT_EQ(FLAGS_paddle_test_uint32, test_int32);
ASSERT_EQ(FLAGS_paddle_test_string, "modified string");
// test FindFlag and SetFlagValue
ASSERT_TRUE(FindFlag("paddle_test_int32"));
SetFlagValue("paddle_test_int32", "9");
ASSERT_EQ(FLAGS_paddle_test_int32, 9);
}
#if defined(_POSIX_C_SOURCE) && \
_POSIX_C_SOURCE >= 200112L // environment for use setenv
bool SetEnvVar(const std::string& var_name, const std::string& var_value) {
int res = setenv(var_name.c_str(), var_value.c_str(), 1);
return res == 0;
}
PD_DEFINE_bool(paddle_test_env_bool, false, "test env bool flag");
PD_DEFINE_double(paddle_test_env_double, 3.14, "test env double flag");
TEST(flags_native_test, SetFlagsFromEnv) {
ASSERT_EQ(FLAGS_paddle_test_env_bool, false);
ASSERT_EQ(FLAGS_paddle_test_env_double, 3.14);
ASSERT_TRUE(SetEnvVar("FLAGS_paddle_test_env_bool", "true"));
ASSERT_TRUE(SetEnvVar("FLAGS_paddle_test_env_double", "2.71"));
// test GetFromEnv
ASSERT_EQ(GetFromEnv<bool>("FLAGS_paddle_test_env_bool", false), true);
ASSERT_EQ(GetFromEnv<int32_t>("FLAGS_int32_not_defined", 34), 34);
// test SetFlagsFromEnv
std::string commandline =
"test --fromenv=paddle_test_env_bool,paddle_test_env_double";
int argc;
char** argv;
SplitCommandlineArg(commandline, &argc, &argv);
ParseCommandLineFlags(&argc, &argv);
delete argv;
ASSERT_EQ(FLAGS_paddle_test_env_bool, true);
ASSERT_EQ(FLAGS_paddle_test_env_double, 2.71);
}
#endif
TEST(flags_native_test, PrintAllFlagHelp) { PrintAllFlagHelp(); }
+130
View File
@@ -0,0 +1,130 @@
// 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/small_vector.h"
#include <cstdlib>
#include <ctime>
#include "glog/logging.h"
#include "gtest/gtest.h"
#include "paddle/fluid/platform/enforce.h"
template <typename T, unsigned N>
static std::vector<T> ToStdVector(const paddle::small_vector<T, N> &vec) {
std::vector<T> std_vec;
std_vec.reserve(vec.size());
for (size_t i = 0; i < vec.size(); ++i) {
std_vec.emplace_back(vec[i]);
}
return std_vec;
}
template <size_t N>
void SmallVectorCheck(size_t n) {
std::srand(std::time(nullptr));
std::vector<int> std_vec;
paddle::small_vector<int, N> vec;
for (size_t i = 0; i < n; ++i) {
int value = rand(); // NOLINT
std_vec.emplace_back(value);
vec.emplace_back(value);
PADDLE_ENFORCE_EQ(std_vec.size(),
vec.size(),
common::errors::InvalidArgument(
"The sizes of std_vec and vec should be equal, but "
"received std_vec.size() = %zu and vec.size() = %zu.",
std_vec.size(),
vec.size()));
PADDLE_ENFORCE_EQ(
std_vec.back(),
vec.back(),
common::errors::InvalidArgument(
"The last elements of std_vec and vec should be equal, but "
"received std_vec.back() = %d and vec.back() = %d.",
std_vec.back(),
vec.back()));
PADDLE_ENFORCE_EQ(vec.back(),
value,
common::errors::InvalidArgument(
"The last element of vec should be equal to value, "
"but received vec.back() = %d and value = %d.",
vec.back(),
value));
}
bool is_equal = (std_vec == ToStdVector(vec));
PADDLE_ENFORCE_EQ(
is_equal,
true,
common::errors::InvalidArgument(
"The std_vec and vec should be equal, but they are not."));
for (size_t i = 0; i < n; ++i) {
PADDLE_ENFORCE_EQ(std_vec.size(),
vec.size(),
common::errors::InvalidArgument(
"The sizes of std_vec and vec should be equal, but "
"received std_vec.size() = %zu and vec.size() = %zu.",
std_vec.size(),
vec.size()));
PADDLE_ENFORCE_EQ(
std_vec.back(),
vec.back(),
common::errors::InvalidArgument(
"The last elements of std_vec and vec should be equal, but "
"received std_vec.back() = %d and vec.back() = %d.",
std_vec.back(),
vec.back()));
std_vec.pop_back();
vec.pop_back();
PADDLE_ENFORCE_EQ(std_vec.size(),
vec.size(),
common::errors::InvalidArgument(
"The sizes of std_vec and vec should be equal, but "
"received std_vec.size() = %zu and vec.size() = %zu.",
std_vec.size(),
vec.size()));
}
PADDLE_ENFORCE_EQ(
std_vec.size(),
static_cast<size_t>(0),
common::errors::InvalidArgument(
"The size of std_vec should be 0, but received vec.size() = %zu.",
vec.size()));
PADDLE_ENFORCE_EQ(
vec.size(),
static_cast<size_t>(0),
common::errors::InvalidArgument(
"The size of vec should be 0, but received vec.size() = %zu.",
vec.size()));
}
TEST(small_vector, small_vector) {
for (size_t i = 0; i < 20; ++i) {
SmallVectorCheck<1>(i);
SmallVectorCheck<10>(i);
SmallVectorCheck<15>(i);
SmallVectorCheck<20>(i);
SmallVectorCheck<21>(i);
SmallVectorCheck<25>(i);
}
}
File diff suppressed because it is too large Load Diff
+13
View File
@@ -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()
+31
View File
@@ -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());
}
+28
View File
@@ -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);
}
+39
View File
@@ -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));
}
+33
View File
@@ -0,0 +1,33 @@
// Copyright (c) 2022 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/variant.h"
#include "gtest/gtest.h"
#include "paddle/phi/core/enforce.h"
TEST(interface_test, type) {
using common::demangle;
paddle::variant<bool, int, float> var;
var = true;
EXPECT_EQ(demangle(var.type().name()), "bool");
var = 0;
EXPECT_EQ(demangle(var.type().name()), "int");
var = 0.f;
EXPECT_EQ(demangle(var.type().name()), "float");
}