LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
stringpathtrietest.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 "stringpathtrietest.h"
10 #include <QtTest>
11 #include <util/sll/prelude.h>
12 #include <stringpathtrie.h>
13 
14 QTEST_APPLESS_MAIN (LC::Util::StringPathTrieTest)
15 
16 namespace LC::Util
17 {
18  using IntTrie = StringPathTrie<int>;
19 
21 
22  QVector<QStringView> AsRefs (const QVector<QString>& lst)
23  {
24  return Util::Map (lst, [] (const auto& str) { return QStringView { str }; });
25  }
26 
27  void StringPathTrieTest::testEmptyTrie ()
28  {
29  IntTrie trie;
30 
31  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar", "baz" })), (FindResult { std::optional<int> {}, 3 }));
32  }
33 
34  void StringPathTrieTest::testEmptyQuery ()
35  {
36  IntTrie trie;
37  trie.Add (AsRefs ({ "foo", "bar", "baz" }), 10);
38 
39  QCOMPARE (trie.Find (AsRefs ({})), FindResult { std::optional<int> {} });
40  }
41 
42  void StringPathTrieTest::testExactMatchSingle ()
43  {
44  IntTrie trie;
45  trie.Add (AsRefs ({ "foo", "bar", "baz" }), 10);
46 
47  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar", "baz" })), FindResult { std::optional<int> { 10 } });
48  }
49 
50  void StringPathTrieTest::testExactMatchOverwriteSingle ()
51  {
52  IntTrie trie;
53  trie.Add (AsRefs ({ "foo", "bar", "baz" }), 10);
54  trie.Add (AsRefs ({ "foo", "bar", "baz" }), 20);
55 
56  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar", "baz" })), FindResult { std::optional<int> { 20 } });
57  }
58 
59  void StringPathTrieTest::testExactMatchMulti ()
60  {
61  IntTrie trie;
62  trie.Add (AsRefs ({ "foo", "bar", "baz1" }), 10);
63  trie.Add (AsRefs ({ "foo", "bar", "baz2" }), 20);
64 
65  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar", "baz1" })), FindResult { std::optional<int> { 10 } });
66  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar", "baz2" })), FindResult { std::optional<int> { 20 } });
67  }
68 
69  void StringPathTrieTest::testExactMatchParentPre ()
70  {
71  IntTrie trie;
72  trie.Add (AsRefs ({ "foo", "bar" }), 10);
73  trie.Add (AsRefs ({ "foo", "bar", "baz" }), 20);
74 
75  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar" })), FindResult { std::optional<int> { 10 } });
76  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar", "baz" })), FindResult { std::optional<int> { 20 } });
77  }
78 
79  void StringPathTrieTest::testExactMatchParentPost ()
80  {
81  IntTrie trie;
82  trie.Add (AsRefs ({ "foo", "bar", "baz" }), 20);
83  trie.Add (AsRefs ({ "foo", "bar" }), 10);
84 
85  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar" })), FindResult { std::optional<int> { 10 } });
86  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar", "baz" })), FindResult { std::optional<int> { 20 } });
87  }
88 
89  void StringPathTrieTest::testPartialMatchLongerQuery ()
90  {
91  IntTrie trie;
92  trie.Add (AsRefs ({ "foo" }), 20);
93  trie.Add (AsRefs ({ "foo", "bar" }), 10);
94 
95  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar", "baz" })), (FindResult { std::optional<int> { 10 }, 1 }));
96  }
97 
98  void StringPathTrieTest::testPartialMatchLongerQueryWithChildren ()
99  {
100  IntTrie trie;
101  trie.Add (AsRefs ({ "foo" }), 20);
102  trie.Add (AsRefs ({ "foo", "bar" }), 10);
103  trie.Add (AsRefs ({ "foo", "bar", "c1" }), 30);
104 
105  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar", "baz" })), (FindResult { std::optional<int> { 10 }, 1 }));
106 
107  trie.Add (AsRefs ({ "foo", "bar", "c2" }), 30);
108 
109  QCOMPARE (trie.Find (AsRefs ({ "foo", "bar", "baz" })), (FindResult { std::optional<int> { 10 }, 1 }));
110  }
111 
112  void StringPathTrieTest::testPartialMatchShorterQuery ()
113  {
114  IntTrie trie;
115  trie.Add (AsRefs ({ "foo", "bar" }), 20);
116  trie.Add (AsRefs ({ "foo", "bar", "baz" }), 10);
117 
118  QCOMPARE (trie.Find (AsRefs ({ "foo" })).Value_, std::optional<int> {});
119  }
120 }
LC::Util::AsRefs
QVector< QStringView > AsRefs(const QVector< QString > &lst)
Definition: stringpathtrietest.cpp:28
LC::Util::FindResult
IntTrie::FindResult FindResult
Definition: stringpathtrietest.cpp:26
LC::Util
Definition: icoreproxy.h:33
LC::Util::Map
auto Map(Container &&c, F &&f) noexcept(noexcept(std::is_nothrow_invocable_v< F, decltype(*c.begin())>))
Definition: prelude.h:149
stringpathtrie.h
Value_
const QVariant Value_
Definition: plotitem.cpp:80
LC::Util::StringPathTrie::Find
FindResult Find(QStringView single) const
Definition: stringpathtrie.h:87
prelude.h
stringpathtrietest.h
LC::Util::StringPathTrie
Definition: stringpathtrie.h:30
LC::Util::StringPathTrieTest
Definition: stringpathtrietest.h:21
LC::Util::IntTrie
StringPathTrie< int > IntTrie
Definition: stringpathtrietest.cpp:24
LC::Util::StringPathTrie::FindResult
Definition: stringpathtrie.h:73