LeechCraft  0.6.70-3565-g2d86529
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
prelude.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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "prelude.h"
31 #include <QtTest>
32 #include <prelude.h>
33 #include <util/util.h>
34 
36 
37 namespace LeechCraft
38 {
39 namespace Util
40 {
41  void PreludeTest::testInvokableWithConst ()
42  {
43  const auto lambda = [] (const QString&) {};
44  static_assert (detail::IsInvokableWithConst<QString, decltype (lambda)> (), "The lambda should be invokable with const T&");
45  static_assert (detail::IsInvokableWithConst<QString&, decltype (lambda)> (), "The lambda should be invokable with const T&");
46  static_assert (detail::IsInvokableWithConst<const QString&, decltype (lambda)> (), "The lambda should be invokable with const T&");
47  static_assert (detail::IsInvokableWithConst<QString&&, decltype (lambda)> (), "The lambda should be invokable with const T&");
48  QCOMPARE (true, true);
49  }
50 
51  void PreludeTest::testInvokableWithNonConst ()
52  {
53  const auto lambda = [] (QString&) {};
54  static_assert (!detail::IsInvokableWithConst<QString, decltype (lambda)> (), "The lambda should not be invokable with const T&");
55  static_assert (!detail::IsInvokableWithConst<QString&, decltype (lambda)> (), "The lambda should not be invokable with const T&");
56  static_assert (!detail::IsInvokableWithConst<const QString&, decltype (lambda)> (), "The lambda should not be invokable with const T&");
57  static_assert (!detail::IsInvokableWithConst<QString&&, decltype (lambda)> (), "The lambda should not be invokable with const T&");
58  QCOMPARE (true, true);
59  }
60 
61  namespace
62  {
63  QMap<int, QString> GetSimpleMap ()
64  {
65  QMap<int, QString> someMap;
66  someMap [0] = "aaa";
67  someMap [1] = "bbb";
68  someMap [2] = "ccc";
69  return someMap;
70  }
71  }
72 
73  void PreludeTest::testMapList ()
74  {
75  QList<int> list { 1, 2, 3 };
76  const auto& otherList = Map (list, [] (int v) { return QString::number (v); });
77 
78  QCOMPARE (otherList, (QStringList { "1", "2", "3" }));
79  }
80 
81  void PreludeTest::testMapMap ()
82  {
83  const auto& map = GetSimpleMap ();
84  const auto& otherList = Map (map, [] (const QString& v) { return v.size (); });
85 
86  QCOMPARE (otherList, (QList<int> { 3, 3, 3 }));
87  }
88 
89  void PreludeTest::testMapMapMutatingVoid ()
90  {
91  auto map = GetSimpleMap ();
92  Map (map, [] (QString& v) { v += v [0]; });
93 
94  QCOMPARE (map, (Util::MakeMap<int, QString> ({ { 0, "aaaa" }, { 1, "bbbb" }, { 2, "cccc" }})));
95  }
96 
97 #if 0
98  namespace
99  {
100  template<typename F>
101  constexpr bool FailsImpl (typename std::result_of<F (void*)>::type*)
102  {
103  return false;
104  }
105 
106  template<typename F>
107  constexpr bool FailsImpl (...)
108  {
109  return true;
110  }
111 
112  template<typename F>
113  constexpr bool Fails ()
114  {
115  return FailsImpl<F> (0);
116  }
117  }
118 
119  void PreludeTest::testMapMapMutatingVoidConst ()
120  {
121  // TODO postponed until some later C++ version where SFINAE is transitive enough
122  // to produce a soft error in FailsImpl() overload above.
123  auto lambda = [] (const auto&)
124  {
125  const auto& map = GetSimpleMap ();
126  Map (map, [] (QString& v) { v += "a"; });
127  };
128  static_assert (Fails<decltype (lambda)> (),
129  "the code should fail");
130 
131  QCOMPARE (true, true);
132  }
133 #endif
134 
135  void PreludeTest::testMapMapNonMutatingVoid ()
136  {
137  auto map = GetSimpleMap ();
138  Map (map, [] (const QString&) {});
139 
140  QCOMPARE (map, GetSimpleMap ());
141  }
142 
143  void PreludeTest::testMapMapNonMutatingVoidConst ()
144  {
145  const auto& map = GetSimpleMap ();
146  Map (map, [] (const QString&) {});
147 
148  QCOMPARE (map, GetSimpleMap ());
149  }
150 
151  void PreludeTest::testMapMember ()
152  {
153  struct Test
154  {
155  int m_a;
156  int m_b;
157  };
158 
159  const QList<Test> tests { { 1, 2 }, { 2, 4 }, { 3, 6 } };
160  const auto& ints = Map (tests, &Test::m_a);
161 
162  QCOMPARE (ints, (QList<int> { 1, 2, 3 }));
163  }
164 
165  void PreludeTest::testMapMemberFunction ()
166  {
167  struct Test
168  {
169  int m_a;
170 
171  int GetA () const
172  {
173  return m_a;
174  }
175  };
176 
177  const QList<Test> tests { { 1 }, { 2 }, { 3 } };
178  const auto& ints = Map (tests, &Test::GetA);
179 
180  QCOMPARE (ints, (QList<int> { 1, 2, 3 }));
181  }
182 }
183 }
auto Map(const Container< T > &c, F f) -> typename std::enable_if<!std::is_same< void, decltype(Invoke(f, std::declval< T >()))>::value, WrapType_t< Container< typename std::decay< decltype(Invoke(f, std::declval< T >()))>::type >>>::type
Definition: prelude.h:117
Definition: anutil.h:38
constexpr bool IsInvokableWithConst()
Definition: prelude.h:110