LeechCraft 0.6.70-17335-ge406ffdcaf
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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 * 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 "preludetest.h"
10#include <QtTest>
11#include <prelude.h>
12#include <qtutil.h>
13
14QTEST_MAIN (LC::Util::PreludeTest)
15
16namespace LC::Util
17{
18 namespace
19 {
20 QMap<int, QString> GetSimpleMap ()
21 {
22 return { { 0, "aaa" }, { 1, "bbb" }, { 2, "ccc" }};
23 }
24 }
25
26 void PreludeTest::testMapList ()
27 {
28 QList<int> list { 1, 2, 3 };
29 const auto& otherList = Map (list, [] (int v) { return QString::number (v); });
30
31 QCOMPARE (otherList, (QStringList { "1", "2", "3" }));
32 }
33
34 using SizeType = decltype (QString {}.size ());
35
36 void PreludeTest::testMapMap ()
37 {
38 const auto& map = GetSimpleMap ();
39 const auto& otherList = Map (map, [] (const QString& v) { return v.size (); });
40
41 QCOMPARE (otherList, (QList<SizeType> { 3, 3, 3 }));
42 }
43
44 void PreludeTest::testMapStringList ()
45 {
46 const QStringList list { "aaa", "bbb", "ccc" };
47 const auto& result = Map (list, [] (const QString& s) { return s.size (); });
48
49 QCOMPARE (result, (QList<SizeType> { 3, 3, 3 }));
50 }
51
52 void PreludeTest::testMapMapStlized ()
53 {
54 const auto& map = GetSimpleMap ();
55 const auto& list = Map (Stlize (map), [] (const std::pair<int, QString>& pair) { return pair.second; });
56
57 QCOMPARE (list, QStringList { map.values () });
58 }
59
60 void PreludeTest::testMapMember ()
61 {
62 struct Test
63 {
64 int m_a;
65 int m_b;
66 };
67
68 const QList<Test> tests { { 1, 2 }, { 2, 4 }, { 3, 6 } };
69 const auto& ints = Map (tests, &Test::m_a);
70
71 QCOMPARE (ints, (QList<int> { 1, 2, 3 }));
72 }
73
74 void PreludeTest::testMapMemberFunction ()
75 {
76 struct Test
77 {
78 int m_a;
79
80 int GetA () const
81 {
82 return m_a;
83 }
84 };
85
86 const QList<Test> tests { { 1 }, { 2 }, { 3 } };
87 const auto& ints = Map (tests, &Test::GetA);
88
89 QCOMPARE (ints, (QList<int> { 1, 2, 3 }));
90 }
91
92 void PreludeTest::testConcatLists ()
93 {
94 QList<QList<int>> listOfLists
95 {
96 { 1, 2 },
97 { 3 },
98 { 4, 5, 6 }
99 };
100
101 const auto& concat = Concat (listOfLists);
102 QCOMPARE (concat, (QList<int> { 1, 2, 3, 4, 5, 6 }));
103 }
104
105 void PreludeTest::testConcatSets ()
106 {
107 QList<QSet<int>> listOfSets
108 {
109 { 1, 2, 3 },
110 { 3, 4 },
111 { 4, 5, 6 }
112 };
113
114 const auto& concat = Concat (listOfSets);
115 QCOMPARE (concat, (QSet<int> { 1, 2, 3, 4, 5, 6 }));
116 }
117}
auto Stlize(Assoc &&assoc) noexcept
Converts an Qt's associative sequence assoc to an STL-like iteratable range.
Definition qtutil.h:48
Container< T > Concat(const Container< Container< T > > &containers)
Definition prelude.h:128
auto Map(Container &&c, F &&f) noexcept(noexcept(std::is_nothrow_invocable_v< F, decltype(*c.begin())>))
Definition prelude.h:104
decltype(QString {}.size()) SizeType