Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TestUUID.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
21 
22 
23 /**
24  @file TestUUID.cpp
25  @brief Tests class UUID
26  @relates UUID
27  @ingroup g_tests
28  @author woelk / evers
29  @date 2010
30 */
31 
32 
33 
34 #include <Base/Image/UUID.hh>
35 
36 #include <vector>
37 
38 using namespace BIAS;
39 using namespace std;
40 
41 void Check(const std::vector<BIAS::UUID> &uuids);
42 void ResetFromString(std::vector<BIAS::UUID> &uuids);
43 void ResetFromLong(std::vector<BIAS::UUID> &uuids);
44 void ResetFromRaw(std::vector<BIAS::UUID> &uuids);
45 
46 int main(int argc, char *argv[])
47 {
48  std::vector<BIAS::UUID> uuids_consecutive, uuids_global;
49  const unsigned num = 100;
50  // generate the uuids
51  for (unsigned i=0; i<num; i++){
52  uuids_consecutive.push_back(BIAS::UUID::GenerateUUID(true));
53  if (!uuids_consecutive.back().IsConsecutive()){
54  BIASABORT;
55  }
56  // false means: not consecutive , so checking for consective order is
57  // not recommended.
58  uuids_global.push_back(BIAS::UUID::GenerateUUID(false));
59  /*if (uuids_global.back().IsConsecutive()){
60  BIASABORT;
61  }*/
62  }
63  Check(uuids_consecutive);
64  Check(uuids_global);
65 
66  ResetFromString(uuids_consecutive);
67  ResetFromString(uuids_global);
68 
69  Check(uuids_consecutive);
70  Check(uuids_global);
71 
72  // ResetFromLong is only valid for consecutive uuids
73  ResetFromLong(uuids_consecutive);
74  Check(uuids_consecutive);
75 
76  ResetFromRaw(uuids_consecutive);
77  ResetFromRaw(uuids_global);
78 
79  Check(uuids_consecutive);
80  Check(uuids_global);
81 
82  for (unsigned i=0; i<num; i++){
83  uuids_consecutive[i].clear();
84  if ( !uuids_consecutive[i].IsNil() || uuids_consecutive[i].IsValid() ){
85  BIASABORT;
86  }
87  uuids_global[i].Invalidate();
88  if ( !uuids_global[i].IsNil() || uuids_global[i].IsValid() ){
89  BIASABORT;
90  }
91  }
92 
93  // empyt constructor must return invalid uid
94  BIAS::UUID uid;
95  if ( !uid.IsNil() || uid.IsValid() ){
96  BIASABORT;
97  }
98 
99  return 0;
100 }
101 
102 
103 void Check(const std::vector<BIAS::UUID> &uuids)
104 {
105  // check if they are unique and non null
106  const unsigned num = uuids.size();
107  for (unsigned i=0; i<num; i++){
108  if (uuids[i].IsNil()){
109  cerr << "uuid is nil: "<<uuids[i]<<endl;
110  BIASABORT;
111  }
112  if (!uuids[i].IsValid()){
113  cerr << "uuid is not valid: "<<uuids[i]<<endl;
114  BIASABORT;
115  }
116  for (unsigned k=0; k<num; k++){
117  if (i==k) continue;
118  if (uuids[i]==uuids[k]){
119  cerr << "uids are not unique: \n "<<uuids[i]<<"\n "<<uuids[k]<<endl;
120  BIASABORT;
121  }
122  }
123  }
124 }
125 
126 
127 void ResetFromString(std::vector<BIAS::UUID> &uuids)
128 {
129  BIAS::UUID tmpuid;
130  string tmp;
131  const unsigned num = uuids.size();
132  for (unsigned i=0; i<num; i++){
133  tmp = uuids[i].GetString();
134  if (!tmpuid.SetFromString(tmp)){
135  BIASABORT;
136  }
137  if (tmpuid!=uuids[i]){
138  BIASABORT;
139  }
140  uuids[i] = tmpuid;
141  }
142 }
143 
144 void ResetFromLong(std::vector<BIAS::UUID> &uuids)
145 {
146  BIAS::UUID tmpuid;
147  unsigned long tmp;
148  const unsigned num = uuids.size();
149  for (unsigned i=0; i<num; i++){
150  tmp = uuids[i].GetLong();
151  if (!tmpuid.SetFromLong(tmp)){
152  BIASABORT;
153  }
154  if (tmpuid!=uuids[i]){
155  cerr << "re-setting uuis from long does not work: "<<tmp
156  << "\n "<<uuids[i]<<"\n "<<tmpuid<<endl;
157  BIASABORT;
158  }
159  uuids[i] = tmpuid;
160  }
161 }
162 
163 
164 void ResetFromRaw(std::vector<BIAS::UUID> &uuids)
165 {
166  BIAS::UUID tmpuid;
167  const unsigned num = uuids.size();
168 #ifndef WIN32
169  for (unsigned i=0; i<num; i++){
170  const unsigned char *tmp = uuids[i].GetUUIDRaw();
171  tmpuid.SetUUIDRaw(tmp);
172  if (tmpuid!=uuids[i]){
173  BIASABORT;
174  }
175  uuids[i] = tmpuid;
176  }
177 #endif
178 }
bool IsNil() const
Definition: UUID.cpp:108
bool SetFromString(const std::string &sID)
construct from string containing ascii uuid.
Definition: UUID.cpp:130
bool SetFromLong(const unsigned long &n)
construct from a unique long.
Definition: UUID.cpp:158
interface class for producing/storing Universally Unique IDentifiers
Definition: UUID.hh:98
static UUID GenerateUUID(const bool &consecutively=DEFAULT_UUID_CONSECUTIVELY)
static function which simply produces a uuid and returns
Definition: UUID.cpp:235
bool IsValid() const
checks whether this uuid is valid(true) or unitialized(false)
Definition: UUID.hh:210
void SetUUIDRaw(const unsigned char *rawid)
Definition: UUID.cpp:474