2 * Name : test_byte_format
3 * Author : Chris Koeritz
4 * Purpose: Puts the byte formatting utilities through their paces.
6 * Copyright (c) 1992-$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 *
13 #include <application/hoople_main.h>
14 #include <basis/astring.h>
15 #include <basis/byte_array.h>
16 #include <basis/functions.h>
17 #include <basis/guards.h>
18 #include <mathematics/chaos.h>
19 #include <loggers/program_wide_logger.h>
20 #include <textual/byte_formatter.h>
21 #include <unit_test/unit_base.h>
22 #include <structures/static_memory_gremlin.h>
24 using namespace application;
25 using namespace basis;
26 using namespace filesystem;
27 using namespace loggers;
28 using namespace mathematics;
29 using namespace structures;
30 using namespace textual;
31 using namespace timely;
32 using namespace unit_test;
34 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
36 class test_byte_format : virtual public unit_base, virtual public application_shell
40 DEFINE_CLASS_NAME("test_byte_format");
44 int test_byte_format::execute()
48 // test out the byte shifting functions on a simple known array.
50 source += 0x23; source += 0x5f; source += 0xa8; source += 0x2d;
51 source += 0xe2; source += 0x61; source += 0x90; source += 0x2d;
52 source += 0xf2; source += 0x38;
54 byte_formatter::bytes_to_shifted_string(source, shifty);
55 // LOG(a_sprintf("A: shifted our %d bytes into: ", source.length())
57 byte_array source_copy;
58 byte_formatter::shifted_string_to_bytes(shifty, source_copy);
59 ASSERT_EQUAL(source_copy, source, "A: shifting corrupted the bytes.");
63 // test out the byte shifting functions on a random array.
64 for (int run = 0; run < 100; run++) {
66 int size = randomizer().inclusive(1, 20923);
67 for (int i = 0; i < size; i++) {
68 source += abyte(randomizer().inclusive(0, 255));
71 byte_formatter::bytes_to_shifted_string(source, shifty);
72 // LOG(a_sprintf("B: shifted our %d bytes into: ", source.length())
74 byte_array source_copy;
75 byte_formatter::shifted_string_to_bytes(shifty, source_copy);
76 ASSERT_EQUAL(source_copy, source, "B: shifting corrupted the bytes.");
81 astring burf("alia bodalia petunia");
82 astring dump(byte_formatter::text_dump((abyte *)burf.s(), burf.length() + 1));
83 //doofus! make this compare the output with expectations.
84 LOG("dumped form is:");
89 abyte fodder[] = { 0x83, 0x0d, 0x93, 0x21, 0x82, 0xfe, 0xef, 0xdc, 0xb9,
90 0xa9, 0x21, 0x54, 0x83, 0x38, 0x65, 0x59, 0x99, 0xff, 0x00, 0xa0,
92 byte_array fred(sizeof(fodder), fodder);
94 byte_formatter::bytes_to_string(fred, as_text1, true);
95 LOG(astring("got string #1 of: ") + as_text1);
97 byte_formatter::bytes_to_string(fred, as_text2, false);
98 LOG(astring("got string #2 of: ") + as_text2);
100 byte_formatter::string_to_bytes(as_text1, convert1);
102 byte_formatter::string_to_bytes(as_text2, convert2);
103 ASSERT_EQUAL(fred, convert1, "first abyte conversion: failed due to inequality");
104 ASSERT_TRUE(fred == convert2, "second abyte conversion: failed due to inequality");
105 // now a harder test.
106 astring as_text3("muggulo x83d x93, x21, x82, xfe, xef, xdc, xb9, "
107 "xa9, x21, x54, x83, x38, x65, x59, x99, xff, x00a0, x293");
108 byte_array harder_convert1;
109 byte_formatter::string_to_bytes(as_text3, harder_convert1);
111 byte_formatter::bytes_to_string(harder_convert1, back3);
112 LOG(astring("got third: ") + back3);
114 astring as_text4("muggulo x83d x93, x21, x82, xfe, xef, xdc, xb9, "
115 "xa9, x21, x54, x83, x38, x65, x59, x99, xff, x00a0, x293gikkor");
116 byte_array harder_convert2;
117 byte_formatter::string_to_bytes(as_text4, harder_convert2);
119 byte_formatter::bytes_to_string(harder_convert2, back4);
120 LOG(astring("got fourth: ") + back4);
121 ASSERT_EQUAL(fred, harder_convert1, "third abyte conversion: failed due to inequality");
122 ASSERT_EQUAL(fred, harder_convert2, "fourth abyte conversion: failed due to inequality");
125 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,
126 0x00, 0x00, 0x09, 0x00 };
127 fred = byte_array(sizeof(fodder2), fodder2);
128 astring as_text5("040006000a020300060048011c002c00040009001700ffff000000000900");
129 byte_array harder_convert3;
130 byte_formatter::string_to_bytes(as_text5, harder_convert3);
132 byte_formatter::bytes_to_string(harder_convert3, back5);
133 LOG(astring("got fifth: ") + back5);
134 ASSERT_EQUAL(fred, harder_convert3, "fifth abyte conversion: failed due to inequality");
136 #ifndef EMBEDDED_BUILD
137 // now a test of parse_dump.
139 byte_formatter::text_dump(fred_dump, fred, 0x993834);
142 byte_array fred_like;
143 byte_formatter::parse_dump(fred_dump, fred_like);
144 ASSERT_EQUAL(fred, fred_like, "parse_dump test: failed due to inequality");
149 basis::un_short test1 = 0x3c5f;
150 LOG("0x3c5f in intel:");
151 LOG(byte_formatter::text_dump((abyte *)&test1, 2));
152 basis::un_int test2 = 0x9eaad0cb;
153 LOG("0x9eaad0cb in intel:");
154 LOG(byte_formatter::text_dump((abyte *)&test2, 4));
156 // now see what they look like as packables.
158 structures::attach(testa, test1);
159 LOG("0x3c5f in package:");
160 LOG(byte_formatter::text_dump(testa));
162 structures::attach(testb, test2);
163 LOG("0x9eaad0cb in package:");
164 LOG(byte_formatter::text_dump(testb));
167 return final_report();
170 HOOPLE_MAIN(test_byte_format, );