feisty meow concerns codebase  2.140
test_entity.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : octopus_entity tester *
4 * Author : Chris Koeritz *
5 * *
6 * Purpose: *
7 * *
8 * Checks that the octopus_entity 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 
21 #include <basis/byte_array.h>
22 #include <basis/guards.h>
23 #include <basis/astring.h>
25 #include <loggers/console_logger.h>
26 #include <loggers/file_logger.h>
28 #include <mathematics/chaos.h>
29 #include <octopus/entity_defs.h>
30 #include <sockets/tcpip_stack.h>
33 #include <unit_test/unit_base.h>
34 
35 #ifdef __WIN32__
36  #include <process.h>
37 #else
38  #include <unistd.h>
39 #endif
40 
41 using namespace application;
42 using namespace basis;
43 using namespace configuration;
44 using namespace loggers;
45 using namespace mathematics;
46 using namespace octopi;
47 using namespace sockets;
48 using namespace textual;
49 using namespace unit_test;
50 
51 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), astring(s))
52 
53 const int ITERATE_EACH_TEST = 1000;
54  // the number of times to repeat each test operation.
55 
56 class test_entity : virtual public unit_base, virtual public application_shell
57 {
58 public:
59  test_entity() : application_shell() {}
60 //class_name()
61  DEFINE_CLASS_NAME("test_entity");
62  virtual int execute();
63 };
64 
65 int test_entity::execute()
66 {
67  FUNCDEF("execute");
68  chaos rando;
70  tcpip_stack stack;
71 
72  octopus_entity blankie;
73  if (!blankie.blank())
74  deadly_error(class_name(), "emptiness test",
75  "the blank entity was not seen as empty.");
76  octopus_entity fullish("gurp", 28, 39, 4);
77  if (fullish.blank())
78  deadly_error(class_name(), "emptiness test",
79  "the non-blank entity was seen as empty.");
80 
81  for (int i = 0; i < ITERATE_EACH_TEST; i++) {
82  // test the basic filling of the values in an entity.
83  octopus_entity blank_ent;
84  int sequencer = rando.inclusive(1, MAXINT32 - 10);
85  int add_in = rando.inclusive(0, MAXINT32 - 10);
86  octopus_entity filled_ent(stack.hostname(), application_configuration::process_id(), sequencer,
87  add_in);
88  blank_ent = octopus_entity(stack.hostname(), application_configuration::process_id(), sequencer,
89  add_in);
90  if (blank_ent != filled_ent)
91  deadly_error(class_name(), "simple reset test",
92  "failed to resolve to same id");
93  astring text1 = filled_ent.to_text();
94  astring text2 = blank_ent.to_text();
95  if (text1 != text2)
96  deadly_error(class_name(), "to_text test", "strings are different");
98  octopus_entity georgio = octopus_entity::from_text(text2);
100  if (georgio != filled_ent)
101  deadly_error(class_name(), "from_text test",
102  "entity is different after from_text");
103 
104  octopus_request_id fudnix(filled_ent, 8232390);
105  astring text3 = fudnix.to_text();
106  octopus_request_id resur = octopus_request_id::from_text(text3);
107  if (resur != fudnix)
108  deadly_error(class_name(), "from_text test",
109  "request id is different after from_text");
110 
111  blank_ent = octopus_entity(); // reset it again forcefully.
112  blank_ent = octopus_entity(filled_ent.hostname(), filled_ent.process_id(),
113  filled_ent.sequencer(), filled_ent.add_in());
114  if (blank_ent != filled_ent)
115  deadly_error(class_name(), "reset from attribs test",
116  "failed to resolve to same id");
117 // LOG(a_sprintf("%d: ", i + 1) + filled_ent.mangled_form());
118 
119  byte_array chunk1;
120  filled_ent.pack(chunk1);
121  octopus_entity unpacked1;
122  unpacked1.unpack(chunk1);
123  if (unpacked1 != filled_ent)
124  deadly_error(class_name(), "pack/unpack test",
125  "failed to return same values");
126 
127  // test of entity packing and size calculation.
128  octopus_entity ent(string_manipulation::make_random_name(1, 428),
129  randomizer().inclusive(0, MAXINT32/2),
130  randomizer().inclusive(0, MAXINT32/2),
131  randomizer().inclusive(0, MAXINT32/2));
132  octopus_request_id bobo(ent, randomizer().inclusive(0, MAXINT32/2));
133  int packed_estimate = bobo.packed_size();
134  byte_array packed_bobo;
135  bobo.pack(packed_bobo);
136  if (packed_bobo.length() != packed_estimate)
137  deadly_error(class_name(), "entity packed_size test",
138  "calculated incorrect packed size");
139  }
140 
141 
142  LOG(astring(class_name()) + ":: works for those functions tested.");
143  return 0;
144 }
145 
146 //hmmm: tests the octopus entity object,
147 // can do exact text check if want but that's not guaranteed to be useful
148 // in the future.
149 
150 HOOPLE_MAIN(test_entity, )
151 
The application_shell is a base object for console programs.
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 very common template for a dynamic array of bytes.
Definition: byte_array.h:36
a platform-independent way to acquire random numbers in a specific range.
Definition: chaos.h:51
int inclusive(int low, int high) const
< Returns a pseudo-random number r, such that "low" <= r <= "high".
Definition: chaos.h:88
Provides a way of identifying users of an octopus object.
Definition: entity_defs.h:35
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
basis::astring to_text() const
conversion to text format for display.
Definition: entity_defs.h:80
bool blank() const
true if the entity is blank, as constructed by default constructor.
Definition: entity_defs.cpp:99
Identifies requests made on an octopus by users.
Definition: entity_defs.h:114
Helpful functions for interacting with TCP/IP stacks.
Definition: tcpip_stack.h:38
basis::astring hostname() const
#define deadly_error(c, f, i)
#define MAXINT32
Maximum 32-bit integer value.
Definition: definitions.h:75
#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.
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
Provides access to the operating system's socket methods.
Definition: base_address.h:26
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
#define randomizer()
#define LOG(s)
Definition: test_entity.cpp:51
const int ITERATE_EACH_TEST
Definition: test_entity.cpp:53
chaos rando