1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 #include "Test.h" 8 #include "SkTemplates.h" 9 #include "SkScopeExit.h" 10 #include <utility> 11 12 namespace { 13 class Moveable { 14 public: 15 Moveable() {} 16 Moveable(Moveable&&) {} 17 Moveable& operator=(Moveable&&) { return *this; } 18 private: 19 Moveable(const Moveable&); 20 Moveable& operator=(const Moveable&); 21 }; 22 template <typename T> void deleter(T*) { } 23 template <typename T> struct Deleter { 24 void operator()(T* t) { delete static_cast<const Moveable*>(t); } 25 }; 26 } // namespace 27 28 DEF_TEST(CPlusPlusEleven_RvalueAndMove, r) { 29 Moveable src1; Moveable dst1(std::move(src1)); 30 Moveable src2, dst2; dst2 = std::move(src2); 31 } 32 33 DEF_TEST(CPlusPlusEleven_constexpr, r) { 34 static constexpr int x = Sk32ToBool(50); 35 REPORTER_ASSERT(r, x == 1); 36 static constexpr int y = SkTPin<int>(100, 0, 10); 37 REPORTER_ASSERT(r, y == 10); 38 } 39 40 namespace { 41 struct MoveableCopyable { 42 bool fCopied; 43 MoveableCopyable() : fCopied(false) {} 44 MoveableCopyable(const MoveableCopyable &o) : fCopied(true) {} 45 MoveableCopyable(MoveableCopyable &&o) : fCopied(o.fCopied) {} 46 }; 47 struct TestClass { 48 MoveableCopyable fFoo; 49 }; 50 } // namespace 51 52 DEF_TEST(CPlusPlusEleven_default_move, r) { 53 TestClass a; 54 TestClass b(a); 55 TestClass c(std::move(a)); 56 REPORTER_ASSERT(r, b.fFoo.fCopied); 57 REPORTER_ASSERT(r, !c.fFoo.fCopied); 58 } 59 60 DEF_TEST(SkAtScopeExit, r) { 61 int x = 5; 62 { 63 SK_AT_SCOPE_EXIT(x--); 64 REPORTER_ASSERT(r, x == 5); 65 } 66 REPORTER_ASSERT(r, x == 4); 67 } 68