mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
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
41 lines
1.2 KiB
C++
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);
|
|
}
|