pytorch/test/cpp/api/memory.cpp
Nikita Shulga 3a66a1cb99 [clang-tidy] Exclude cppcoreguidelines-avoid-magic-numbers (#57841)
Summary:
Add cppcoreguidelines-avoid-magic-numbers exclusion to clang-tidy
Remove existing nolint warnings using following script:
```
for file in `git ls-files | grep -v \.py`; do gsed '/^ *\/\/ NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)/d' -i  $file; done
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/57841

Reviewed By: samestep

Differential Revision: D28295045

Pulled By: malfet

fbshipit-source-id: 7c6e8d1213c9593f169ed3df6a916498f1a97163
2021-05-07 20:02:33 -07:00

41 lines
1.2 KiB
C++

#include <gtest/gtest.h>
#include <torch/csrc/utils/memory.h>
#include <c10/util/Optional.h>
struct TestValue {
explicit TestValue(const int& x) : lvalue_(x) {}
explicit TestValue(int&& x) : rvalue_(x) {}
c10::optional<int> lvalue_;
c10::optional<int> rvalue_;
};
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(MakeUniqueTest, ForwardRvaluesCorrectly) {
auto ptr = torch::make_unique<TestValue>(123);
ASSERT_FALSE(ptr->lvalue_.has_value());
ASSERT_TRUE(ptr->rvalue_.has_value());
ASSERT_EQ(*ptr->rvalue_, 123);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(MakeUniqueTest, ForwardLvaluesCorrectly) {
int x = 5;
auto ptr = torch::make_unique<TestValue>(x);
ASSERT_TRUE(ptr->lvalue_.has_value());
ASSERT_EQ(*ptr->lvalue_, 5);
ASSERT_FALSE(ptr->rvalue_.has_value());
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(MakeUniqueTest, CanConstructUniquePtrOfArray) {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
auto ptr = torch::make_unique<int[]>(3);
// Value initialization is required by the standard.
ASSERT_EQ(ptr[0], 0);
ASSERT_EQ(ptr[1], 0);
ASSERT_EQ(ptr[2], 0);
}