pytorch/caffe2/core/init_test.cc
Nikita Shulga 4cb534f92e Make PyTorch code-base clang-tidy compliant (#56892)
Summary:
This is an automatic change generated by the following script:
```
#!/usr/bin/env python3
from subprocess import check_output, check_call
import os

def get_compiled_files_list():
    import json
    with open("build/compile_commands.json") as f:
        data = json.load(f)
    files = [os.path.relpath(node['file']) for node in data]
    for idx, fname in enumerate(files):
        if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'):
            files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')]
    return files

def run_clang_tidy(fname):
    check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"])
    changes = check_output(["git", "ls-files", "-m"])
    if len(changes) == 0:
        return
    check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"])

def main():
    git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n")
    compiled_files = get_compiled_files_list()
    for idx, fname in enumerate(git_files):
        if fname not in compiled_files:
            continue
        if fname.startswith("caffe2/contrib/aten/"):
            continue
        print(f"[{idx}/{len(git_files)}] Processing {fname}")
        run_clang_tidy(fname)

if __name__ == "__main__":
    main()
```

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

Reviewed By: H-Huang

Differential Revision: D27991944

Pulled By: malfet

fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
2021-04-28 14:10:25 -07:00

80 lines
2.6 KiB
C++

#include <iostream>
#include <memory>
#include <gtest/gtest.h>
#include "caffe2/core/init.h"
#include "caffe2/core/logging.h"
namespace caffe2 {
namespace {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
bool gTestInitFunctionHasBeenRun = false;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
bool gTestFailInitFunctionHasBeenRun = false;
bool TestInitFunction(int*, char***) {
gTestInitFunctionHasBeenRun = true;
return true;
}
bool TestFailInitFunction(int*, char***) {
gTestFailInitFunctionHasBeenRun = true;
return false;
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
REGISTER_CAFFE2_INIT_FUNCTION(
TestInitFunction,
&TestInitFunction,
"Just a test to see if GlobalInit invokes "
"registered functions correctly.");
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
int dummy_argc = 1;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
const char* dummy_name = "foo";
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-pro-type-const-cast)
char** dummy_argv = const_cast<char**>(&dummy_name);
} // namespace
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(InitTest, TestInitFunctionHasRun) {
caffe2::GlobalInit(&dummy_argc, &dummy_argv);
EXPECT_TRUE(gTestInitFunctionHasBeenRun);
EXPECT_FALSE(gTestFailInitFunctionHasBeenRun);
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(InitTest, CanRerunGlobalInit) {
caffe2::GlobalInit(&dummy_argc, &dummy_argv);
EXPECT_TRUE(caffe2::GlobalInit(&dummy_argc, &dummy_argv));
}
void LateRegisterInitFunction() {
::caffe2::InitRegisterer testInitFunc(
TestInitFunction, false, "This should succeed but warn");
}
void LateRegisterEarlyInitFunction() {
::caffe2::InitRegisterer testSecondInitFunc(
TestInitFunction, true, "This should fail for early init");
}
void LateRegisterFailInitFunction() {
::caffe2::InitRegisterer testSecondInitFunc(
TestFailInitFunction, false, "This should fail for failed init");
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(InitTest, FailLateRegisterInitFunction) {
caffe2::GlobalInit(&dummy_argc, &dummy_argv);
LateRegisterInitFunction();
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
EXPECT_THROW(LateRegisterEarlyInitFunction(), ::c10::Error);
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
EXPECT_THROW(LateRegisterFailInitFunction(), ::c10::Error);
EXPECT_TRUE(gTestInitFunctionHasBeenRun);
EXPECT_TRUE(gTestFailInitFunctionHasBeenRun);
}
} // namespace caffe2