VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstUuid.cpp@ 20554

Last change on this file since 20554 was 19205, checked in by vboxsync, 16 years ago

tstUuid: Updated to RTTest and added tests for the new Utf16 functions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.7 KB
Line 
1/* $Id: tstUuid.cpp 19205 2009-04-27 10:03:40Z vboxsync $ */
2/** @file
3 * IPRT Testcase - UUID.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/uuid.h>
36#include <iprt/test.h>
37#include <iprt/stream.h>
38#include <iprt/err.h>
39#include <iprt/string.h>
40#include <iprt/initterm.h>
41
42
43int main(int argc, char **argv)
44{
45 int rc;
46 RTTEST hTest;
47 if ( RT_FAILURE(rc = RTR3Init())
48 || RT_FAILURE(rc = RTTestCreate("tstUuid", &hTest)))
49 {
50 RTPrintf("tstUuid: RTR3Init or RTTestCreate failed: %Rrc\n", rc);
51 return 1;
52 }
53 RTTestBanner(hTest);
54
55
56#define CHECK_RC() \
57 do { if (RT_FAILURE(rc)) { RTTestFailed(hTest, "line %d: rc=%Rrc", __LINE__, rc); } } while (0)
58
59 RTTestSub(hTest, "RTUuidClear & RTUuisIsNull");
60 RTUUID UuidNull;
61 rc = RTUuidClear(&UuidNull); CHECK_RC();
62
63 RTTEST_CHECK(hTest, RTUuidIsNull(&UuidNull));
64 RTTEST_CHECK(hTest, RTUuidCompare(&UuidNull, &UuidNull) == 0);
65
66 RTTestSub(hTest, "RTUuidCreate");
67 RTUUID Uuid;
68 rc = RTUuidCreate(&Uuid); CHECK_RC();
69 RTTEST_CHECK(hTest, !RTUuidIsNull(&Uuid));
70 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid) == 0);
71 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &UuidNull) > 0);
72 RTTEST_CHECK(hTest, RTUuidCompare(&UuidNull, &Uuid) < 0);
73
74 RTTestSub(hTest, "RTUuidToStr");
75 char sz[RTUUID_STR_LENGTH];
76 rc = RTUuidToStr(&Uuid, sz, sizeof(sz)); CHECK_RC();
77 RTTEST_CHECK(hTest, strlen(sz) == RTUUID_STR_LENGTH - 1);
78 RTTestPrintf(hTest, RTTESTLVL_INFO, "UUID=%s\n", sz);
79
80 RTTestSub(hTest, "RTUuidFromStr");
81 RTUUID Uuid2;
82 rc = RTUuidFromStr(&Uuid2, sz); CHECK_RC();
83 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
84
85 RTTestSub(hTest, "RTUuidToUtf16");
86 RTUTF16 wsz[RTUUID_STR_LENGTH];
87 rc = RTUuidToUtf16(&Uuid, wsz, sizeof(wsz)); CHECK_RC();
88 RTTEST_CHECK(hTest, RTUtf16Len(wsz) == RTUUID_STR_LENGTH - 1);
89
90 RTTestSub(hTest, "RTUuidFromUtf16");
91 rc = RTUuidFromUtf16(&Uuid2, wsz); CHECK_RC();
92 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid2) == 0);
93
94
95 /*
96 * Check the binary representation.
97 */
98 RTTestSub(hTest, "Binary representation");
99 RTUUID Uuid3;
100 Uuid3.au8[0] = 0x01;
101 Uuid3.au8[1] = 0x23;
102 Uuid3.au8[2] = 0x45;
103 Uuid3.au8[3] = 0x67;
104 Uuid3.au8[4] = 0x89;
105 Uuid3.au8[5] = 0xab;
106 Uuid3.au8[6] = 0xcd;
107 Uuid3.au8[7] = 0x4f;
108 Uuid3.au8[8] = 0x10;
109 Uuid3.au8[9] = 0xb2;
110 Uuid3.au8[10] = 0x54;
111 Uuid3.au8[11] = 0x76;
112 Uuid3.au8[12] = 0x98;
113 Uuid3.au8[13] = 0xba;
114 Uuid3.au8[14] = 0xdc;
115 Uuid3.au8[15] = 0xfe;
116 Uuid3.Gen.u8ClockSeqHiAndReserved = (Uuid3.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
117 Uuid3.Gen.u16TimeHiAndVersion = (Uuid3.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
118 const char *pszUuid3 = "67452301-ab89-4fcd-90b2-547698badcfe";
119 rc = RTUuidToStr(&Uuid3, sz, sizeof(sz)); CHECK_RC();
120 RTTEST_CHECK(hTest, strcmp(sz, pszUuid3) == 0);
121 rc = RTUuidFromStr(&Uuid, pszUuid3); CHECK_RC();
122 RTTEST_CHECK(hTest, RTUuidCompare(&Uuid, &Uuid3) == 0);
123 RTTEST_CHECK(hTest, memcmp(&Uuid3, &Uuid, sizeof(Uuid)) == 0);
124
125 /*
126 * checking the clock seq and time hi and version bits...
127 */
128 RTTestSub(hTest, "Clock seq, time hi, version bits");
129 RTUUID Uuid4Changes;
130 Uuid4Changes.au64[0] = 0;
131 Uuid4Changes.au64[1] = 0;
132
133 RTUUID Uuid4Prev;
134 RTUuidCreate(&Uuid4Prev);
135
136 for (unsigned i = 0; i < 1024; i++)
137 {
138 RTUUID Uuid4;
139 RTUuidCreate(&Uuid4);
140
141 Uuid4Changes.au64[0] |= Uuid4.au64[0] ^ Uuid4Prev.au64[0];
142 Uuid4Changes.au64[1] |= Uuid4.au64[1] ^ Uuid4Prev.au64[1];
143
144#if 0 /** @todo make a bit string/dumper similar to %Rhxs/d. */
145 RTPrintf("tstUuid: %d %d %d %d-%d %d %d %d %d %d %d %d-%d %d %d %d ; %d %d %d %d-%d %d %d %d %d %d %d %d-%d %d %d %d\n",
146 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(0)),
147 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(1)),
148 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(2)),
149 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(3)),
150 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(4)),
151 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(5)),
152 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(6)),
153 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(7)),
154 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(8)),
155 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(9)),
156 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(10)),
157 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(11)),
158 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(12)),
159 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(13)),
160 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(14)),
161 !!(Uuid4.Gen.u16ClockSeq & RT_BIT(15)),
162
163 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(0)),
164 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(1)),
165 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(2)),
166 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(3)),
167 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(4)),
168 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(5)),
169 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(6)),
170 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(7)),
171 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(8)),
172 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(9)),
173 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(10)),
174 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(11)),
175 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(12)),
176 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(13)),
177 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(14)),
178 !!(Uuid4.Gen.u16TimeHiAndVersion & RT_BIT(15))
179 );
180#endif
181 Uuid4Prev = Uuid4;
182 }
183
184 RTUUID Uuid4Fixed;
185 Uuid4Fixed.au64[0] = ~Uuid4Changes.au64[0];
186 Uuid4Fixed.au64[1] = ~Uuid4Changes.au64[1];
187 RTTestPrintf(hTest, RTTESTLVL_INFO, "fixed bits: %RTuuid (mask)\n", &Uuid4Fixed);
188 RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: raw: %.*Rhxs\n", sizeof(Uuid4Fixed), &Uuid4Fixed);
189
190 Uuid4Prev.au64[0] &= Uuid4Fixed.au64[0];
191 Uuid4Prev.au64[1] &= Uuid4Fixed.au64[1];
192 RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: fixed bits: %RTuuid (value)\n", &Uuid4Prev);
193 RTTestPrintf(hTest, RTTESTLVL_INFO, "tstUuid: raw: %.*Rhxs\n", sizeof(Uuid4Prev), &Uuid4Prev);
194
195 /*
196 * Summary.
197 */
198 return RTTestSummaryAndDestroy(hTest);
199}
200
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette