feisty meow concerns codebase 2.140
test_span_manager.cpp
Go to the documentation of this file.
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
16#include <basis/array.h>
17#include <basis/functions.h>
18#include <basis/guards.h>
19#include <loggers/file_logger.h>
22#include <unit_test/unit_base.h>
23
24using namespace application;
25using namespace basis;
26//using namespace filesystem;
27using namespace loggers;
28//using namespace mathematics;
29using namespace sockets;
30using namespace structures;
31//using namespace textual;
32//using namespace timely;
33using 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);
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
63class test_span_manager : public virtual unit_base, public virtual application_shell
64{
65public:
66 test_span_manager() {}
67 DEFINE_CLASS_NAME("test_span_manager");
68 virtual int execute();
69};
70
71int test_span_manager::execute()
72{
73 FUNCDEF("execute");
74 span_manager fred(452);
75
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
157HOOPLE_MAIN(test_span_manager, );
158
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
int length() const
Returns the current reported length of the allocated C array.
Definition array.h:115
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
A simple object that wraps a templated array of ints.
Definition array.h:275
Manages lists of numbers representing the completion of some activity.
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
A logger that sends to the console screen using the standard output device.
Provides access to the operating system's socket methods.
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
Useful support functions for unit testing, especially within hoople.
Definition unit_base.cpp:35
#define INIT_STUFF
#define COMP(to_compare, i)
stuffer.number_of_spans = stuff_index / 2; \ old
#define SEND(send_to)
#define STUFF(a, b)
#define LOG(to_print)
#define ASSERT_EQUAL(a, b, test_name)
Definition unit_base.h:38