|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <ATen/ATen.h> |
| 4 | +#include <ATen/core/Vitals.h> |
| 5 | +#include <cstdlib> |
| 6 | + |
| 7 | +using namespace at::vitals; |
| 8 | + |
| 9 | +TEST(Vitals, Basic) { |
| 10 | + std::stringstream buffer; |
| 11 | + |
| 12 | + std::streambuf* sbuf = std::cout.rdbuf(); |
| 13 | + std::cout.rdbuf(buffer.rdbuf()); |
| 14 | + { |
| 15 | +#ifdef _WIN32 |
| 16 | + _putenv("TORCH_VITAL=1"); |
| 17 | +#else |
| 18 | + setenv("TORCH_VITAL", "1", 1); |
| 19 | +#endif |
| 20 | + TORCH_VITAL_DEFINE(Testing); |
| 21 | + TORCH_VITAL(Testing, Attribute0) << 1; |
| 22 | + TORCH_VITAL(Testing, Attribute1) << "1"; |
| 23 | + TORCH_VITAL(Testing, Attribute2) << 1.0f; |
| 24 | + TORCH_VITAL(Testing, Attribute3) << 1.0; |
| 25 | + auto t = at::ones({1, 1}); |
| 26 | + TORCH_VITAL(Testing, Attribute4) << t; |
| 27 | + } |
| 28 | + std::cout.rdbuf(sbuf); |
| 29 | + |
| 30 | + auto s = buffer.str(); |
| 31 | + ASSERT_TRUE(s.find("Testing.Attribute0\t\t 1") != std::string::npos); |
| 32 | + ASSERT_TRUE(s.find("Testing.Attribute1\t\t 1") != std::string::npos); |
| 33 | + ASSERT_TRUE(s.find("Testing.Attribute2\t\t 1") != std::string::npos); |
| 34 | + ASSERT_TRUE(s.find("Testing.Attribute3\t\t 1") != std::string::npos); |
| 35 | + ASSERT_TRUE(s.find("Testing.Attribute4\t\t 1") != std::string::npos); |
| 36 | +} |
| 37 | + |
| 38 | +TEST(Vitals, MultiString) { |
| 39 | + std::stringstream buffer; |
| 40 | + |
| 41 | + std::streambuf* sbuf = std::cout.rdbuf(); |
| 42 | + std::cout.rdbuf(buffer.rdbuf()); |
| 43 | + { |
| 44 | +#ifdef _WIN32 |
| 45 | + _putenv("TORCH_VITAL=1"); |
| 46 | +#else |
| 47 | + setenv("TORCH_VITAL", "1", 1); |
| 48 | +#endif |
| 49 | + TORCH_VITAL_DEFINE(Testing); |
| 50 | + TORCH_VITAL(Testing, Attribute0) << 1 << " of " << 2; |
| 51 | + TORCH_VITAL(Testing, Attribute1) << 1; |
| 52 | + TORCH_VITAL(Testing, Attribute1) << " of "; |
| 53 | + TORCH_VITAL(Testing, Attribute1) << 2; |
| 54 | + } |
| 55 | + std::cout.rdbuf(sbuf); |
| 56 | + |
| 57 | + auto s = buffer.str(); |
| 58 | + ASSERT_TRUE(s.find("Testing.Attribute0\t\t 1 of 2") != std::string::npos); |
| 59 | + ASSERT_TRUE(s.find("Testing.Attribute1\t\t 1 of 2") != std::string::npos); |
| 60 | +} |
| 61 | + |
| 62 | +TEST(Vitals, OnAndOff) { |
| 63 | + for (auto i = 0; i < 2; ++i) { |
| 64 | + std::stringstream buffer; |
| 65 | + |
| 66 | + std::streambuf* sbuf = std::cout.rdbuf(); |
| 67 | + std::cout.rdbuf(buffer.rdbuf()); |
| 68 | + { |
| 69 | +#ifdef _WIN32 |
| 70 | + if (i) { |
| 71 | + _putenv("TORCH_VITAL=1"); |
| 72 | + } else { |
| 73 | + _putenv("TORCH_VITAL=0"); |
| 74 | + } |
| 75 | +#else |
| 76 | + setenv("TORCH_VITAL", i ? "1" : "", 1); |
| 77 | +#endif |
| 78 | + TORCH_VITAL_DEFINE(Testing); |
| 79 | + TORCH_VITAL(Testing, Attribute0) << 1; |
| 80 | + } |
| 81 | + std::cout.rdbuf(sbuf); |
| 82 | + |
| 83 | + auto s = buffer.str(); |
| 84 | + auto f = s.find("Testing.Attribute0\t\t 1"); |
| 85 | + if (i) { |
| 86 | + ASSERT_TRUE(f != std::string::npos); |
| 87 | + } else { |
| 88 | + ASSERT_TRUE(f == std::string::npos); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments