2000-01-15 02:00:06 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2001-03-21 19:46:49 +00:00
|
|
|
* Copyright (c) 1997-2001, International Business Machines Corporation and
|
2000-01-15 02:00:06 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
|
|
|
/********************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File CBIAPTS.C
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
* Name Description
|
|
|
|
* Madhu Katragadda Creation
|
2000-08-14 23:35:57 +00:00
|
|
|
*********************************************************************************/
|
1999-08-16 21:50:52 +00:00
|
|
|
/*C API TEST FOR BREAKITERATOR */
|
|
|
|
/**
|
|
|
|
* This is an API test. It doesn't test very many cases, and doesn't
|
|
|
|
* try to test the full functionality. It just calls each function in the class and
|
|
|
|
* verifies that it works on a basic level.
|
|
|
|
**/
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/uloc.h"
|
|
|
|
#include "unicode/ubrk.h"
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/ustring.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "cintltst.h"
|
|
|
|
#include "cbiapts.h"
|
|
|
|
|
2001-05-31 22:48:59 +00:00
|
|
|
void addBrkIterAPITest(TestNode** root);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
void addBrkIterAPITest(TestNode** root)
|
|
|
|
{
|
2001-05-31 22:48:59 +00:00
|
|
|
addTest(root, &TestBreakIteratorCAPI, "tstxtbd/cbiapts/TestBreakIteratorCAPI");
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:42:08 +00:00
|
|
|
#define CLONETEST_ITERATOR_COUNT 2
|
|
|
|
|
2000-11-21 04:05:39 +00:00
|
|
|
static void TestBreakIteratorCAPI()
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2000-06-22 01:09:15 +00:00
|
|
|
UBreakIterator *word, *sentence, *line, *character, *b, *bogus;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start,pos,end,to;
|
2000-06-22 01:09:15 +00:00
|
|
|
int32_t i;
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t count = 0;
|
2001-02-21 23:42:08 +00:00
|
|
|
|
2001-05-31 22:58:53 +00:00
|
|
|
UBreakIterator * someIterators [CLONETEST_ITERATOR_COUNT];
|
|
|
|
UBreakIterator * someClonedIterators [CLONETEST_ITERATOR_COUNT];
|
|
|
|
UBreakIterator * brk;
|
2001-09-26 21:09:18 +00:00
|
|
|
UChar text[51]; /* Keep this odd to test for 64-bit memory alignment */
|
2002-03-28 01:14:02 +00:00
|
|
|
/* NOTE: This doesn't reliably force mis-alignment of following items. */
|
2001-05-31 22:58:53 +00:00
|
|
|
uint8_t buffer [CLONETEST_ITERATOR_COUNT] [U_BRK_SAFECLONE_BUFFERSIZE];
|
|
|
|
int32_t bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
|
2001-02-21 23:42:08 +00:00
|
|
|
|
2002-03-08 18:58:46 +00:00
|
|
|
/* Note: the adjacent "" are concatenating strings, not adding a \" to the
|
|
|
|
string, which is probably what whoever wrote this intended. Don't fix,
|
|
|
|
because it would throw off the hard coded break positions in the following
|
|
|
|
tests. */
|
1999-08-16 21:50:52 +00:00
|
|
|
u_uastrcpy(text, "He's from Africa. ""Mr. Livingston, I presume?"" Yeah");
|
|
|
|
|
2001-02-21 23:42:08 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/*test ubrk_open()*/
|
|
|
|
log_verbose("\nTesting BreakIterator open functions\n");
|
|
|
|
|
|
|
|
/* Use french for fun */
|
|
|
|
word = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)){
|
1999-08-16 21:50:52 +00:00
|
|
|
log_err("FAIL: Error in ubrk_open() for word breakiterator: %s\n", myErrorName(status));
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
log_verbose("PASS: Successfully opened word breakiterator\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
sentence = ubrk_open(UBRK_SENTENCE, "en_US", text, u_strlen(text), &status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)){
|
1999-08-16 21:50:52 +00:00
|
|
|
log_err("FAIL: Error in ubrk_open() for sentence breakiterator: %s\n", myErrorName(status));
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
log_verbose("PASS: Successfully opened sentence breakiterator\n");
|
|
|
|
}
|
|
|
|
|
2000-06-22 01:09:15 +00:00
|
|
|
line = ubrk_open(UBRK_LINE, "en_US", text, u_strlen(text), &status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)){
|
1999-08-16 21:50:52 +00:00
|
|
|
log_err("FAIL: Error in ubrk_open() for line breakiterator: %s\n", myErrorName(status));
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
log_verbose("PASS: Successfully opened line breakiterator\n");
|
|
|
|
}
|
|
|
|
|
2000-06-22 01:09:15 +00:00
|
|
|
character = ubrk_open(UBRK_CHARACTER, "en_US", text, u_strlen(text), &status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)){
|
1999-08-16 21:50:52 +00:00
|
|
|
log_err("FAIL: Error in ubrk_open() for character breakiterator: %s\n", myErrorName(status));
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
log_verbose("PASS: Successfully opened character breakiterator\n");
|
|
|
|
}
|
2000-06-22 01:09:15 +00:00
|
|
|
/*trying to open an illegal iterator*/
|
2002-03-01 00:46:21 +00:00
|
|
|
bogus = ubrk_open((UBreakIteratorType)5, "en_US", text, u_strlen(text), &status);
|
2000-06-22 01:09:15 +00:00
|
|
|
if(U_SUCCESS(status)){
|
2002-03-01 01:33:36 +00:00
|
|
|
log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_MEMORY_ALLOCATION_ERROR\n");
|
2000-06-22 01:09:15 +00:00
|
|
|
}
|
|
|
|
if(U_FAILURE(status)){
|
|
|
|
if(status != U_MEMORY_ALLOCATION_ERROR){
|
|
|
|
log_err("FAIL: Error in ubrk_open() for BOGUS breakiterator. Expected U_MEMORY_ALLOCATION_ERROR\n Got %s\n", myErrorName(status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status=U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-06-22 01:09:15 +00:00
|
|
|
|
|
|
|
/* ======= Test ubrk_countAvialable() and ubrk_getAvialable() */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
log_verbose("\nTesting ubrk_countAvailable() and ubrk_getAvailable()\n");
|
|
|
|
count=ubrk_countAvailable();
|
|
|
|
/* use something sensible w/o hardcoding the count */
|
|
|
|
if(count < 0){
|
|
|
|
log_err("FAIL: Error in ubrk_countAvialable() returned %d\n", count);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
log_verbose("PASS: ubrk_countAvialable() successful returned %d\n", count);
|
|
|
|
}
|
2000-06-22 01:09:15 +00:00
|
|
|
for(i=0;i<count;i++)
|
2000-11-21 04:05:39 +00:00
|
|
|
{
|
2000-06-22 01:09:15 +00:00
|
|
|
log_verbose("%s\n", ubrk_getAvailable(i));
|
2000-11-21 04:05:39 +00:00
|
|
|
if (ubrk_getAvailable(i) == 0)
|
|
|
|
log_err("No locale for which breakiterator is applicable\n");
|
|
|
|
else
|
|
|
|
log_verbose("A locale %s for which breakiterator is applicable\n",ubrk_getAvailable(i));
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/*========Test ubrk_first(), ubrk_last()...... and other functions*/
|
2000-11-21 04:05:39 +00:00
|
|
|
|
|
|
|
log_verbose("\nTesting the functions for word\n");
|
1999-08-16 21:50:52 +00:00
|
|
|
start = ubrk_first(word);
|
|
|
|
if(start!=0)
|
|
|
|
log_err("error ubrk_start(word) did not return 0\n");
|
|
|
|
log_verbose("first (word = %d\n", (int32_t)start);
|
|
|
|
pos=ubrk_next(word);
|
|
|
|
if(pos!=4)
|
|
|
|
log_err("error ubrk_next(word) did not return 4\n");
|
|
|
|
log_verbose("next (word = %d\n", (int32_t)pos);
|
|
|
|
pos=ubrk_following(word, 4);
|
|
|
|
if(pos!=5)
|
|
|
|
log_err("error ubrl_following(word,4) did not return 6\n");
|
|
|
|
log_verbose("next (word = %d\n", (int32_t)pos);
|
|
|
|
end=ubrk_last(word);
|
|
|
|
if(end!=49)
|
|
|
|
log_err("error ubrk_last(word) did not return 49\n");
|
|
|
|
log_verbose("last (word = %d\n", (int32_t)end);
|
|
|
|
|
|
|
|
pos=ubrk_previous(word);
|
|
|
|
log_verbose("%d %d\n", end, pos);
|
|
|
|
|
|
|
|
pos=ubrk_previous(word);
|
|
|
|
log_verbose("%d \n", pos);
|
|
|
|
|
2002-03-08 18:58:46 +00:00
|
|
|
if (ubrk_isBoundary(word, 2) != FALSE) {
|
|
|
|
log_err("error ubrk_isBoundary(word, 2) did not return FALSE\n");
|
|
|
|
}
|
|
|
|
pos=ubrk_current(word);
|
|
|
|
if (pos != 4) {
|
|
|
|
log_err("error ubrk_current() != 4 after ubrk_isBoundary(word, 2)\n");
|
|
|
|
}
|
|
|
|
if (ubrk_isBoundary(word, 4) != TRUE) {
|
|
|
|
log_err("error ubrk_isBoundary(word, 4) did not return TRUE\n");
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log_verbose("\nTesting the functions for character\n");
|
|
|
|
ubrk_first(character);
|
|
|
|
pos = ubrk_following(character, 5);
|
2000-06-22 01:09:15 +00:00
|
|
|
if(pos!=6)
|
|
|
|
log_err("error ubrk_following(character,5) did not return 6\n");
|
1999-08-16 21:50:52 +00:00
|
|
|
log_verbose("Following (character,5) = %d\n", (int32_t)pos);
|
|
|
|
pos=ubrk_following(character, 18);
|
2000-06-22 01:09:15 +00:00
|
|
|
if(pos!=19)
|
|
|
|
log_err("error ubrk_following(character,18) did not return 19\n");
|
1999-08-16 21:50:52 +00:00
|
|
|
log_verbose("Followingcharacter,18) = %d\n", (int32_t)pos);
|
|
|
|
pos=ubrk_preceding(character, 22);
|
2000-06-22 01:09:15 +00:00
|
|
|
if(pos!=21)
|
|
|
|
log_err("error ubrk_preceding(character,22) did not return 21\n");
|
1999-08-16 21:50:52 +00:00
|
|
|
log_verbose("preceding(character,22) = %d\n", (int32_t)pos);
|
|
|
|
|
|
|
|
|
|
|
|
log_verbose("\nTesting the functions for line\n");
|
2000-06-22 01:09:15 +00:00
|
|
|
pos=ubrk_first(line);
|
|
|
|
if(pos != 0)
|
|
|
|
log_err("error ubrk_first(line) returned %d, expected 0\n", (int32_t)pos);
|
1999-08-16 21:50:52 +00:00
|
|
|
pos = ubrk_next(line);
|
|
|
|
pos=ubrk_following(line, 18);
|
|
|
|
if(pos!=22)
|
|
|
|
log_err("error ubrk_following(line) did not return 22\n");
|
|
|
|
log_verbose("following (line) = %d\n", (int32_t)pos);
|
|
|
|
|
|
|
|
|
|
|
|
log_verbose("\nTesting the functions for sentence\n");
|
|
|
|
ubrk_first(sentence);
|
|
|
|
pos = ubrk_current(sentence);
|
|
|
|
log_verbose("Current(sentence) = %d\n", (int32_t)pos);
|
|
|
|
pos = ubrk_last(sentence);
|
|
|
|
if(pos!=49)
|
|
|
|
log_err("error ubrk_last for sentence did not return 49\n");
|
|
|
|
log_verbose("Last (sentence) = %d\n", (int32_t)pos);
|
|
|
|
ubrk_first(sentence);
|
|
|
|
to = ubrk_following( sentence, 0 );
|
|
|
|
if (to == 0) log_err("ubrk_following returned 0\n");
|
|
|
|
to = ubrk_preceding( sentence, to );
|
|
|
|
if (to != 0) log_err("ubrk_preceding didn't return 0\n");
|
|
|
|
if (ubrk_first(sentence)!=ubrk_current(sentence)) {
|
|
|
|
log_err("error in ubrk_first() or ubrk_current()\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---- */
|
|
|
|
/*Testing ubrk_open and ubrk_close()*/
|
|
|
|
log_verbose("\nTesting open and close for us locale\n");
|
|
|
|
b = ubrk_open(UBRK_WORD, "fr_FR", text, u_strlen(text), &status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
log_err("ubrk_open for word returned NULL: %s\n", myErrorName(status));
|
|
|
|
}
|
|
|
|
ubrk_close(b);
|
|
|
|
|
|
|
|
ubrk_close(word);
|
|
|
|
ubrk_close(sentence);
|
|
|
|
ubrk_close(line);
|
|
|
|
ubrk_close(character);
|
|
|
|
|
2001-02-21 23:42:08 +00:00
|
|
|
/*Testing ubrk_safeClone */
|
|
|
|
|
2001-05-31 22:58:53 +00:00
|
|
|
/* US & Thai - rule-based & dictionary based */
|
|
|
|
someIterators[0] = ubrk_open(UBRK_WORD, "en_US", text, u_strlen(text), &status);
|
|
|
|
someIterators[1] = ubrk_open(UBRK_WORD, "th_TH", text, u_strlen(text), &status);
|
|
|
|
|
2001-02-21 23:42:08 +00:00
|
|
|
/* test each type of iterator */
|
|
|
|
for (i = 0; i < CLONETEST_ITERATOR_COUNT; i++)
|
2001-05-31 22:58:53 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Check the various error & informational states */
|
|
|
|
|
|
|
|
/* Null status - just returns NULL */
|
|
|
|
if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, 0))
|
|
|
|
{
|
|
|
|
log_err("FAIL: Cloned Iterator failed to deal correctly with null status\n");
|
|
|
|
}
|
|
|
|
/* error status - should return 0 & keep error the same */
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || status != U_MEMORY_ALLOCATION_ERROR)
|
|
|
|
{
|
|
|
|
log_err("FAIL: Cloned Iterator failed to deal correctly with incoming error status\n");
|
|
|
|
}
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
/* Null buffer size pointer - just returns NULL & set error to U_ILLEGAL_ARGUMENT_ERROR*/
|
|
|
|
if (0 != ubrk_safeClone(someIterators[i], buffer[i], 0, &status) || status != U_ILLEGAL_ARGUMENT_ERROR)
|
|
|
|
{
|
|
|
|
log_err("FAIL: Cloned Iterator failed to deal correctly with null bufferSize pointer\n");
|
|
|
|
}
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
/* buffer size pointer is 0 - fill in pbufferSize with a size */
|
|
|
|
bufferSize = 0;
|
|
|
|
if (0 != ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status) || U_FAILURE(status) || bufferSize <= 0)
|
|
|
|
{
|
|
|
|
log_err("FAIL: Cloned Iterator failed a sizing request ('preflighting')\n");
|
|
|
|
}
|
|
|
|
/* Verify our define is large enough */
|
|
|
|
if (U_BRK_SAFECLONE_BUFFERSIZE < bufferSize)
|
|
|
|
{
|
|
|
|
log_err("FAIL: Pre-calculated buffer size is too small\n");
|
|
|
|
}
|
|
|
|
/* Verify we can use this run-time calculated size */
|
|
|
|
if (0 == (brk = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status)) || U_FAILURE(status))
|
|
|
|
{
|
|
|
|
log_err("FAIL: Iterator can't be cloned with run-time size\n");
|
|
|
|
}
|
|
|
|
if (brk) ubrk_close(brk);
|
|
|
|
/* size one byte too small - should allocate & let us know */
|
|
|
|
--bufferSize;
|
|
|
|
if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_ERROR)
|
|
|
|
{
|
|
|
|
log_err("FAIL: Cloned Iterator failed to deal correctly with too-small buffer size\n");
|
|
|
|
}
|
|
|
|
if (brk) ubrk_close(brk);
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
|
|
|
|
|
|
|
|
/* Null buffer pointer - return Iterator & set error to U_SAFECLONE_ALLOCATED_ERROR */
|
|
|
|
if (0 == (brk = ubrk_safeClone(someIterators[i], 0, &bufferSize, &status)) || status != U_SAFECLONE_ALLOCATED_ERROR)
|
|
|
|
{
|
|
|
|
log_err("FAIL: Cloned Iterator failed to deal correctly with null buffer pointer\n");
|
|
|
|
}
|
|
|
|
if (brk) ubrk_close(brk);
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
|
2002-03-28 01:14:02 +00:00
|
|
|
/* Mis-aligned buffer pointer. */
|
|
|
|
{
|
|
|
|
char stackBuf[U_BRK_SAFECLONE_BUFFERSIZE+sizeof(void *)];
|
|
|
|
void *p;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
brk = ubrk_safeClone(someIterators[i], &stackBuf[1], &bufferSize, &status);
|
|
|
|
if (U_FAILURE(status) || brk == 0) {
|
|
|
|
log_err("FAIL: Cloned Iterator failed with misaligned buffer pointer\n");
|
|
|
|
}
|
|
|
|
if (status == U_SAFECLONE_ALLOCATED_ERROR) {
|
|
|
|
log_err("FAIL: Cloned Iterator allocated when using a mis-aligned buffer.\n");
|
|
|
|
}
|
|
|
|
offset = (char *)&p-(char*)brk;
|
|
|
|
if (offset < 0) {
|
|
|
|
offset = -offset;
|
|
|
|
}
|
|
|
|
if (offset % sizeof(void *) != 0) {
|
|
|
|
log_err("FAIL: Cloned Iterator failed to align correctly with misaligned buffer pointer\n");
|
|
|
|
}
|
|
|
|
if (brk) ubrk_close(brk);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-31 22:58:53 +00:00
|
|
|
/* Null Iterator - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
|
|
|
|
if (0 != ubrk_safeClone(0, buffer[i], &bufferSize, &status) || status != U_ILLEGAL_ARGUMENT_ERROR)
|
|
|
|
{
|
|
|
|
log_err("FAIL: Cloned Iterator failed to deal correctly with null Iterator pointer\n");
|
|
|
|
}
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
/* Do these cloned Iterators work at all - make a first & next call */
|
|
|
|
bufferSize = U_BRK_SAFECLONE_BUFFERSIZE;
|
|
|
|
someClonedIterators[i] = ubrk_safeClone(someIterators[i], buffer[i], &bufferSize, &status);
|
|
|
|
|
|
|
|
start = ubrk_first(someClonedIterators[i]);
|
2001-02-21 23:42:08 +00:00
|
|
|
if(start!=0)
|
|
|
|
log_err("error ubrk_start(clone) did not return 0\n");
|
|
|
|
pos=ubrk_next(someClonedIterators[i]);
|
|
|
|
if(pos!=4)
|
|
|
|
log_err("error ubrk_next(clone) did not return 4\n");
|
2001-05-31 22:58:53 +00:00
|
|
|
|
|
|
|
ubrk_close(someClonedIterators[i]);
|
|
|
|
ubrk_close(someIterators[i]);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|