blob: fb3e765f1cec7c90b3ac10972e16b175b865cf6a [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 */
Ben Wagnereed61282018-04-17 14:14:51 -04007
halcanary4871f222016-08-26 13:17:44 -07008#include "SkScopeExit.h"
Ben Wagnereed61282018-04-17 14:14:51 -04009#include "SkTypes.h"
10#include "Test.h"
11
mtklein1138be42016-01-24 19:49:24 -080012#include <utility>
13
14namespace {
15class Moveable {
16public:
17 Moveable() {}
18 Moveable(Moveable&&) {}
19 Moveable& operator=(Moveable&&) { return *this; }
20private:
21 Moveable(const Moveable&);
22 Moveable& operator=(const Moveable&);
23};
24template <typename T> void deleter(T*) { }
25template <typename T> struct Deleter {
26 void operator()(T* t) { delete static_cast<const Moveable*>(t); }
27};
28} // namespace
29
30DEF_TEST(CPlusPlusEleven_RvalueAndMove, r) {
31 Moveable src1; Moveable dst1(std::move(src1));
32 Moveable src2, dst2; dst2 = std::move(src2);
33}
halcanarya0af7712016-05-23 09:11:58 -070034
35DEF_TEST(CPlusPlusEleven_constexpr, r) {
36 static constexpr int x = Sk32ToBool(50);
37 REPORTER_ASSERT(r, x == 1);
38 static constexpr int y = SkTPin<int>(100, 0, 10);
39 REPORTER_ASSERT(r, y == 10);
40}
halcanarydabd4f02016-08-03 11:16:56 -070041
42namespace {
43struct MoveableCopyable {
44 bool fCopied;
45 MoveableCopyable() : fCopied(false) {}
46 MoveableCopyable(const MoveableCopyable &o) : fCopied(true) {}
47 MoveableCopyable(MoveableCopyable &&o) : fCopied(o.fCopied) {}
48};
49struct TestClass {
50 MoveableCopyable fFoo;
51};
52} // namespace
53
54DEF_TEST(CPlusPlusEleven_default_move, r) {
55 TestClass a;
56 TestClass b(a);
57 TestClass c(std::move(a));
58 REPORTER_ASSERT(r, b.fFoo.fCopied);
59 REPORTER_ASSERT(r, !c.fFoo.fCopied);
60}
halcanary4871f222016-08-26 13:17:44 -070061
62DEF_TEST(SkAtScopeExit, r) {
63 int x = 5;
64 {
65 SK_AT_SCOPE_EXIT(x--);
66 REPORTER_ASSERT(r, x == 5);
67 }
68 REPORTER_ASSERT(r, x == 4);
69}