LeechCraft  0.6.70-15082-g543737046d
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  * 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 "stlizetest.h"
10 #include <QtTest>
11 #include <qtutil.h>
12 
13 QTEST_MAIN (LC::Util::StlizeTest)
14 
15 namespace LC
16 {
17 namespace Util
18 {
19  namespace
20  {
21  QMap<int, QString> GetSimpleMap ()
22  {
23  QMap<int, QString> someMap;
24  someMap [0] = "aaa";
25  someMap [1] = "bbb";
26  someMap [2] = "ccc";
27  return someMap;
28  }
29  }
30 
31  void StlizeTest::testConst ()
32  {
33  const auto& map = GetSimpleMap ();
34 
35  QStringList list;
36  for (const auto& pair : Util::Stlize (map))
37  list << pair.second;
38 
39  QCOMPARE (list, (QStringList { "aaa", "bbb", "ccc" }));
40  }
41 
42  void StlizeTest::testNonConst ()
43  {
44  auto map = GetSimpleMap ();
45 
46  QStringList list;
47  for (const auto& pair : Util::Stlize (map))
48  list << pair.second;
49 
50  QCOMPARE (list, (QStringList { "aaa", "bbb", "ccc" }));
51  }
52 
53  void StlizeTest::testNonConstModify ()
54  {
55  auto getMap = []
56  {
57  QMap<int, QString> someMap;
58  someMap [0] = "aaa";
59  someMap [1] = "bbb";
60  someMap [2] = "ccc";
61  return someMap;
62  };
63 
64  auto map = getMap ();
65 
66  QStringList list;
67  for (const auto& pair : Util::Stlize (map))
68  {
69  list << pair.second;
70  pair.second.clear ();
71  }
72 
73  QCOMPARE (list, (QStringList { "aaa", "bbb", "ccc" }));
74  QCOMPARE (true, (std::all_of (map.begin (), map.end (), [] (const QString& str) { return str.isEmpty (); })));
75  }
76 
77  void StlizeTest::testRvalue ()
78  {
79  QStringList list;
80  for (const auto& pair : Util::Stlize (GetSimpleMap ()))
81  list << pair.second;
82 
83  QCOMPARE (list, (QStringList { "aaa", "bbb", "ccc" }));
84  }
85 
86  void StlizeTest::testAnyOf ()
87  {
88  const auto& stlized = Util::Stlize (GetSimpleMap ());
89  const bool hasBbb = std::any_of (stlized.begin (), stlized.end (),
90  [] (const auto& pair) { return pair.second == "bbb"; });
91 
92  QCOMPARE (hasBbb, true);
93  }
94 
95  namespace
96  {
97  QMap<int, int> GetBigMap ()
98  {
99  QMap<int, int> map;
100  for (int i = 0; i < 1500000; ++i)
101  map [i] = i * 2;
102  return map;
103  }
104  }
105 
106  void StlizeTest::benchmarkPlain ()
107  {
108  const auto& map = GetBigMap ();
109  QBENCHMARK {
110  volatile int sum = 0;
111  for (auto value : map)
112  sum = sum + value;
113  }
114  }
115 
116  void StlizeTest::benchmarkStlized ()
117  {
118  const auto& map = GetBigMap ();
119  QBENCHMARK {
120  volatile int sum = 0;
121  for (const auto& pair : Util::Stlize (map))
122  sum = sum + pair.second;
123  }
124  }
125 }
126 }
127 
stlizetest.h
LC::Util::StlizeTest
Definition: stlizetest.h:29
LC::Util::Stlize
auto Stlize(Assoc &&assoc) noexcept
Converts an Qt's associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:49
qtutil.h
LC
Definition: constants.h:14
QMap
Definition: anutil.h:17