8ab45b2d62
X-SVN-Rev: 16986
154 lines
4.2 KiB
C++
154 lines
4.2 KiB
C++
/********************************************************************
|
|
* COPYRIGHT:
|
|
* Copyright (c) 2002-2003, International Business Machines Corporation and
|
|
* others. All Rights Reserved.
|
|
********************************************************************/
|
|
|
|
// Test parts of UVector and UStack
|
|
|
|
#include "intltest.h"
|
|
|
|
#include "uvectest.h"
|
|
#include "uvector.h"
|
|
#include "uhash.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
//---------------------------------------------------------------------------
|
|
//
|
|
// Test class boilerplate
|
|
//
|
|
//---------------------------------------------------------------------------
|
|
UVectorTest::UVectorTest()
|
|
{
|
|
};
|
|
|
|
|
|
UVectorTest::~UVectorTest()
|
|
{
|
|
};
|
|
|
|
|
|
|
|
void UVectorTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
|
|
{
|
|
if (exec) logln("TestSuite UVectorTest: ");
|
|
switch (index) {
|
|
|
|
case 0: name = "UVector_API";
|
|
if (exec) UVector_API();
|
|
break;
|
|
case 1: name = "UStack_API";
|
|
if (exec) UStack_API();
|
|
break;
|
|
default: name = "";
|
|
break; //needed to end loop
|
|
}
|
|
}
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
//
|
|
// Error Checking / Reporting macros used in all of the tests.
|
|
//
|
|
//---------------------------------------------------------------------------
|
|
#define TEST_CHECK_STATUS(status) \
|
|
if (U_FAILURE(status)) {\
|
|
errln("UVectorTest failure at line %d. status=%s\n", __LINE__, u_errorName(status));\
|
|
return;\
|
|
}
|
|
|
|
#define TEST_ASSERT(expr) \
|
|
if ((expr)==FALSE) {\
|
|
errln("UVectorTest failure at line %d.\n", __LINE__);\
|
|
}
|
|
|
|
U_CDECL_BEGIN
|
|
static int8_t U_CALLCONV
|
|
UVectorTest_compareInt32(const UHashTok key1, const UHashTok key2) {
|
|
if (key1.integer > key2.integer) {
|
|
return 1;
|
|
}
|
|
else if (key1.integer < key2.integer) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int8_t U_CALLCONV
|
|
UVectorTest_compareCstrings(const UHashTok key1, const UHashTok key2) {
|
|
return !strcmp((const char *)key1.pointer, (const char *)key2.pointer);
|
|
}
|
|
U_CDECL_END
|
|
|
|
//---------------------------------------------------------------------------
|
|
//
|
|
// UVector_API Check for basic functionality of UVector.
|
|
//
|
|
//---------------------------------------------------------------------------
|
|
void UVectorTest::UVector_API() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
UVector *a;
|
|
|
|
a = new UVector(status);
|
|
TEST_CHECK_STATUS(status);
|
|
delete a;
|
|
|
|
status = U_ZERO_ERROR;
|
|
a = new UVector(2000, status);
|
|
TEST_CHECK_STATUS(status);
|
|
delete a;
|
|
|
|
status = U_ZERO_ERROR;
|
|
a = new UVector(status);
|
|
a->sortedInsert(10, UVectorTest_compareInt32, status);
|
|
a->sortedInsert(20, UVectorTest_compareInt32, status);
|
|
a->sortedInsert(30, UVectorTest_compareInt32, status);
|
|
a->sortedInsert(15, UVectorTest_compareInt32, status);
|
|
TEST_CHECK_STATUS(status);
|
|
TEST_ASSERT(a->elementAti(0) == 10);
|
|
TEST_ASSERT(a->elementAti(1) == 15);
|
|
TEST_ASSERT(a->elementAti(2) == 20);
|
|
TEST_ASSERT(a->elementAti(3) == 30);
|
|
delete a;
|
|
};
|
|
|
|
void UVectorTest::UStack_API() {
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
UStack *a;
|
|
|
|
a = new UStack(status);
|
|
TEST_CHECK_STATUS(status);
|
|
delete a;
|
|
|
|
status = U_ZERO_ERROR;
|
|
a = new UStack(2000, status);
|
|
TEST_CHECK_STATUS(status);
|
|
delete a;
|
|
|
|
status = U_ZERO_ERROR;
|
|
a = new UStack(NULL, NULL, 2000, status);
|
|
TEST_CHECK_STATUS(status);
|
|
delete a;
|
|
|
|
status = U_ZERO_ERROR;
|
|
a = new UStack(NULL, UVectorTest_compareCstrings, status);
|
|
TEST_ASSERT(a->empty());
|
|
a->push((void*)"abc", status);
|
|
TEST_ASSERT(!a->empty());
|
|
a->push((void*)"bcde", status);
|
|
a->push((void*)"cde", status);
|
|
TEST_CHECK_STATUS(status);
|
|
TEST_ASSERT(strcmp("cde", (const char *)a->peek()) == 0);
|
|
TEST_ASSERT(a->search((void*)"cde") == 1);
|
|
TEST_ASSERT(a->search((void*)"bcde") == 2);
|
|
TEST_ASSERT(a->search((void*)"abc") == 3);
|
|
TEST_ASSERT(strcmp("abc", (const char *)a->firstElement()) == 0);
|
|
TEST_ASSERT(strcmp("cde", (const char *)a->lastElement()) == 0);
|
|
TEST_ASSERT(strcmp("cde", (const char *)a->pop()) == 0);
|
|
TEST_ASSERT(strcmp("bcde", (const char *)a->pop()) == 0);
|
|
TEST_ASSERT(strcmp("abc", (const char *)a->pop()) == 0);
|
|
delete a;
|
|
}
|