getting these octopus tests updated. not quite there yet.
[feisty_meow.git] / octopi / library / tests_octopus / t_bin.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : entity_data_bin tester                                            *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *  Purpose:                                                                   *
7 *                                                                             *
8 *    Checks that the entity_data_bin class is behaving as expected.           *
9 *                                                                             *
10 *******************************************************************************
11 * Copyright (c) 2002-$now By Author.  This program is free software; you can  *
12 * redistribute it and/or modify it under the terms of the GNU General Public  *
13 * License as published by the Free Software Foundation; either version 2 of   *
14 * the License or (at your option) any later version.  This is online at:      *
15 *     http://www.fsf.org/copyleft/gpl.html                                    *
16 * Please send any updates to: fred@gruntose.com                               *
17 \*****************************************************************************/
18
19 #include <application/hoople_main.h>
20 #include <basis/byte_array.h>
21 #include <mathematics/chaos.h>
22 #include <basis/functions.h>
23 #include <basis/guards.h>
24 #include <basis/astring.h>
25 #include <application/application_shell.h>
26 #include <loggers/console_logger.h>
27 #include <loggers/program_wide_logger.h>
28 #include <structures/static_memory_gremlin.h>
29 #include <octopus/entity_data_bin.h>
30 #include <octopus/entity_defs.h>
31 #include <tentacles/security_infoton.h>
32 #include <textual/string_manipulation.h>
33
34 #include <stdio.h>
35
36 using namespace application;
37 using namespace basis;
38 using namespace loggers;
39 using namespace octopi;
40 using namespace textual;
41
42 const int ITEM_COUNT = 10000;
43   // the number of times to repeat each test operation.
44
45 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger().get(), astring(s))
46 #define BASE_LOG(s) EMERGENCY_LOG(program_wide_logger().get(), astring(s))
47
48 class test_bin : public application_shell
49 {
50 public:
51   test_bin() : application_shell() {}
52   DEFINE_CLASS_NAME("test_bin");
53   int execute();
54 };
55
56 //////////////
57   
58 int test_bin::execute()
59 {
60   FUNCDEF("execute");
61   char c = '\0';
62
63   array<octopus_request_id> item_list;
64
65   entity_data_bin *bing = new entity_data_bin(10 * MEGABYTE);
66
67   enum test_types { ANY = 1, ENT, ID };
68
69   for (int q = ANY; q <= ID; q++) {
70 LOG(a_sprintf("test type %d beginning...%c", q, c));
71   // using c just shuts up warnings.
72 //LOG("note memory usage and hit a key:");
73 //c = getchar();
74
75     program_wide_logger().get().eol(parser_bits::NO_ENDING);
76     for (int i = 1; i <= ITEM_COUNT; i++) {
77       // test the basic filling of the values in an entity.
78       octopus_request_id req_id;
79       int sequencer = randomizer().inclusive(1, MAXINT32 - 10);
80       int add_in = randomizer().inclusive(0, MAXINT32 - 10);
81       int process_id = randomizer().inclusive(0, MAXINT32 - 10);
82       req_id._entity = octopus_entity(string_manipulation::make_random_name(),
83           process_id, sequencer, add_in);
84       req_id._request_num = randomizer().inclusive(1, MAXINT32 - 10);
85       infoton *torp = new security_infoton;
86       bing->add_item(torp, req_id);
87       item_list += req_id;
88
89       if (! (i % 50) ) {
90         printf("^");
91         fflush(NIL);
92       }
93     }
94     program_wide_logger().get().eol(parser_bits::CRLF_AT_END);
95     LOG("");
96
97     int items_seen = 0;
98
99     program_wide_logger().get().eol(parser_bits::NO_ENDING);
100     if (q == ANY) {
101       while (item_list.length()) {
102         octopus_request_id id;
103         infoton *found = bing->acquire_for_any(id);
104         if (!found) 
105           deadly_error(class_name(), "ANY", "item was missing");
106         WHACK(found);
107         items_seen++;
108         if (! (items_seen % 50) ) {
109           printf("v");
110           fflush(NIL);
111         }
112         bool saw_it = false;
113         for (int q = 0; q < item_list.length(); q++) {
114           if (item_list[q] == id) {
115             saw_it = true;
116             item_list.zap(q, q);
117             break;
118           }
119         }
120         if (!saw_it)
121           deadly_error(class_name(), "ANY", "didn't see id for the item");
122       }
123     } else if (q == ENT) {
124       while (item_list.length()) {
125         octopus_request_id id;
126         infoton *found = bing->acquire_for_entity(item_list[0]._entity, id);
127         if (!found) 
128           deadly_error(class_name(), "ENT", "item was missing");
129         WHACK(found);
130         items_seen++;
131         if (! (items_seen % 50) ) {
132           printf("v");
133           fflush(NIL);
134         }
135         bool saw_it = false;
136         for (int q = 0; q < item_list.length(); q++) {
137           if (item_list[q] == id) {
138             saw_it = true;
139             item_list.zap(q, q);
140             break;
141           }
142         }
143         if (!saw_it)
144           deadly_error(class_name(), "ENT", "didn't see id for the item");
145       }
146     } else if (q == ID) {
147       for (int j = 0; j < item_list.length(); j++) {
148         infoton *found = bing->acquire_for_identifier(item_list[j]);
149         if (!found) 
150           deadly_error(class_name(), "ENT", "item was missing");
151         WHACK(found);
152         items_seen++;
153         if (! (items_seen % 50) ) {
154           printf("v");
155           fflush(NIL);
156         }
157         item_list.zap(j, j);
158         j--;  // skip back.
159       }
160     } else {
161       deadly_error(class_name(), "looping", "bad enum value");
162     }
163     program_wide_logger().get().eol(parser_bits::CRLF_AT_END);
164     LOG("");
165     item_list.reset();
166     item_list.shrink();
167
168     if (bing->entities())
169       deadly_error(class_name(), "check left", "there are still contents in table!");
170
171     bing->clean_out_deadwood();
172
173 LOG(a_sprintf("test type %d ending...", q));
174 //LOG("note memory usage and hit a key:");
175 //c = getchar();
176   }
177
178   WHACK(bing);
179 LOG("done testing, zapped bin, now should be low memory.");
180 //c = getchar();
181
182   LOG("octopus_entity:: works for those functions tested.");
183   return 0;
184 }
185
186 HOOPLE_MAIN(test_bin, )
187