diff options
author | parsons <parsons@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1999-01-24 02:54:43 +0000 |
---|---|---|
committer | parsons <parsons@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1999-01-24 02:54:43 +0000 |
commit | 06db72508992862ffbd7bdb6e09ea0eeaf773b7f (patch) | |
tree | 3335db89cf39a02810516aa181c2f4464127cc2c /tests/CDR_Test.cpp | |
parent | 98dacf0a148b6d9b4b9cced5072602cd255ad84e (diff) | |
download | ATCD-06db72508992862ffbd7bdb6e09ea0eeaf773b7f.tar.gz |
*** empty log message ***
Diffstat (limited to 'tests/CDR_Test.cpp')
-rw-r--r-- | tests/CDR_Test.cpp | 428 |
1 files changed, 428 insertions, 0 deletions
diff --git a/tests/CDR_Test.cpp b/tests/CDR_Test.cpp new file mode 100644 index 00000000000..646cac11e0c --- /dev/null +++ b/tests/CDR_Test.cpp @@ -0,0 +1,428 @@ +// $Id$ +// +// ============================================================================ +// +// = LIBRARY +// tests +// +// = FILENAME +// CDR_Test.cpp +// +// = DESCRIPTION +// Checks the functionality of the ACE CDR streams. +// +// = AUTHORS +// Istvan Buki +// Jeff Parsons +// +// ============================================================================ + +#include "ace/Get_Opt.h" +#include "ace/CDR.h" + +#if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 +USELIB("..\ace\aced.lib"); +//--------------------------------------------------------------------------- +#endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */ + +static int n = 4096; +static int nloops = 100; + +struct CDR_Test_Types +{ + CDR_Test_Types (void); + + CDR_Octet o; + short s; + long l; + const char *str; + double d; + + enum { + ARRAY_SIZE = 10 + }; + + short a[ARRAY_SIZE]; +}; + +CDR_Test_Types::CDR_Test_Types (void) + : o (1), s (2), l (4), str ("abc"), d (8) +{ + for (int i = 0; i < CDR_Test_Types::ARRAY_SIZE; ++i) + { + a[i] = i; + } +} + +static int +test_put (ACE_OutputCDR &cdr, CDR_Test_Types &test_types) +{ + for (int i = 0; i < n; ++i) + { + if (cdr.write_octet (test_types.o) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "write_octet[%d] failed\n", + i), + 1); + if (cdr.write_short (test_types.s) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "write_short[%d] failed\n", + i), + 1); + if (cdr.write_octet (test_types.o) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "write_octet-2[%d] failed\n", + i), + 1); + if (cdr.write_long (test_types.l) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "write_long[%d] failed\n", + i), + 1); + if (cdr.write_long (test_types.l) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "write_long-2[%d] failed\n", + i), + 1); + + if (cdr.write_string (test_types.str) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "write_string[%d] failed\n", + i), + 1); + + } + + return 0; +} + +static int +test_get (ACE_InputCDR &cdr, const CDR_Test_Types &test_types) +{ + CDR_Octet xo; + short xs; + long xl; + + for (int i = 0; i < n; ++i) + { + if (cdr.read_octet (xo) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "read_octet[%d] failed\n", + i), + 1); + if (xo != test_types.o) + ACE_ERROR_RETURN ((LM_ERROR, + "octet[%d] differs\n", + i), + 1); + if (cdr.read_short (xs) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "read_short[%d] failed\n", + i), 1); + if (xs != test_types.s) + ACE_ERROR_RETURN ((LM_ERROR, + "short[%d] differs\n", + i), + 1); + if (cdr.read_octet (xo) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "read_octet-2[%d] failed\n", + i), + 1); + if (xo != test_types.o) + ACE_ERROR_RETURN ((LM_ERROR, + "octet-2[%d] differs\n", + i), + 1); + if (cdr.read_long (xl) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "read_long[%d] failed\n", + i), + 1); + if (xl != test_types.l) + ACE_ERROR_RETURN ((LM_ERROR, + "long[%d] differs\n", + i), + 1); + if (cdr.read_long (xl) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "read_long-2[%d] failed\n", + i), + 1); + if (xl != test_types.l) + ACE_ERROR_RETURN ((LM_ERROR, + "long-2[%d] differs\n", + i), + 1); + + char *xstr; + if (cdr.read_string (xstr) == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "read_string2[%d] failed\n", + i), + 1); + if (ACE_OS::strcmp (xstr, test_types.str) != 0) + ACE_ERROR_RETURN ((LM_ERROR, + "string[%d] differs\n", + i), + 1); + } + return 0; +} + +static int +short_stream (void ) +{ + // counter + unsigned int i; + + // Build an output stream + ACE_OutputCDR os; + + // Basic types for output + char ch = 'A'; + ACE_CString str ("Test String"); + short s = -123; + unsigned short us = 123; + long l = -65800L; + unsigned long ul = 65800UL; + float f = 1.23f; + double d = 123.456789; + + // Arrays for output + short s_array[3] = { -1, 0, 1 }; + long l_array[3] = { -345678, 0, 345678 }; + float f_array[3] = { -1.23f, 0.0f, 1.23f }; + double d_array[3] = { -123.456789, 0.0, 123.456789 }; + + ACE_OutputCDR::from_char fc (ch); + os << fc; + os << str; + os << s; + os << us; + os << l; + os << ul; + os << f; + os << d; + os.write_short_array (s_array, 3); + os.write_long_array (l_array, 3); + os.write_float_array (f_array, 3); + os.write_double_array (d_array, 3); + + const ACE_Message_Block *out_mb = os.begin (); + unsigned int len = out_mb->length (); + + // Create an input stream (copy constructor) + ACE_InputCDR is (os); + const ACE_Message_Block *in_mb = is.start (); + + if (in_mb->length () != len) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "buffer length not preserved"), + 1); + + u_long in_chunk, out_chunk; + + for (i = 0; i < len; i++) + { + in_chunk = u_long (* (in_mb->rd_ptr () + i)); + out_chunk = u_long (* (out_mb->rd_ptr () + i)); + if (in_chunk != out_chunk ) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "buffer contents not preserved"), + 1); + } + + // Basic types for input + char ch1 = '\0'; + ACE_CString str1; + short s1 = 0; + unsigned short us1 = 0; + long l1 = 0L; + unsigned long ul1 = 0UL; + float f1 = 0.0f; + double d1 = 0.0; + + // Arrays for input + short s_array1[3]; + long l_array1[3]; + float f_array1[3]; + double d_array1[3]; + + ACE_DEBUG ((LM_DEBUG, + "Checking operators and arrays\n\n")); + + ACE_InputCDR::to_char tc (ch1); + is >> tc; + is >> str1; + is >> s1; + is >> us1; + is >> l1; + is >> ul1; + is >> f1; + is >> d1; + is.read_short_array (s_array1, 3); + is.read_long_array (l_array1, 3); + is.read_float_array (f_array1, 3); + is.read_double_array (d_array1, 3); + + if (ch1 != ch) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "char transfer error"), + 1); + + if (str1 != str) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "string transfer error"), + 1); + + if (s1 != s) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "short transfer error"), + 1); + + if (us1 != us) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "ushort transfer error"), + 1); + + if (l1 != l) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "long transfer error"), + 1); + + if (ul1 != ul) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "ulong transfer error"), + 1); + + if (f1 != f) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "float transfer error"), + 1); + + if (d1 != d) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "double transfer error"), + 1); + + for (i = 0 ; i < 3; i++) + if (s_array1[i] != s_array[i]) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "short array transfer error"), + 1); + + for (i = 0 ; i < 3; i++) + if (l_array1[i] != l_array[i]) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "long array transfer error"), + 1); + + for (i = 0 ; i < 3; i++) + if (f_array1[i] != f_array[i]) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "float array transfer error"), + 1); + + for (i = 0 ; i < 3; i++) + if (d_array1[i] != d_array[i]) + ACE_ERROR_RETURN ((LM_ERROR, + "%p\n", + "double array transfer error"), + 1); + + return 0; +} + +int +main (int argc, char *argv[]) +{ + ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("This is ACE Version %u.%u.%u\n\n"), + ACE::major_version (), + ACE::minor_version(), + ACE::beta_version())); + + ACE_Get_Opt get_opt (argc, argv, "dn:l:"); + int opt, debug = 0; + + while ((opt = get_opt ()) != EOF) + { + switch (opt) + { + case 'd': + debug++; + break; + case 'n': + n = ACE_OS::atoi (get_opt.optarg); + break; + case 'l': + nloops = ACE_OS::atoi (get_opt.optarg); + break; + case '?': + default: + ACE_DEBUG ((LM_DEBUG, + "Usage: %s " + "-d debug" + "-n <num> " + "-l <loops> " + "\n", + argv[0])); + return -1; + } + } + + ACE_DEBUG ((LM_DEBUG, + "Testing ACE CDR functions - short stream\n\n")); + + if (short_stream () != 0 ) + { + return 1; + } + + ACE_DEBUG ((LM_DEBUG, + "Short stream - no errors\n\n" + "Testing basic types - long stream\n\n")); + + for (int i = 0; i < nloops; ++i) + { + ACE_OutputCDR output; + CDR_Test_Types test_types; + + if (test_put (output, test_types) != 0) + { + return 1; + } + ACE_InputCDR input (output); + if (debug > 0) + { + ACE_DEBUG ((LM_DEBUG, "Output CDR: \n")); + ACE_HEX_DUMP ((LM_DEBUG, input.rd_ptr(), 64)); + ACE_DEBUG ((LM_DEBUG, "Input CDR: \n")); + ACE_HEX_DUMP ((LM_DEBUG, input.rd_ptr(), 64)); + } + + if (test_get (input, test_types) != 0) + { + return 1; + } + } + + ACE_DEBUG ((LM_DEBUG, + "Long stream - no errors\n\n")); + + return 0; +} + |