LeechCraft  0.6.70-6645-gcd10d7e
Modular cross-platform feature rich live environment.
preludetest.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 "preludetest.h"
31 #include <QtTest>
32 #include <prelude.h>
33 #include <qtutil.h>
34 #include <util/util.h>
35 
37 
38 namespace LeechCraft
39 {
40 namespace Util
41 {
42  void PreludeTest::testInvokableWithConst ()
43  {
44  const auto lambda = [] (const QString&) {};
45  static_assert (detail::IsInvokableWithConst<QString, decltype (lambda)> (), "The lambda should be invokable with T");
46  static_assert (detail::IsInvokableWithConst<QString&, decltype (lambda)> (), "The lambda should be invokable with T&");
47  static_assert (detail::IsInvokableWithConst<const QString&, decltype (lambda)> (), "The lambda should be invokable with const T&");
48  static_assert (detail::IsInvokableWithConst<QString&&, decltype (lambda)> (), "The lambda should be invokable with T&&");
49  QCOMPARE (true, true);
50  }
51 
52  void PreludeTest::testInvokableWithNonConst ()
53  {
54  const auto lambda = [] (QString&) {};
55  static_assert (!detail::IsInvokableWithConst<QString, decltype (lambda)> (), "The lambda should not be invokable with T");
56  static_assert (!detail::IsInvokableWithConst<QString&, decltype (lambda)> (), "The lambda should not be invokable with T&");
57  static_assert (!detail::IsInvokableWithConst<const QString&, decltype (lambda)> (), "The lambda should not be invokable with const T&");
58  static_assert (!detail::IsInvokableWithConst<QString&&, decltype (lambda)> (), "The lambda should not be invokable with T&&");
59  QCOMPARE (true, true);
60  }
61 
62  namespace
63  {
64  QMap<int, QString> GetSimpleMap ()
65  {
66  QMap<int, QString> someMap;
67  someMap [0] = "aaa";
68  someMap [1] = "bbb";
69  someMap [2] = "ccc";
70  return someMap;
71  }
72  }
73 
74  void PreludeTest::testMapList ()
75  {
76  QList<int> list { 1, 2, 3 };
77  const auto& otherList = Map (list, [] (int v) { return QString::number (v); });
78 
79  QCOMPARE (otherList, (QStringList { "1", "2", "3" }));
80  }
81 
82  void PreludeTest::testMapMap ()
83  {
84  const auto& map = GetSimpleMap ();
85  const auto& otherList = Map (map, [] (const QString& v) { return v.size (); });
86 
87  QCOMPARE (otherList, (QList<int> { 3, 3, 3 }));
88  }
89 
90  void PreludeTest::testMapMapMutatingVoid ()
91  {
92  auto map = GetSimpleMap ();
93  Map (map, [] (QString& v) { v += v [0]; });
94 
95  QCOMPARE (map, (Util::MakeMap<int, QString> ({ { 0, "aaaa" }, { 1, "bbbb" }, { 2, "cccc" }})));
96  }
97 
98 #if 0
99  namespace
100  {
101  template<typename F>
102  constexpr bool FailsImpl (typename std::result_of<F (void*)>::type*)
103  {
104  return false;
105  }
106 
107  template<typename F>
108  constexpr bool FailsImpl (...)
109  {
110  return true;
111  }
112 
113  template<typename F>
114  constexpr bool Fails ()
115  {
116  return FailsImpl<F> (0);
117  }
118  }
119 
120  void PreludeTest::testMapMapMutatingVoidConst ()
121  {
122  // TODO postponed until some later C++ version where SFINAE is transitive enough
123  // to produce a soft error in FailsImpl() overload above.
124  auto lambda = [] (const auto&)
125  {
126  const auto& map = GetSimpleMap ();
127  Map (map, [] (QString& v) { v += "a"; });
128  };
129  static_assert (Fails<decltype (lambda)> (),
130  "the code should fail");
131 
132  QCOMPARE (true, true);
133  }
134 #endif
135 
136  void PreludeTest::testMapMapNonMutatingVoid ()
137  {
138  auto map = GetSimpleMap ();
139  Map (map, [] (const QString&) {});
140 
141  QCOMPARE (map, GetSimpleMap ());
142  }
143 
144  void PreludeTest::testMapMapNonMutatingVoidConst ()
145  {
146  const auto& map = GetSimpleMap ();
147  Map (map, [] (const QString&) {});
148 
149  QCOMPARE (map, GetSimpleMap ());
150  }
151 
152  void PreludeTest::testMapStringList ()
153  {
154  const QStringList list { "aaa", "bbb", "ccc" };
155  const auto& result = Map (list, [] (const QString& s) { return s.size (); });
156 
157  QCOMPARE (result, (QList<int> { 3, 3, 3 }));
158  }
159 
160  void PreludeTest::testMapMapStlized ()
161  {
162  const auto& map = GetSimpleMap ();
163  const auto& list = Map (Stlize (map), [] (const std::pair<int, QString>& pair) { return pair.second; });
164 
165  QCOMPARE (list, QStringList { map.values () });
166  }
167 
168  void PreludeTest::testMapMember ()
169  {
170  struct Test
171  {
172  int m_a;
173  int m_b;
174  };
175 
176  const QList<Test> tests { { 1, 2 }, { 2, 4 }, { 3, 6 } };
177  const auto& ints = Map (tests, &Test::m_a);
178 
179  QCOMPARE (ints, (QList<int> { 1, 2, 3 }));
180  }
181 
182  void PreludeTest::testMapMemberFunction ()
183  {
184  struct Test
185  {
186  int m_a;
187 
188  int GetA () const
189  {
190  return m_a;
191  }
192  };
193 
194  const QList<Test> tests { { 1 }, { 2 }, { 3 } };
195  const auto& ints = Map (tests, &Test::GetA);
196 
197  QCOMPARE (ints, (QList<int> { 1, 2, 3 }));
198  }
199 }
200 }
auto Stlize(Assoc &&assoc) -> detail::StlAssocRange< decltype(assoc.begin()), Assoc, PairType >
Converts an Qt&#39;s associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:126
auto Map(const Container< T > &c, F f) -> typename std::enable_if<!std::is_same< void, decltype(Invoke(f,*c.begin()))>::value, WrapType_t< Container< typename std::decay< decltype(Invoke(f,*c.begin()))>::type >>>::type
Definition: prelude.h:129
Definition: anutil.h:38
constexpr bool IsInvokableWithConst()
Definition: prelude.h:110