feisty meow concerns codebase 2.140
test_byte_format.cpp
Go to the documentation of this file.
1/*
2* Name : test_byte_format
3* Author : Chris Koeritz
4* Purpose: Puts the byte formatting utilities through their paces.
5**
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 *
11*/
12
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>
21#include <unit_test/unit_base.h>
23
24using namespace application;
25using namespace basis;
26using namespace filesystem;
27using namespace loggers;
28using namespace mathematics;
29using namespace structures;
30using namespace textual;
31using namespace timely;
32using namespace unit_test;
33
34#define LOG(to_print) EMERGENCY_LOG(program_wide_logger::get(), astring(to_print))
35
36class test_byte_format : virtual public unit_base, virtual public application_shell
37{
38public:
39 test_byte_format() {}
40 DEFINE_CLASS_NAME("test_byte_format");
41 int execute();
42};
43
44int test_byte_format::execute()
45{
46 FUNCDEF("execute");
47 {
48 // test out the byte shifting functions on a simple known array.
49 byte_array source;
50 source += 0x23; source += 0x5f; source += 0xa8; source += 0x2d;
51 source += 0xe2; source += 0x61; source += 0x90; source += 0x2d;
52 source += 0xf2; source += 0x38;
53 astring shifty;
55// LOG(a_sprintf("A: shifted our %d bytes into: ", source.length())
56// + shifty);
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.");
60 }
61
62 {
63 // test out the byte shifting functions on a random array.
64 for (int run = 0; run < 100; run++) {
65 byte_array source;
66 int size = randomizer().inclusive(1, 20923);
67 for (int i = 0; i < size; i++) {
68 source += abyte(randomizer().inclusive(0, 255));
69 }
70 astring shifty;
72// LOG(a_sprintf("B: shifted our %d bytes into: ", source.length())
73// + shifty);
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.");
77 }
78 }
79
80 {
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:");
85 LOG("");
86 LOG(dump);
87 }
88 {
89 abyte fodder[] = { 0x83, 0x0d, 0x93, 0x21, 0x82, 0xfe, 0xef, 0xdc, 0xb9,
90 0xa9, 0x21, 0x54, 0x83, 0x38, 0x65, 0x59, 0x99, 0xff, 0x00, 0xa0,
91 0x29, 0x03 };
92 byte_array fred(sizeof(fodder), fodder);
93 astring as_text1;
94 byte_formatter::bytes_to_string(fred, as_text1, true);
95 LOG(astring("got string #1 of: ") + as_text1);
96 astring as_text2;
97 byte_formatter::bytes_to_string(fred, as_text2, false);
98 LOG(astring("got string #2 of: ") + as_text2);
99 byte_array convert1;
100 byte_formatter::string_to_bytes(as_text1, convert1);
101 byte_array convert2;
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);
110astring back3;
111byte_formatter::bytes_to_string(harder_convert1, back3);
112LOG(astring("got third: ") + back3);
113
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);
118astring back4;
119byte_formatter::bytes_to_string(harder_convert2, back4);
120LOG(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");
123
124 abyte fodder2[] = {
1250x04, 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,
1260x00, 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);
131astring back5;
132byte_formatter::bytes_to_string(harder_convert3, back5);
133LOG(astring("got fifth: ") + back5);
134 ASSERT_EQUAL(fred, harder_convert3, "fifth abyte conversion: failed due to inequality");
135
136#ifndef EMBEDDED_BUILD
137 // now a test of parse_dump.
138 astring fred_dump;
139 byte_formatter::text_dump(fred_dump, fred, 0x993834);
140LOG("fred dump...");
141LOG(fred_dump);
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");
145#endif
146 }
147 {
148 // an endian tester.
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));
155
156 // now see what they look like as packables.
157 byte_array testa;
158 structures::attach(testa, test1);
159 LOG("0x3c5f in package:");
161 byte_array testb;
162 structures::attach(testb, test2);
163 LOG("0x9eaad0cb in package:");
165 }
166
167 return final_report();
168}
169
170HOOPLE_MAIN(test_byte_format, );
171
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
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
static void shifted_string_to_bytes(const basis::astring &to_convert, basis::byte_array &as_array)
unshifts a string "to_convert" back into a byte_array.
static void text_dump(basis::astring &output, const basis::abyte *location, basis::un_int length, basis::un_int label=0, const char *eol="\n")
prints out a block of memory in a human readable form.
static void bytes_to_string(const basis::byte_array &to_convert, basis::astring &as_string, bool space_delimited=true)
converts a byte_array into a string.
static void string_to_bytes(const basis::astring &to_convert, basis::byte_array &as_array)
wrangles the string "to_convert" into an equivalent byte form "as_array".
static void parse_dump(const basis::astring &dumped_form, basis::byte_array &bytes_found)
this operation performs the inverse of a text_dump.
static void bytes_to_shifted_string(const basis::byte_array &to_convert, basis::astring &as_string)
this is a special purpose converter from bytes to character strings.
#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
unsigned char abyte
A fairly important unit which is seldom defined...
Definition definitions.h:51
unsigned int un_int
Abbreviated name for unsigned integers.
Definition definitions.h:62
unsigned short un_short
Abbreviated name for unsigned short integers.
Definition definitions.h:64
A platform independent way to obtain the timestamp of a file.
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
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
void attach(byte_array &packed_form, const byte_array &to_attach)
Packs a byte_array "to_attach" into "packed_form".
#include <time.h>
Useful support functions for unit testing, especially within hoople.
Definition unit_base.cpp:35
#define randomizer()
#define LOG(to_print)
#define ASSERT_EQUAL(a, b, test_name)
Definition unit_base.h:38
#define ASSERT_TRUE(a, test_name)
Definition unit_base.h:46