2004-12-20 23:38:05 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2006-03-24 23:12:30 +00:00
|
|
|
* Copyright (c) 2004-2006, International Business Machines Corporation and
|
2004-12-20 23:38:05 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
// Test parts of UVector and UStack
|
|
|
|
|
|
|
|
#include "intltest.h"
|
|
|
|
|
|
|
|
#include "uvectest.h"
|
|
|
|
#include "uvector.h"
|
2004-12-22 21:57:43 +00:00
|
|
|
#include "hash.h"
|
2004-12-20 23:38:05 +00:00
|
|
|
|
|
|
|
#include "cstring.h"
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Test class boilerplate
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
UVectorTest::UVectorTest()
|
|
|
|
{
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-12-20 23:38:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
UVectorTest::~UVectorTest()
|
|
|
|
{
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-12-20 23:38:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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";
|
2004-12-22 21:57:43 +00:00
|
|
|
if (exec) UVector_API();
|
2004-12-20 23:38:05 +00:00
|
|
|
break;
|
|
|
|
case 1: name = "UStack_API";
|
2004-12-22 21:57:43 +00:00
|
|
|
if (exec) UStack_API();
|
2004-12-20 23:38:05 +00:00
|
|
|
break;
|
2004-12-22 21:57:43 +00:00
|
|
|
case 2: name = "Hashtable_API";
|
|
|
|
if (exec) Hashtable_API();
|
|
|
|
break;
|
|
|
|
default: name = "";
|
2004-12-20 23:38:05 +00:00
|
|
|
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__);\
|
|
|
|
}
|
|
|
|
|
2004-12-23 20:16:56 +00:00
|
|
|
static int8_t
|
2004-12-21 23:51:25 +00:00
|
|
|
UVectorTest_compareInt32(UHashTok key1, UHashTok key2) {
|
2004-12-20 23:38:05 +00:00
|
|
|
if (key1.integer > key2.integer) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (key1.integer < key2.integer) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-23 20:16:56 +00:00
|
|
|
U_CDECL_BEGIN
|
2004-12-20 23:38:05 +00:00
|
|
|
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);
|
2004-12-21 23:02:17 +00:00
|
|
|
a->sortedInsert((int32_t)10, UVectorTest_compareInt32, status);
|
|
|
|
a->sortedInsert((int32_t)20, UVectorTest_compareInt32, status);
|
|
|
|
a->sortedInsert((int32_t)30, UVectorTest_compareInt32, status);
|
|
|
|
a->sortedInsert((int32_t)15, UVectorTest_compareInt32, status);
|
2004-12-20 23:38:05 +00:00
|
|
|
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);
|
2006-03-24 23:12:30 +00:00
|
|
|
TEST_ASSERT(a->indexOf((int32_t)3) == -1);
|
|
|
|
TEST_ASSERT(a->indexOf((int32_t)15) == 1);
|
|
|
|
TEST_ASSERT(a->indexOf((int32_t)15, 2) == -1);
|
|
|
|
TEST_ASSERT(a->contains((int32_t)15));
|
|
|
|
TEST_ASSERT(!a->contains((int32_t)5));
|
2004-12-20 23:38:05 +00:00
|
|
|
delete a;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-12-20 23:38:05 +00:00
|
|
|
|
|
|
|
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());
|
2004-12-21 22:26:39 +00:00
|
|
|
a->push((void*)"abc", status);
|
2004-12-20 23:38:05 +00:00
|
|
|
TEST_ASSERT(!a->empty());
|
2004-12-21 22:26:39 +00:00
|
|
|
a->push((void*)"bcde", status);
|
|
|
|
a->push((void*)"cde", status);
|
2004-12-20 23:38:05 +00:00
|
|
|
TEST_CHECK_STATUS(status);
|
|
|
|
TEST_ASSERT(strcmp("cde", (const char *)a->peek()) == 0);
|
2004-12-21 22:26:39 +00:00
|
|
|
TEST_ASSERT(a->search((void*)"cde") == 1);
|
|
|
|
TEST_ASSERT(a->search((void*)"bcde") == 2);
|
|
|
|
TEST_ASSERT(a->search((void*)"abc") == 3);
|
2004-12-20 23:38:05 +00:00
|
|
|
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;
|
|
|
|
}
|
2004-12-22 21:57:43 +00:00
|
|
|
|
2006-11-15 00:13:51 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
static UBool U_CALLCONV neverTRUE(const UHashTok /*key1*/, const UHashTok /*key2*/) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CDECL_END
|
|
|
|
|
2004-12-22 21:57:43 +00:00
|
|
|
void UVectorTest::Hashtable_API() {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
Hashtable *a = new Hashtable(status);
|
|
|
|
TEST_ASSERT((a->puti("a", 1, status) == 0));
|
|
|
|
TEST_ASSERT((a->find("a") != NULL));
|
|
|
|
TEST_ASSERT((a->find("b") == NULL));
|
|
|
|
TEST_ASSERT((a->puti("b", 2, status) == 0));
|
|
|
|
TEST_ASSERT((a->find("b") != NULL));
|
|
|
|
TEST_ASSERT((a->removei("a") == 1));
|
|
|
|
TEST_ASSERT((a->find("a") == NULL));
|
2006-11-15 00:13:51 +00:00
|
|
|
|
|
|
|
/* verify that setValueCompartor works */
|
|
|
|
Hashtable b(status);
|
|
|
|
TEST_ASSERT((!a->equals(b)));
|
|
|
|
TEST_ASSERT((b.puti("b", 2, status) == 0));
|
|
|
|
TEST_ASSERT((!a->equals(b))); // Without a value comparator, this will be FALSE by default.
|
|
|
|
b.setValueCompartor(uhash_compareLong);
|
|
|
|
TEST_ASSERT((!a->equals(b)));
|
|
|
|
a->setValueCompartor(uhash_compareLong);
|
|
|
|
TEST_ASSERT((a->equals(b)));
|
|
|
|
TEST_ASSERT((a->equals(*a))); // This better be reflexive.
|
|
|
|
|
|
|
|
/* verify that setKeyCompartor works */
|
|
|
|
TEST_ASSERT((a->puti("a", 1, status) == 0));
|
|
|
|
TEST_ASSERT((a->find("a") != NULL));
|
|
|
|
a->setKeyCompartor(neverTRUE);
|
|
|
|
TEST_ASSERT((a->find("a") == NULL));
|
|
|
|
|
2004-12-22 21:57:43 +00:00
|
|
|
delete a;
|
|
|
|
}
|
|
|
|
|