feisty meow concerns codebase  2.140
t_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 
19 #include <basis/byte_array.h>
20 #include <mathematics/chaos.h>
21 #include <basis/guards.h>
22 #include <basis/astring.h>
23 #include <octopus/entity_defs.h>
25 #include <loggers/console_logger.h>
26 #include <loggers/file_logger.h>
28 #include <sockets/tcpip_stack.h>
30 
31 #ifdef __WIN32__
32  #include <process.h>
33 #else
34  #include <unistd.h>
35 #endif
36 
37 const int ITERATE_EACH_TEST = 1000;
38  // the number of times to repeat each test operation.
39 
40 class test_entity : public application_shell
41 {
42 public:
43  test_entity() : application_shell(class_name()) {}
44  DEFINE_CLASS_NAME("test_entity");
45  virtual int execute();
46 };
47 
48 int test_entity::execute()
49 {
50  chaos rando;
51  SET_DEFAULT_COMBO_LOGGER;
52  tcpip_stack stack;
53 
54  octopus_entity blankie;
55  if (!blankie.blank())
56  deadly_error(class_name(), "emptiness test",
57  "the blank entity was not seen as empty.");
58  octopus_entity fullish("gurp", 28, 39, 4);
59  if (fullish.blank())
60  deadly_error(class_name(), "emptiness test",
61  "the non-blank entity was seen as empty.");
62 
63  for (int i = 0; i < ITERATE_EACH_TEST; i++) {
64  // test the basic filling of the values in an entity.
65  octopus_entity blank_ent;
66  int sequencer = rando.inclusive(1, MAXINT - 10);
67  int add_in = rando.inclusive(0, MAXINT - 10);
68  octopus_entity filled_ent(stack.hostname(), application_configuration::process_id(), sequencer,
69  add_in);
70  blank_ent = octopus_entity(stack.hostname(), application_configuration::process_id(), sequencer,
71  add_in);
72  if (blank_ent != filled_ent)
73  deadly_error(class_name(), "simple reset test",
74  "failed to resolve to same id");
75  astring text1 = filled_ent.to_text();
76  astring text2 = blank_ent.to_text();
77  if (text1 != text2)
78  deadly_error(class_name(), "to_text test", "strings are different");
80  octopus_entity georgio = octopus_entity::from_text(text2);
82  if (georgio != filled_ent)
83  deadly_error(class_name(), "from_text test",
84  "entity is different after from_text");
85 
86  octopus_request_id fudnix(filled_ent, 8232390);
87  astring text3 = fudnix.to_text();
88  octopus_request_id resur = octopus_request_id::from_text(text3);
89  if (resur != fudnix)
90  deadly_error(class_name(), "from_text test",
91  "request id is different after from_text");
92 
93  blank_ent = octopus_entity(); // reset it again forcefully.
94  blank_ent = octopus_entity(filled_ent.hostname(), filled_ent.process_id(),
95  filled_ent.sequencer(), filled_ent.add_in());
96  if (blank_ent != filled_ent)
97  deadly_error(class_name(), "reset from attribs test",
98  "failed to resolve to same id");
99 // log(a_sprintf("%d: ", i + 1) + filled_ent.mangled_form());
100 
101  byte_array chunk1;
102  filled_ent.pack(chunk1);
103  octopus_entity unpacked1;
104  unpacked1.unpack(chunk1);
105  if (unpacked1 != filled_ent)
106  deadly_error(class_name(), "pack/unpack test",
107  "failed to return same values");
108 
109  // test of entity packing and size calculation.
110  octopus_entity ent(string_manipulation::make_random_name(1, 428),
111  randomizer().inclusive(0, MAXINT/2),
112  randomizer().inclusive(0, MAXINT/2),
113  randomizer().inclusive(0, MAXINT/2));
114  octopus_request_id bobo(ent, randomizer().inclusive(0, MAXINT/2));
115  int packed_estimate = bobo.packed_size();
116  byte_array packed_bobo;
117  bobo.pack(packed_bobo);
118  if (packed_bobo.length() != packed_estimate)
119  deadly_error(class_name(), "entity packed_size test",
120  "calculated incorrect packed size");
121  }
122 
123 
124  log("octopus_entity:: works for those functions tested.");
125  return 0;
126 }
127 
128 //hmmm: tests the octopus entity object,
129 // can do exact text check if want but that's not guaranteed to be useful
130 // in the future.
131 
132 HOOPLE_MAIN(test_entity, )
133 
#define deadly_error(c, f, i)
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:45
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
#define randomizer()
const int ITERATE_EACH_TEST
Definition: t_entity.cpp:37
chaos rando