t_entity.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : octopus_entity tester                                             *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Checks that the octopus_entity class is behaving as expected.            *
00009 *                                                                             *
00010 *******************************************************************************
00011 * Copyright (c) 2002-$now By Author.  This program is free software; you can  *
00012 * redistribute it and/or modify it under the terms of the GNU General Public  *
00013 * License as published by the Free Software Foundation; either version 2 of   *
00014 * the License or (at your option) any later version.  This is online at:      *
00015 *     http://www.fsf.org/copyleft/gpl.html                                    *
00016 * Please send any updates to: fred@gruntose.com                               *
00017 \*****************************************************************************/
00018 
00019 #include <basis/byte_array.h>
00020 #include <basis/chaos.h>
00021 #include <basis/guards.h>
00022 #include <basis/istring.h>
00023 #include <octopus/entity_defs.h>
00024 #include <opsystem/application_shell.h>
00025 #include <loggers/console_logger.h>
00026 #include <loggers/file_logger.h>
00027 #include <data_struct/static_memory_gremlin.h>
00028 #include <sockets/tcpip_stack.h>
00029 #include <textual/string_manipulation.h>
00030 
00031 #ifdef __WIN32__
00032   #include <process.h>
00033 #else
00034   #include <unistd.h>
00035 #endif
00036 
00037 const int ITERATE_EACH_TEST = 1000;
00038   // the number of times to repeat each test operation.
00039 
00040 class test_entity : public application_shell
00041 {
00042 public:
00043   test_entity() : application_shell(class_name()) {}
00044   IMPLEMENT_CLASS_NAME("test_entity");
00045   virtual int execute();
00046 };
00047 
00048 int test_entity::execute()
00049 {
00050   chaos rando;
00051   SET_DEFAULT_COMBO_LOGGER;
00052   tcpip_stack stack;
00053 
00054   octopus_entity blankie;
00055   if (!blankie.blank())
00056     deadly_error(class_name(), "emptiness test",
00057         "the blank entity was not seen as empty.");
00058   octopus_entity fullish("gurp", 28, 39, 4);
00059   if (fullish.blank())
00060     deadly_error(class_name(), "emptiness test",
00061         "the non-blank entity was seen as empty.");
00062 
00063   for (int i = 0; i < ITERATE_EACH_TEST; i++) {
00064     // test the basic filling of the values in an entity.
00065     octopus_entity blank_ent;
00066     int sequencer = rando.inclusive(1, MAXINT - 10);
00067     int add_in = rando.inclusive(0, MAXINT - 10);
00068     octopus_entity filled_ent(stack.hostname(), portable::process_id(), sequencer,
00069         add_in);
00070     blank_ent = octopus_entity(stack.hostname(), portable::process_id(), sequencer,
00071         add_in);
00072     if (blank_ent != filled_ent)
00073       deadly_error(class_name(), "simple reset test",
00074           "failed to resolve to same id");
00075     istring text1 = filled_ent.to_text();
00076     istring text2 = blank_ent.to_text();
00077     if (text1 != text2)
00078       deadly_error(class_name(), "to_text test", "strings are different");
00080     octopus_entity georgio = octopus_entity::from_text(text2);
00082     if (georgio != filled_ent)
00083       deadly_error(class_name(), "from_text test",
00084           "entity is different after from_text");
00085 
00086     octopus_request_id fudnix(filled_ent, 8232390);
00087     istring text3 = fudnix.to_text();
00088     octopus_request_id resur = octopus_request_id::from_text(text3);
00089     if (resur != fudnix)
00090       deadly_error(class_name(), "from_text test",
00091           "request id is different after from_text");
00092     
00093     blank_ent = octopus_entity();  // reset it again forcefully.
00094     blank_ent = octopus_entity(filled_ent.hostname(), filled_ent.process_id(),
00095         filled_ent.sequencer(), filled_ent.add_in());
00096     if (blank_ent != filled_ent)
00097       deadly_error(class_name(), "reset from attribs test",
00098           "failed to resolve to same id");
00099 //    log(isprintf("%d: ", i + 1) + filled_ent.mangled_form());
00100 
00101     byte_array chunk1;
00102     filled_ent.pack(chunk1);
00103     octopus_entity unpacked1;
00104     unpacked1.unpack(chunk1);
00105     if (unpacked1 != filled_ent)
00106       deadly_error(class_name(), "pack/unpack test",
00107           "failed to return same values");
00108 
00109     // test of entity packing and size calculation.
00110     octopus_entity ent(string_manipulation::make_random_name(1, 428),
00111             randomizer().inclusive(0, MAXINT/2),
00112             randomizer().inclusive(0, MAXINT/2),
00113             randomizer().inclusive(0, MAXINT/2));
00114     octopus_request_id bobo(ent, randomizer().inclusive(0, MAXINT/2));
00115     int packed_estimate = bobo.packed_size();
00116     byte_array packed_bobo;
00117     bobo.pack(packed_bobo);
00118     if (packed_bobo.length() != packed_estimate)
00119       deadly_error(class_name(), "entity packed_size test",
00120           "calculated incorrect packed size");
00121   }
00122 
00123 
00124   log("octopus_entity:: works for those functions tested.");
00125   return 0;
00126 }
00127 
00128 //hmmm: tests the octopus entity object,
00129 //      can do exact text check if want but that's not guaranteed to be useful
00130 //      in the future.
00131 
00132 HOOPLE_MAIN(test_entity, )
00133 

Generated on Fri Nov 28 04:29:39 2008 for HOOPLE Libraries by  doxygen 1.5.1