1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2002-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
12 #include "unicode/utypes.h"
13 #include "unicode/unistr.h"
14 #include "unicode/ustring.h"
16 #include "unicode/testtype.h"
17 #include "unicode/utimer.h"
20 // Forward declarations from uoptions.h.
22 typedef struct UOption UOption
;
24 #if !UCONFIG_NO_CONVERSION
27 // Use the TESTCASE macro in subclasses of UPerfTest. Define the
28 // runIndexedTest method in this fashion:
30 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
31 //| const char* &name, char* /*par*/) {
33 //| TESTCASE(0,TestSomething);
34 //| TESTCASE(1,TestSomethingElse);
35 //| TESTCASE(2,TestAnotherThing);
42 #define TESTCASE(id,test) \
50 // More convenient macros. These allow easy reordering of the test cases.
51 // Copied from intltest.h, and adjusted to not logln() but return a UPerfFunction.
53 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
54 //| const char* &name, char* /*par*/) {
55 //| TESTCASE_AUTO_BEGIN;
56 //| TESTCASE_AUTO(TestSomething);
57 //| TESTCASE_AUTO(TestSomethingElse);
58 //| TESTCASE_AUTO(TestAnotherThing);
59 //| TESTCASE_AUTO_END;
62 #define TESTCASE_AUTO_BEGIN \
64 int32_t testCaseAutoNumber = 0
66 #define TESTCASE_AUTO(test) \
67 if (index == testCaseAutoNumber++) { \
75 #define TESTCASE_AUTO_END \
81 * Subclasses of PerfTest will need to create subclasses of
82 * Function that define a call() method which contains the code to
83 * be timed. They then call setTestFunction() in their "Test..."
84 * method to establish this as the current test functor.
86 class T_CTEST_EXPORT_API UPerfFunction
{
91 virtual ~UPerfFunction();
94 * Subclasses must implement this method to do the action to be
97 virtual void call(UErrorCode
* status
)=0;
100 * Subclasses must implement this method to return positive
101 * integer indicating the number of operations in a single
102 * call to this object's call() method.
104 virtual long getOperationsPerIteration()=0;
106 * Subclasses should override this method to return either positive
107 * or negative integer indicating the number of events in a single
108 * call to this object's call() method, if applicable
109 * e.g: Number of breaks / iterations for break iterator
111 virtual long getEventsPerIteration(){
115 * Call call() n times in a tight loop and return the elapsed
116 * milliseconds. If n is small and call() is fast the return
117 * result may be zero. Small return values have limited
118 * meaningfulness, depending on the underlying CPU and OS.
120 virtual double time(int32_t n
, UErrorCode
* status
) {
122 utimer_getTime(&start
);
126 utimer_getTime(&stop
);
127 return utimer_getDeltaSeconds(&start
,&stop
); // ms
133 class T_CTEST_EXPORT_API UPerfTest
{
136 UBool
runTest( char* name
= NULL
, char* par
= NULL
); // not to be overidden
138 virtual void usage( void ) ;
140 virtual ~UPerfTest();
142 void setCaller( UPerfTest
* callingTest
); // for internal use only
144 void setPath( char* path
); // for internal use only
146 ULine
* getLines(UErrorCode
& status
);
148 const UChar
* getBuffer(int32_t& len
,UErrorCode
& status
);
151 UPerfTest(int32_t argc
, const char* argv
[], UErrorCode
& status
);
153 UPerfTest(int32_t argc
, const char* argv
[],
154 UOption addOptions
[], int32_t addOptionsCount
,
155 const char *addUsage
,
158 void init(UOption addOptions
[], int32_t addOptionsCount
,
161 virtual UPerfFunction
* runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
= NULL
); // overide !
163 virtual UBool
runTestLoop( char* testname
, char* par
);
165 virtual UBool
callTest( UPerfTest
& testToBeCalled
, char* par
);
169 const char * _addUsage
;
170 char* resolvedFileName
;
172 const char* encoding
;
174 const char* fileName
;
175 const char* sourceDir
;
176 int32_t _remainingArgc
;
190 char* path
; // specifies subtests
194 static UPerfTest
* gTest
;
195 static const char gUsageString
[];