ACE+TAO-7_0_8
[ACE_TAO.git] / ACE / tests / UUID_Test.cpp
blob972464ed2ceed2c0c1daea0039874612f8a47cdf
2 //=============================================================================
3 /**
4 * @file UUID_Test.cpp
6 * Test the ACE UUID class which generates unique id's
8 * @author Andrew T. Finnel <andrew@activesol.net> and Yamuna Krishnmaurthy <yamuna@oomworks.com>
9 */
10 //=============================================================================
13 #include "test_config.h"
14 #include "ace/UUID.h"
15 #include "ace/Auto_Ptr.h"
17 class Tester
19 public:
20 int test ();
23 int
24 Tester::test ()
26 int retval = 0;
28 // Generate UUID
29 std::unique_ptr <ACE_Utils::UUID> uuid (ACE_Utils::UUID_GENERATOR::instance ()->generate_UUID ());
30 ACE_CString uuid_str (uuid->to_string ()->c_str ());
31 ACE_DEBUG ((LM_DEBUG,
32 ACE_TEXT ("Generated UUID\n %C\n"),
33 uuid_str.c_str ()));
35 #ifndef ACE_LACKS_SSCANF
36 // Construct UUID from string
37 ACE_Utils::UUID new_uuid (uuid_str);
39 ACE_DEBUG ((LM_DEBUG,
40 ACE_TEXT ("UUID Constructed from above Generated UUID\n %C\n"),
41 new_uuid.to_string ()->c_str ()));
43 // Construct UUID from string by assigning it
44 ACE_Utils::UUID new_uuid_assign;
45 new_uuid_assign.from_string (new_uuid.to_string ()->c_str ());
46 ACE_DEBUG ((LM_DEBUG,
47 ACE_TEXT ("UUID Constructed from above Generated UUID ")
48 ACE_TEXT ("with assign\n %C\n"),
49 new_uuid_assign.to_string ()->c_str ()));
51 if (new_uuid != new_uuid_assign)
52 ACE_ERROR_RETURN ((LM_ERROR,
53 ACE_TEXT ("Error: UUIDs are not the same\n")),
54 -1);
56 // Check the hash value of the 2 UUIDs
58 if (new_uuid.hash () != new_uuid_assign.hash ())
59 ACE_ERROR_RETURN ((LM_ERROR,
60 ACE_TEXT ("Error: hash value of UUIDs are ")
61 ACE_TEXT ("not the same")),
62 -1);
64 #else
65 const ACE_Utils::UUID &new_uuid = *uuid;
66 #endif
68 // Construct UUID using the copy constructor
69 ACE_Utils::UUID new_uuid_copy (new_uuid);
70 ACE_DEBUG ((LM_DEBUG,
71 ACE_TEXT ("UUID constructed from above Generated UUID")
72 ACE_TEXT (" with copy\n %C\n"),
73 new_uuid_copy.to_string ()->c_str ()));
75 if (new_uuid != new_uuid_copy)
76 ACE_ERROR_RETURN ((LM_ERROR,
77 ACE_TEXT ("Error: UUIDs are not the same ")
78 ACE_TEXT ("with copy\n")),
79 -1);
81 #ifndef ACE_LACKS_SSCANF
82 ACE_Utils::UUID nil_uuid (*ACE_Utils::UUID::NIL_UUID.to_string ());
83 ACE_DEBUG ((LM_DEBUG,
84 ACE_TEXT ("UUID Constructed from NIL_UUID with ")
85 ACE_TEXT ("string copy\n %C\n"),
86 nil_uuid.to_string ()->c_str ()));
88 if (nil_uuid != ACE_Utils::UUID::NIL_UUID)
89 ACE_ERROR_RETURN ((LM_ERROR,
90 ACE_TEXT ("Error: UUIDs are not the same with ")
91 ACE_TEXT ("NIL_UUID string copy\n")),
92 -1);
93 #endif
95 // Construct UUID using the assignment constructor
96 ACE_Utils::UUID new_uuid_assigment;
97 new_uuid_assigment = new_uuid;
98 ACE_DEBUG ((LM_DEBUG,
99 ACE_TEXT ("UUID Constructed from above Generated UUID ")
100 ACE_TEXT ("with assignment\n %C\n"),
101 new_uuid_assigment.to_string ()->c_str ()));
103 if (new_uuid != new_uuid_assigment)
104 ACE_ERROR_RETURN ((LM_ERROR,
105 ACE_TEXT ("Error: UUIDs are not the same "
106 ACE_TEXT ("with assignment\n"))),
107 -1);
109 // Generate UUID with process and thread ids.
110 std::unique_ptr <ACE_Utils::UUID>
111 uuid_with_tp_id (ACE_Utils::UUID_GENERATOR::instance ()->generate_UUID (0x0001, 0xc0));
113 ACE_DEBUG ((LM_DEBUG,
114 ACE_TEXT ("UUID with Thread and Process ID\n %C\n"),
115 uuid_with_tp_id->to_string ()->c_str ()));
117 if (new_uuid == *uuid_with_tp_id)
118 ACE_ERROR_RETURN ((LM_ERROR,
119 ACE_TEXT ("Error: UUIDs are the same\n")),
120 -1);
122 #ifndef ACE_LACKS_SSCANF
123 // Construct UUID from string
124 ACE_Utils::UUID new_uuid_with_tp_id (uuid_with_tp_id->to_string ()->c_str ());
125 ACE_DEBUG ((LM_DEBUG,
126 ACE_TEXT ("UUID with Thread and Process ID reconstructed ")
127 ACE_TEXT ("from above UUID \n %C\n"),
128 new_uuid_with_tp_id.to_string ()->c_str ()));
129 #endif
131 return retval;
134 int run_main(int, ACE_TCHAR* [])
136 ACE_START_TEST (ACE_TEXT ("UUID_Test"));
138 Tester tester;
140 int const result = tester.test();
142 if (result == 0)
143 ACE_DEBUG((LM_DEBUG,
144 ACE_TEXT ("UUID_Test succeeded\n")));
145 else
146 ACE_ERROR((LM_ERROR,
147 ACE_TEXT ("UUID_Test failed\n")));
149 ACE_END_TEST;
151 return result;