Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / tests / UUID_Test.cpp
blobf505e1a6ae319fb7feb7efe6cedc50df11d104ec
1 //=============================================================================
2 /**
3 * @file UUID_Test.cpp
5 * Test the ACE UUID class which generates unique id's
7 * @author Andrew T. Finnel <andrew@activesol.net> and Yamuna Krishnmaurthy <yamuna@oomworks.com>
8 */
9 //=============================================================================
11 #include "test_config.h"
12 #include "ace/UUID.h"
13 #include <memory>
15 class Tester
17 public:
18 int test ();
21 int
22 Tester::test ()
24 int retval = 0;
26 // Generate UUID
27 std::unique_ptr <ACE_Utils::UUID> uuid (ACE_Utils::UUID_GENERATOR::instance ()->generate_UUID ());
28 ACE_CString uuid_str (uuid->to_string ()->c_str ());
29 ACE_DEBUG ((LM_DEBUG,
30 ACE_TEXT ("Generated UUID\n %C\n"),
31 uuid_str.c_str ()));
33 #ifndef ACE_LACKS_SSCANF
34 // Construct UUID from string
35 ACE_Utils::UUID new_uuid (uuid_str);
37 ACE_DEBUG ((LM_DEBUG,
38 ACE_TEXT ("UUID Constructed from above Generated UUID\n %C\n"),
39 new_uuid.to_string ()->c_str ()));
41 // Construct UUID from string by assigning it
42 ACE_Utils::UUID new_uuid_assign;
43 new_uuid_assign.from_string (new_uuid.to_string ()->c_str ());
44 ACE_DEBUG ((LM_DEBUG,
45 ACE_TEXT ("UUID Constructed from above Generated UUID ")
46 ACE_TEXT ("with assign\n %C\n"),
47 new_uuid_assign.to_string ()->c_str ()));
49 if (new_uuid != new_uuid_assign)
50 ACE_ERROR_RETURN ((LM_ERROR,
51 ACE_TEXT ("Error: UUIDs are not the same\n")),
52 -1);
54 // Check the hash value of the 2 UUIDs
56 if (new_uuid.hash () != new_uuid_assign.hash ())
57 ACE_ERROR_RETURN ((LM_ERROR,
58 ACE_TEXT ("Error: hash value of UUIDs are ")
59 ACE_TEXT ("not the same")),
60 -1);
62 #else
63 const ACE_Utils::UUID &new_uuid = *uuid;
64 #endif
66 // Construct UUID using the copy constructor
67 ACE_Utils::UUID new_uuid_copy (new_uuid);
68 ACE_DEBUG ((LM_DEBUG,
69 ACE_TEXT ("UUID constructed from above Generated UUID")
70 ACE_TEXT (" with copy\n %C\n"),
71 new_uuid_copy.to_string ()->c_str ()));
73 if (new_uuid != new_uuid_copy)
74 ACE_ERROR_RETURN ((LM_ERROR,
75 ACE_TEXT ("Error: UUIDs are not the same ")
76 ACE_TEXT ("with copy\n")),
77 -1);
79 #ifndef ACE_LACKS_SSCANF
80 ACE_Utils::UUID nil_uuid (*ACE_Utils::UUID::NIL_UUID.to_string ());
81 ACE_DEBUG ((LM_DEBUG,
82 ACE_TEXT ("UUID Constructed from NIL_UUID with ")
83 ACE_TEXT ("string copy\n %C\n"),
84 nil_uuid.to_string ()->c_str ()));
86 if (nil_uuid != ACE_Utils::UUID::NIL_UUID)
87 ACE_ERROR_RETURN ((LM_ERROR,
88 ACE_TEXT ("Error: UUIDs are not the same with ")
89 ACE_TEXT ("NIL_UUID string copy\n")),
90 -1);
91 #endif
93 // Construct UUID using the assignment constructor
94 ACE_Utils::UUID new_uuid_assigment;
95 new_uuid_assigment = new_uuid;
96 ACE_DEBUG ((LM_DEBUG,
97 ACE_TEXT ("UUID Constructed from above Generated UUID ")
98 ACE_TEXT ("with assignment\n %C\n"),
99 new_uuid_assigment.to_string ()->c_str ()));
101 if (new_uuid != new_uuid_assigment)
102 ACE_ERROR_RETURN ((LM_ERROR,
103 ACE_TEXT ("Error: UUIDs are not the same "
104 ACE_TEXT ("with assignment\n"))),
105 -1);
107 // Generate UUID with process and thread ids.
108 std::unique_ptr <ACE_Utils::UUID>
109 uuid_with_tp_id (ACE_Utils::UUID_GENERATOR::instance ()->generate_UUID (0x0001, 0xc0));
111 ACE_DEBUG ((LM_DEBUG,
112 ACE_TEXT ("UUID with Thread and Process ID\n %C\n"),
113 uuid_with_tp_id->to_string ()->c_str ()));
115 if (new_uuid == *uuid_with_tp_id)
116 ACE_ERROR_RETURN ((LM_ERROR,
117 ACE_TEXT ("Error: UUIDs are the same\n")),
118 -1);
120 #ifndef ACE_LACKS_SSCANF
121 // Construct UUID from string
122 ACE_Utils::UUID new_uuid_with_tp_id (uuid_with_tp_id->to_string ()->c_str ());
123 ACE_DEBUG ((LM_DEBUG,
124 ACE_TEXT ("UUID with Thread and Process ID reconstructed ")
125 ACE_TEXT ("from above UUID \n %C\n"),
126 new_uuid_with_tp_id.to_string ()->c_str ()));
127 #endif
129 return retval;
132 int run_main(int, ACE_TCHAR* [])
134 ACE_START_TEST (ACE_TEXT ("UUID_Test"));
136 Tester tester;
138 int const result = tester.test();
140 if (result == 0)
141 ACE_DEBUG((LM_DEBUG,
142 ACE_TEXT ("UUID_Test succeeded\n")));
143 else
144 ACE_ERROR((LM_ERROR,
145 ACE_TEXT ("UUID_Test failed\n")));
147 ACE_END_TEST;
149 return result;