#endif
#if !UCONFIG_NO_FILE_IO
TESTCASE_AUTO(TestUnicodeFiles);
- TESTCASE_AUTO(TestEmptyString);
#endif
TESTCASE_AUTO(TestGetAvailableLocales);
TESTCASE_AUTO(TestGetDisplayName);
}
-//---------------------------------------------------------------------------
-//
-// class BITestData Holds a set of Break iterator test data and results
-// Includes
-// - the string data to be broken
-// - a vector of the expected break positions.
-// - a vector of source line numbers for the data,
-// (to help see where errors occured.)
-// - The expected break tag values.
-// - Vectors of actual break positions and tag values.
-// - Functions for comparing actual with expected and
-// reporting errors.
-//
-//----------------------------------------------------------------------------
-class BITestData {
-public:
- UnicodeString fDataToBreak;
- UVector fExpectedBreakPositions;
- UVector fExpectedTags;
- UVector fLineNum;
- UVector fActualBreakPositions; // Test Results.
- UVector fActualTags;
-
- BITestData(UErrorCode &status);
- void addDataChunk(const char *data, int32_t tag, int32_t lineNum, UErrorCode status);
- void checkResults(const char *heading, RBBITest *test);
- void err(const char *heading, RBBITest *test, int32_t expectedIdx, int32_t actualIdx);
- void clearResults();
-};
-
-//
-// Constructor.
-//
-BITestData::BITestData(UErrorCode &status)
-: fExpectedBreakPositions(status), fExpectedTags(status), fLineNum(status), fActualBreakPositions(status),
- fActualTags(status)
-{
-}
-
-//
-// addDataChunk. Add a section (non-breaking) piece if data to the test data.
-// The macro form collects the line number, which is helpful
-// when tracking down failures.
-//
-// A null data item is inserted at the start of each test's data
-// to put the starting zero into the data list. The position saved for
-// each non-null item is its ending position.
-//
-#define ADD_DATACHUNK(td, data, tag, status) td.addDataChunk(data, tag, __LINE__, status);
-void BITestData::addDataChunk(const char *data, int32_t tag, int32_t lineNum, UErrorCode status) {
- if (U_FAILURE(status)) {return;}
- if (data != NULL) {
- fDataToBreak.append(CharsToUnicodeString(data));
- }
- fExpectedBreakPositions.addElement(fDataToBreak.length(), status);
- fExpectedTags.addElement(tag, status);
- fLineNum.addElement(lineNum, status);
-}
-
-
-//
-// checkResults. Compare the actual and expected break positions, report any differences.
-//
-void BITestData::checkResults(const char *heading, RBBITest *test) {
- int32_t expectedIndex = 0;
- int32_t actualIndex = 0;
-
- for (;;) {
- // If we've run through both the expected and actual results vectors, we're done.
- // break out of the loop.
- if (expectedIndex >= fExpectedBreakPositions.size() &&
- actualIndex >= fActualBreakPositions.size()) {
- break;
- }
-
-
- if (expectedIndex >= fExpectedBreakPositions.size()) {
- err(heading, test, expectedIndex-1, actualIndex);
- actualIndex++;
- continue;
- }
-
- if (actualIndex >= fActualBreakPositions.size()) {
- err(heading, test, expectedIndex, actualIndex-1);
- expectedIndex++;
- continue;
- }
-
- if (fActualBreakPositions.elementAti(actualIndex) != fExpectedBreakPositions.elementAti(expectedIndex)) {
- err(heading, test, expectedIndex, actualIndex);
- // Try to resync the positions of the indices, to avoid a rash of spurious erros.
- if (fActualBreakPositions.elementAti(actualIndex) < fExpectedBreakPositions.elementAti(expectedIndex)) {
- actualIndex++;
- } else {
- expectedIndex++;
- }
- continue;
- }
-
- if (fActualTags.elementAti(actualIndex) != fExpectedTags.elementAti(expectedIndex)) {
- test->errln("%s, tag mismatch. Test Line = %d, expected tag=%d, got %d",
- heading, fLineNum.elementAt(expectedIndex),
- fExpectedTags.elementAti(expectedIndex), fActualTags.elementAti(actualIndex));
- }
-
- actualIndex++;
- expectedIndex++;
- }
-}
-
-//
-// err - An error was found. Report it, along with information about where the
-// incorrectly broken test data appeared in the source file.
-//
-void BITestData::err(const char *heading, RBBITest *test, int32_t expectedIdx, int32_t actualIdx)
-{
- int32_t expected = fExpectedBreakPositions.elementAti(expectedIdx);
- int32_t actual = fActualBreakPositions.elementAti(actualIdx);
- int32_t o = 0;
- int32_t line = fLineNum.elementAti(expectedIdx);
- if (expectedIdx > 0) {
- // The line numbers are off by one because a premature break occurs somewhere
- // within the previous item, rather than at the start of the current (expected) item.
- // We want to report the offset of the unexpected break from the start of
- // this previous item.
- o = actual - fExpectedBreakPositions.elementAti(expectedIdx-1);
- }
- if (actual < expected) {
- test->errln("%s unexpected break at offset %d in test item from line %d. actual break: %d expected break: %d", heading, o, line, actual, expected);
- } else {
- test->errln("%s Failed to find break at end of item from line %d. actual break: %d expected break: %d", heading, line, actual, expected);
- }
-}
-
-
-void BITestData::clearResults() {
- fActualBreakPositions.removeAllElements();
- fActualTags.removeAllElements();
-}
-
-
//--------------------------------------------------------------------------------------
//
// RBBITest constructor and destructor
delete bi;
}
-//----------------------------------------------------------------------------
-//
-// generalIteratorTest Given a break iterator and a set of test data,
-// Run the tests and report the results.
-//
-//----------------------------------------------------------------------------
-void RBBITest::generalIteratorTest(RuleBasedBreakIterator& bi, BITestData &td)
-{
-
- bi.setText(td.fDataToBreak);
-
- testFirstAndNext(bi, td);
-
- testLastAndPrevious(bi, td);
-
- testFollowing(bi, td);
- testPreceding(bi, td);
- testIsBoundary(bi, td);
- doMultipleSelectionTest(bi, td);
-}
-
-
-//
-// testFirstAndNext. Run the iterator forwards in the obvious first(), next()
-// kind of loop.
-//
-void RBBITest::testFirstAndNext(RuleBasedBreakIterator& bi, BITestData &td)
-{
- UErrorCode status = U_ZERO_ERROR;
- int32_t p;
- int32_t lastP = -1;
- int32_t tag;
-
- logln("Test first and next");
- bi.setText(td.fDataToBreak);
- td.clearResults();
-
- for (p=bi.first(); p!=RuleBasedBreakIterator::DONE; p=bi.next()) {
- td.fActualBreakPositions.addElement(p, status); // Save result.
- tag = bi.getRuleStatus();
- td.fActualTags.addElement(tag, status);
- if (p <= lastP) {
- // If the iterator is not making forward progress, stop.
- // No need to raise an error here, it'll be detected in the normal check of results.
- break;
- }
- lastP = p;
- }
- td.checkResults("testFirstAndNext", this);
-}
-
-
-//
-// TestLastAndPrevious. Run the iterator backwards, starting with last().
-//
-void RBBITest::testLastAndPrevious(RuleBasedBreakIterator& bi, BITestData &td)
-{
- UErrorCode status = U_ZERO_ERROR;
- int32_t p;
- int32_t lastP = 0x7ffffffe;
- int32_t tag;
-
- logln("Test last and previous");
- bi.setText(td.fDataToBreak);
- td.clearResults();
-
- for (p=bi.last(); p!=RuleBasedBreakIterator::DONE; p=bi.previous()) {
- // Save break position. Insert it at start of vector of results, shoving
- // already-saved results further towards the end.
- td.fActualBreakPositions.insertElementAt(p, 0, status);
- // bi.previous(); // TODO: Why does this fix things up????
- // bi.next();
- tag = bi.getRuleStatus();
- td.fActualTags.insertElementAt(tag, 0, status);
- if (p >= lastP) {
- // If the iterator is not making progress, stop.
- // No need to raise an error here, it'll be detected in the normal check of results.
- break;
- }
- lastP = p;
- }
- td.checkResults("testLastAndPrevious", this);
-}
-
-
-void RBBITest::testFollowing(RuleBasedBreakIterator& bi, BITestData &td)
-{
- UErrorCode status = U_ZERO_ERROR;
- int32_t p;
- int32_t tag;
- int32_t lastP = -2; // A value that will never be returned as a break position.
- // cannot be -1; that is returned for DONE.
- int i;
-
- logln("testFollowing():");
- bi.setText(td.fDataToBreak);
- td.clearResults();
-
- // Save the starting point, since we won't get that out of following.
- p = bi.first();
- td.fActualBreakPositions.addElement(p, status); // Save result.
- tag = bi.getRuleStatus();
- td.fActualTags.addElement(tag, status);
-
- for (i = 0; i <= td.fDataToBreak.length()+1; i++) {
- p = bi.following(i);
- if (p != lastP) {
- if (p == RuleBasedBreakIterator::DONE) {
- break;
- }
- // We've reached a new break position. Save it.
- td.fActualBreakPositions.addElement(p, status); // Save result.
- tag = bi.getRuleStatus();
- td.fActualTags.addElement(tag, status);
- lastP = p;
- }
- }
- // The loop normally exits by means of the break in the middle.
- // Make sure that the index was at the correct position for the break iterator to have
- // returned DONE.
- if (i != td.fDataToBreak.length()) {
- errln("testFollowing(): iterator returned DONE prematurely.");
- }
-
- // Full check of all results.
- td.checkResults("testFollowing", this);
-}
-
-
-
-void RBBITest::testPreceding(RuleBasedBreakIterator& bi, BITestData &td) {
- UErrorCode status = U_ZERO_ERROR;
- int32_t p;
- int32_t tag;
- int32_t lastP = 0x7ffffffe;
- int i;
-
- logln("testPreceding():");
- bi.setText(td.fDataToBreak);
- td.clearResults();
-
- p = bi.last();
- td.fActualBreakPositions.addElement(p, status);
- tag = bi.getRuleStatus();
- td.fActualTags.addElement(tag, status);
-
- for (i = td.fDataToBreak.length(); i>=-1; i--) {
- p = bi.preceding(i);
- if (p != lastP) {
- if (p == RuleBasedBreakIterator::DONE) {
- break;
- }
- // We've reached a new break position. Save it.
- td.fActualBreakPositions.insertElementAt(p, 0, status);
- lastP = p;
- tag = bi.getRuleStatus();
- td.fActualTags.insertElementAt(tag, 0, status);
- }
- }
- // The loop normally exits by means of the break in the middle.
- // Make sure that the index was at the correct position for the break iterator to have
- // returned DONE.
- if (i != 0) {
- errln("testPreceding(): iterator returned DONE prematurely.");
- }
-
- // Full check of all results.
- td.checkResults("testPreceding", this);
-}
-
-
-
-void RBBITest::testIsBoundary(RuleBasedBreakIterator& bi, BITestData &td) {
- UErrorCode status = U_ZERO_ERROR;
- int i;
- int32_t tag;
-
- logln("testIsBoundary():");
- bi.setText(td.fDataToBreak);
- td.clearResults();
-
- for (i = 0; i <= td.fDataToBreak.length(); i++) {
- if (bi.isBoundary(i)) {
- td.fActualBreakPositions.addElement(i, status); // Save result.
- tag = bi.getRuleStatus();
- td.fActualTags.addElement(tag, status);
- }
- }
- td.checkResults("testIsBoundary: ", this);
-}
-
-
-
-void RBBITest::doMultipleSelectionTest(RuleBasedBreakIterator& iterator, BITestData &td)
-{
- iterator.setText(td.fDataToBreak);
-
- RuleBasedBreakIterator* testIterator =(RuleBasedBreakIterator*)iterator.clone();
- int32_t offset = iterator.first();
- int32_t testOffset;
- int32_t count = 0;
-
- logln("doMultipleSelectionTest text of length: %d", td.fDataToBreak.length());
-
- if (*testIterator != iterator)
- errln("clone() or operator!= failed: two clones compared unequal");
-
- do {
- testOffset = testIterator->first();
- testOffset = testIterator->next(count);
- if (offset != testOffset)
- errln(UnicodeString("next(n) and next() not returning consistent results: for step ") + count + ", next(n) returned " + testOffset + " and next() had " + offset);
-
- if (offset != RuleBasedBreakIterator::DONE) {
- count++;
- offset = iterator.next();
-
- if (offset != RuleBasedBreakIterator::DONE && *testIterator == iterator) {
- errln("operator== failed: Two unequal iterators compared equal. count=%d offset=%d", count, offset);
- if (count > 10000 || offset == -1) {
- errln("operator== failed too many times. Stopping test.");
- if (offset == -1) {
- errln("Does (RuleBasedBreakIterator::DONE == -1)?");
- }
- return;
- }
- }
- }
- } while (offset != RuleBasedBreakIterator::DONE);
-
- // now do it backwards...
- offset = iterator.last();
- count = 0;
-
- do {
- testOffset = testIterator->last();
- testOffset = testIterator->next(count); // next() with a negative arg is same as previous
- if (offset != testOffset)
- errln(UnicodeString("next(n) and next() not returning consistent results: for step ") + count + ", next(n) returned " + testOffset + " and next() had " + offset);
-
- if (offset != RuleBasedBreakIterator::DONE) {
- count--;
- offset = iterator.previous();
- }
- } while (offset != RuleBasedBreakIterator::DONE);
-
- delete testIterator;
-}
-
//---------------------------------------------
//
// other tests
//
//---------------------------------------------
-void RBBITest::TestEmptyString()
-{
- UnicodeString text = "";
- UErrorCode status = U_ZERO_ERROR;
-
- BITestData x(status);
- ADD_DATACHUNK(x, "", 0, status); // Break at start of data
- RuleBasedBreakIterator* bi = (RuleBasedBreakIterator *)BreakIterator::createLineInstance(Locale::getDefault(), status);
- if (U_FAILURE(status))
- {
- errcheckln(status, "Failed to create the BreakIterator for default locale in TestEmptyString. - %s", u_errorName(status));
- return;
- }
- generalIteratorTest(*bi, x);
- delete bi;
-}
void RBBITest::TestGetAvailableLocales()
{
package com.ibm.icu.dev.test.rbbi;
import java.text.StringCharacterIterator;
-import java.util.ArrayList;
-import java.util.List;
import java.util.Locale;
-import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import com.ibm.icu.text.FilteredBreakIteratorBuilder;
import com.ibm.icu.util.ULocale;
-@SuppressWarnings("unused")
@RunWith(JUnit4.class)
public class BreakIteratorTest extends TestFmwk
{
- private BreakIterator characterBreak;
- private BreakIterator wordBreak;
- private BreakIterator lineBreak;
- private BreakIterator sentenceBreak;
- private BreakIterator titleBreak;
-
public BreakIteratorTest()
{
}
- @Before
- public void init(){
- characterBreak = BreakIterator.getCharacterInstance();
- wordBreak = BreakIterator.getWordInstance();
- lineBreak = BreakIterator.getLineInstance();
- //logln("Creating sentence iterator...");
- sentenceBreak = BreakIterator.getSentenceInstance();
- //logln("Finished creating sentence iterator...");
- titleBreak = BreakIterator.getTitleInstance();
- }
+
//=========================================================================
// general test subroutines
//=========================================================================
- private List<String> _testFirstAndNext(BreakIterator bi, String text) {
- int p = bi.first();
- int lastP = p;
- List<String> result = new ArrayList<String>();
-
- if (p != 0)
- errln("first() returned " + p + " instead of 0");
- while (p != BreakIterator.DONE) {
- p = bi.next();
- if (p != BreakIterator.DONE) {
- if (p <= lastP)
- errln("next() failed to move forward: next() on position "
- + lastP + " yielded " + p);
-
- result.add(text.substring(lastP, p));
- }
- else {
- if (lastP != text.length())
- errln("next() returned DONE prematurely: offset was "
- + lastP + " instead of " + text.length());
- }
- lastP = p;
- }
- return result;
- }
-
- private List<String> _testLastAndPrevious(BreakIterator bi, String text) {
- int p = bi.last();
- int lastP = p;
- List<String> result = new ArrayList<String>();
-
- if (p != text.length())
- errln("last() returned " + p + " instead of " + text.length());
- while (p != BreakIterator.DONE) {
- p = bi.previous();
- if (p != BreakIterator.DONE) {
- if (p >= lastP)
- errln("previous() failed to move backward: previous() on position "
- + lastP + " yielded " + p);
-
- result.add(0, text.substring(p, lastP));
- }
- else {
- if (lastP != 0)
- errln("previous() returned DONE prematurely: offset was "
- + lastP + " instead of 0");
- }
- lastP = p;
- }
- return result;
- }
-
- private void compareFragmentLists(String f1Name, String f2Name, List<String> f1, List<String> f2) {
- int p1 = 0;
- int p2 = 0;
- String s1;
- String s2;
- int t1 = 0;
- int t2 = 0;
-
- while (p1 < f1.size() && p2 < f2.size()) {
- s1 = f1.get(p1);
- s2 = f2.get(p2);
- t1 += s1.length();
- t2 += s2.length();
-
- if (s1.equals(s2)) {
- debugLogln(" >" + s1 + "<");
- ++p1;
- ++p2;
- }
- else {
- int tempT1 = t1;
- int tempT2 = t2;
- int tempP1 = p1;
- int tempP2 = p2;
-
- while (tempT1 != tempT2 && tempP1 < f1.size() && tempP2 < f2.size()) {
- while (tempT1 < tempT2 && tempP1 < f1.size()) {
- tempT1 += (f1.get(tempP1)).length();
- ++tempP1;
- }
- while (tempT2 < tempT1 && tempP2 < f2.size()) {
- tempT2 += (f2.get(tempP2)).length();
- ++tempP2;
- }
- }
- logln("*** " + f1Name + " has:");
- while (p1 <= tempP1 && p1 < f1.size()) {
- s1 = f1.get(p1);
- t1 += s1.length();
- debugLogln(" *** >" + s1 + "<");
- ++p1;
- }
- logln("***** " + f2Name + " has:");
- while (p2 <= tempP2 && p2 < f2.size()) {
- s2 = f2.get(p2);
- t2 += s2.length();
- debugLogln(" ***** >" + s2 + "<");
- ++p2;
- }
- errln("Discrepancy between " + f1Name + " and " + f2Name);
- }
- }
- }
-
- private void _testFollowing(BreakIterator bi, String text, int[] boundaries) {
- logln("testFollowing():");
- int p = 2;
- for (int i = 0; i <= text.length(); i++) {
- if (i == boundaries[p])
- ++p;
-
- int b = bi.following(i);
- logln("bi.following(" + i + ") -> " + b);
- if (b != boundaries[p])
- errln("Wrong result from following() for " + i + ": expected " + boundaries[p]
- + ", got " + b);
- }
- }
-
- private void _testPreceding(BreakIterator bi, String text, int[] boundaries) {
- logln("testPreceding():");
- int p = 0;
- for (int i = 0; i <= text.length(); i++) {
- int b = bi.preceding(i);
- logln("bi.preceding(" + i + ") -> " + b);
- if (b != boundaries[p])
- errln("Wrong result from preceding() for " + i + ": expected " + boundaries[p]
- + ", got " + b);
-
- if (i == boundaries[p + 1])
- ++p;
- }
- }
-
- private void _testIsBoundary(BreakIterator bi, String text, int[] boundaries) {
- logln("testIsBoundary():");
- int p = 1;
- boolean isB;
- for (int i = 0; i <= text.length(); i++) {
- isB = bi.isBoundary(i);
- logln("bi.isBoundary(" + i + ") -> " + isB);
-
- if (i == boundaries[p]) {
- if (!isB)
- errln("Wrong result from isBoundary() for " + i + ": expected true, got false");
- ++p;
- }
- else {
- if (isB)
- errln("Wrong result from isBoundary() for " + i + ": expected false, got true");
- }
- }
- }
private void doOtherInvariantTest(BreakIterator tb, String testChars)
{
errln("Didn't get break at end of string.");
}
- // The Following two tests are ported from ICU4C 1.8.1 [Richard/GCL]
- /**
- * Port From: ICU4C v1.8.1 : textbounds : IntlTestTextBoundary
- * Source File: $ICU4CRoot/source/test/intltest/ittxtbd.cpp
- **/
- /**
- * test methods preceding, following and isBoundary
- **/
- @Test
- public void TestPreceding() {
- String words3 = "aaa bbb ccc";
- BreakIterator e = BreakIterator.getWordInstance(Locale.getDefault());
- e.setText( words3 );
- e.first();
- int p1 = e.next();
- int p2 = e.next();
- int p3 = e.next();
- int p4 = e.next();
-
- int f = e.following(p2+1);
- int p = e.preceding(p2+1);
- if (f!=p3)
- errln("IntlTestTextBoundary::TestPreceding: f!=p3");
- if (p!=p2)
- errln("IntlTestTextBoundary::TestPreceding: p!=p2");
-
- if (p1+1!=p2)
- errln("IntlTestTextBoundary::TestPreceding: p1+1!=p2");
-
- if (p3+1!=p4)
- errln("IntlTestTextBoundary::TestPreceding: p3+1!=p4");
-
- if (!e.isBoundary(p2) || e.isBoundary(p2+1) || !e.isBoundary(p3))
- {
- errln("IntlTestTextBoundary::TestPreceding: isBoundary err");
- }
- }
+ // The Following test is ported from ICU4C 1.8.1 [Richard/GCL]
/**
* Ticket#5615