Initial commit of C character properties performance test

X-SVN-Rev: 10124
This commit is contained in:
Syn Wee Quek 2002-10-31 00:37:54 +00:00
parent fd3b348097
commit 904f1c3f04
2 changed files with 506 additions and 0 deletions

View File

@ -0,0 +1,248 @@
/********************************************************************
* COPYRIGHT:
* Copyright (C) 2002 IBM, Inc. All Rights Reserved.
*
********************************************************************/
/*****************************************************************************
* File charperf.cpp
*
* Modification History:
* Name Description
* Syn Wee Quek First Version
******************************************************************************
*/
/**
* This program tests character properties performance.
* APIs tested:
* ICU4C
* Windows
*/
#include "charperf.h"
#include "uoptions.h"
UOption options[] = {
UOPTION_DEF("min", 'n', UOPT_REQUIRES_ARG),
UOPTION_DEF("min", 'x', UOPT_REQUIRES_ARG),
};
int MIN_OPTION_ = 0;
int MAX_OPTION_ = 1;
int main(int argc, const char *argv[])
{
UErrorCode status = U_ZERO_ERROR;
CharPerformanceTest test(argc, argv, status);
if (U_FAILURE(status)){
return status;
}
if (test.run() == FALSE){
fprintf(stderr, "FAILED: Tests could not be run please check the "
"arguments.\n");
return -1;
}
return 0;
}
CharPerformanceTest::CharPerformanceTest(int32_t argc, const char *argv[],
UErrorCode &status)
: UPerfTest(argc, argv, status)
{
if (status== U_ILLEGAL_ARGUMENT_ERROR){
fprintf(stderr,gUsageString, "charperf");
return;
}
if (U_FAILURE(status)){
fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n",
u_errorName(status));
return;
}
if (_remainingArgc < 0) {
// that means there are some -names not matched in the super class
// first tag is always skipped in u_parseArgs
int size = - _remainingArgc;
argv += argc - size;
argc = size;
_remainingArgc = u_parseArgs(argc, (char**)argv,
(int32_t)(sizeof(options)/sizeof(options[0])), options);
}
MIN_ = 0;
MAX_ = 0xffff;
if (options[MIN_OPTION_].doesOccur) {
MIN_ = atoi(options[MIN_OPTION_].value);
}
if (options[MAX_OPTION_].doesOccur) {
MAX_ = atoi(options[MAX_OPTION_].value);
}
}
CharPerformanceTest::~CharPerformanceTest()
{
}
UPerfFunction* CharPerformanceTest::runIndexedTest(int32_t index, UBool exec,
const char *&name,
char* par)
{
switch (index) {
TESTCASE(0, TestIsAlpha);
TESTCASE(1, TestIsUpper);
TESTCASE(2, TestIsLower);
TESTCASE(3, TestIsDigit);
TESTCASE(4, TestIsSpace);
TESTCASE(5, TestIsAlphaNumeric);
TESTCASE(6, TestIsPrint);
TESTCASE(7, TestIsControl);
TESTCASE(8, TestToLower);
TESTCASE(9, TestToUpper);
TESTCASE(10, TestIsWhiteSpace);
TESTCASE(11, TestStdLibIsAlpha);
TESTCASE(12, TestStdLibIsUpper);
TESTCASE(13, TestStdLibIsLower);
TESTCASE(14, TestStdLibIsDigit);
TESTCASE(15, TestStdLibIsSpace);
TESTCASE(16, TestStdLibIsAlphaNumeric);
TESTCASE(17, TestStdLibIsPrint);
TESTCASE(18, TestStdLibIsControl);
TESTCASE(19, TestStdLibToLower);
TESTCASE(20, TestStdLibToUpper);
TESTCASE(21, TestStdLibIsWhiteSpace);
default:
name = "";
return NULL;
}
return NULL;
}
UPerfFunction* CharPerformanceTest::TestIsAlpha()
{
return new CharPerfFunction(isAlpha, MIN_, MAX_);
}
UPerfFunction* CharPerformanceTest::TestIsUpper()
{
return new CharPerfFunction(isUpper, MIN_, MAX_);
}
UPerfFunction* CharPerformanceTest::TestIsLower()
{
return new CharPerfFunction(isLower, MIN_, MAX_);
}
UPerfFunction* CharPerformanceTest::TestIsDigit()
{
return new CharPerfFunction(isDigit, MIN_, MAX_);
}
UPerfFunction* CharPerformanceTest::TestIsSpace()
{
return new CharPerfFunction(isSpace, MIN_, MAX_);
}
UPerfFunction* CharPerformanceTest::TestIsAlphaNumeric()
{
return new CharPerfFunction(isAlphaNumeric, MIN_, MAX_);
}
/**
* This test may be different since c lib has a type PUNCT and it is printable.
* iswgraph is not used for testing since it is a subset of iswprint with the
* exception of returning true for white spaces. no match found in icu4c.
*/
UPerfFunction* CharPerformanceTest::TestIsPrint()
{
return new CharPerfFunction(isPrint, MIN_, MAX_);
}
UPerfFunction* CharPerformanceTest::TestIsControl()
{
return new CharPerfFunction(isControl, MIN_, MAX_);
}
UPerfFunction* CharPerformanceTest::TestToLower()
{
return new CharPerfFunction(toLower, MIN_, MAX_);
}
UPerfFunction* CharPerformanceTest::TestToUpper()
{
return new CharPerfFunction(toUpper, MIN_, MAX_);
}
UPerfFunction* CharPerformanceTest::TestIsWhiteSpace()
{
return new CharPerfFunction(isWhiteSpace, MIN_, MAX_);
}
UPerfFunction* CharPerformanceTest::TestStdLibIsAlpha()
{
return new StdLibCharPerfFunction(StdLibIsAlpha, (wchar_t)MIN_,
(wchar_t)MAX_);
}
UPerfFunction* CharPerformanceTest::TestStdLibIsUpper()
{
return new StdLibCharPerfFunction(StdLibIsUpper, (wchar_t)MIN_,
(wchar_t)MAX_);
}
UPerfFunction* CharPerformanceTest::TestStdLibIsLower()
{
return new StdLibCharPerfFunction(StdLibIsLower, (wchar_t)MIN_,
(wchar_t)MAX_);
}
UPerfFunction* CharPerformanceTest::TestStdLibIsDigit()
{
return new StdLibCharPerfFunction(StdLibIsDigit, (wchar_t)MIN_,
(wchar_t)MAX_);
}
UPerfFunction* CharPerformanceTest::TestStdLibIsSpace()
{
return new StdLibCharPerfFunction(StdLibIsSpace, (wchar_t)MIN_,
(wchar_t)MAX_);
}
UPerfFunction* CharPerformanceTest::TestStdLibIsAlphaNumeric()
{
return new StdLibCharPerfFunction(StdLibIsAlphaNumeric, (wchar_t)MIN_,
(wchar_t)MAX_);
}
/**
* This test may be different since c lib has a type PUNCT and it is printable.
* iswgraph is not used for testing since it is a subset of iswprint with the
* exception of returning true for white spaces. no match found in icu4c.
*/
UPerfFunction* CharPerformanceTest::TestStdLibIsPrint()
{
return new StdLibCharPerfFunction(StdLibIsPrint, (wchar_t)MIN_,
(wchar_t)MAX_);
}
UPerfFunction* CharPerformanceTest::TestStdLibIsControl()
{
return new StdLibCharPerfFunction(StdLibIsControl, (wchar_t)MIN_,
(wchar_t)MAX_);
}
UPerfFunction* CharPerformanceTest::TestStdLibToLower()
{
return new StdLibCharPerfFunction(StdLibToLower, (wchar_t)MIN_,
(wchar_t)MAX_);
}
UPerfFunction* CharPerformanceTest::TestStdLibToUpper()
{
return new StdLibCharPerfFunction(StdLibToUpper, (wchar_t)MIN_,
(wchar_t)MAX_);
}
UPerfFunction* CharPerformanceTest::TestStdLibIsWhiteSpace()
{
return new StdLibCharPerfFunction(StdLibIsWhiteSpace, (wchar_t)MIN_,
(wchar_t)MAX_);
}

View File

@ -0,0 +1,258 @@
/*
**********************************************************************
* Copyright (c) 2002, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
**********************************************************************
*/
#ifndef _CHARPERF_H
#define _CHARPERF_H
#include "uperf.h"
#include "unicode/uchar.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
typedef void (*CharPerfFn)(UChar32 ch);
typedef void (*StdLibCharPerfFn)(wchar_t ch);
class CharPerfFunction : public UPerfFunction
{
public:
virtual void call()
{
for (UChar32 i = MIN_; i < MAX_; i ++) {
(*m_fn_)(i);
}
}
virtual long getOperationsPerIteration()
{
return MAX_ - MIN_;
}
virtual long getEventsPerIteration()
{
return -1;
}
UErrorCode getStatus()
{
return U_ZERO_ERROR;
}
CharPerfFunction(CharPerfFn func, UChar32 min, UChar32 max)
{
m_fn_ = func;
MIN_ = min;
MAX_ = max;
}
private:
CharPerfFn m_fn_;
UChar32 MIN_;
UChar32 MAX_;
};
class StdLibCharPerfFunction : public UPerfFunction
{
public:
virtual void call()
{
// note wchar_t is unsigned, it will revert to 0 once it reaches
// 65535
for (wchar_t i = MIN_; i < MAX_; i ++) {
(*m_fn_)(i);
}
}
virtual long getOperationsPerIteration()
{
return MAX_ - MIN_;
}
virtual long getEventsPerIteration()
{
return -1;
}
UErrorCode getStatus()
{
return U_ZERO_ERROR;
}
StdLibCharPerfFunction(StdLibCharPerfFn func, wchar_t min, wchar_t max)
{
m_fn_ = func;
MIN_ = min;
MAX_ = max;
}
~StdLibCharPerfFunction()
{
}
private:
StdLibCharPerfFn m_fn_;
wchar_t MIN_;
wchar_t MAX_;
};
class CharPerformanceTest : public UPerfTest
{
public:
CharPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status);
~CharPerformanceTest();
virtual UPerfFunction* runIndexedTest(int32_t index, UBool exec,
const char *&name,
char *par = NULL);
UPerfFunction* TestIsAlpha();
UPerfFunction* TestIsUpper();
UPerfFunction* TestIsLower();
UPerfFunction* TestIsDigit();
UPerfFunction* TestIsSpace();
UPerfFunction* TestIsAlphaNumeric();
UPerfFunction* TestIsPrint();
UPerfFunction* TestIsControl();
UPerfFunction* TestToLower();
UPerfFunction* TestToUpper();
UPerfFunction* TestIsWhiteSpace();
UPerfFunction* TestStdLibIsAlpha();
UPerfFunction* TestStdLibIsUpper();
UPerfFunction* TestStdLibIsLower();
UPerfFunction* TestStdLibIsDigit();
UPerfFunction* TestStdLibIsSpace();
UPerfFunction* TestStdLibIsAlphaNumeric();
UPerfFunction* TestStdLibIsPrint();
UPerfFunction* TestStdLibIsControl();
UPerfFunction* TestStdLibToLower();
UPerfFunction* TestStdLibToUpper();
UPerfFunction* TestStdLibIsWhiteSpace();
private:
UChar32 MIN_;
UChar32 MAX_;
};
void isAlpha(UChar32 ch)
{
u_isalpha(ch);
}
void isUpper(UChar32 ch)
{
u_isupper(ch);
}
void isLower(UChar32 ch)
{
u_islower(ch);
}
void isDigit(UChar32 ch)
{
u_isdigit(ch);
}
void isSpace(UChar32 ch)
{
u_isspace(ch);
}
void isAlphaNumeric(UChar32 ch)
{
u_isalnum(ch);
}
/**
* This test may be different since c lib has a type PUNCT and it is printable.
* iswgraph is not used for testing since it is a subset of iswprint with the
* exception of returning true for white spaces. no match found in icu4c.
*/
void isPrint(UChar32 ch)
{
u_isprint(ch);
}
void isControl(UChar32 ch)
{
u_iscntrl(ch);
}
void toLower(UChar32 ch)
{
u_tolower(ch);
}
void toUpper(UChar32 ch)
{
u_toupper(ch);
}
void isWhiteSpace(UChar32 ch)
{
u_isWhitespace(ch);
}
void StdLibIsAlpha(wchar_t ch)
{
iswalpha(ch);
}
void StdLibIsUpper(wchar_t ch)
{
iswupper(ch);
}
void StdLibIsLower(wchar_t ch)
{
iswlower(ch);
}
void StdLibIsDigit(wchar_t ch)
{
iswdigit(ch);
}
void StdLibIsSpace(wchar_t ch)
{
iswspace(ch);
}
void StdLibIsAlphaNumeric(wchar_t ch)
{
iswalnum(ch);
}
/**
* This test may be different since c lib has a type PUNCT and it is printable.
* iswgraph is not used for testing since it is a subset of iswprint with the
* exception of returning true for white spaces. no match found in icu4c.
*/
void StdLibIsPrint(wchar_t ch)
{
iswprint(ch);
}
void StdLibIsControl(wchar_t ch)
{
iswcntrl(ch);
}
void StdLibToLower(wchar_t ch)
{
towlower(ch);
}
void StdLibToUpper(wchar_t ch)
{
towupper(ch);
}
void StdLibIsWhiteSpace(wchar_t ch)
{
iswspace(ch);
}
#endif // CHARPERF_H