forked from movetk/movetk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tree.cpp
More file actions
142 lines (120 loc) · 3.8 KB
/
Copy pathtest_tree.cpp
File metadata and controls
142 lines (120 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (C) 2018-2020 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
//
// Created by Mitra, Aniket on 09/01/2019.
//
#include "catch2/catch.hpp"
#include "movetk/ds/Tree.h"
#include "movetk/utils/Iterators.h"
using namespace std;
/*!
*
* @brief values stored by each node of the tree
*/
template <class T>
class Values{
private:
std::string id;
std::vector<T> values;
public:
typedef typename std::vector<T>::const_iterator Iterator;
Values(std::string& identifier):id(identifier){}
void operator()(T& value){
values.push_back(value);
}
std::string& operator()(){
return id;
}
Iterator begin(){
return cbegin(values);
}
Iterator end(){
return cend(values);
}
};
TEST_CASE("Create and search in a Trie", "[test_trie") {
typedef movetk_support::TrieNode<const char, Values<int> > _Node;
movetk_support::Tree<_Node> tree(std::make_unique<_Node>('1'));
std::vector< std::pair<std::string, size_t> > leaves;
std::string str = "aabc";
int value = 1;
tree.insert( begin(str), end(str), value );
//values[*leaf].push_back(1);
str = "aabda";
value = 2;
tree.insert( begin(str), end(str), value );
str = "aabda";
value = 4;
tree.insert( begin(str), end(str), value );
std::string search_str = "aabcaj";
_Node::reference result = tree.find( cbegin(search_str), cend(search_str) );
REQUIRE (tree.get_match_size() == 4);
REQUIRE ((*result)() == 'c');
REQUIRE (std::distance(result->v_begin(), result->v_end()) == 1);
auto it = result->v_begin();
REQUIRE (*it == 1);
// counts number of leaves in each branch of the tree
tree.find(movetk_core::movetk_back_insert_iterator(leaves));
REQUIRE (leaves.size() == 2);
REQUIRE ( leaves[0].first.compare("aabc") == 0 );
REQUIRE (leaves[0].second == 1);
REQUIRE ( leaves[1].first.compare("aabda") == 0 );
REQUIRE (leaves[1].second == 2);
}
//TEST_CASE("Create and search in a binary tree", "[test_btree") {
//// typedef movetk_support::BinaryNode<int, Values<char> > Node;
//// int rkey = 0;
//// movetk_support::Tree<Node> tree( std::make_unique<Node >(rkey) );
//// std::vector< std::pair<std::string, size_t> > leaves;
//// std::vector<int> input1{0,1,0,1,1};
//// char value = 'a';
//// tree.insert( begin(input1), end(input1), value );
////
//// value = 'b';
//// std::vector<int> input2{0,1,1,1,0,1};
//// tree.insert( begin(input2), end(input2), value );
//
////
//// Node::reference result = tree.find( cbegin(search_str), cend(search_str) );
////
//// REQUIRE (tree.get_match_size() == 4);
////
//// REQUIRE ((*result)() == 'c');
////
//// REQUIRE (std::distance(result->v_begin(), result->v_end()) == 1);
////
//// auto it = result->v_begin();
////
//// REQUIRE (*it == 1);
////
//// // counts number of leaves in each branch of the tree
//// tree.find(movetk_core::movetk_back_insert_iterator(leaves));
////
//// REQUIRE (leaves.size() == 2);
////
//// REQUIRE ( leaves[0].first.compare("aabc") == 0 );
////
//// REQUIRE (leaves[0].second == 1);
////
//// REQUIRE ( leaves[1].first.compare("aabda") == 0 );
////
//// REQUIRE (leaves[1].second == 2);
//
//
//}