~ubuntu-branches/ubuntu/gutsy/icu/gutsy

« back to all changes in this revision

Viewing changes to source/test/intltest/ustrtest.cpp

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2005-05-21 22:44:31 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: package-import@ubuntu.com-20050521224431-r7rktfhnu1n4tf1g
Tags: 2.1-2.1
Rename icu-doc to icu21-doc. icu-doc is built by the icu28 package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/********************************************************************
2
2
 * COPYRIGHT: 
3
 
 * Copyright (c) 1997-2001, International Business Machines Corporation and
 
3
 * Copyright (c) 1997-2002, International Business Machines Corporation and
4
4
 * others. All Rights Reserved.
5
5
 ********************************************************************/
6
6
 
24
24
 
25
25
#endif
26
26
 
27
 
UnicodeStringTest::UnicodeStringTest()
28
 
{
29
 
}
30
 
 
31
 
UnicodeStringTest::~UnicodeStringTest()
32
 
{
33
 
}
34
 
 
35
 
void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
36
 
{
37
 
    if (exec) logln("TestSuite LocaleTest: ");
 
27
void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char *par)
 
28
{
 
29
    if (exec) logln("TestSuite UnicodeStringTest: ");
38
30
    switch (index) {
39
31
        case 0: name = "TestBasicManipulation"; if (exec) TestBasicManipulation(); break;
40
32
        case 1: name = "TestCompare"; if (exec) TestCompare(); break;
41
33
        case 2: name = "TestExtract"; if (exec) TestExtract(); break;
42
34
        case 3: name = "TestRemoveReplace"; if (exec) TestRemoveReplace(); break;
43
 
        case 4: name = "TestCaseConversion"; if (exec) TestCaseConversion(); break;
 
35
        case 4:
 
36
            name = "StringCaseTest";
 
37
            if (exec) {
 
38
                StringCaseTest test;
 
39
                callTest(test, par);
 
40
                break;
 
41
            }
44
42
        case 5: name = "TestNothing"; break;
45
43
        case 6: name = "TestSearching"; if (exec) TestSearching(); break;
46
44
        case 7: name = "TestSpacePadding"; if (exec) TestSpacePadding(); break;
394
392
        errln("UnicodeString.extract(-1) failed to stop reading the string.");
395
393
    }
396
394
 
397
 
    for (UTextOffset i = 0; i < 12; i++) {
398
 
        if (test1[(UTextOffset)(11 + i)] != test2[i]) {
 
395
    for (int32_t i = 0; i < 12; i++) {
 
396
        if (test1[(int32_t)(11 + i)] != test2[i]) {
399
397
            errln(UnicodeString("extracting into a UnicodeString failed at position ") + i);
400
398
            break;
401
399
        }
402
 
        if (test1[(UTextOffset)(11 + i)] != test3[i]) {
 
400
        if (test1[(int32_t)(11 + i)] != test3[i]) {
403
401
            errln(UnicodeString("extracting into an array of UChar failed at position ") + i);
404
402
            break;
405
403
        }
406
 
        if (((char)test1[(UTextOffset)(11 + i)]) != test4b[i]) {
 
404
        if (((char)test1[(int32_t)(11 + i)]) != test4b[i]) {
407
405
            errln(UnicodeString("extracting into an array of char failed at position ") + i);
408
406
            break;
409
407
        }
410
 
        if (test1[(UTextOffset)(11 + i)] != test5[i]) {
 
408
        if (test1[(int32_t)(11 + i)] != test5[i]) {
411
409
            errln(UnicodeString("extracting with extractBetween failed at position ") + i);
412
410
            break;
413
411
        }
562
560
              "  expected \"The SPAM in SPAM SPAM SPAM on the SPAM\",\n"
563
561
              "  got \"" + test1 + "\"");
564
562
 
565
 
    for (UTextOffset i = 0; i < test1.length(); i++)
 
563
    for (int32_t i = 0; i < test1.length(); i++)
566
564
        if (test5[i] != 0x53 && test5[i] != 0x50 && test5[i] != 0x41 && test5[i] != 0x4d && test5[i] != 0x20)
567
565
            test1[i] = 0x78;
568
566
 
577
575
}
578
576
 
579
577
void
580
 
UnicodeStringTest::TestCaseConversion()
581
 
{
582
 
    UChar uppercaseGreek[] =
583
 
        { 0x399, 0x395, 0x3a3, 0x3a5, 0x3a3, 0x20, 0x03a7, 0x3a1, 0x399, 0x3a3, 0x3a4,
584
 
        0x39f, 0x3a3, 0 };
585
 
        // "IESUS CHRISTOS"
586
 
 
587
 
    UChar lowercaseGreek[] = 
588
 
        { 0x3b9, 0x3b5, 0x3c3, 0x3c5, 0x3c2, 0x20, 0x03c7, 0x3c1, 0x3b9, 0x3c3, 0x3c4,
589
 
        0x3bf, 0x3c2, 0 };
590
 
        // "iesus christos"
591
 
 
592
 
    UChar lowercaseTurkish[] = 
593
 
        { 0x69, 0x73, 0x74, 0x61, 0x6e, 0x62, 0x75, 0x6c, 0x2c, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x63, 0x6f, 
594
 
        0x6e, 0x73, 0x74, 0x61, 0x6e, 0x74, 0x0131, 0x6e, 0x6f, 0x70, 0x6c, 0x65, 0x21, 0 };
595
 
 
596
 
    UChar uppercaseTurkish[] = 
597
 
        { 0x54, 0x4f, 0x50, 0x4b, 0x41, 0x50, 0x49, 0x20, 0x50, 0x41, 0x4c, 0x41, 0x43, 0x45, 0x2c, 0x20,
598
 
        0x0130, 0x53, 0x54, 0x41, 0x4e, 0x42, 0x55, 0x4c, 0 };
599
 
    
600
 
    UnicodeString expectedResult;
601
 
    UnicodeString   test3;
602
 
 
603
 
    test3 += (UChar32)0x0130;
604
 
    test3 += "STANBUL, NOT CONSTANTINOPLE!";
605
 
 
606
 
    UnicodeString   test4(test3);
607
 
    test4.toLower();
608
 
    expectedResult = "istanbul, not constantinople!";
609
 
    if (test4 != expectedResult)
610
 
        errln("1. toLower failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
611
 
 
612
 
    test4 = test3;
613
 
    test4.toLower(Locale("tr", "TR"));
614
 
    expectedResult = lowercaseTurkish;
615
 
    if (test4 != expectedResult)
616
 
        errln("2. toLower failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
617
 
 
618
 
    test3 = "topkap";
619
 
    test3 += (UChar32)0x0131;
620
 
    test3 += " palace, istanbul";
621
 
    test4 = test3;
622
 
 
623
 
    test4.toUpper();
624
 
    expectedResult = "TOPKAPI PALACE, ISTANBUL";
625
 
    if (test4 != expectedResult)
626
 
        errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
627
 
 
628
 
    test4 = test3;
629
 
    test4.toUpper(Locale("tr", "TR"));
630
 
    expectedResult = uppercaseTurkish;
631
 
    if (test4 != expectedResult)
632
 
        errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
633
 
 
634
 
    test3 = CharsToUnicodeString("S\\u00FC\\u00DFmayrstra\\u00DFe");
635
 
 
636
 
    test3.toUpper(Locale("de", "DE"));
637
 
    expectedResult = CharsToUnicodeString("S\\u00DCSSMAYRSTRASSE");
638
 
    if (test3 != expectedResult)
639
 
        errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test3 + "\".");
640
 
    
641
 
    test4.replace(0, test4.length(), uppercaseGreek);
642
 
 
643
 
    test4.toLower(Locale("el", "GR"));
644
 
    expectedResult = lowercaseGreek;
645
 
    if (test4 != expectedResult)
646
 
        errln("toLower failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
647
 
    
648
 
    test4.replace(0, test4.length(), lowercaseGreek);
649
 
 
650
 
    test4.toUpper();
651
 
    expectedResult = uppercaseGreek;
652
 
    if (test4 != expectedResult)
653
 
        errln("toUpper failed: expected \"" + expectedResult + "\", got \"" + test4 + "\".");
654
 
 
655
 
    // more string case mapping tests with the new implementation
656
 
    {
657
 
        static const UChar
658
 
 
659
 
        beforeLower[]= { 0x61, 0x42, 0x49,  0x3a3, 0xdf, 0x3a3, 0x2f, 0xd93f, 0xdfff },
660
 
        lowerRoot[]=   { 0x61, 0x62, 0x69,  0x3c3, 0xdf, 0x3c2, 0x2f, 0xd93f, 0xdfff },
661
 
        lowerTurkish[]={ 0x61, 0x62, 0x131, 0x3c3, 0xdf, 0x3c2, 0x2f, 0xd93f, 0xdfff },
662
 
 
663
 
        beforeUpper[]= { 0x61, 0x42, 0x69,  0x3c2, 0xdf,       0x3c3, 0x2f, 0xfb03,           0xfb03,           0xfb03,           0xd93f, 0xdfff },
664
 
        upperRoot[]=   { 0x41, 0x42, 0x49,  0x3a3, 0x53, 0x53, 0x3a3, 0x2f, 0x46, 0x46, 0x49, 0x46, 0x46, 0x49, 0x46, 0x46, 0x49, 0xd93f, 0xdfff },
665
 
        upperTurkish[]={ 0x41, 0x42, 0x130, 0x3a3, 0x53, 0x53, 0x3a3, 0x2f, 0x46, 0x46, 0x49, 0x46, 0x46, 0x49, 0x46, 0x46, 0x49, 0xd93f, 0xdfff },
666
 
 
667
 
        beforeMiniUpper[]=  { 0xdf, 0x61 },
668
 
        miniUpper[]=        { 0x53, 0x53, 0x41 };
669
 
 
670
 
        UnicodeString s;
671
 
 
672
 
        /* lowercase with root locale */
673
 
        s=UnicodeString(FALSE, beforeLower, (int32_t)(sizeof(beforeLower)/U_SIZEOF_UCHAR));
674
 
        s.toLower("");
675
 
        if( s.length()!=(sizeof(lowerRoot)/U_SIZEOF_UCHAR) ||
676
 
            s!=UnicodeString(FALSE, lowerRoot, s.length())
677
 
        ) {
678
 
            errln("error in toLower(root locale)=\"" + s + "\" expected \"" + UnicodeString(FALSE, lowerRoot, (int32_t)(sizeof(lowerRoot)/U_SIZEOF_UCHAR)) + "\"");
679
 
        }
680
 
 
681
 
        /* lowercase with turkish locale */
682
 
        s=UnicodeString(FALSE, beforeLower, (int32_t)(sizeof(beforeLower)/U_SIZEOF_UCHAR));
683
 
        s.setCharAt(0, beforeLower[0]).toLower(Locale("tr"));
684
 
        if( s.length()!=(sizeof(lowerTurkish)/U_SIZEOF_UCHAR) ||
685
 
            s!=UnicodeString(FALSE, lowerTurkish, s.length())
686
 
        ) {
687
 
            errln("error in toLower(turkish locale)=\"" + s + "\" expected \"" + UnicodeString(FALSE, lowerTurkish, (int32_t)(sizeof(lowerTurkish)/U_SIZEOF_UCHAR)) + "\"");
688
 
        }
689
 
 
690
 
        /* uppercase with root locale */
691
 
        s=UnicodeString(FALSE, beforeUpper, (int32_t)(sizeof(beforeUpper)/U_SIZEOF_UCHAR));
692
 
        s.setCharAt(0, beforeUpper[0]).toUpper();
693
 
        if( s.length()!=(sizeof(upperRoot)/U_SIZEOF_UCHAR) ||
694
 
            s!=UnicodeString(FALSE, upperRoot, s.length())
695
 
        ) {
696
 
            errln("error in toUpper(root locale)=\"" + s + "\" expected \"" + UnicodeString(FALSE, upperRoot, (int32_t)(sizeof(upperRoot)/U_SIZEOF_UCHAR)) + "\"");
697
 
        }
698
 
 
699
 
        /* uppercase with turkish locale */
700
 
        s=UnicodeString(FALSE, beforeUpper, (int32_t)(sizeof(beforeUpper)/U_SIZEOF_UCHAR));
701
 
        s.toUpper(Locale("tr"));
702
 
        if( s.length()!=(sizeof(upperTurkish)/U_SIZEOF_UCHAR) ||
703
 
            s!=UnicodeString(FALSE, upperTurkish, s.length())
704
 
        ) {
705
 
            errln("error in toUpper(turkish locale)=\"" + s + "\" expected \"" + UnicodeString(FALSE, upperTurkish, (int32_t)(sizeof(upperTurkish)/U_SIZEOF_UCHAR)) + "\"");
706
 
        }
707
 
 
708
 
        /* uppercase a short string with root locale */
709
 
        s=UnicodeString(FALSE, beforeMiniUpper, (int32_t)(sizeof(beforeMiniUpper)/U_SIZEOF_UCHAR));
710
 
        s.setCharAt(0, beforeMiniUpper[0]).toUpper("");
711
 
        if( s.length()!=(sizeof(miniUpper)/U_SIZEOF_UCHAR) ||
712
 
            s!=UnicodeString(FALSE, miniUpper, s.length())
713
 
        ) {
714
 
            errln("error in toUpper(root locale)=\"" + s + "\" expected \"" + UnicodeString(FALSE, miniUpper, (int32_t)(sizeof(miniUpper)/U_SIZEOF_UCHAR)) + "\"");
715
 
        }
716
 
    }
717
 
 
718
 
    // test some supplementary characters (>= Unicode 3.1)
719
 
    {
720
 
        UnicodeString t;
721
 
 
722
 
        UnicodeString
723
 
            deseretInput=UnicodeString("\\U0001043C\\U00010414", "").unescape(),
724
 
            deseretLower=UnicodeString("\\U0001043C\\U0001043C", "").unescape(),
725
 
            deseretUpper=UnicodeString("\\U00010414\\U00010414", "").unescape();
726
 
        (t=deseretInput).toLower();
727
 
        if(t!=deseretLower) {
728
 
            errln("error lowercasing Deseret (plane 1) characters");
729
 
        }
730
 
        (t=deseretInput).toUpper();
731
 
        if(t!=deseretUpper) {
732
 
            errln("error uppercasing Deseret (plane 1) characters");
733
 
        }
734
 
    }
735
 
 
736
 
    // test some more cases that looked like problems
737
 
    {
738
 
        UnicodeString t;
739
 
 
740
 
        UnicodeString
741
 
            ljInput=UnicodeString("ab'cD \\uFB00i\\u0131I\\u0130 \\u01C7\\u01C8\\u01C9 \\U0001043C\\U00010414", "").unescape(),
742
 
            ljLower=UnicodeString("ab'cd \\uFB00i\\u0131ii \\u01C9\\u01C9\\u01C9 \\U0001043C\\U0001043C", "").unescape(),
743
 
            ljUpper=UnicodeString("AB'CD FFIII\\u0130 \\u01C7\\u01C7\\u01C7 \\U00010414\\U00010414", "").unescape();
744
 
        (t=ljInput).toLower("en");
745
 
        if(t!=ljLower) {
746
 
            errln("error lowercasing LJ characters");
747
 
        }
748
 
        (t=ljInput).toUpper("en");
749
 
        if(t!=ljUpper) {
750
 
            errln("error uppercasing LJ characters");
751
 
        }
752
 
    }
753
 
 
754
 
    // Unicode 3.1.1 SpecialCasing tests
755
 
    {
756
 
        UnicodeString t;
757
 
 
758
 
        // sigmas preceded and/or followed by cased letters
759
 
        UnicodeString
760
 
            sigmas=UnicodeString("i\\u0307\\u03a3\\u0308j \\u0307\\u03a3\\u0308j i\\u00ad\\u03a3\\u0308 \\u0307\\u03a3\\u0308 ", "").unescape(),
761
 
            sigmasLower=UnicodeString("i\\u0307\\u03c3\\u0308j \\u0307\\u03c3\\u0308j i\\u00ad\\u03c2\\u0308 \\u0307\\u03c3\\u0308 ", "").unescape(),
762
 
            sigmasUpper=UnicodeString("I\\u0307\\u03a3\\u0308J \\u0307\\u03a3\\u0308J I\\u00ad\\u03a3\\u0308 \\u0307\\u03a3\\u0308 ", "").unescape();
763
 
 
764
 
        (t=sigmas).toLower();
765
 
        if(t!=sigmasLower) {
766
 
            errln("error in sigmas.toLower()=\"" + t + "\" expected \"" + sigmasLower + "\"");
767
 
        }
768
 
 
769
 
        (t=sigmas).toUpper();
770
 
        if(t!=sigmasUpper) {
771
 
            errln("error in sigmas.toUpper()=\"" + t + "\" expected \"" + sigmasUpper + "\"");
772
 
        }
773
 
 
774
 
        // turkish & azerbaijani dotless i & dotted I
775
 
        // remove dot above if there was a capital I before and there are no more accents above
776
 
        UnicodeString
777
 
            dots=UnicodeString("I \\u0130 I\\u0307 I\\u0327\\u0307 I\\u0301\\u0307 I\\u0327\\u0307\\u0301", "").unescape(),
778
 
            dotsTurkish=UnicodeString("\\u0131 i i i\\u0327 \\u0131\\u0301\\u0307 i\\u0327\\u0307\\u0301", "").unescape(),
779
 
            dotsDefault=UnicodeString("i i i i\\u0327 i\\u0301\\u0307 i\\u0327\\u0307\\u0301", "").unescape();
780
 
 
781
 
        (t=dots).toLower("tr");
782
 
        if(t!=dotsTurkish) {
783
 
            errln("error in dots.toLower(tr)=\"" + t + "\" expected \"" + dotsTurkish + "\"");
784
 
        }
785
 
 
786
 
        (t=dots).toLower("de");
787
 
        if(t!=dotsDefault) {
788
 
            errln("error in dots.toLower(de)=\"" + t + "\" expected \"" + dotsDefault + "\"");
789
 
        }
790
 
    }
791
 
 
792
 
    // more Unicode 3.1.1 tests
793
 
    {
794
 
        UnicodeString t;
795
 
 
796
 
        // lithuanian dot above in uppercasing
797
 
        UnicodeString
798
 
            dots=UnicodeString("a\\u0307 \\u0307 i\\u0307 j\\u0327\\u0307 j\\u0301\\u0307", "").unescape(),
799
 
            dotsLithuanian=UnicodeString("A\\u0307 \\u0307 I J\\u0327 J\\u0301\\u0307", "").unescape(),
800
 
            dotsDefault=UnicodeString("A\\u0307 \\u0307 I\\u0307 J\\u0327\\u0307 J\\u0301\\u0307", "").unescape();
801
 
 
802
 
        (t=dots).toUpper("lt");
803
 
        if(t!=dotsLithuanian) {
804
 
            errln("error in dots.toUpper(lt)=\"" + t + "\" expected \"" + dotsLithuanian + "\"");
805
 
        }
806
 
 
807
 
        (t=dots).toUpper("de");
808
 
        if(t!=dotsDefault) {
809
 
            errln("error in dots.toUpper(de)=\"" + t + "\" expected \"" + dotsDefault + "\"");
810
 
        }
811
 
 
812
 
        // lithuanian adds dot above to i in lowercasing if there are more above accents
813
 
        UnicodeString
814
 
            i=UnicodeString("I I\\u0301 J J\\u0301 \\u012e \\u012e\\u0301 \\u00cc\\u00cd\\u0128", "").unescape(),
815
 
            iLithuanian=UnicodeString("i i\\u0307\\u0301 j j\\u0307\\u0301 \\u012f \\u012f\\u0307\\u0301 i\\u0307\\u0300i\\u0307\\u0301i\\u0307\\u0303", "").unescape(),
816
 
            iDefault=UnicodeString("i i\\u0301 j j\\u0301 \\u012f \\u012f\\u0301 \\u00ec\\u00ed\\u0129", "").unescape();
817
 
 
818
 
        (t=i).toLower("lt");
819
 
        if(t!=iLithuanian) {
820
 
            errln("error in i.toLower(lt)=\"" + t + "\" expected \"" + iLithuanian + "\"");
821
 
        }
822
 
 
823
 
        (t=i).toLower("de");
824
 
        if(t!=iDefault) {
825
 
            errln("error in i.toLower(de)=\"" + t + "\" expected \"" + iDefault + "\"");
826
 
        }
827
 
    }
828
 
 
829
 
    // test case folding
830
 
    {
831
 
        UnicodeString
832
 
            s=UNICODE_STRING("A\\u00df\\u00b5\\ufb03\\U0001040c\\u0131", 35).unescape(),
833
 
            f=UNICODE_STRING("ass\\u03bcffi\\U00010434i", 23).unescape(),
834
 
            t;
835
 
 
836
 
        (t=s).foldCase();
837
 
        if(f!=t) {
838
 
            errln("error in foldCase(\"" + s + "\", default)=\"" + t + "\" but expected \"" + f + "\"");
839
 
        }
840
 
 
841
 
        // alternate handling for dotted I/dotless i (U+0130, U+0131)
842
 
        f.setCharAt(f.length()-1, 0x131);
843
 
        (t=s).foldCase(U_FOLD_CASE_EXCLUDE_SPECIAL_I);
844
 
        if(f!=t) {
845
 
            errln("error in foldCase(\"" + s + "\", U_FOLD_CASE_EXCLUDE_SPECIAL_I)=\"" + t + "\" but expected \"" + f + "\"");
846
 
        }
847
 
    }
848
 
}
849
 
 
850
 
void
851
578
UnicodeStringTest::TestSearching()
852
579
{
853
580
    UnicodeString test1("test test ttest tetest testesteststt");
869
596
    UnicodeString test4(testChar32);
870
597
 
871
598
    uint16_t occurrences = 0;
872
 
    UTextOffset startPos = 0;
 
599
    int32_t startPos = 0;
873
600
    for ( ;
874
601
          startPos != -1 && startPos < test1.length();
875
602
          (startPos = test1.indexOf(test2, startPos)) != -1 ? (++occurrences, startPos += 4) : 0)
884
611
    if (occurrences != 4)
885
612
        errln("indexOf with starting offset failed: expected to find 4 occurrences, found " + occurrences);
886
613
 
887
 
    UTextOffset endPos = 28;
 
614
    int32_t endPos = 28;
888
615
    for ( occurrences = 0, startPos = 5;
889
616
          startPos != -1 && startPos < test1.length();
890
617
          (startPos = test1.indexOf(test2, startPos, endPos - startPos)) != -1 ? (++occurrences, startPos += 4) : 0)