HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Oreo
|
8.0.0_r4
下载
查看原文件
收藏
根目录
frameworks
native
libs
vr
libpdx
variant_tests.cpp
#include
#include
#include
#include
#include
#include
#include
using namespace android::pdx; using namespace android::pdx::rpc; namespace { struct BaseType { BaseType(int value) : value(value) {} int value; }; struct DerivedType : BaseType { DerivedType(int value) : BaseType{value} {}; }; template
class TestType { public: TestType(const T& value) : value_(value) {} TestType(T&& value) : value_(std::move(value)) {} TestType(const TestType&) = default; TestType(TestType&&) = default; TestType& operator=(const TestType&) = default; TestType& operator=(TestType&&) = default; const T& get() const { return value_; } T&& take() { return std::move(value_); } private: T value_; }; template
class InstrumentType { public: InstrumentType(const T& value) : value_(value) { constructor_count_++; } InstrumentType(T&& value) : value_(std::move(value)) { constructor_count_++; } InstrumentType(const InstrumentType& other) : value_(other.value_) { constructor_count_++; } InstrumentType(InstrumentType&& other) : value_(std::move(other.value_)) { constructor_count_++; } InstrumentType(const TestType
& other) : value_(other.get()) { constructor_count_++; } InstrumentType(TestType
&& other) : value_(other.take()) { constructor_count_++; } ~InstrumentType() { destructor_count_++; } InstrumentType& operator=(const InstrumentType& other) { copy_assignment_count_++; value_ = other.value_; return *this; } InstrumentType& operator=(InstrumentType&& other) { move_assignment_count_++; value_ = std::move(other.value_); return *this; } InstrumentType& operator=(const TestType
& other) { copy_assignment_count_++; value_ = other.get(); return *this; } InstrumentType& operator=(TestType
&& other) { move_assignment_count_++; value_ = other.take(); return *this; } static std::size_t constructor_count() { return constructor_count_; } static std::size_t destructor_count() { return destructor_count_; } static std::size_t move_assignment_count() { return move_assignment_count_; } static std::size_t copy_assignment_count() { return copy_assignment_count_; } const T& get() const { return value_; } T&& take() { return std::move(value_); } static void clear() { constructor_count_ = 0; destructor_count_ = 0; move_assignment_count_ = 0; copy_assignment_count_ = 0; } private: T value_; static std::size_t constructor_count_; static std::size_t destructor_count_; static std::size_t move_assignment_count_; static std::size_t copy_assignment_count_; }; template
std::size_t InstrumentType
::constructor_count_ = 0; template
std::size_t InstrumentType
::destructor_count_ = 0; template
std::size_t InstrumentType
::move_assignment_count_ = 0; template
std::size_t InstrumentType
::copy_assignment_count_ = 0; } // anonymous namespace TEST(Variant, Assignment) { // Assert basic type properties. { Variant
v; ASSERT_EQ(-1, v.index()); ASSERT_FALSE(v.is
()); ASSERT_FALSE(v.is
()); ASSERT_FALSE(v.is
()); } { Variant
v; v = 10; ASSERT_EQ(0, v.index()); ASSERT_TRUE(v.is
()); ASSERT_FALSE(v.is
()); ASSERT_FALSE(v.is
()); EXPECT_EQ(10, std::get
(v)); } { Variant
v; v = false; ASSERT_EQ(1, v.index()); ASSERT_FALSE(v.is
()); ASSERT_TRUE(v.is
()); ASSERT_FALSE(v.is
()); EXPECT_EQ(false, std::get
(v)); } { Variant
v; v = 1.0f; ASSERT_EQ(2, v.index()); ASSERT_FALSE(v.is
()); ASSERT_FALSE(v.is
()); ASSERT_TRUE(v.is
()); EXPECT_FLOAT_EQ(1.0f, std::get
(v)); } { Variant
v; // ERROR: More than one type is implicitly convertible from double. // v = 1.0; v = static_cast
(1.0); } { Variant
v; double x = 1.1; v = static_cast
(x); ASSERT_EQ(2, v.index()); ASSERT_FALSE(v.is
()); ASSERT_FALSE(v.is
()); ASSERT_TRUE(v.is
()); EXPECT_FLOAT_EQ(1.1, std::get
(v)); } { Variant
v; ASSERT_EQ(-1, v.index()); ASSERT_FALSE(v.is
()); ASSERT_FALSE(v.is
()); } { Variant
v; v = 20; ASSERT_EQ(0, v.index()); ASSERT_TRUE(v.is
()); ASSERT_FALSE(v.is
()); EXPECT_EQ(20, std::get
(v)); } { Variant
v; v = std::string("test"); ASSERT_EQ(1, v.index()); ASSERT_FALSE(v.is
()); ASSERT_TRUE(v.is
()); EXPECT_EQ("test", std::get
(v)); } { Variant
v; v = "test"; ASSERT_EQ(1, v.index()); ASSERT_FALSE(v.is
()); ASSERT_TRUE(v.is
()); EXPECT_EQ("test", std::get
(v)); } { Variant
v1; Variant
v2; v1 = "test"; ASSERT_TRUE(v1.is
()); v2 = v1; ASSERT_TRUE(v2.is
()); EXPECT_EQ("test", std::get
(v2)); } { Variant
a(1); Variant
b; ASSERT_TRUE(!a.empty()); ASSERT_TRUE(b.empty()); a = b; ASSERT_TRUE(a.empty()); ASSERT_TRUE(b.empty()); } { Variant
v; // ERROR: More than one type is implicitly convertible from nullptr. // v = nullptr; v = static_cast
(nullptr); EXPECT_TRUE(v.is
()); v = static_cast
(nullptr); EXPECT_TRUE(v.is
()); } { Variant
v; int a = 10; char b = 20; v = &b; ASSERT_TRUE(v.is
()); EXPECT_EQ(&b, std::get
(v)); EXPECT_EQ(b, *std::get
(v)); v = &a; ASSERT_TRUE(v.is
()); EXPECT_EQ(&a, std::get
(v)); EXPECT_EQ(a, *std::get
(v)); } { using IntRef = std::reference_wrapper
; Variant
v; int a = 10; v = a; ASSERT_TRUE(v.is
()); EXPECT_EQ(a, std::get
(v)); a = 20; EXPECT_EQ(a, std::get
(v)); } } TEST(Variant, MoveAssignment) { { Variant
v; std::string s = "test"; v = std::move(s); EXPECT_TRUE(s.empty()); ASSERT_TRUE(v.is
()); EXPECT_EQ("test", std::get
(v)); } { Variant
v("test"); std::string s = "fizz"; s = std::move(std::get
(v)); ASSERT_TRUE(v.is
()); EXPECT_TRUE(std::get
(v).empty()); EXPECT_EQ("test", s); } { Variant
a("test"); Variant
b; b = std::move(a); ASSERT_TRUE(a.is
()); ASSERT_TRUE(b.is
()); EXPECT_TRUE(std::get
(a).empty()); EXPECT_EQ("test", std::get
(b)); } { Variant
a("test"); Variant
b("fizz"); b = std::move(a); ASSERT_TRUE(a.is
()); ASSERT_TRUE(b.is
()); EXPECT_TRUE(std::get
(a).empty()); EXPECT_EQ("test", std::get
(b)); } { Variant
a("test"); Variant
b(10); b = std::move(a); ASSERT_TRUE(a.is
()); ASSERT_TRUE(b.is
()); EXPECT_TRUE(std::get
(a).empty()); EXPECT_EQ("test", std::get
(b)); } { Variant
a(10); Variant
b("test"); b = std::move(a); ASSERT_TRUE(a.is
()); ASSERT_TRUE(b.is
()); EXPECT_EQ(10, std::get
(a)); EXPECT_EQ(10, std::get
(b)); } } TEST(Variant, Constructor) { { Variant
v(true); EXPECT_TRUE(v.is
()); } { Variant
v(10); EXPECT_TRUE(v.is
()); } { Variant
v(10.1f); EXPECT_TRUE(v.is
()); } { Variant
v(10.); EXPECT_TRUE(v.is
()); } { TestType
i(1); Variant
v(i.take()); ASSERT_TRUE(v.is
()); EXPECT_EQ(1, std::get
(v)); } { TestType
i(1); Variant
v(i.get()); ASSERT_TRUE(v.is
()); EXPECT_EQ(1, std::get
(v)); } { TestType
b(true); Variant
v(b.take()); ASSERT_TRUE(v.is
()); EXPECT_EQ(true, std::get
(v)); } { TestType
b(true); Variant
v(b.get()); ASSERT_TRUE(v.is
()); EXPECT_EQ(true, std::get
(v)); } { Variant
c("test"); Variant
s(c); ASSERT_TRUE(s.is
()); EXPECT_EQ("test", std::get
(s)); } { Variant
a(true); Variant
b(a); ASSERT_TRUE(b.is
()); } { using IntRef = std::reference_wrapper
; int a = 10; Variant
v(a); TestType
t(a); ASSERT_TRUE(v.is
()); EXPECT_EQ(a, std::get
(v)); EXPECT_EQ(a, t.get()); a = 20; EXPECT_EQ(a, std::get
(v)); EXPECT_EQ(a, t.get()); } } // Verify correct ctor/dtor and assignment behavior used an instrumented type. TEST(Variant, CopyMoveConstructAssign) { { InstrumentType
::clear(); // Default construct to empty, no InstrumentType activity. Variant
> v; ASSERT_EQ(0u, InstrumentType
::constructor_count()); ASSERT_EQ(0u, InstrumentType
::destructor_count()); ASSERT_EQ(0u, InstrumentType
::move_assignment_count()); ASSERT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); // Construct from int type, no InstrumentType activity. Variant
> v; v = 10; EXPECT_EQ(0u, InstrumentType
::constructor_count()); EXPECT_EQ(0u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); // Construct from int type, no InstrumentType activity. Variant
> v(10); EXPECT_EQ(0u, InstrumentType
::constructor_count()); EXPECT_EQ(0u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); // Construct from temporary, temporary ctor/dtor. Variant
> v; v = InstrumentType
(25); EXPECT_EQ(2u, InstrumentType
::constructor_count()); EXPECT_EQ(1u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); // Construct from temporary, temporary ctor/dtor. Variant
> v(InstrumentType
(25)); EXPECT_EQ(2u, InstrumentType
::constructor_count()); EXPECT_EQ(1u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); // Construct from temporary, temporary ctor/dtor. Variant
> v(InstrumentType
(25)); // Assign from temporary, temporary ctor/dtor. v = InstrumentType
(35); EXPECT_EQ(3u, InstrumentType
::constructor_count()); EXPECT_EQ(2u, InstrumentType
::destructor_count()); EXPECT_EQ(1u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); // Construct from temporary, temporary ctor/dtor. Variant
> v(InstrumentType
(25)); // dtor. v = 10; EXPECT_EQ(2u, InstrumentType
::constructor_count()); EXPECT_EQ(2u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); // Construct from temporary, temporary ctor/dtor. Variant
> v(InstrumentType
(25)); EXPECT_EQ(2u, InstrumentType
::constructor_count()); EXPECT_EQ(1u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } EXPECT_EQ(2u, InstrumentType
::constructor_count()); EXPECT_EQ(2u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); { InstrumentType
::clear(); // Construct from other temporary. Variant
> v(TestType
(10)); EXPECT_EQ(1u, InstrumentType
::constructor_count()); EXPECT_EQ(0u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); // Construct from other temporary. Variant
> v(TestType
(10)); // Assign from other temporary. v = TestType
(11); EXPECT_EQ(1u, InstrumentType
::constructor_count()); EXPECT_EQ(0u, InstrumentType
::destructor_count()); EXPECT_EQ(1u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); // Construct from other temporary. Variant
> v(TestType
(10)); // Assign from empty Variant. v = Variant
>(); EXPECT_EQ(1u, InstrumentType
::constructor_count()); EXPECT_EQ(1u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); TestType
other(10); // Construct from other. Variant
> v(other); EXPECT_EQ(1u, InstrumentType
::constructor_count()); EXPECT_EQ(0u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(0u, InstrumentType
::copy_assignment_count()); } { InstrumentType
::clear(); // Construct from other temporary. Variant
> v(TestType
(0)); TestType
other(10); // Assign from other. v = other; EXPECT_EQ(1u, InstrumentType
::constructor_count()); EXPECT_EQ(0u, InstrumentType
::destructor_count()); EXPECT_EQ(0u, InstrumentType
::move_assignment_count()); EXPECT_EQ(1u, InstrumentType
::copy_assignment_count()); } } TEST(Variant, MoveConstructor) { { std::unique_ptr
pointer = std::make_unique
(10); Variant