# TestHLAtypes.cc

Go to the documentation of this file.

    00001 // ----------------------------------------------------------------------------
    00002 // TestHLAtypes.cc - Module test for the IEEE 1516.2 compliant datatypes
    00003 // Copyright (C) 2008  Petr Gotthard <petr.gotthard@centrum.cz>
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License version 2.1, as published by the Free Software Foundation.
    00008 //
    00009 // This library is distributed in the hope that it will be useful,
    00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012 // Lesser General Public License for more details.
    00013 //
    00014 // $Id: TestHLAtypes.cc,v 1.3 2008/11/18 17:38:59 gotthardp Exp $
    00015 // ----------------------------------------------------------------------------
    00016
    00017 #include <memory>
    00018 #include <sstream>
    00019
    00020 #include <HLAtypesIEEE1516.hh>
    00021
    00022 using namespace libhla;
    00023
    00024 // IEEE 1516.2, Section 4.12.9.1
    00025 int test1()
    00026 {
    00027     typedef
    00028       HLAfixedRecord<
    00029 #define FIELD_A 0
    00030         HLAfixedField<FIELD_A, HLAoctet,
    00031 #define FIELD_B 1
    00032         HLAfixedField<FIELD_B, HLAboolean,
    00033 #define FIELD_C 2
    00034         HLAfixedField<FIELD_C, HLAfloat64BE
    00035         > > > > TA;
    00036
    00037     HLAdata<TA> A;
    00038     (*A).field<FIELD_A>() = 'A';
    00039     (*A).field<FIELD_B>() = HLAtrue;
    00040     (*A).field<FIELD_C>() = 3.14;
    00041
    00042     std::stringstream result;
    00043     A.print(result);
    00044
    00045     const char* correct =
    00046         "0000:  41 00 00 00 00 00 00 01 40 09 1e b8 51 eb 85 1f\n";
    00047
    00048     if(strcmp(result.str().c_str(), correct) != 0) {
    00049         std::cerr << "test1: <output> does not match expected result" << std::endl
    00050             << result.str() << std::endl << correct << std::endl;
    00051         return 1;
    00052     }
    00053     else {
    00054         std::cout << result.str();
    00055         return 0;
    00056     }
    00057 }
    00058
    00059 // TODO: Add support for HLAvariantRecord
    00060
    00061 // IEEE 1516.2, Section 4.12.9.3
    00062 int test3()
    00063 {
    00064     typedef
    00065       HLAfixedRecord<
    00066 #define FIELD_A 0
    00067         HLAfixedField<FIELD_A, HLAinteger32BE,
    00068 #define FIELD_B 1
    00069         HLAfixedField<FIELD_B, HLAoctet
    00070         > > > TA;
    00071
    00072     typedef HLAfixedArray<TA,2> TB;
    00073
    00074     HLAdata<TB> B;
    00075     (*B)[0].field<FIELD_B>() = '0';
    00076     (*B)[1].field<FIELD_A>() = 42;
    00077     (*B)[1].field<FIELD_B>() = '1';
    00078
    00079     std::stringstream result;
    00080     B.print(result);
    00081
    00082     const char* correct =
    00083         "0000:  00 00 00 00 30 00 00 00 00 00 00 2a 31\n";
    00084
    00085     if(strcmp(result.str().c_str(), correct) != 0) {
    00086         std::cerr << "test2: <output> does not match expected result" << std::endl
    00087             << result.str() << std::endl << correct << std::endl;
    00088         return 1;
    00089     }
    00090     else {
    00091         std::cout << result.str();
    00092         return 0;
    00093     }
    00094 }
    00095
    00096 // IEEE 1516.2, Section 4.12.9.4
    00097 int test4()
    00098 {
    00099     typedef HLAvariableArray<HLAfloat64BE> TA;
    00100
    00101     HLAdata<TA> A;
    00102     (*A).set_size(1);
    00103     (*A)[0] = 3.14;
    00104
    00105     std::stringstream result;
    00106     A.print(result);
    00107
    00108     const char* correct =
    00109         "0000:  00 00 00 01 00 00 00 00 40 09 1e b8 51 eb 85 1f\n";
    00110
    00111     if(strcmp(result.str().c_str(), correct) != 0) {
    00112         std::cerr << "test4: <output> does not match expected result" << std::endl
    00113             << result.str() << std::endl << correct << std::endl;
    00114         return 1;
    00115     }
    00116     else {
    00117         std::cout << result.str();
    00118         return 0;
    00119     }
    00120 }
    00121
    00122 int test5()
    00123 {
    00124     typedef
    00125       HLAvariableArray<
    00126         HLAfixedRecord<
    00127 #define FIELD_A 0
    00128           HLAfixedField<FIELD_A, HLAvariableArray<HLAfloat64LE>,
    00129 #define FIELD_B 1
    00130           HLAfixedField<FIELD_B, HLAvariableArray<HLAinteger32LE>
    00131           > > > > TA;
    00132
    00133     HLAdata<TA> A;
    00134     (*A).set_size(1);
    00135     (*A)[0].field<FIELD_A>().set_size(1);
    00136     (*A)[0].field<FIELD_A>()[0] = 3.14;
    00137     (*A)[0].field<FIELD_B>().set_size(1);
    00138     (*A)[0].field<FIELD_B>()[0] = 7;
    00139
    00140     (*A)[0].field<FIELD_A>().set_size(2);
    00141     (*A)[0].field<FIELD_A>()[1] = 2.718;
    00142
    00143     std::stringstream result;
    00144     A.print(result);
    00145
    00146     const char* correct =
    00147         "0000:  00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 00\n"
    00148         "0010:  1f 85 eb 51 b8 1e 09 40 58 39 b4 c8 76 be 05 40\n"
    00149         "0020:  00 00 00 01 07 00 00 00\n";
    00150
    00151     if(strcmp(result.str().c_str(), correct) != 0) {
    00152         std::cerr << "test1: <output> does not match expected result" << std::endl
    00153             << result.str() << std::endl << correct << std::endl;
    00154         return 1;
    00155     }
    00156     else {
    00157         std::cout << result.str();
    00158         return 0;
    00159     }
    00160 }
    00161
    00162 int test6()
    00163 {
    00164     HLAdata<HLAASCIIstring> A;
    00165     *A = "UFO";
    00166
    00167     std::stringstream result;
    00168     A.print(result);
    00169
    00170     const char* correct =
    00171         "0000:  00 00 00 03 55 46 4f\n";
    00172
    00173     if(strcmp(result.str().c_str(), correct) != 0) {
    00174         std::cerr << "test6: <output> does not match expected result" << std::endl
    00175             << result.str() << std::endl << correct << std::endl;
    00176         return 1;
    00177     }
    00178     else {
    00179         std::cout << result.str();
    00180         return 0;
    00181     }
    00182 }
    00183
    00184 int main(int argc, char* argv[])
    00185 {
    00186     int result = 0;
    00187
    00188     std::cerr << "Host byte-order: "
    00189 #ifdef HOST_IS_BIG_ENDIAN
    00190         << "big-endian" << std::endl;
    00191 #else
    00192         << "little-endian" << std::endl;
    00193 #endif
    00194
    00195     result += test1();
    00196     result += test3();
    00197     result += test4();
    00198     result += test5();
    00199     result += test6();
    00200
    00201     return result;
    00202 }
    00203
    00204 // $Id: TestHLAtypes.cc,v 1.3 2008/11/18 17:38:59 gotthardp Exp $