2 **********************************************************************
3 * Copyright (c) 2002-2014, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
10 #include "unicode/utypes.h"
11 #include "unicode/unistr.h"
12 #include "unicode/ustring.h"
14 #include "unicode/testtype.h"
15 #include "unicode/utimer.h"
18 // Forward declarations from uoptions.h.
20 typedef struct UOption UOption
;
22 #if !UCONFIG_NO_CONVERSION
25 // Use the TESTCASE macro in subclasses of UPerfTest. Define the
26 // runIndexedTest method in this fashion:
28 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
29 //| const char* &name, char* /*par*/) {
31 //| TESTCASE(0,TestSomething);
32 //| TESTCASE(1,TestSomethingElse);
33 //| TESTCASE(2,TestAnotherThing);
40 #define TESTCASE(id,test) \
48 // More convenient macros. These allow easy reordering of the test cases.
49 // Copied from intltest.h, and adjusted to not logln() but return a UPerfFunction.
51 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
52 //| const char* &name, char* /*par*/) {
53 //| TESTCASE_AUTO_BEGIN;
54 //| TESTCASE_AUTO(TestSomething);
55 //| TESTCASE_AUTO(TestSomethingElse);
56 //| TESTCASE_AUTO(TestAnotherThing);
57 //| TESTCASE_AUTO_END;
60 #define TESTCASE_AUTO_BEGIN \
62 int32_t testCaseAutoNumber = 0
64 #define TESTCASE_AUTO(test) \
65 if (index == testCaseAutoNumber++) { \
73 #define TESTCASE_AUTO_END \
79 * Subclasses of PerfTest will need to create subclasses of
80 * Function that define a call() method which contains the code to
81 * be timed. They then call setTestFunction() in their "Test..."
82 * method to establish this as the current test functor.
84 class T_CTEST_EXPORT_API UPerfFunction
{
89 virtual ~UPerfFunction();
92 * Subclasses must implement this method to do the action to be
95 virtual void call(UErrorCode
* status
)=0;
98 * Subclasses must implement this method to return positive
99 * integer indicating the number of operations in a single
100 * call to this object's call() method.
102 virtual long getOperationsPerIteration()=0;
104 * Subclasses should override this method to return either positive
105 * or negative integer indicating the number of events in a single
106 * call to this object's call() method, if applicable
107 * e.g: Number of breaks / iterations for break iterator
109 virtual long getEventsPerIteration(){
113 * Call call() n times in a tight loop and return the elapsed
114 * milliseconds. If n is small and call() is fast the return
115 * result may be zero. Small return values have limited
116 * meaningfulness, depending on the underlying CPU and OS.
118 virtual double time(int32_t n
, UErrorCode
* status
) {
120 utimer_getTime(&start
);
124 utimer_getTime(&stop
);
125 return utimer_getDeltaSeconds(&start
,&stop
); // ms
131 class T_CTEST_EXPORT_API UPerfTest
{
134 UBool
runTest( char* name
= NULL
, char* par
= NULL
); // not to be overidden
136 virtual void usage( void ) ;
138 virtual ~UPerfTest();
140 void setCaller( UPerfTest
* callingTest
); // for internal use only
142 void setPath( char* path
); // for internal use only
144 ULine
* getLines(UErrorCode
& status
);
146 const UChar
* getBuffer(int32_t& len
,UErrorCode
& status
);
149 UPerfTest(int32_t argc
, const char* argv
[], UErrorCode
& status
);
151 UPerfTest(int32_t argc
, const char* argv
[],
152 UOption addOptions
[], int32_t addOptionsCount
,
153 const char *addUsage
,
156 void init(UOption addOptions
[], int32_t addOptionsCount
,
159 virtual UPerfFunction
* runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
= NULL
); // overide !
161 virtual UBool
runTestLoop( char* testname
, char* par
);
163 virtual UBool
callTest( UPerfTest
& testToBeCalled
, char* par
);
167 const char * _addUsage
;
168 char* resolvedFileName
;
170 const char* encoding
;
172 const char* fileName
;
173 const char* sourceDir
;
174 int32_t _remainingArgc
;
188 char* path
; // specifies subtests
192 static UPerfTest
* gTest
;
193 static const char gUsageString
[];