pytorch/test/cpp/jit/test_qualified_name.cpp
Michael Suo dfdb86a595 big cpp test reorg (#24801)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/24801

This is to fix the ODR-violations in fbcode static builds, which have been broken for several months.

This PR is unfortunately quite large, but the changes are only mechanical:
1. Tests defined in header files -> tests defined in cpp files
2. Remove the `torch::jit::testing` namespace -> `torch::jit`.
3. Single `test.h` file that aggregates all tests.
4. Separate out files for gtest and python versions of the tests instead of using a build flag
5. Add a readme for how to add a new test, and explaining a bit about why the cpp tests are the way they are.

Test Plan: Imported from OSS

Differential Revision: D16878605

Pulled By: suo

fbshipit-source-id: 27b5c077dadd990a5f74e25d01731f9c1f491603
2019-08-18 16:49:56 -07:00

69 lines
2.0 KiB
C++

#include <ATen/core/qualified_name.h>
#include <c10/util/Exception.h>
#include "test/cpp/jit/test_base.h"
using c10::QualifiedName;
namespace torch {
namespace jit {
void testQualifiedName() {
{
// Test prefix construction
auto foo = QualifiedName("foo");
auto bar = QualifiedName(foo, "bar");
auto baz = QualifiedName(bar, "baz");
ASSERT_EQ(baz.qualifiedName(), "foo.bar.baz");
ASSERT_EQ(baz.prefix(), "foo.bar");
ASSERT_EQ(baz.name(), "baz");
auto nullstate = QualifiedName();
ASSERT_EQ(nullstate.qualifiedName(), "");
ASSERT_EQ(nullstate.prefix(), "");
ASSERT_EQ(nullstate.name(), "");
}
{
// Test dotted construction
auto foo = QualifiedName("foo.bar.baz");
ASSERT_EQ(foo.qualifiedName(), "foo.bar.baz");
ASSERT_EQ(foo.prefix(), "foo.bar");
ASSERT_EQ(foo.name(), "baz");
auto bar = QualifiedName("bar");
ASSERT_EQ(bar.qualifiedName(), "bar");
ASSERT_EQ(bar.prefix(), "");
ASSERT_EQ(bar.name(), "bar");
}
{
// throw some bad inputs at it
ASSERT_ANY_THROW(QualifiedName("foo..bar"));
ASSERT_ANY_THROW(QualifiedName(".foo.bar"));
ASSERT_ANY_THROW(QualifiedName("foo.bar."));
ASSERT_ANY_THROW(QualifiedName(""));
}
{
// test equality api
auto foo1 = QualifiedName("foo.bar.baz");
auto foo2 = QualifiedName("foo.bar.baz");
auto foo3 = QualifiedName("bar.bar.baz");
ASSERT_EQ(foo1, foo2);
ASSERT_NE(foo1, foo3);
auto bar1 = QualifiedName("sup");
auto bar2 = QualifiedName("sup");
ASSERT_EQ(foo1, foo2);
}
{
// test prefix api
auto foo1 = QualifiedName("foo.bar.baz");
auto foo2 = QualifiedName("foo.bar");
auto foo3 = QualifiedName("bar.bar.baz");
auto foo4 = QualifiedName("foo.bar");
ASSERT_TRUE(foo2.isPrefixOf(foo1));
ASSERT_TRUE(foo2.isPrefixOf(foo4));
ASSERT_TRUE(foo4.isPrefixOf(foo2));
ASSERT_FALSE(foo1.isPrefixOf(foo2));
ASSERT_FALSE(foo2.isPrefixOf(foo3));
}
}
} // namespace jit
} // namespace torch