LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
stlizetest.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 "stlizetest.h"
31 #include <QtTest>
32 #include <qtutil.h>
33 
34 QTEST_MAIN (LC::Util::StlizeTest)
35 
36 namespace LC
37 {
38 namespace Util
39 {
40  namespace
41  {
42  QMap<int, QString> GetSimpleMap ()
43  {
44  QMap<int, QString> someMap;
45  someMap [0] = "aaa";
46  someMap [1] = "bbb";
47  someMap [2] = "ccc";
48  return someMap;
49  }
50  }
51 
52  void StlizeTest::testConst ()
53  {
54  const auto& map = GetSimpleMap ();
55 
56  QStringList list;
57  for (const auto& pair : Util::Stlize (map))
58  list << pair.second;
59 
60  QCOMPARE (list, (QStringList { "aaa", "bbb", "ccc" }));
61  }
62 
63  void StlizeTest::testNonConst ()
64  {
65  auto map = GetSimpleMap ();
66 
67  QStringList list;
68  for (const auto& pair : Util::Stlize (map))
69  list << pair.second;
70 
71  QCOMPARE (list, (QStringList { "aaa", "bbb", "ccc" }));
72  }
73 
74  void StlizeTest::testNonConstModify ()
75  {
76  auto getMap = []
77  {
78  QMap<int, QString> someMap;
79  someMap [0] = "aaa";
80  someMap [1] = "bbb";
81  someMap [2] = "ccc";
82  return someMap;
83  };
84 
85  auto map = getMap ();
86 
87  QStringList list;
88  for (const auto& pair : Util::Stlize (map))
89  {
90  list << pair.second;
91  pair.second.clear ();
92  }
93 
94  QCOMPARE (list, (QStringList { "aaa", "bbb", "ccc" }));
95  QCOMPARE (true, (std::all_of (map.begin (), map.end (), [] (const QString& str) { return str.isEmpty (); })));
96  }
97 
98  void StlizeTest::testRvalue ()
99  {
100  QStringList list;
101  for (const auto& pair : Util::Stlize (GetSimpleMap ()))
102  list << pair.second;
103 
104  QCOMPARE (list, (QStringList { "aaa", "bbb", "ccc" }));
105  }
106 
107  void StlizeTest::testAnyOf ()
108  {
109  const auto& stlized = Util::Stlize (GetSimpleMap ());
110  const bool hasBbb = std::any_of (stlized.begin (), stlized.end (),
111  [] (const auto& pair) { return pair.second == "bbb"; });
112 
113  QCOMPARE (hasBbb, true);
114  }
115 
116  namespace
117  {
118  QMap<int, int> GetBigMap ()
119  {
120  QMap<int, int> map;
121  for (int i = 0; i < 1500000; ++i)
122  map [i] = i * 2;
123  return map;
124  }
125  }
126 
127  void StlizeTest::benchmarkPlain ()
128  {
129  const auto& map = GetBigMap ();
130  QBENCHMARK {
131  volatile int sum = 0;
132  for (auto value : map)
133  sum = sum + value;
134  }
135  }
136 
137  void StlizeTest::benchmarkStlized ()
138  {
139  const auto& map = GetBigMap ();
140  QBENCHMARK {
141  volatile int sum = 0;
142  for (const auto& pair : Util::Stlize (map))
143  sum = sum + pair.second;
144  }
145  }
146 }
147 }
148 
auto Stlize(Assoc &&assoc)
Converts an Qt&#39;s associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:115
Definition: constants.h:35
Definition: anutil.h:38