blob: bbd8a12f88dcee4c03505af4f8e98717d88a8c97 [file] [log] [blame]
mtklein1138be42016-01-24 19:49:24 -08001/*
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"
halcanary4871f222016-08-26 13:17:44 -07009#include "SkScopeExit.h"
mtklein1138be42016-01-24 19:49:24 -080010#include <utility>
11
12namespace {
13class Moveable {
14public:
15 Moveable() {}
16 Moveable(Moveable&&) {}
17 Moveable& operator=(Moveable&&) { return *this; }
18private:
19 Moveable(const Moveable&);
20 Moveable& operator=(const Moveable&);
21};
22template <typename T> void deleter(T*) { }
23template <typename T> struct Deleter {
24 void operator()(T* t) { delete static_cast<const Moveable*>(t); }
25};
26} // namespace
27
28DEF_TEST(CPlusPlusEleven_RvalueAndMove, r) {
29 Moveable src1; Moveable dst1(std::move(src1));
30 Moveable src2, dst2; dst2 = std::move(src2);
31}
halcanarya0af7712016-05-23 09:11:58 -070032
33DEF_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}
halcanarydabd4f02016-08-03 11:16:56 -070039
40namespace {
41struct MoveableCopyable {
42 bool fCopied;
43 MoveableCopyable() : fCopied(false) {}
44 MoveableCopyable(const MoveableCopyable &o) : fCopied(true) {}
45 MoveableCopyable(MoveableCopyable &&o) : fCopied(o.fCopied) {}
46};
47struct TestClass {
48 MoveableCopyable fFoo;
49};
50} // namespace
51
52DEF_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}
halcanary4871f222016-08-26 13:17:44 -070059
60DEF_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}