1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
11 #include <anna/core/util/CommandLine.hpp>
12 #include <anna/core/DataBlock.hpp>
13 #include <anna/core/tracing/TraceWriter.hpp>
14 #include <anna/core/functions.hpp>
16 #include <anna/app/Application.hpp>
18 #include <anna/comm/comm.hpp>
22 class MyCodec : public Codec {
24 MyCodec () : Codec (0xaa) ,
27 attach ("string", a_string);
28 attach ("integer", a_integer);
29 attach ("block", a_dataBlock);
30 attach ("string", a_string2);
31 attach ("float", a_float);
32 attach ("double", a_double);
35 void set (const std::string& value) throw () { a_string = a_string2 = value; a_string2 += value; }
36 void set (const int value) throw () { a_integer = value; }
37 void set (const anna::DataBlock& value) throw () { a_dataBlock = value; }
38 void set (const float value) throw () { a_float = value; }
39 void set (const double value) throw () { a_double = value; }
41 const std::string& getString () const throw () { return a_string; }
42 const int getInteger () const throw () { return a_integer; }
43 const anna::DataBlock& getDataBlock () const throw (anna::RuntimeException) { return a_dataBlock; }
44 const std::string& getString2 () const throw () { return a_string2; }
45 float getFloat () const throw () { return a_float; }
46 float getDouble () const throw () { return a_double; }
51 anna::DataBlock a_dataBlock;
52 std::string a_string2;
57 class Test : public anna::app::Application {
65 void run () throw (anna::RuntimeException);
71 int main (int argc, const char** argv)
73 CommandLine& commandLine (CommandLine::instantiate ());
75 TraceWriter* traceWriter = new TraceWriter;
78 commandLine.initialize (argv, argc);
79 commandLine.verify ();
81 Logger::initialize ("testfunctions", traceWriter);
82 Logger::setLevel (Logger::Debug);
86 catch (Exception& ex) {
87 cout << ex.asString () << endl;
96 anna::app::Application ("testfunctions", "Comprobacion de la clase comm::Codec", "1.0.0")
98 CommandLine& commandLine (CommandLine::instantiate ());
100 commandLine.add ("string", CommandLine::Argument::Mandatory, "Cadena a enviar");
101 commandLine.add ("i", CommandLine::Argument::Mandatory, "Enterno a enviar");
105 throw (RuntimeException)
107 CommandLine& cm (CommandLine::instantiate ());
109 const char* string = cm.getValue ("string");
114 a_input.set (string);
115 a_input.set (cm.getIntegerValue ("i"));
116 a_input.set (ff = 1.12345);
117 a_input.set (dd = 2.12345);
119 DataBlock dataBlock (true);
121 for (int i = strlen (string) - 1; i > 0; i --)
122 dataBlock += string [i];
124 a_input.set (dataBlock);
126 const DataBlock& result (a_input.code ());
128 a_output.decode (result);
130 cout << "String: " << a_output.getString () << endl;
131 cout << "Integer: " << a_output.getInteger () << endl;
132 cout << "Data block: " << anna::functions::asString (a_output.getDataBlock ()) << endl;
133 cout << "String2: " << a_output.getString2 () << endl << endl;
134 cout << "Float: " << a_output.getFloat () << endl << endl;
135 cout << "Double: " << a_output.getDouble () << endl << endl;
138 * Para comprobar que el modo de codificar los bytes son igual que en la versiĆ³n de Java
140 char buffer [sizeof (S64)];
143 comm::functions::codeShort (buffer, ss);
145 dataBlock.append (buffer, sizeof (ss));
146 cout << anna::functions::asString ("Short: %d - 0x%x", ss, ss);
147 cout << anna::functions::asString (dataBlock) << endl << endl;
148 ss = comm::functions::decodeShort (buffer);
149 cout << anna::functions::asString ("Short2: %d - 0x%x\n\n", ss, ss);
152 comm::functions::codeInteger (buffer, xx);
154 dataBlock.append (buffer, sizeof (xx));
155 cout << anna::functions::asString ("Integer: %d - 0x%x", xx, xx);
156 cout << anna::functions::asString (dataBlock) << endl << endl;
157 xx = comm::functions::decodeInteger (buffer);
158 cout << anna::functions::asString ("Integer2: %d - 0x%x\n\n", xx, xx);
160 // S64 ll = 31604938272LL;
161 S64 ll = 98765432101234LL;
162 comm::functions::codeInteger64 (buffer, ll);
164 dataBlock.append (buffer, sizeof (ll));
165 cout << anna::functions::asString ("Integer64: %lld - 0x%llx", ll, ll);
166 cout << anna::functions::asString (dataBlock) << endl << endl;
167 ll = comm::functions::decodeInteger64 (buffer);
168 cout << anna::functions::asString ("Integer64-2: %lld - 0x%llx\n\n", ll, ll);
170 cout << "Sizeof (float/int): " << sizeof (float) << "/" << sizeof (int) << endl;
174 anna_memcpy (&ii, &ff, sizeof (ff));
175 comm::functions::codeInteger (buffer, ii);
176 cout << "Float I: " << ff << " (" << anna::functions::asHexString (ii) << ") " << endl;
179 ii = comm::functions::decodeInteger (buffer);
180 anna_memcpy (&ff2, &ii, sizeof (ff));
181 cout << "Float O: " << ff2 << " (" << anna::functions::asHexString (ii) << ") " << endl;
183 dd = -123123123.3232;
185 anna_memcpy (&ii64, &dd, sizeof (dd));
186 comm::functions::codeInteger64 (buffer, ii64);
187 cout << "Double I: " << dd << " (" << anna::functions::asHexString (ii64) << ") " << endl;
190 ii64 = comm::functions::decodeInteger64 (buffer);
191 anna_memcpy (&dd2, &ii64, sizeof (dd));
192 cout << "Double O: " << dd2 << " (" << anna::functions::asHexString (ii64) << ") " << endl;