1 /*****************************************************************************\
3 * Name : test_span_manager *
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 1991-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
15 #include <application/hoople_main.h>
16 #include <basis/array.h>
17 #include <basis/functions.h>
18 #include <basis/guards.h>
19 #include <loggers/file_logger.h>
20 #include <sockets/span_manager.h>
21 #include <structures/static_memory_gremlin.h>
22 #include <unit_test/unit_base.h>
24 using namespace application;
25 using namespace basis;
26 //using namespace filesystem;
27 using namespace loggers;
28 //using namespace mathematics;
29 using namespace sockets;
30 using namespace structures;
31 //using namespace textual;
32 //using namespace timely;
33 using namespace unit_test;
35 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
37 #define DEBUG_SPAN_MANAGER
38 // uncomment for noisier output.
42 // INIT_STUFF gets the macros ready for use.
43 #define INIT_STUFF int_array stuffer;
45 // STUFF puts two numbers (a span) into the next place in the storage array.
46 #define STUFF(a, b) { \
47 ASSERT_FALSE(stuffer.length() / 2 + 1 > MAX_SPANS, "STUFF: too many for spans"); \
48 stuffer.concatenate(a); stuffer.concatenate(b); \
51 // SEND updates the span manager with the current state of the storage array and resets the
52 // counter for the next set of spans.
53 #define SEND(send_to) \
54 send_to.update(stuffer); stuffer.reset(0);
55 /// stuffer.number_of_spans = stuff_index / 2; \ old
57 // COMP compares the list of spans at position "i" with a value "to_compare".
58 #define COMP(to_compare, i) { \
59 ASSERT_EQUAL(to_compare, rec_list[i], "comparison found incorrect"); \
60 if (to_compare != rec_list[i]) LOG(a_sprintf("failed at index %d", i)); \
63 class test_span_manager : public virtual unit_base, public virtual application_shell
66 test_span_manager() {}
67 DEFINE_CLASS_NAME("test_span_manager");
68 virtual int execute();
71 int test_span_manager::execute()
74 span_manager fred(452);
78 // stuffs a bunch of spans into the storage array.
89 // stuffs the rest few in.
94 #ifdef DEBUG_SPAN_MANAGER
95 LOG(astring(astring::SPRINTF, "received sequence so far is %d",
96 fred.received_sequence()));
97 LOG("making received list:");
101 fred.make_received_list(rec_list);
103 #ifdef DEBUG_SPAN_MANAGER
104 LOG(fred.print_received_list());
107 // the loop goes through the list of received spans and sees if they're
108 // in the right places.
109 for (int i = 0; i < rec_list.length(); i += 2) {
111 case 0: COMP(3, i); COMP(6, i+1); break;
112 case 2: COMP(8, i); COMP(8, i+1); break;
113 case 4: COMP(13, i); COMP(17, i+1); break;
114 case 6: COMP(27, i); COMP(31, i+1); break;
115 case 8: COMP(80, i); COMP(83, i+1); break;
116 case 10: COMP(96, i); COMP(123, i+1); break;
117 case 12: COMP(151, i); COMP(430, i+1); break;
121 ASSERT_EQUAL(fred.missing_sequence(), 0, "missing: should not have wrong sequence");
123 // some more items are stuffed in, including the beginning few.
128 ASSERT_EQUAL(fred.missing_sequence(), 18, "missing 2: should not have wrong sequence");
129 #ifdef DEBUG_SPAN_MANAGER
130 LOG("here are the missing ones now:");
131 LOG(fred.print_missing_list());
133 fred.make_missing_list(rec_list);
134 // this loop looks into the list of missing places and makes sure they're
136 for (int j = 0; j < rec_list.length(); j+=2) {
138 case 0: COMP(18, j); COMP(26, j+1); break;
139 case 2: COMP(32, j); COMP(79, j+1); break;
140 case 4: COMP(84, j); COMP(95, j+1); break;
141 case 6: COMP(124, j); COMP(150, j+1); break;
142 case 8: COMP(431, j); COMP(451, j+1); break;
148 #ifdef DEBUG_SPAN_MANAGER
149 LOG("should be filled out now:");
150 LOG(fred.print_received_list());
152 ASSERT_EQUAL(fred.received_sequence(), 451, "received sequence should be filled out");
154 return final_report();
157 HOOPLE_MAIN(test_span_manager, );