xref: /trunk/main/stoc/test/testconv.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_stoc.hxx"
30 
31 #include <sal/main.h>
32 #include <osl/diagnose.h>
33 #include <rtl/ustrbuf.hxx>
34 #include <cppuhelper/servicefactory.hxx>
35 
36 #include <com/sun/star/lang/XComponent.hpp>
37 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <com/sun/star/script/XTypeConverter.hpp>
39 #include <com/sun/star/reflection/FieldAccessMode.hpp>
40 #include <com/sun/star/registry/XImplementationRegistration.hpp>
41 
42 #include <float.h>
43 #include <stdio.h>
44 
45 
46 using namespace rtl;
47 using namespace cppu;
48 using namespace osl;
49 using namespace com::sun::star::uno;
50 using namespace com::sun::star::lang;
51 using namespace com::sun::star::script;
52 using namespace com::sun::star::reflection;
53 using namespace com::sun::star::registry;
54 
55 const double MIN_DOUBLE     = -DBL_MAX;
56 const double MAX_DOUBLE     = DBL_MAX;
57 const double MIN_FLOAT      = -FLT_MAX;
58 const double MAX_FLOAT      = FLT_MAX;
59 
60 //==================================================================================================
61 static void printValue( const Any & rVal )
62 {
63     // print value
64     OString aStr( OUStringToOString( rVal.getValueType().getTypeName(), RTL_TEXTENCODING_ISO_8859_1 ) );
65     printf( "(%s)", aStr.getStr() );
66 
67     switch (rVal.getValueTypeClass())
68     {
69     case TypeClass_VOID:
70         printf( "void" );
71         break;
72     case TypeClass_ANY:
73         if (rVal.hasValue())
74             printValue( *(Any *)rVal.getValue() );
75         break;
76     case TypeClass_BOOLEAN:
77         printf( "%s", (*(sal_Bool *)rVal.getValue() ? "true" : "false") );
78         break;
79     case TypeClass_CHAR:
80     {
81         char ar[2];
82         ar[0] = (char)*(sal_Unicode *)rVal.getValue();
83         ar[1] = 0;
84         printf( ar );
85         break;
86     }
87     case TypeClass_BYTE:
88         printf( "%x", (int)*(sal_Int8 *)rVal.getValue() );
89         break;
90     case TypeClass_SHORT:
91         printf( "%x", *(sal_Int16 *)rVal.getValue() );
92         break;
93     case TypeClass_UNSIGNED_SHORT:
94         printf( "%x", *(sal_uInt16 *)rVal.getValue() );
95         break;
96     case TypeClass_LONG:
97         printf( "%lx", static_cast<long>(*(sal_Int32 *)rVal.getValue()) );
98         break;
99     case TypeClass_UNSIGNED_LONG:
100         printf( "%lx", static_cast<unsigned long>(*(sal_uInt32 *)rVal.getValue()) );
101         break;
102     case TypeClass_HYPER:
103         printf( "%lx", (long)*(sal_Int64 *)rVal.getValue() );
104         break;
105     case TypeClass_UNSIGNED_HYPER:
106         printf( "%lx", (unsigned long)*(sal_uInt64 *)rVal.getValue() );
107         break;
108     case TypeClass_FLOAT:
109         printf( "%f", *(float *)rVal.getValue() );
110         break;
111     case TypeClass_DOUBLE:
112         printf( "%g", *(double *)rVal.getValue() );
113         break;
114     case TypeClass_STRING:
115     {
116         OString aStr2( OUStringToOString( *(OUString *)rVal.getValue(), RTL_TEXTENCODING_ISO_8859_1 ) );
117         printf( aStr2.getStr() );
118         break;
119     }
120     case TypeClass_ENUM:
121     {
122         typelib_EnumTypeDescription * pEnumTD = 0;
123         TYPELIB_DANGER_GET( (typelib_TypeDescription **)&pEnumTD, rVal.getValueTypeRef() );
124 
125         for ( sal_Int32 nPos = pEnumTD->nEnumValues; nPos--; )
126         {
127             if (pEnumTD->pEnumValues[nPos] == *(int *)rVal.getValue())
128             {
129                 printf( OUStringToOString(pEnumTD->ppEnumNames[nPos]->buffer, RTL_TEXTENCODING_ASCII_US).getStr() );
130                 TYPELIB_DANGER_RELEASE( (typelib_TypeDescription *)pEnumTD );
131                 return;
132             }
133         }
134         TYPELIB_DANGER_RELEASE( (typelib_TypeDescription *)pEnumTD );
135         printf( ">ENUM not found!<" );
136         break;
137     }
138     case TypeClass_SEQUENCE:
139     {
140         uno_Sequence * pSeq = *(uno_Sequence **)rVal.getValue();
141         typelib_TypeDescription * pSeqTD = 0;
142         TYPELIB_DANGER_GET( &pSeqTD, rVal.getValueTypeRef() );
143         typelib_TypeDescription * pElemTD = 0;
144         TYPELIB_DANGER_GET( &pElemTD, ((typelib_IndirectTypeDescription *)pSeqTD)->pType );
145 
146         sal_Int32 nLen = pSeq->nElements;
147         if (nLen)
148         {
149             printf( "{ " );
150             for ( sal_Int32 nPos = 0; nPos < nLen; ++nPos )
151             {
152                 printValue( Any( ((char *)pSeq->elements) + (nPos * pElemTD->nSize), pElemTD ) );
153                 if (nPos < (nLen-1))
154                     printf( ", " );
155             }
156             printf( " }" );
157         }
158 
159         TYPELIB_DANGER_RELEASE( pElemTD );
160         TYPELIB_DANGER_RELEASE( pSeqTD );
161         break;
162     }
163 
164     default:
165         printf( ">not printable<" );
166         break;
167     }
168 }
169 
170 static Reference< XTypeConverter > s_xConverter;
171 
172 //==================================================================================================
173 static sal_Bool convertTo( const Type & rDestType, const Any & rVal, sal_Bool bExpectSuccess )
174 {
175     sal_Bool bCanConvert = sal_False;
176     Any aRet;
177 
178     OString aExcMsg;
179 
180     try
181     {
182         aRet = s_xConverter->convertTo( rVal, rDestType );
183         bCanConvert = sal_True;
184     }
185     catch (Exception & rExc)
186     {
187         aExcMsg = OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US );
188     }
189 
190     if (bExpectSuccess && !bCanConvert)
191     {
192         printf( "# conversion of " );
193         printValue( rVal );
194         printf( " to " );
195         printf( OUStringToOString(rDestType.getTypeName(), RTL_TEXTENCODING_ASCII_US).getStr() );
196         printf( " failed, but success was expected! [" );
197         printf( aExcMsg.getStr() );
198         printf( "]\n" );
199         aRet = s_xConverter->convertTo( rVal, rDestType );
200 #if OSL_DEBUG_LEVEL > 1
201         // for debugging, to trace again
202         try
203         {
204             aRet = s_xConverter->convertTo( rVal, rDestType );
205         }
206         catch (Exception &)
207         {
208         }
209 #endif
210         return sal_False;
211     }
212     if (!bExpectSuccess && bCanConvert)
213     {
214         printf( "# conversion of " );
215         printValue( rVal );
216         printf( " to " );
217         printValue( aRet );
218         printf( " was successful, but was not expected to be!\n" );
219 #if OSL_DEBUG_LEVEL > 1
220         // for debugging, to trace again
221         aRet = s_xConverter->convertTo( rVal, rDestType );
222 #endif
223         return sal_False;
224     }
225 
226 #ifdef __RECONVERSION_OUTPUT__
227 //= re-conversion output =
228     if (bCanConvert)
229     {
230         // re convert to original type
231         sal_Bool bReConvert = sal_False;
232         Any aRet2;
233 
234         try
235         {
236             aRet2 = s_xConverter->convertTo( aRet, rVal.getValueType() );
237             bReConvert = sal_True;
238         }
239         catch (Exception & rExc)
240         {
241             aExcMsg = OUStringToOString( rExc.Message, RTL_TEXTENCODING_ISO_8859_1 );
242         }
243 
244         if (bReConvert)
245         {
246             if (rVal != aRet2)
247             {
248                 printf( "# re-conversion of " );
249                 printValue( rVal );
250                 printf( " to " );
251                 printValue( aRet );
252                 printf( " to " );
253                 printValue( aRet2 );
254                 printf( ": first and last do not match!\n" );
255             }
256         }
257         else
258         {
259             printf( "# re-conversion of " );
260             printValue( aRet );
261             printf( " to " );
262             printf( rVal.getValueType().getTypeName().getStr() );
263             printf( " failed! [" );
264             printf( aExcMsg.getStr() );
265             printf( "]\n" );
266         }
267     }
268 #endif
269 
270     return sal_True;
271 }
272 
273 
274 //==================================================================================================
275 typedef struct _ConvBlock
276 {
277     Any         _value;
278     sal_Bool    _toString, _toDouble, _toFloat;
279     sal_Bool    _toUINT32, _toINT32, _toUINT16, _toINT16, _toBYTE, _toBOOL, _toChar;
280     sal_Bool    _toTypeClass, _toSeqINT16, _toSeqAny;
281 
282     _ConvBlock()
283     {
284     }
285     _ConvBlock( const Any & rValue_,
286                 sal_Bool toString_, sal_Bool toDouble_, sal_Bool toFloat_,
287                 sal_Bool toUINT32_, sal_Bool toINT32_, sal_Bool toUINT16_, sal_Bool toINT16_,
288                 sal_Bool toBYTE_, sal_Bool toBOOL_, sal_Bool toChar_,
289                 sal_Bool toTypeClass_, sal_Bool toSeqINT16_, sal_Bool toSeqAny_ )
290         : _value( rValue_ )
291         , _toString( toString_ ), _toDouble( toDouble_ ), _toFloat( toFloat_ )
292         , _toUINT32( toUINT32_ ), _toINT32( toINT32_ ), _toUINT16( toUINT16_ ), _toINT16( toINT16_ )
293         , _toBYTE( toBYTE_ ), _toBOOL( toBOOL_ ), _toChar( toChar_ )
294         , _toTypeClass( toTypeClass_ ), _toSeqINT16( toSeqINT16_ ), _toSeqAny( toSeqAny_ )
295     {
296     }
297 } ConvBlock;
298 
299 
300 //==================================================================================================
301 static sal_Int32 initBlocks( ConvBlock * pTestBlocks )
302 {
303     Any aVal;
304 
305     sal_uInt32 nElems = 0;
306 
307     // ==BYTE==
308     aVal <<= OUString::createFromAscii( "0xff" );
309     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 );
310     aVal <<= OUString::createFromAscii( "255" );
311     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 );
312     aVal <<= (sal_Int8)0xffu;
313     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0 );
314                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
315     aVal <<= OUString::createFromAscii( "0x80" );
316     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 );
317     aVal <<= OUString::createFromAscii( "128" );
318     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 );
319     aVal <<= (sal_Int8)( 0x80u );
320     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0 );
321                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
322     aVal <<= OUString::createFromAscii( "0x7f" );
323     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
324     aVal <<= OUString::createFromAscii( "127" );
325     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
326     aVal <<= (sal_Int8)( 0x7f );
327     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 );
328                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
329     aVal <<= OUString::createFromAscii( "5" );
330     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0 );
331     aVal <<= OUString::createFromAscii( "+5" );
332     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
333     aVal <<= (sal_Int8)( 5 );
334     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 );
335                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
336     aVal <<= OUString::createFromAscii( "-5" );
337     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0 );
338                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
339     aVal <<= (sal_Int8)( -5 );
340     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0 );
341                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
342     aVal <<= OUString::createFromAscii( "256" );
343     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 );
344                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
345     // ==UINT16==
346     aVal <<= OUString::createFromAscii( "65535" );
347     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 );
348     aVal <<= OUString::createFromAscii( "0xffff" );
349     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 );
350     aVal <<= (sal_uInt16)( 0xffff );
351     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0 );
352                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
353     aVal <<= OUString::createFromAscii( "32768" );
354     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 );
355     aVal <<= (sal_uInt16)( 0x8000 );
356     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0 );
357                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
358     aVal <<= OUString::createFromAscii( "32767" );
359     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 );
360     aVal <<= OUString::createFromAscii( "0x7fff" );
361     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 );
362     aVal <<= (sal_uInt16)( 0x7fff );
363     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0 );
364                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
365     aVal <<= OUString::createFromAscii( "256" );
366     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 );
367     aVal <<= OUString::createFromAscii( "0x100" );
368     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 );
369     aVal <<= (sal_uInt16)( 0x100 );
370     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0 );
371                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
372     aVal <<= (sal_uInt16)( 5 );
373     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 );
374                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
375     aVal <<= (sal_uInt16)( -5 ); // is 0xfffb
376     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0 );
377                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
378     // ==INT16==
379     aVal <<= (sal_Int16)( -1 );
380     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0 );
381                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
382     aVal <<= (sal_Int16)( -0x8000 );
383     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0 );
384                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
385     aVal <<= (sal_Int16)( 0x7fff );
386     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0 );
387                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
388     aVal <<= (sal_Int16)( 0x100 );
389     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0 );
390                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
391     aVal <<= (sal_Int16)( 5 );
392     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 );
393                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
394     aVal <<= (sal_Int16)( -5 );
395     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0 );
396                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
397     // ==UINT32==
398     aVal <<= OUString::createFromAscii( "+4294967295" );
399     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
400     aVal <<= OUString::createFromAscii( "4294967295" );
401     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
402     aVal <<= OUString::createFromAscii( "0xffffffff" );
403     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
404     aVal <<= (sal_uInt32)( 0xffffffff );
405     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
406                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
407     aVal <<= OUString::createFromAscii( "-2147483648" );
408     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 );
409     aVal <<= OUString::createFromAscii( "-0x80000000" );
410     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 );
411                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
412     aVal <<= (sal_uInt32)( 0x80000000 );
413     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
414                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
415     aVal <<= OUString::createFromAscii( "2147483647" );
416     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 );
417     aVal <<= OUString::createFromAscii( "0x7fffffff" );
418     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 );
419     aVal <<= (sal_uInt32)( 0x7fffffff );
420     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0 );
421                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
422     aVal <<= OUString::createFromAscii( "65536" );
423     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 );
424     aVal <<= OUString::createFromAscii( "0x10000" );
425     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 );
426     aVal <<= (sal_uInt32)( 0x10000 );
427     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0 );
428                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
429     aVal <<= (sal_uInt32)( 0x8000 );
430     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0 );
431                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
432     aVal <<= (sal_uInt32)( 5 );
433     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 );
434                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
435     aVal <<= OUString::createFromAscii( "0xfffffffb" );
436     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
437     aVal <<= (sal_uInt32)( -5 ); // is 0xfffffffb
438     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
439                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
440     // ==INT32==
441     aVal <<= (sal_Int32)( 0xffffffff ); // is -1
442     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0 );
443                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
444     aVal <<= (sal_Int32)( 0x80000000 );
445     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0 );
446                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
447     aVal <<= (sal_Int32)( 0x7fffffff );
448     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0 );
449                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
450     aVal <<= (sal_Int32)( 0x10000 );
451     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0 );
452                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
453     aVal <<= (sal_Int32)( -0x8001 );
454     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0 );
455                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
456     aVal <<= (sal_Int32)( 5 );
457     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 );
458                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
459     aVal <<= (sal_Int32)( -5 );
460     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0 );
461                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
462     // ==FLOAT==
463     aVal <<= OUString::createFromAscii( "-3.4e+38" );
464     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
465     aVal <<= (float)( MIN_FLOAT );
466     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
467                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
468     aVal <<= OUString::createFromAscii( "+3.4e+38" );
469     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
470     aVal <<= (float)( MAX_FLOAT );
471     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
472                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
473     aVal <<= OUString::createFromAscii( "9e-20" );
474     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
475     aVal <<= (float)( 9e-20 );
476     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 );
477                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
478     aVal <<= OUString::createFromAscii( "+.7071067811865" );
479     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
480     aVal <<= (float)( .7071067811865 );
481     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 );
482                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
483     aVal <<= OUString::createFromAscii( "3.14159265359" );
484     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
485     aVal <<= (float)( 3.14159265359 );
486     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 );
487                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
488     aVal <<= (float)( 5 );
489     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 );
490                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
491     // ==DOUBLE==
492     aVal <<= OUString::createFromAscii( "-1.7976931348623155e+308" );
493     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
494     aVal <<= (double)( MIN_DOUBLE );
495     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
496                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
497     aVal <<= OUString::createFromAscii( "1.7976931348623155e+308" );
498     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
499     aVal <<= (double)( MAX_DOUBLE );
500     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
501                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
502     aVal <<= (double)( MIN_FLOAT );
503     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
504                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
505     aVal <<= (double)( MAX_FLOAT );
506     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
507                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
508     aVal <<= (double)( -((double)0x80000000) );
509     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0 );
510                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
511     aVal <<= (double)( -((double)0x80000001) );
512     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
513                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
514     aVal <<= (double)( 0x7fffffff );
515     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0 );
516                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
517     aVal <<= (double)( 0x80000000 );
518     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
519                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
520     aVal <<= (double)( 0xffffffff );
521     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
522                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
523     aVal <<= OUString::createFromAscii( "0x100000000" );
524     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
525 #ifndef OS2
526     aVal <<= (double)( SAL_CONST_INT64(0x100000000) );
527     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
528 #endif
529                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
530     aVal <<= (double)( 5 );
531     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 );
532                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
533     // ==CHAR==
534     sal_Unicode c = 'A';
535     aVal.setValue( &c, ::getCharCppuType() );
536     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 );
537                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
538     aVal <<= OUString::createFromAscii( "A" );
539     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 );
540                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
541     // ==BOOL==
542     aVal <<= OUString::createFromAscii( "0" );
543     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 );
544                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
545     aVal <<= OUString::createFromAscii( "1" );
546     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 );
547                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
548     aVal <<= OUString::createFromAscii( "False" );
549     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
550                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
551     aVal <<= OUString::createFromAscii( "true" );
552     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 );
553                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
554 
555     sal_Bool bTmp = sal_True;
556     aVal.setValue( &bTmp, getBooleanCppuType() );
557     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 );
558                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
559     // ==ZERO STRINGS==
560     aVal <<= OUString();
561     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
562                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
563     aVal <<= OUString::createFromAscii( "-" );
564     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0 );
565                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
566     aVal <<= OUString::createFromAscii( "-0" );
567     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
568                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
569     // ==TYPECLASS ENUM==
570     aVal <<= OUString::createFromAscii( "eNuM" );
571     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 );
572                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
573     aVal <<= OUString::createFromAscii( "DOUBLE" );
574     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 );
575                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
576     int e = 1;
577     aVal.setValue( &e, ::getCppuType( (const TypeClass *)0 ) );
578     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0 );
579                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
580     aVal.setValue( &e, ::getCppuType( (const FieldAccessMode *)0 ) );
581     pTestBlocks[nElems++] = ConvBlock( aVal, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 );
582                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
583     // ==SEQ of INT==
584     Sequence< sal_Int32 > aINT32Seq( 3 ), aINT32Seq2( 3 );
585     sal_Int32 * pINT32Seq = aINT32Seq.getArray();
586     pINT32Seq[0]     = -32768;
587     pINT32Seq[1]     = 0;
588     pINT32Seq[2]     = 32767;
589     aVal <<= aINT32Seq;
590     pTestBlocks[nElems++] = ConvBlock( aVal, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 );
591                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
592     pINT32Seq = aINT32Seq2.getArray();
593     pINT32Seq[0]     = -32768;
594     pINT32Seq[1]     = -32769;
595     pINT32Seq[2]     = 32767;
596     aVal <<= aINT32Seq2;
597     pTestBlocks[nElems++] = ConvBlock( aVal, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 );
598                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
599     // ==SEQ of ANY==
600     Sequence< Any > aAnySeq( 2 ), aAnySeq2( 2 ), aAnySeq3( 2 );
601     Any * pAnySeq    = aAnySeq.getArray();
602     pAnySeq[0]       = makeAny( aINT32Seq );
603     pAnySeq[1]       = makeAny( OUString::createFromAscii("lala") );
604     aVal <<= aAnySeq;
605     pTestBlocks[nElems++] = ConvBlock( aVal, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 );
606                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
607     pAnySeq = aAnySeq2.getArray();
608     pAnySeq[0]       <<= (sal_Int32)4711;
609     pAnySeq[1]       <<= OUString::createFromAscii("0815");
610     aVal <<= aAnySeq2;
611     pTestBlocks[nElems++] = ConvBlock( aVal, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 );
612                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
613     pAnySeq = aAnySeq3.getArray();
614     pAnySeq[0]       <<= OUString::createFromAscii("TypeClass_UNION");
615     pAnySeq[1]       <<= OUString::createFromAscii("TypeClass_ENUM");
616     aVal <<= aAnySeq3;
617     pTestBlocks[nElems++] = ConvBlock( aVal, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 );
618                                          // st,do,fl,u3,i3,u1,i1,by,bo,ch,tc,si,sa
619     return nElems;
620 }
621 
622 //==================================================================================================
623 static void test_Conversion( const Reference< XMultiServiceFactory > & xMgr )
624 {
625     printf( "test_Conversion(): start...\n" );
626 
627     Reference< XTypeConverter > xConverter( xMgr->createInstance(
628         OUString::createFromAscii( "com.sun.star.script.Converter" ) ), UNO_QUERY );
629 
630     ConvBlock * pTestBlocks = new ConvBlock[256];
631     sal_Int32 nPos = initBlocks( pTestBlocks );
632 
633     s_xConverter = xConverter;
634     while (nPos--)
635     {
636         const ConvBlock& rBlock = pTestBlocks[nPos];
637         const Any & rVal        = rBlock._value;
638 
639         convertTo( ::getCppuType( (const OUString *)0 ), rVal, rBlock._toString );
640         convertTo( ::getCppuType( (const float *)0 ), rVal, rBlock._toFloat );
641         convertTo( ::getCppuType( (const double *)0 ), rVal, rBlock._toDouble );
642         convertTo( ::getCppuType( (const sal_uInt32 *)0 ), rVal, rBlock._toUINT32 );
643         convertTo( ::getCppuType( (const sal_Int32 *)0 ), rVal, rBlock._toINT32 );
644         convertTo( ::getCppuType( (const sal_uInt16 *)0 ), rVal, rBlock._toUINT16 );
645         convertTo( ::getCppuType( (const sal_Int16 *)0 ), rVal, rBlock._toINT16 );
646         convertTo( ::getCppuType( (const sal_Int8 *)0 ), rVal, rBlock._toBYTE );
647         convertTo( ::getBooleanCppuType(), rVal, rBlock._toBOOL );
648         convertTo( ::getCharCppuType(), rVal, rBlock._toChar );
649         convertTo( ::getCppuType( (const TypeClass *)0 ), rVal, rBlock._toTypeClass );
650         convertTo( ::getCppuType( (const Sequence< sal_Int16 > *)0 ), rVal, rBlock._toSeqINT16 );
651         convertTo( ::getCppuType( (const Sequence< Any > *)0 ), rVal, rBlock._toSeqAny );
652 
653         convertTo( ::getVoidCppuType(), rVal, sal_True ); // anything converts to void
654     }
655     s_xConverter.clear();
656 
657     delete [] pTestBlocks;
658 
659     Any aRet;
660     aRet = xConverter->convertTo( Any( &xMgr, ::getCppuType( (const Reference< XMultiServiceFactory > *)0 ) ),
661                                   ::getCppuType( (const Reference< XServiceInfo > *)0 ) );
662     aRet = xConverter->convertTo( aRet, ::getCppuType( (const Reference< XMultiServiceFactory > *)0 ) );
663     aRet = xConverter->convertTo( aRet, ::getCppuType( (const Reference< XServiceInfo > *)0 ) );
664     aRet <<= SAL_CONST_INT64(0x7fffffffffffffff);
665     aRet = xConverter->convertTo( aRet, ::getCppuType( (const sal_uInt64 *)0 ) );
666     OSL_ASSERT( *(const sal_uInt64 *)aRet.getValue() == SAL_CONST_UINT64(0x7fffffffffffffff) );
667     aRet <<= SAL_CONST_UINT64(0xffffffffffffffff);
668     aRet = xConverter->convertTo( aRet, ::getCppuType( (const sal_uInt64 *)0 ) );
669     OSL_ASSERT( *(const sal_uInt64 *)aRet.getValue() == SAL_CONST_UINT64(0xffffffffffffffff) );
670     aRet <<= SAL_CONST_INT64(-1);
671     aRet = xConverter->convertTo( aRet, ::getCppuType( (const sal_Int8 *)0 ) );
672     OSL_ASSERT( *(const sal_Int8 *)aRet.getValue() == (-1) );
673     printf( "test_Conversion(): end.\n" );
674 }
675 
676 SAL_IMPLEMENT_MAIN()
677 {
678     Reference< XMultiServiceFactory > xMgr( createRegistryServiceFactory( OUString::createFromAscii("stoctest.rdb") ) );
679 
680     try
681     {
682         Reference< XImplementationRegistration > xImplReg(
683             xMgr->createInstance( OUString::createFromAscii("com.sun.star.registry.ImplementationRegistration") ), UNO_QUERY );
684         OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
685 
686         OUString aLibName(
687             RTL_CONSTASCII_USTRINGPARAM("stocservices.uno" SAL_DLLEXTENSION) );
688         xImplReg->registerImplementation(
689             OUString::createFromAscii("com.sun.star.loader.SharedLibrary"),
690             aLibName, Reference< XSimpleRegistry >() );
691 
692         test_Conversion( xMgr );
693     }
694     catch (Exception & rExc)
695     {
696         OSL_ENSURE( sal_False, "### exception occured!" );
697         OString aMsg( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
698         OSL_TRACE( "### exception occured: " );
699         OSL_TRACE( aMsg.getStr() );
700         OSL_TRACE( "\n" );
701     }
702 
703     Reference< XComponent >( xMgr, UNO_QUERY )->dispose();
704     return 0;
705 }
706