merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / qa / rtl / uuid / rtl_Uuid.cxx
blob0c4bcefe66acac1d8456938cb69faf7de516381a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
31 #include <math.h>
32 #include <stdio.h>
34 #include <testshl/simpleheader.hxx>
35 #include <rtl/uuid.h>
36 #include <rtl/ustring.h>
37 #include <rtl/ustring.hxx>
39 #ifdef UNX
40 #include <unistd.h>
41 #include <time.h>
42 #endif
44 using namespace rtl;
46 /** print a UNI_CODE String. And also print some comments of the string.
48 inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
50 t_print("#%s #printUString_u# ", msg );
51 rtl::OString aString;
52 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
53 t_print("%s\n", (char *)aString.getStr( ) );
56 /************************************************************************
57 * For diagnostics( from sal/test/testuuid.cxx )
58 ************************************************************************/
60 void printUuid( sal_uInt8 *pNode )
62 for( sal_Int32 i1 = 0 ; i1 < 4 ; i1++ )
64 for( sal_Int32 i2 = 0 ; i2 < 4 ; i2++ )
66 printf( "%02x" , pNode[i1*4 +i2] );
68 if( i1 == 3 )
69 break;
70 printf( "-" );
73 printf( "\n# " );
76 namespace rtl_Uuid
78 class createUuid : public CppUnit::TestFixture
80 public:
81 // initialise your test code values here.
82 void setUp()
86 void tearDown()
90 #define TEST_UUID 20
91 void createUuid_001()
93 sal_uInt8 aNode[TEST_UUID][16];
94 sal_Int32 i,i2;
95 for( i = 0 ; i < TEST_UUID ; i ++ )
97 rtl_createUuid( aNode[i], 0, sal_False );
99 sal_Bool bRes = sal_True;
100 for( i = 0 ; i < TEST_UUID ; i ++ )
102 for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
104 if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0 )
106 bRes = sal_False;
107 break;
110 if ( bRes == sal_False )
111 break;
113 CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes == sal_True );
116 void createUuid_002()
118 sal_uInt8 pNode[16];
119 sal_uInt8 aNode[TEST_UUID][16];
120 sal_Int32 i,i2;
121 for( i = 0 ; i < TEST_UUID ; i ++ )
123 rtl_createUuid( aNode[i], pNode, sal_True );
125 sal_Bool bRes = sal_True;
126 for( i = 0 ; i < TEST_UUID ; i ++ )
128 //printUuid( aNode[i] );
129 for( i2 = i+1 ; i2 < TEST_UUID ; i2 ++ )
131 if ( rtl_compareUuid( aNode[i] , aNode[i2] ) == 0 )
133 bRes = sal_False;
134 break;
137 if ( bRes == sal_False )
138 break;
140 CPPUNIT_ASSERT_MESSAGE("createUuid: every uuid must be different.", bRes == sal_True );
143 CPPUNIT_TEST_SUITE(createUuid);
144 CPPUNIT_TEST(createUuid_001);
145 //CPPUNIT_TEST(createUuid_002);
146 CPPUNIT_TEST_SUITE_END();
147 }; // class createUuid
149 namespace ThreadHelper
151 void thread_sleep(sal_Int32 _nSec)
153 #ifdef WNT //Windows
154 Sleep(_nSec * 10 );
155 #endif
156 #if ( defined UNX ) || ( defined OS2 ) //Unix
157 sleep( _nSec );
158 #endif
162 class createNamedUuid : public CppUnit::TestFixture
164 public:
165 // initialise your test code values here.
166 void setUp()
170 void tearDown()
174 void createNamedUuid_001()
176 sal_uInt8 NameSpace_DNS[16] = RTL_UUID_NAMESPACE_DNS;
177 sal_uInt8 NameSpace_URL[16] = RTL_UUID_NAMESPACE_URL;
178 sal_uInt8 pPriorCalculatedUUID[16] = {
179 0x52,0xc9,0x30,0xa5,
180 0xd1,0x61,0x3b,0x16,
181 0x98,0xc5,0xf8,0xd1,
182 0x10,0x10,0x6d,0x4d };
184 sal_uInt8 pNamedUUID[16], pNamedUUID2[16];
186 // Same name does generate the same uuid
187 rtl_String *pName = 0;
188 rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Name" );
189 rtl_createNamedUuid( pNamedUUID , NameSpace_DNS , pName );
190 rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
191 CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) == 0 );
192 CPPUNIT_ASSERT_MESSAGE( "Same name should generate the same uuid", ! memcmp( pNamedUUID , pPriorCalculatedUUID , 16 ) );
194 // Different names does not generate the same uuid
195 rtl_string_newFromStr( &pName , "this is a bla.blubs.DNS-Namf" );
196 rtl_createNamedUuid( pNamedUUID2 , NameSpace_DNS , pName );
197 CPPUNIT_ASSERT_MESSAGE("Different names does not generate the same uuid.", memcmp( pNamedUUID , pNamedUUID2 , 16 ) );
199 // the same name with different namespace uuid produces different uuids
200 rtl_createNamedUuid( pNamedUUID , NameSpace_URL , pName );
201 CPPUNIT_ASSERT_MESSAGE( " same name with different namespace uuid produces different uuids", memcmp( pNamedUUID , pNamedUUID2 , 16 ) && rtl_compareUuid( pNamedUUID , pNamedUUID2 ) != 0);
203 //test compareUuid
204 if ( rtl_compareUuid( pNamedUUID , pNamedUUID2 ) > 0 )
205 { CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) < 0);
207 else
208 CPPUNIT_ASSERT_MESSAGE( " compare uuids", rtl_compareUuid( pNamedUUID2 , pNamedUUID ) > 0);
210 rtl_string_release( pName );
213 CPPUNIT_TEST_SUITE(createNamedUuid);
214 CPPUNIT_TEST(createNamedUuid_001);
215 CPPUNIT_TEST_SUITE_END();
216 }; // class createNamedUuid
218 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Uuid::createUuid, "rtl_Uuid");
219 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Uuid::createNamedUuid, "rtl_Uuid");
220 } // namespace rtl_Uuid
222 // -----------------------------------------------------------------------------
224 // this macro creates an empty function, which will called by the RegisterAllFunctions()
225 // to let the user the possibility to also register some functions by hand.
226 NOADDITIONAL;