first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / octopi / library / tests_sockets / test_span_manager.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : test_span_manager                                                 *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
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 \*****************************************************************************/
14
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>
23
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;
34
35 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
36
37 #define DEBUG_SPAN_MANAGER
38   // uncomment for noisier output.
39
40 #define MAX_SPANS 8
41
42 // INIT_STUFF gets the macros ready for use.
43 #define INIT_STUFF int_array stuffer;
44
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); \
49 }
50
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
56
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)); \
61 }
62
63 class test_span_manager : public virtual unit_base, public virtual application_shell
64 {
65 public:
66   test_span_manager() {}
67   DEFINE_CLASS_NAME("test_span_manager");
68   virtual int execute();
69 };
70
71 int test_span_manager::execute()
72 {
73   FUNCDEF("execute");
74   span_manager fred(452);
75
76   INIT_STUFF;
77
78   // stuffs a bunch of spans into the storage array.
79   STUFF(8, 8);
80   STUFF(27, 29);
81   STUFF(28, 31);
82   STUFF(3, 5);
83   STUFF(80, 83);
84   STUFF(96, 123);
85   STUFF(3, 6);
86   STUFF(212, 430);
87   SEND(fred);
88   
89   // stuffs the rest few in.
90   STUFF(13, 17);
91   STUFF(151, 250);
92   SEND(fred);
93
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:");
98 #endif
99
100   int_array rec_list;
101   fred.make_received_list(rec_list);
102
103 #ifdef DEBUG_SPAN_MANAGER
104   LOG(fred.print_received_list());
105 #endif
106
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) {
110     switch (i) {
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;
118     }
119   }
120
121   ASSERT_EQUAL(fred.missing_sequence(), 0, "missing: should not have wrong sequence");
122
123   // some more items are stuffed in, including the beginning few.
124   STUFF(0, 5);
125   STUFF(7, 7);
126   STUFF(9, 12);
127   SEND(fred);
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());
132 #endif
133   fred.make_missing_list(rec_list);
134   // this loop looks into the list of missing places and makes sure they're
135   // the right holes.
136   for (int j = 0; j < rec_list.length(); j+=2) {
137     switch (j) {
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;
143     }
144   }
145
146   STUFF(0, 451);
147   SEND(fred);
148 #ifdef DEBUG_SPAN_MANAGER
149   LOG("should be filled out now:");
150   LOG(fred.print_received_list());
151 #endif
152   ASSERT_EQUAL(fred.received_sequence(), 451, "received sequence should be filled out");
153
154   return final_report();
155 }
156
157 HOOPLE_MAIN(test_span_manager, );
158