pytorch/caffe2/core/module_test.cc
Nikita Shulga a9b0a921d5 Disable avoid-non-const-global-variables lint check (#62008)
Summary:
As GoogleTest `TEST` macro is non-compliant with it as well as `DEFINE_DISPATCH`

All changes but the ones to `.clang-tidy` are generated using following script:
```
for i in `find . -type f -iname "*.c*" -or -iname "*.h"|xargs grep cppcoreguidelines-avoid-non-const-global-variables|cut -f1 -d:|sort|uniq`;  do sed -i "/\/\/ NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)/d" $i; done
```

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

Reviewed By: driazati, r-barnes

Differential Revision: D29838584

Pulled By: malfet

fbshipit-source-id: 1b2f8602c945bd4ce50a9bfdd204755556e31d13
2021-07-22 18:04:40 -07:00

79 lines
2.3 KiB
C++

#include <iostream>
#include <memory>
#include "caffe2/core/module.h"
#include "caffe2/core/operator.h"
#include <gtest/gtest.h>
#include "caffe2/core/logging.h"
// An explicitly defined module, testing correctness when we statically link a
// module
CAFFE2_MODULE(caffe2_module_test_static, "Static module for testing.");
namespace caffe2 {
class Caffe2ModuleTestStaticDummyOp : public OperatorBase {
public:
using OperatorBase::OperatorBase;
bool Run(int /* unused */ /*stream_id*/) override {
return true;
}
virtual string type() {
return "base";
}
};
REGISTER_CPU_OPERATOR(
Caffe2ModuleTestStaticDummy, Caffe2ModuleTestStaticDummyOp);
OPERATOR_SCHEMA(Caffe2ModuleTestStaticDummy);
TEST(ModuleTest, StaticModule) {
const string name = "caffe2_module_test_static";
const auto& modules = CurrentModules();
EXPECT_EQ(modules.count(name), 1);
EXPECT_TRUE(HasModule(name));
// LoadModule should not raise an error, since the module is already present.
LoadModule(name);
// Even a non-existing path should not cause error.
LoadModule(name, "/does/not/exist.so");
EXPECT_EQ(modules.count(name), 1);
EXPECT_TRUE(HasModule(name));
// The module will then introduce the Caffe2ModuleTestStaticDummyOp.
OperatorDef op_def;
Workspace ws;
op_def.set_type("Caffe2ModuleTestStaticDummy");
unique_ptr<OperatorBase> op = CreateOperator(op_def, &ws);
EXPECT_NE(nullptr, op.get());
}
#ifdef CAFFE2_BUILD_SHARED_LIBS
TEST(ModuleTest, DynamicModule) {
const string name = "caffe2_module_test_dynamic";
const auto& modules = CurrentModules();
EXPECT_EQ(modules.count(name), 0);
EXPECT_FALSE(HasModule(name));
// Before loading, we should not be able to create the op.
OperatorDef op_def;
Workspace ws;
op_def.set_type("Caffe2ModuleTestDynamicDummy");
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
EXPECT_THROW(
CreateOperator(op_def, &ws),
EnforceNotMet);
// LoadModule should load the proper module.
LoadModule(name);
EXPECT_EQ(modules.count(name), 1);
EXPECT_TRUE(HasModule(name));
// The module will then introduce the Caffe2ModuleTestDynamicDummyOp.
unique_ptr<OperatorBase> op_after_load = CreateOperator(op_def, &ws);
EXPECT_NE(nullptr, op_after_load.get());
}
#endif
} // namespace caffe2