source: tags/before-XString-Modif/XXX/include/tree/tree_test.cc @ 895

Last change on this file since 895 was 834, checked in by garnier, 16 years ago

import all except CVS

File size: 3.9 KB
Line 
1/*
2Copyright (c) 2003-2006, Troy Aaron Johnson
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
9  * Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14  * Neither the name of Troy Aaron Johnson nor the names of any
15    contributors may be used to endorse or promote products derived from
16    this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28POSSIBILITY OF SUCH DAMAGE.
29*/
30
31#include <algorithm>
32#include <cassert>
33#include <iostream>
34#include <list>
35#include <string>
36
37#include "tree.h"
38
39using namespace taj;
40
41bool pruning_predicate(const tree<std::string>::iterator& node)
42{
43  return (*node == "B");
44}
45
46template <class TreeType>
47void print(const tree<TreeType>& t)
48{
49  std::cout << "preorder traversal: " << std::endl;
50  for (typename tree<TreeType>::const_preorder_iterator i = t.beginPre(); i != t.endPre(); ++i)
51  {
52    std::cout << *i << std::endl;
53  }
54  std::cout << std::endl;
55
56  std::cout << "postorder traversal: " << std::endl;
57  for (typename tree<TreeType>::const_postorder_iterator i = t.beginPost(); i != t.endPost(); ++i)
58  {
59    std::cout << *i << std::endl;
60  }
61  std::cout << std::endl;
62
63  std::cout << "bfs traversal: " << std::endl;
64  for (typename tree<TreeType>::const_bfs_iterator i = t.beginBfs(); i != i.endBfs(); ++i)
65  {
66    std::cout << *i << std::endl;
67  }
68  std::cout << std::endl;
69}
70
71void pointer_specialization_test(void)
72{
73  tree<int*> pint_tree;
74
75  int x = 1, y = 2, z = 3;
76
77  tree<int*>::iterator i(pint_tree.setRoot(&x));
78
79  i.push_back(&y);
80  i.push_back(&z);
81
82  std::cout << "int* tree root is " << **i << std::endl;
83
84  tree<int*>::sibling_iterator si(i.beginChildren());
85
86  tree<char*> t1;
87  tree<long*> t2;
88  tree<float*> t3;
89  tree<double*> t4;
90  tree<std::string*> t5;
91  tree<unsigned char*> t6;
92  tree<unsigned long*> t7;
93}
94
95int main(int argc, char* argv[])
96{
97  tree<std::string> t;
98
99  assert(t.empty());
100  assert(t.size() == 0);
101
102  tree<std::string>::sibling_iterator i(t.setRoot("root"));
103
104  assert(!t.empty());
105  assert(t.size() == 1);
106  assert(*i == "root");
107
108  *i = "new root";
109
110  assert(*i == "new root");
111
112  tree<std::string>::sibling_iterator j(i.push_back("left"));
113  assert(t.size() == 2);
114  tree<std::string>::sibling_iterator k(i.push_back("right"));
115  assert(t.size() == 3);
116
117  assert(*j == "left");
118  assert(*k == "right");
119
120  j.push_back("A");
121  j.push_back("B");
122
123  k.push_back("C");
124  k.push_back("D");
125
126  assert(find(t.beginPre(), t.endPre(), "C") != t.endPre());
127  assert(find(t.beginPre(), t.endPre(), "Z") == t.endPre());
128
129  std::cout << "tree height is " << t.height() << std::endl;
130
131  print(t);
132
133  tree<std::string> copy_of_t(t);
134
135  print(copy_of_t);
136
137  std::list<int> int_list;
138  for (int i = 1; i < 20; ++i)
139    int_list.push_back(i);
140
141  tree<int> int_tree(int_list.begin(), int_list.end(), 2);
142
143  print(int_tree);
144
145  pointer_specialization_test();
146}
Note: See TracBrowser for help on using the repository browser.