LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
currytest.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "currytest.h"
10 #include <memory>
11 #include <QtTest>
12 #include <curry.h>
13 
14 QTEST_APPLESS_MAIN (LC::Util::CurryTest)
15 
16 namespace LC::Util
17 {
18  void CurryTest::testBasic ()
19  {
20  auto sum = [] (int a, int b, int c) { return a + b + c; };
21  QCOMPARE (Curry (sum) (1) (2) (3), 6);
22 
23  auto stored = Curry (sum);
24  QCOMPARE (stored (1) (2) (3), 6);
25  QCOMPARE (stored (0) (2) (3), 5);
26 
27  auto storedApplied = Curry (sum) (0);
28  QCOMPARE (storedApplied (2) (3), 5);
29  }
30 
31  void CurryTest::testMoveArgs ()
32  {
33  auto func = [] (std::unique_ptr<int> a, std::unique_ptr<int> b) { return *a + *b; };
34  QCOMPARE (Curry (func) (std::make_unique<int> (1)) (std::make_unique<int> (2)), 3);
35 
36  auto curried = Curry (func) (std::make_unique<int> (1));
37  QCOMPARE (std::move (curried) (std::make_unique<int> (2)), 3);
38  }
39 
40  void CurryTest::testMoveFun ()
41  {
42  auto ptr = std::make_unique<int> (10);
43  auto func = [ptr = std::move (ptr)] (std::unique_ptr<int> a, std::unique_ptr<int> b) { return *ptr + *a + *b; };
44  QCOMPARE (Curry (std::move (func)) (std::make_unique<int> (1)) (std::make_unique<int> (2)), 13);
45  }
46 
47  void CurryTest::testRValueFun ()
48  {
49  auto sum = [] (int&& a, int&& b, int&& c) { return a + b + c; };
50  QCOMPARE (Curry (sum) (1) (2) (3), 6);
51  }
52 
53  void CurryTest::testRefModifications ()
54  {
55  int a = 5;
56  int b = 6;
57  auto func = [] (int& a, int& b) { ++a; ++b; return a + b; };
58  QCOMPARE (Curry (func) (a) (b), 13);
59  QCOMPARE (a, 6);
60  QCOMPARE (b, 7);
61  }
62 
63  namespace
64  {
65  template<typename T>
66  struct Counter
67  {
68  static inline int DefConstrs_ = 0;
69  static inline int CopyConstrs_ = 0;
70  static inline int CopyAssignments_ = 0;
71  static inline int MoveConstrs_ = 0;
72  static inline int MoveAssignments_ = 0;
73  static inline int Dtors_ = 0;
74 
75  Counter ()
76  {
77  ++DefConstrs_;
78  }
79 
80  Counter (const Counter&)
81  {
82  ++CopyConstrs_;
83  }
84 
85  Counter (Counter&&)
86  {
87  ++MoveConstrs_;
88  }
89 
90  Counter& operator= (const Counter&)
91  {
92  ++CopyAssignments_;
93  return *this;
94  }
95 
96  Counter& operator= (Counter&&)
97  {
98  ++MoveAssignments_;
99  return *this;
100  }
101 
102  ~Counter ()
103  {
104  ++Dtors_;
105  }
106  };
107  }
108 
109  void CurryTest::testNoExtraCopiesByValue ()
110  {
111  using C1 = Counter<struct Tag1>;
112  using C2 = Counter<struct Tag2>;
113 
114  auto func = [] (C1, C2) { return 0; };
115  QCOMPARE (Curry (func) (C1 {}) (C2 {}), 0);
116 
117  QCOMPARE (C1::CopyConstrs_, 0);
118  QCOMPARE (C2::CopyConstrs_, 0);
119  QCOMPARE (C1::CopyAssignments_, 0);
120  QCOMPARE (C2::CopyAssignments_, 0);
121 
122  QCOMPARE (C1::MoveConstrs_, 2);
123  QCOMPARE (C2::MoveConstrs_, 1);
124  QCOMPARE (C1::MoveAssignments_, 0);
125  QCOMPARE (C2::MoveAssignments_, 0);
126  }
127 
128  void CurryTest::testNoExtraCopiesByRef ()
129  {
130  using C1 = Counter<struct Tag1>;
131  using C2 = Counter<struct Tag2>;
132 
133  auto func = [] (C1&&, C2&&) { return 0; };
134  QCOMPARE (Curry (func) (C1 {}) (C2 {}), 0);
135 
136  QCOMPARE (C1::CopyConstrs_, 0);
137  QCOMPARE (C2::CopyConstrs_, 0);
138  QCOMPARE (C1::CopyAssignments_, 0);
139  QCOMPARE (C2::CopyAssignments_, 0);
140 
141  QCOMPARE (C1::MoveConstrs_, 1);
142  QCOMPARE (C2::MoveConstrs_, 0);
143  QCOMPARE (C1::MoveAssignments_, 0);
144  QCOMPARE (C2::MoveAssignments_, 0);
145  }
146 
147  void CurryTest::testNoExtraCopiesByConstRef ()
148  {
149  using C1 = Counter<struct Tag1>;
150  using C2 = Counter<struct Tag2>;
151 
152  auto func = [] (const C1&, const C2&) { return 0; };
153  QCOMPARE (Curry (func) (C1 {}) (C2 {}), 0);
154 
155  QCOMPARE (C1::CopyConstrs_, 0);
156  QCOMPARE (C2::CopyConstrs_, 0);
157  QCOMPARE (C1::CopyAssignments_, 0);
158  QCOMPARE (C2::CopyAssignments_, 0);
159 
160  QCOMPARE (C1::MoveConstrs_, 1);
161  QCOMPARE (C2::MoveConstrs_, 0);
162  QCOMPARE (C1::MoveAssignments_, 0);
163  QCOMPARE (C2::MoveAssignments_, 0);
164  }
165 
166  void CurryTest::testNoExtraCopiesByConstRefToExisting ()
167  {
168  using C1 = Counter<struct Tag1>;
169  using C2 = Counter<struct Tag2>;
170 
171  auto func = [] (const C1&, const C2&) { return 0; };
172  C1 c1;
173  C2 c2;
174  QCOMPARE (Curry (func) (c1) (c2), 0);
175 
176  QCOMPARE (C1::CopyConstrs_, 0);
177  QCOMPARE (C2::CopyConstrs_, 0);
178  QCOMPARE (C1::CopyAssignments_, 0);
179  QCOMPARE (C2::CopyAssignments_, 0);
180 
181  QCOMPARE (C1::MoveConstrs_, 0);
182  QCOMPARE (C2::MoveConstrs_, 0);
183  QCOMPARE (C1::MoveAssignments_, 0);
184  QCOMPARE (C2::MoveAssignments_, 0);
185  }
186 }
LC::Util
Definition: icoreproxy.h:33
LC::Util::Curry
CurryImpl< std::decay_t< F >, Args... > Curry(F &&f, Args &&... args)
Definition: curry.h:84
currytest.h
curry.h
LC::Util::CurryTest
Definition: currytest.h:21