chunkedseq
container library for large in-memory data sets
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_seq.hpp
Go to the documentation of this file.
1 
13 #include "cmdline.hpp"
14 
15 
16 #ifndef _PASL_TEST_SEQ_H_
17 #define _PASL_TEST_SEQ_H_
18 
19 template <class Seq, class Item, class ItemGenerator = Item>
20 class TestSeq {
21 public:
22 
23  /*---------------------------------------------------*/
24  // Auxiliary function for iterated push
25 
26  static void push(Seq& s, int nb, int offset) {
27  for (int i = 0; i < nb; i++) {
28  int j = offset+i;
29  Item x = ItemGenerator::from_int(j);
30  s.push_back(x);
31  }
32  }
33 
34  /*---------------------------------------------------*/
35  // Auxiliary function for printing
36 
37  static void print_seq(Seq& s) {
38  s.template print<ItemGenerator>();
39  printf("\n");
40  }
41 
42  /*---------------------------------------------------*/
43 
44  enum fifo_or_lifo { lifo, fifo };
45 
46  template<fifo_or_lifo mode>
47  static void test_pushpop() {
48  size_t nb = (size_t) pasl::util::cmdline::parse_or_default_int64("nb", 35);
49  Seq s;
50  printf("------------\nStarting\n");
51  for (int i = 0; i < nb; i++) {
52  printf("-------------\nPushing front %d\n", i);
53  s.push_front(ItemGenerator::from_int(i));
54  print_seq(s);
55  s.check();
56  }
57  std::cout << "-----" << std::endl;
58  std::cout << "size=" << s.size() << std::endl;
59  print_seq(s);
60  Seq xxx = s;
61  print_seq(xxx);
62  std::cout << "-----" << std::endl;
63  for (int i = nb-1; i >= 0; i--) {
64  print_seq(s);
65  printf("-------------\nPopping %s %d\n", ((lifo) ? "front" : "back"), i);
66  Item r;
67  if (mode == lifo)
68  r = s.pop_front();
69  else
70  r = s.pop_back();
71  printf(" =%d\n", ItemGenerator::to_int(r));
72  ItemGenerator::free(r);
73  s.check();
74  }
75  }
76 
77  /*---------------------------------------------------*/
78 
79  static void test_concat() {
80  Seq s;
81  Seq t;
82  size_t nbs = (size_t) pasl::util::cmdline::parse_or_default_int64("nbs", 2);
83  size_t nbt = (size_t) pasl::util::cmdline::parse_or_default_int64("nbt", 5);
84  push(s, nbs, 0);
85  print_seq(s);
86  push(t, nbt, nbs);
87  print_seq(t);
88  s.concat(t);
89  printf("ok\n");
90  print_seq(s);
91  printf("ok\n");
92  s.check();
93  }
94 
95  /*---------------------------------------------------*/
96 
97  static void test_split() {
98  Seq s;
99  size_t nb = (size_t) pasl::util::cmdline::parse_or_default_int64("nb", 32);
100  for (size_t i = 0; i <= nb; i++) {
101  printf("======= Splitting at %lu ======\n", i);
102  Seq t;
103  Seq u;
104  //Seq::debug_alloc();
105  push(t, nb, 0);
106  print_seq(t);
107  //Seq::debug_alloc();
108  t.split(i, u);
109  int sz = t.size();
110  //Seq::debug_alloc();
111  print_seq(t);
112  print_seq(u);
113  assert(sz == i);
114  }
115  }
116 
117  /*---------------------------------------------------*/
118  /*
119  static void test_split_index() {
120  Seq s;
121  size_t nb = (size_t) pasl::util::cmdline::parse_or_default_int64("nb", 32);
122  size_t pos = (size_t) pasl::util::cmdline::parse_or_default_int64("pos", 9);
123  for (int i = 0; i < nb; i++) {
124  Seq t;
125  Seq u;
126  //Seq::debug_alloc();
127  push(t, nb, 0);
128  print_seq(t);
129  //Seq::debug_alloc();
130  t.split(pos, u);
131  //Seq::debug_alloc();
132  print_seq(t);
133  print_seq(u);
134  printf("=============\n");
135  }
136  }
137  */
138 
139  /*---------------------------------------------------*/
140 
141  /*
142  static void test_split_weight() {
143  Seq s;
144  int nb = 32;
145  size_t pos = 9;
146  for (int i = 0; i < nb; i++) {
147  Seq t;
148  Seq u;
149  //Seq::debug_alloc();
150  push(t, nb, 0);
151  print_seq(t);
152  //Seq::debug_alloc();
153  t.split_by_index(u, pos);
154  //Seq::debug_alloc();
155  print_seq(t);
156  printf("=============\n= %d\n", u.pop_front());
157  print_seq(u);
158  printf("=============\n");
159  }
160  */
161 
162  /*---------------------------------------------------*/
163 
164  // requires commenting out BOOTCHUNKSEQ_TEST if nb is large (e.g. 125)
165 
166  static void test_split_concat() {
167  size_t nb = (size_t) pasl::util::cmdline::parse_or_default_int64("nb", 15);
168  for (int i = 1; i < nb; i++) {
169  printf("%d\n", i);
170  Seq t;
171  Seq u;
172  push(t, i, 0);
173  for (size_t pos = 0; pos < i; pos++) {
174  printf("*****========Splitting %lu=====*****\n", pos);
175  t.split(pos, u);
176  // print_seq(t);
177  // print_seq(u);
178  printf("*****========Concatenating %lu=====*****\n", pos);
179  t.concat(u);
180  // print_seq(t);
181  assert(t.size() == i);
182  t.check();
183  }
184  // print_seq(t);
185  }
186  }
187 
188  /*---------------------------------------------------*/
189 
190  using thunk_t = std::function<void ()>;
191 
192  static void execute_test() {
193  pasl::util::cmdline::argmap<thunk_t> c;
194  c.add("push_pop_lifo", [&]() { test_pushpop<lifo>(); });
195  c.add("push_pop_fifo", [&]() { test_pushpop<fifo>(); });
196  c.add("concat", [&]() { test_concat(); });
197  c.add("split", [&]() { test_split(); });
198  c.add("split_concat", [&]() { test_split_concat(); });
199  pasl::util::cmdline::dispatch_by_argmap_with_default_all(c, "only");
200 
201  }
202 
203  /*---------------------------------------------------*/
204 
205 }; // end class
206 
207 #endif
static void test_pushpop()
Definition: test_seq.hpp:47
static void test_concat()
Definition: test_seq.hpp:79
static void test_split()
Definition: test_seq.hpp:97
static void print_seq(Seq &s)
Definition: test_seq.hpp:37
static void execute_test()
Definition: test_seq.hpp:192
static void test_split_concat()
Definition: test_seq.hpp:166
fifo_or_lifo
Definition: test_seq.hpp:44
std::function< void()> thunk_t
Definition: test_seq.hpp:190
static void push(Seq &s, int nb, int offset)
Definition: test_seq.hpp:26
bytes_8 Item
Definition: do_fifo.cpp:107