| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <__bits/test/tests.hpp>
|
|---|
| 8 | #include <functional>
|
|---|
| 9 | #include <type_traits>
|
|---|
| 10 | #include <utility>
|
|---|
| 11 |
|
|---|
| 12 | using namespace std::placeholders;
|
|---|
| 13 |
|
|---|
| 14 | namespace std::test
|
|---|
| 15 | {
|
|---|
| 16 | namespace aux
|
|---|
| 17 | {
|
|---|
| 18 | int f1(int a, int b)
|
|---|
| 19 | {
|
|---|
| 20 | return a + b;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | int f2(int a, int b)
|
|---|
| 24 | {
|
|---|
| 25 | return a * 10 + b;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | void f3(int& a, int& b)
|
|---|
| 29 | {
|
|---|
| 30 | a = 42;
|
|---|
| 31 | b = 1337;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | struct Foo
|
|---|
| 35 | {
|
|---|
| 36 | int add(int a)
|
|---|
| 37 | {
|
|---|
| 38 | return a + data;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | int data;
|
|---|
| 42 | };
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | bool functional_test::run(bool report)
|
|---|
| 46 | {
|
|---|
| 47 | report_ = report;
|
|---|
| 48 | start();
|
|---|
| 49 |
|
|---|
| 50 | test_reference_wrapper();
|
|---|
| 51 | test_function();
|
|---|
| 52 | test_bind();
|
|---|
| 53 |
|
|---|
| 54 | return end();
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | const char* functional_test::name()
|
|---|
| 58 | {
|
|---|
| 59 | return "functional";
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | void functional_test::test_reference_wrapper()
|
|---|
| 63 | {
|
|---|
| 64 | int x{4};
|
|---|
| 65 | auto ref = std::ref(x);
|
|---|
| 66 |
|
|---|
| 67 | test("reference_wrapper equivalence after construction (cast)", (ref == x));
|
|---|
| 68 | test("reference_wrapper equivalence after construction (get)", (ref.get() == x));
|
|---|
| 69 |
|
|---|
| 70 | ref.get() = 5;
|
|---|
| 71 | test_eq("reference_wrapper equivalence after modification pt1", ref.get(), 5);
|
|---|
| 72 | test_eq("reference_wrapper equivalence after modification pt2", x, 5);
|
|---|
| 73 |
|
|---|
| 74 | int y{10};
|
|---|
| 75 | ref = y;
|
|---|
| 76 | test_eq("reference_wrapper equivalence after assignment pt1", ref.get(), 10);
|
|---|
| 77 | test_eq("reference_wrapper equivalence after assignment pt2", x, 5);
|
|---|
| 78 |
|
|---|
| 79 | std::function<int(int, int)> wrapped_f1{&aux::f1};
|
|---|
| 80 | auto fref = std::ref(wrapped_f1);
|
|---|
| 81 | auto res = fref(2, 5);
|
|---|
| 82 | test_eq("reference_wrapper function invoke", res, 7);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void functional_test::test_function()
|
|---|
| 86 | {
|
|---|
| 87 | std::function<int(int, int)> f1{&aux::f1};
|
|---|
| 88 | auto res1 = f1(1, 2);
|
|---|
| 89 |
|
|---|
| 90 | test_eq("function from function pointer", res1, 3);
|
|---|
| 91 |
|
|---|
| 92 | int x{};
|
|---|
| 93 | std::function<char(char)> f2{[&](auto c){ x = 42; return ++c; }};
|
|---|
| 94 | auto res2 = f2('B');
|
|---|
| 95 |
|
|---|
| 96 | test_eq("function from lambda", res2, 'C');
|
|---|
| 97 | test_eq("function from lambda - capture", x, 42);
|
|---|
| 98 |
|
|---|
| 99 | test("function operator bool", (bool)f2);
|
|---|
| 100 | f2 = nullptr;
|
|---|
| 101 | test("function nullptr assignment", !f2);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void functional_test::test_bind()
|
|---|
| 105 | {
|
|---|
| 106 | auto f1 = std::bind(aux::f1, _1, 1);
|
|---|
| 107 | auto res1 = f1(3);
|
|---|
| 108 |
|
|---|
| 109 | test_eq("bind placeholder", res1, 4);
|
|---|
| 110 |
|
|---|
| 111 | auto f2 = std::bind(aux::f2, _2, _1);
|
|---|
| 112 | auto res2 = f2(5, 6);
|
|---|
| 113 |
|
|---|
| 114 | test_eq("bind reverse placeholder order", res2, 65);
|
|---|
| 115 |
|
|---|
| 116 | int x{};
|
|---|
| 117 | int y{};
|
|---|
| 118 | auto f3 = std::bind(aux::f3, _1, std::ref(y));
|
|---|
| 119 | f3(std::ref(x));
|
|---|
| 120 |
|
|---|
| 121 | test_eq("bind std::ref as bound", y, 1337);
|
|---|
| 122 | test_eq("bind std::ref as unbound", x, 42);
|
|---|
| 123 |
|
|---|
| 124 | auto f4 = std::bind(aux::f2, x, y);
|
|---|
| 125 | auto res3 = f4();
|
|---|
| 126 |
|
|---|
| 127 | test_eq("bind all arguments bound", res3, 1757);
|
|---|
| 128 |
|
|---|
| 129 | aux::Foo foo{5};
|
|---|
| 130 | auto f5 = std::mem_fn(&aux::Foo::add);
|
|---|
| 131 | auto res4 = f5(&foo, 4);
|
|---|
| 132 |
|
|---|
| 133 | test_eq("mem_fn", res4, 9);
|
|---|
| 134 |
|
|---|
| 135 | /* auto f5 = std::bind(&aux::Foo::add, _1, _2); */
|
|---|
| 136 | /* auto res4 = f4(&foo, 10); */
|
|---|
| 137 |
|
|---|
| 138 | /* test_eq("bind to member function", res4, 19); */
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|