mirror of
https://github.com/pytorch/pytorch.git
synced 2026-10-28 18:56:51 +08:00
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
29 lines
867 B
C++
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);
|
|
}
|