t_byte_format.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_text_dump                                                    *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Puts the byte formatting utilities through their paces.                  *
00009 *                                                                             *
00010 *******************************************************************************
00011 * Copyright (c) 1992-$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 \*****************************************************************************/
00017 
00018 #include <basis/byte_array.h>
00019 #include <basis/chaos.h>
00020 #include <basis/function.h>
00021 #include <basis/guards.h>
00022 #include <basis/istring.h>
00023 #include <basis/packable.h>
00024 #include <opsystem/application_shell.h>
00025 #include <data_struct/static_memory_gremlin.h>
00026 #include <textual/byte_format.h>
00027 
00028 HOOPLE_STARTUP_CODE;
00029 
00030 class test_text_dump : public application_shell
00031 {
00032 public:
00033   test_text_dump();
00034   IMPLEMENT_CLASS_NAME("test_text_dump");
00035   int execute();
00036 };
00037 
00038 test_text_dump::test_text_dump() : application_shell("t_textdump")
00039 {}
00040 
00041 int test_text_dump::execute()
00042 {
00043   FUNCDEF("execute");
00044   {
00045     // test out the byte shifting functions on a simple known array.
00046     byte_array source;
00047     source += 0x23; source += 0x5f; source += 0xa8; source += 0x2d; 
00048     source += 0xe2; source += 0x61; source += 0x90; source += 0x2d; 
00049     source += 0xf2; source += 0x38; 
00050     istring shifty;
00051     byte_format::bytes_to_shifted_string(source, shifty);
00052 //    log(isprintf("A: shifted our %d bytes into: ", source.length())
00053 //        + shifty);
00054     byte_array source_copy;
00055     byte_format::shifted_string_to_bytes(shifty, source_copy);
00056     if (source_copy != source) {
00057       deadly_error(class_name(), func, "A: shifting corrupted the bytes.");
00058     }
00059   }
00060 
00061   {
00062     // test out the byte shifting functions on a random array.
00063     for (int run = 0; run < 100; run++) {
00064       byte_array source;
00065       int size = randomizer().inclusive(1, 20923);
00066       for (int i = 0; i < size; i++) {
00067         source += byte(randomizer().inclusive(0, 255));
00068       }
00069       istring shifty;
00070       byte_format::bytes_to_shifted_string(source, shifty);
00071 //      log(isprintf("B: shifted our %d bytes into: ", source.length())
00072 //          + shifty);
00073       byte_array source_copy;
00074       byte_format::shifted_string_to_bytes(shifty, source_copy);
00075       if (source_copy != source) {
00076         deadly_error(class_name(), func, "B: shifting corrupted the bytes.");
00077       }
00078     }
00079   }
00080 
00081   {
00082     istring burf("alia bodalia petunia");
00083     istring dump(byte_format::text_dump((byte *)burf.s(), burf.length() + 1));
00084 //doofus!  make this compare the output with expectations.
00085     log("dumped form is:");
00086     log("");
00087     log(dump);
00088   }
00089   {
00090     byte fodder[] = { 0x83, 0x0d, 0x93, 0x21, 0x82, 0xfe, 0xef, 0xdc, 0xb9,
00091         0xa9, 0x21, 0x54, 0x83, 0x38, 0x65, 0x59, 0x99, 0xff, 0x00, 0xa0,
00092         0x29, 0x03 };
00093     byte_array fred(sizeof(fodder), fodder);
00094     istring as_text1;
00095     byte_format::bytes_to_string(fred, as_text1, true);
00096     log(istring("got string #1 of: ") + as_text1);
00097     istring as_text2;
00098     byte_format::bytes_to_string(fred, as_text2, false);
00099     log(istring("got string #2 of: ") + as_text2);
00100     byte_array convert1;
00101     byte_format::string_to_bytes(as_text1, convert1);
00102     byte_array convert2;
00103     byte_format::string_to_bytes(as_text2, convert2);
00104     if (fred != convert1)
00105       deadly_error("test_byte_formatters", "first byte conversion",
00106           "failed due to inequality");
00107     if ( ! (fred == convert2))
00108       deadly_error("test_byte_formatters", "second byte conversion",
00109           "failed due to inequality");
00110     // now a harder test.
00111     istring as_text3("muggulo x83d x93, x21, x82, xfe, xef, xdc, xb9, "
00112         "xa9, x21, x54, x83, x38, x65, x59, x99, xff, x00a0, x293");
00113     byte_array harder_convert1;
00114     byte_format::string_to_bytes(as_text3, harder_convert1);
00115 istring back3;
00116 byte_format::bytes_to_string(harder_convert1, back3);
00117 log(istring("got third: ") + back3);
00118 
00119     istring as_text4("muggulo x83d x93, x21, x82, xfe, xef, xdc, xb9, "
00120         "xa9, x21, x54, x83, x38, x65, x59, x99, xff, x00a0, x293gikkor");
00121     byte_array harder_convert2;
00122     byte_format::string_to_bytes(as_text4, harder_convert2);
00123 istring back4;
00124 byte_format::bytes_to_string(harder_convert2, back4);
00125 log(istring("got fourth: ") + back4);
00126     if (fred != harder_convert1)
00127       deadly_error("test_byte_formatters", "third byte conversion",
00128           "failed due to inequality");
00129     if (fred != harder_convert2)
00130       deadly_error("test_byte_formatters", "fourth byte conversion",
00131           "failed due to inequality");
00132 
00133     byte fodder2[] = {
00134 0x04, 0x00, 0x06, 0x00, 0x0a, 0x02, 0x03, 0x00, 0x06, 0x00, 0x48, 0x01, 0x1c, 0x00, 0x2c, 0x00, 0x04, 0x00, 0x09, 0x00, 0x17, 0x00, 0xff, 0xff, 0x00, 0x00,
00135 0x00, 0x00, 0x09, 0x00 };
00136     fred = byte_array(sizeof(fodder2), fodder2);
00137     istring as_text5("040006000a020300060048011c002c00040009001700ffff000000000900");
00138     byte_array harder_convert3;
00139     byte_format::string_to_bytes(as_text5, harder_convert3);
00140 istring back5;
00141 byte_format::bytes_to_string(harder_convert3, back5);
00142 log(istring("got fifth: ") + back5);
00143     if (fred != harder_convert3)
00144       deadly_error("test_byte_formatters", "fifth byte conversion",
00145           "failed due to inequality");
00146 
00147 #ifndef EMBEDDED_BUILD
00148     // now a test of parse_dump.
00149     istring fred_dump;
00150     byte_format::text_dump(fred_dump, fred, 0x993834);
00151 log("fred dump...");
00152 log(fred_dump);
00153     byte_array fred_like;
00154     byte_format::parse_dump(fred_dump, fred_like);
00155     if (fred != fred_like)
00156       deadly_error("test_byte_formatters", "parse_dump test",
00157           "failed due to inequality");
00158 #endif
00159   }
00160   {
00161     // an endian tester.
00162     u_short test1 = 0x3c5f;
00163     log("0x3c5f in intel:");
00164     log(byte_format::text_dump((byte *)&test1, 2));
00165     u_int test2 = 0x9eaad0cb;
00166     log("0x9eaad0cb in intel:");
00167     log(byte_format::text_dump((byte *)&test2, 4));
00168 
00169     // now see what they look like as packables.
00170     byte_array testa;
00171     basis::attach(testa, test1);
00172     log("0x3c5f in package:");
00173     log(byte_format::text_dump(testa));
00174     byte_array testb;
00175     basis::attach(testb, test2);
00176     log("0x9eaad0cb in package:");
00177     log(byte_format::text_dump(testb));
00178   }
00179 
00180   guards::alert_message("byte_formatters:: work for those functions tested.");
00181   return 0;
00182 }
00183 
00184 int main()
00185 {
00186   test_text_dump to_test;
00187   return to_test.execute();
00188 }
00189 

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