Files
Aaron Gokaslan 8e0279cb50 [BE][Ez]: Use CPP20 rvalue overload for ostringstream (#187208)
Followup to #186552 . Uses the rvalue overload of ostringstream newly introduced in CPP20 to reduce the amount of string copy allowing us to steal the internal string buffer.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/187208
Approved by: https://github.com/d4l3k, https://github.com/lakshayg, https://github.com/malfet
2026-07-10 23:33:00 +00:00

29 lines
867 B
C++

#include <gtest/gtest.h>
#include <torch/torch.h>
#include <iostream>
#include <sstream>
TEST(PrintSciModeTest, ToggleScientificNotation) {
auto t = torch::tensor({0.00001, 100000.0});
// Test with scientific notation enabled
torch::set_printoption_sci_mode(true);
std::ostringstream oss1;
oss1 << t;
auto out1 = std::move(oss1).str();
std::cout << "With sci_mode=true: '" << out1 << '\'' << std::endl;
EXPECT_TRUE(
out1.find("e-") != std::string::npos ||
out1.find("e+") != std::string::npos);
// Test with scientific notation disabled
torch::set_printoption_sci_mode(false);
std::ostringstream oss2;
oss2 << t;
auto out2 = std::move(oss2).str();
std::cout << "With sci_mode=false: '" << out2 << '\'' << std::endl;
EXPECT_TRUE(
out2.find("e-") == std::string::npos &&
out2.find("e+") == std::string::npos);
}