first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / octopi / library / tests_sockets / test_sequence_tracker.cpp
1 /*
2 *  Name   : test_sequence_tracker
3 *  Author : Chris Koeritz
4 *  Purpose: Runs a couple of tests on the sequence tracker object.
5 **
6 * Copyright (c) 2003-$now By Author.  This program is free software; you can  *
7 * redistribute it and/or modify it under the terms of the GNU General Public  *
8 * License as published by the Free Software Foundation; either version 2 of   *
9 * the License or (at your option) any later version.  This is online at:      *
10 *     http://www.fsf.org/copyleft/gpl.html                                    *
11 * Please send any updates to: fred@gruntose.com                               *
12 */
13
14 #include <application/hoople_main.h>
15 #include <basis/byte_array.h>
16 #include <basis/astring.h>
17 #include <loggers/program_wide_logger.h>
18 #include <mathematics/chaos.h>
19 #include <sockets/machine_uid.h>
20 #include <sockets/sequence_tracker.h>
21 #include <structures/set.h>
22 #include <structures/static_memory_gremlin.h>
23 #include <unit_test/unit_base.h>
24
25 //#include <stdio.h>
26 //#include <string.h>
27
28 using namespace application;
29 using namespace basis;
30 using namespace loggers;
31 using namespace mathematics;
32 using namespace sockets;
33 using namespace structures;
34 using namespace textual;
35 using namespace timely;
36 using namespace unit_test;
37
38 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
39
40 class test_sequence_tracker : public virtual unit_base, virtual public application_shell
41 {
42 public:
43   test_sequence_tracker() {}
44   DEFINE_CLASS_NAME("test_sequence_tracker");
45   virtual int execute();
46 };
47
48 int test_sequence_tracker::execute()
49 {
50   FUNCDEF("execute");
51   // some arbitrary ip addresses.
52   abyte arb1[] = { 127, 0, 0, 1 };
53   abyte arb2[] = { 192, 168, 0, 1 };
54   abyte arb3[] = { 28, 42, 56, 253 };
55
56   machine_uid eep(machine_uid::TCPIP_LOCATION, byte_array(4, arb1));
57   machine_uid op(machine_uid::TCPIP_LOCATION, byte_array(4, arb2));
58   machine_uid ork(machine_uid::TCPIP_LOCATION, byte_array(4, arb3));
59
60   sequence_tracker chevy(1 * MINUTE_ms, 10 * MINUTE_ms);
61
62   int_set eep_set;
63   int adds = randomizer().inclusive(400, 900);
64   int starter = 12092;
65   while (adds--) {
66     int seq = starter + randomizer().inclusive(1, 129);
67     eep_set += seq;
68     chevy.add_pair(eep, seq);
69   }
70
71   int_set op_set;
72   adds = randomizer().inclusive(200, 600);
73   starter = 1222;
74   while (adds--) {
75     int seq = starter + randomizer().inclusive(1, 129);
76     op_set += seq;
77     chevy.add_pair(op, seq);
78   }
79
80   int_set ork_set;
81   adds = randomizer().inclusive(200, 600);
82   starter = 992981;
83   while (adds--) {
84     int seq = starter + randomizer().inclusive(1, 129);
85     ork_set += seq;
86     chevy.add_pair(ork, seq);
87   }
88
89   int i;
90   for (i = 0; i < eep_set.elements(); i++) {
91     int seq = eep_set[i];
92     if (!chevy.have_seen(eep, seq)) {
93       log(a_sprintf("missing sequence is %d", seq));
94       log(chevy.text_form(true));
95       deadly_error("test_sequence_tracker", "eep check", "missing sequence");
96     }
97   }
98   for (i = 0; i < op_set.elements(); i++) {
99     int seq = op_set[i];
100     if (!chevy.have_seen(op, seq)) {
101       log(a_sprintf("missing sequence is %d", seq));
102       log(chevy.text_form(true));
103       deadly_error("test_sequence_tracker", "op check", "missing sequence");
104     }
105   }
106   for (i = 0; i < ork_set.elements(); i++) {
107     int seq = ork_set[i];
108     if (!chevy.have_seen(ork, seq)) {
109       log(a_sprintf("missing sequence is %d", seq));
110       log(chevy.text_form(true));
111       deadly_error("test_sequence_tracker", "ork check", "missing sequence");
112     }
113   }
114
115   return final_report();
116 }
117
118 HOOPLE_MAIN(test_sequence_tracker, )
119