mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
This was a sneaky one: the StringCordView default constructor allocates. Differential Revision: [D73129448](https://our.internmc.facebook.com/intern/diff/D73129448/) Pull Request resolved: https://github.com/pytorch/pytorch/pull/151800 Approved by: https://github.com/malfet, https://github.com/cyyever, https://github.com/Skylion007 ghstack dependencies: #151682
59 lines
1.7 KiB
C++
59 lines
1.7 KiB
C++
#include <gtest/gtest.h>
|
|
#include <torch/csrc/jit/frontend/source_range.h>
|
|
|
|
using namespace ::testing;
|
|
using namespace ::torch::jit;
|
|
|
|
TEST(SourceRangeTest, test_find) {
|
|
std::vector<std::shared_ptr<std::string>> strings;
|
|
strings.push_back(std::make_shared<std::string>("hello world"));
|
|
strings.push_back(std::make_shared<std::string>("nihaoma"));
|
|
|
|
std::vector<std::string_view> pieces{*strings[0], *strings[1]};
|
|
|
|
StringCordView view(pieces, strings);
|
|
|
|
auto x = view.find("rldni", 0);
|
|
EXPECT_EQ(x, 8);
|
|
}
|
|
|
|
TEST(SourceRangeTest, test_substr) {
|
|
std::vector<std::shared_ptr<std::string>> strings;
|
|
strings.push_back(std::make_shared<std::string>("hello world"));
|
|
strings.push_back(std::make_shared<std::string>("nihaoma"));
|
|
|
|
std::vector<std::string_view> pieces{*strings[0], *strings[1]};
|
|
|
|
StringCordView view(pieces, strings);
|
|
|
|
auto x = view.substr(4, 10).str();
|
|
EXPECT_EQ(x, view.str().substr(4, 10));
|
|
EXPECT_EQ(view.substr(0, view.size()).str(), view.str());
|
|
}
|
|
|
|
TEST(SourceRangeTest, test_iter) {
|
|
std::vector<std::shared_ptr<std::string>> strings;
|
|
strings.push_back(std::make_shared<std::string>("hello world"));
|
|
strings.push_back(std::make_shared<std::string>("nihaoma"));
|
|
|
|
std::vector<std::string_view> pieces{*strings[0], *strings[1]};
|
|
|
|
StringCordView view(pieces, strings);
|
|
|
|
auto iter = view.iter_for_pos(5);
|
|
EXPECT_EQ(*iter, ' ');
|
|
EXPECT_EQ(iter.rest_line(), " world");
|
|
EXPECT_EQ(*iter.next_iter(), 'w');
|
|
EXPECT_EQ(iter.pos(), 5);
|
|
|
|
iter = view.iter_for_pos(13);
|
|
EXPECT_EQ(iter.pos(), 13);
|
|
}
|
|
|
|
TEST(SourceRangeTest, SimpleString) {
|
|
Source src("hello");
|
|
EXPECT_EQ(src.num_lines(), 1);
|
|
EXPECT_EQ(src.get_line(0), "hello");
|
|
EXPECT_EQ(src.text_str().str(), "hello");
|
|
}
|