summaryrefslogtreecommitdiff
path: root/src/tests/common/unique_function.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/common/unique_function.cpp')
-rw-r--r--src/tests/common/unique_function.cpp110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/tests/common/unique_function.cpp b/src/tests/common/unique_function.cpp
new file mode 100644
index 000000000..aa6e86593
--- /dev/null
+++ b/src/tests/common/unique_function.cpp
@@ -0,0 +1,110 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <string>
6
7#include <catch2/catch.hpp>
8
9#include "common/unique_function.h"
10
11namespace {
12struct Noisy {
13 Noisy() : state{"Default constructed"} {}
14 Noisy(Noisy&& rhs) noexcept : state{"Move constructed"} {
15 rhs.state = "Moved away";
16 }
17 Noisy& operator=(Noisy&& rhs) noexcept {
18 state = "Move assigned";
19 rhs.state = "Moved away";
20 return *this;
21 }
22 Noisy(const Noisy&) : state{"Copied constructed"} {}
23 Noisy& operator=(const Noisy&) {
24 state = "Copied assigned";
25 return *this;
26 }
27
28 std::string state;
29};
30} // Anonymous namespace
31
32TEST_CASE("UniqueFunction", "[common]") {
33 SECTION("Capture reference") {
34 int value = 0;
35 Common::UniqueFunction<void> func = [&value] { value = 5; };
36 func();
37 REQUIRE(value == 5);
38 }
39 SECTION("Capture pointer") {
40 int value = 0;
41 int* pointer = &value;
42 Common::UniqueFunction<void> func = [pointer] { *pointer = 5; };
43 func();
44 REQUIRE(value == 5);
45 }
46 SECTION("Move object") {
47 Noisy noisy;
48 REQUIRE(noisy.state == "Default constructed");
49
50 Common::UniqueFunction<void> func = [noisy = std::move(noisy)] {
51 REQUIRE(noisy.state == "Move constructed");
52 };
53 REQUIRE(noisy.state == "Moved away");
54 func();
55 }
56 SECTION("Move construct function") {
57 int value = 0;
58 Common::UniqueFunction<void> func = [&value] { value = 5; };
59 Common::UniqueFunction<void> new_func = std::move(func);
60 new_func();
61 REQUIRE(value == 5);
62 }
63 SECTION("Move assign function") {
64 int value = 0;
65 Common::UniqueFunction<void> func = [&value] { value = 5; };
66 Common::UniqueFunction<void> new_func;
67 new_func = std::move(func);
68 new_func();
69 REQUIRE(value == 5);
70 }
71 SECTION("Default construct then assign function") {
72 int value = 0;
73 Common::UniqueFunction<void> func;
74 func = [&value] { value = 5; };
75 func();
76 REQUIRE(value == 5);
77 }
78 SECTION("Pass arguments") {
79 int result = 0;
80 Common::UniqueFunction<void, int, int> func = [&result](int a, int b) { result = a + b; };
81 func(5, 4);
82 REQUIRE(result == 9);
83 }
84 SECTION("Pass arguments and return value") {
85 Common::UniqueFunction<int, int, int> func = [](int a, int b) { return a + b; };
86 REQUIRE(func(5, 4) == 9);
87 }
88 SECTION("Destructor") {
89 int num_destroyed = 0;
90 struct Foo {
91 Foo(int* num_) : num{num_} {}
92 Foo(Foo&& rhs) : num{std::exchange(rhs.num, nullptr)} {}
93 Foo(const Foo&) = delete;
94
95 ~Foo() {
96 if (num) {
97 ++*num;
98 }
99 }
100
101 int* num = nullptr;
102 };
103 Foo object{&num_destroyed};
104 {
105 Common::UniqueFunction<void> func = [object = std::move(object)] {};
106 REQUIRE(num_destroyed == 0);
107 }
108 REQUIRE(num_destroyed == 1);
109 }
110}