summaryrefslogtreecommitdiff
path: root/ACE/tests/UUID_Test.cpp
blob: 972464ed2ceed2c0c1daea0039874612f8a47cdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

//=============================================================================
/**
 *  @file    UUID_Test.cpp
 *
 *  Test the ACE UUID class which generates unique id's
 *
 *  @author Andrew T. Finnel <andrew@activesol.net> and Yamuna Krishnmaurthy <yamuna@oomworks.com>
 */
//=============================================================================


#include "test_config.h"
#include "ace/UUID.h"
#include "ace/Auto_Ptr.h"

class Tester
{
public:
  int test ();
};

int
Tester::test ()
{
  int retval = 0;

  // Generate UUID
  std::unique_ptr <ACE_Utils::UUID> uuid (ACE_Utils::UUID_GENERATOR::instance ()->generate_UUID ());
  ACE_CString uuid_str (uuid->to_string ()->c_str ());
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("Generated UUID\n %C\n"),
              uuid_str.c_str ()));

#ifndef ACE_LACKS_SSCANF
  // Construct UUID from string
  ACE_Utils::UUID new_uuid (uuid_str);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("UUID Constructed from above Generated UUID\n %C\n"),
              new_uuid.to_string ()->c_str ()));

  // Construct UUID from string by assigning it
  ACE_Utils::UUID new_uuid_assign;
  new_uuid_assign.from_string (new_uuid.to_string ()->c_str ());
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("UUID Constructed from above Generated UUID ")
              ACE_TEXT ("with assign\n %C\n"),
              new_uuid_assign.to_string ()->c_str ()));

  if (new_uuid != new_uuid_assign)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Error: UUIDs are not the same\n")),
                       -1);

  // Check the hash value of the 2 UUIDs

  if (new_uuid.hash () != new_uuid_assign.hash ())
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Error: hash value of UUIDs are ")
                       ACE_TEXT ("not the same")),
                       -1);

#else
  const ACE_Utils::UUID &new_uuid = *uuid;
#endif

  // Construct UUID using the copy constructor
  ACE_Utils::UUID new_uuid_copy (new_uuid);
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("UUID constructed from above Generated UUID")
              ACE_TEXT (" with copy\n %C\n"),
              new_uuid_copy.to_string ()->c_str ()));

  if (new_uuid != new_uuid_copy)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Error: UUIDs are not the same ")
                       ACE_TEXT ("with copy\n")),
                       -1);

#ifndef ACE_LACKS_SSCANF
  ACE_Utils::UUID nil_uuid (*ACE_Utils::UUID::NIL_UUID.to_string ());
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("UUID Constructed from NIL_UUID with ")
              ACE_TEXT ("string copy\n %C\n"),
              nil_uuid.to_string ()->c_str ()));

  if (nil_uuid != ACE_Utils::UUID::NIL_UUID)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Error: UUIDs are not the same with ")
                       ACE_TEXT ("NIL_UUID string copy\n")),
                       -1);
#endif

  // Construct UUID using the assignment constructor
  ACE_Utils::UUID new_uuid_assigment;
  new_uuid_assigment = new_uuid;
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("UUID Constructed from above Generated UUID ")
              ACE_TEXT ("with assignment\n %C\n"),
              new_uuid_assigment.to_string ()->c_str ()));

  if (new_uuid != new_uuid_assigment)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Error: UUIDs are not the same "
                       ACE_TEXT ("with assignment\n"))),
                       -1);

  // Generate UUID with process and thread ids.
  std::unique_ptr <ACE_Utils::UUID>
    uuid_with_tp_id (ACE_Utils::UUID_GENERATOR::instance ()->generate_UUID (0x0001, 0xc0));

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("UUID with Thread and Process ID\n %C\n"),
              uuid_with_tp_id->to_string ()->c_str ()));

  if (new_uuid == *uuid_with_tp_id)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Error: UUIDs are the same\n")),
                       -1);

#ifndef ACE_LACKS_SSCANF
  // Construct UUID from string
  ACE_Utils::UUID new_uuid_with_tp_id (uuid_with_tp_id->to_string ()->c_str ());
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("UUID with Thread and Process ID reconstructed ")
              ACE_TEXT ("from above UUID \n %C\n"),
              new_uuid_with_tp_id.to_string ()->c_str ()));
#endif

  return retval;
}

int run_main(int, ACE_TCHAR* [])
{
  ACE_START_TEST (ACE_TEXT ("UUID_Test"));

  Tester tester;

  int const result = tester.test();

  if (result == 0)
    ACE_DEBUG((LM_DEBUG,
               ACE_TEXT ("UUID_Test succeeded\n")));
  else
    ACE_ERROR((LM_ERROR,
               ACE_TEXT ("UUID_Test failed\n")));

  ACE_END_TEST;

  return result;
}