Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / ACEXML / tests / util / test.cpp
blob1b17f3861bf9723097d7dce1ee0732350ff53682
1 // A simple test for performance of the ACEXML_escape_string() function
3 #include "ACEXML/common/XML_Util.h"
5 #include "ace/OS_main.h"
6 #include "ace/Time_Value.h"
7 #include "ace/OS_NS_sys_time.h"
8 #include "ace/Log_Msg.h"
10 const int MAX_ITERATIONS = 100 * 1000;
11 const int NUM_TEST_STRS = 6;
13 static bool is_escaped(const ACEXML_String& s)
15 if (s[0] != ACE_TEXT('&'))
16 return false;
17 if (s[s.length() - 1] != ACE_TEXT(';'))
18 return false;
19 return true;
22 static int run_tests(ACEXML_String test_strings[NUM_TEST_STRS], int iterations)
24 // Test 1 - Escape the strings using a new temporary string each iteration.
25 ACE_Time_Value start = ACE_OS::gettimeofday();
26 int i = 0;
27 for (i = 0; i < iterations; ++i)
29 ACEXML_String tmp = ACEXML_escape_string(test_strings[i % NUM_TEST_STRS]);
30 if (! is_escaped(tmp))
32 ACE_ERROR((LM_ERROR, "Error: Failed to escape string\n"));
33 return 1;
36 ACE_DEBUG((LM_DEBUG, "Test1 took %dms\n", (ACE_OS::gettimeofday() - start).msec()));
38 // Test 2 - Escape the strings using a shared temporary string. This shouldn't
39 // be any faster than Test 1 as long as the compiler has return value optimization.
40 ACEXML_String tmp;
41 start = ACE_OS::gettimeofday();
42 for (i = 0; i < iterations; ++i)
44 tmp = ACEXML_escape_string(test_strings[i % NUM_TEST_STRS]);
45 if (! is_escaped(tmp))
47 ACE_ERROR((LM_ERROR, "Error: Failed to escape string\n"));
48 return 1;
51 ACE_DEBUG((LM_DEBUG, "Test2 took %dms\n", (ACE_OS::gettimeofday() - start).msec()));
53 // Test 3 - Escape the strings using a shared temporary string. This time, we use
54 // the alternate form of ACEXML_escape_string() so that our temporary buffer is reused.
55 tmp.clear(1);
56 start = ACE_OS::gettimeofday();
57 for (i = 0; i < iterations; ++i)
59 ACEXML_escape_string(test_strings[i % NUM_TEST_STRS], tmp);
60 if (! is_escaped(tmp))
62 ACE_ERROR((LM_ERROR, "Error: Failed to escape string\n"));
63 return 1;
66 ACE_DEBUG((LM_DEBUG, "Test3 took %dms\n", (ACE_OS::gettimeofday() - start).msec()));
68 // Test 4 - Same as Test 3, except that the tmp buffer shouldn't have to resize.
69 start = ACE_OS::gettimeofday();
70 for (i = 0; i < iterations; ++i)
72 ACEXML_escape_string(test_strings[i % NUM_TEST_STRS], tmp);
73 if (! is_escaped(tmp))
75 ACE_ERROR((LM_ERROR, "Error: Failed to escape string\n"));
76 return 1;
79 ACE_DEBUG((LM_DEBUG, "Test4 took %dms\n", (ACE_OS::gettimeofday() - start).msec()));
80 return 0;
83 int ACE_TMAIN (int, ACE_TCHAR *[])
85 ACEXML_String test_strings[NUM_TEST_STRS] = {
86 ACE_TEXT("\"xxxxx\"xxxxxxxx xx\"xxxxxx xxxxxx\"xxxxxxxxxx xxxxxxxx\"xxxxxx\""),
87 ACE_TEXT("'xxxxx\'xxxxxxxx' xxxxxxxx xx'xxxxxxxx'xxxxxx xxxxxxx'xxxxxxx'"),
88 ACE_TEXT("&xxxx&xxxxxxxxx &xxxxxxxx xxxxx&xxxxxxxxxxx xxxx&xxxxxxxxxx&"),
89 ACE_TEXT(">xx>xxxxxxxxxxx >xxxxxxxx xxxxx>xxxxxxxxxxx xxxxx>xxxxxxxxx>"),
90 ACE_TEXT("<xxxxx<xxxxxxxx xxxxxxxx <xxxxxxxxxxxxxxx<x xxxxxxxxxxxxxx<"),
91 ACE_TEXT("&xxxx\"xxxxxxx&xx xxx'xxxxx xx<xxxxxxx>xxxxxxx xx\"xxxxxxxxxxxx>"),
94 if (run_tests(test_strings, MAX_ITERATIONS) != 0)
95 return 1;
97 ACE_DEBUG((LM_DEBUG, "Rerun tests with larger strings\n"));
98 for (int i = 0; i < NUM_TEST_STRS; ++i)
100 for (int j = 0; j < 5; ++j)
102 test_strings[i] += test_strings[i];
106 if (run_tests(test_strings, MAX_ITERATIONS / 10) != 0)
107 return 1;
109 return 0;