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

« back to all changes in this revision

Viewing changes to source/samples/search/search.cpp

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**************************************************************************
2
 
*
3
 
*   Copyright (C) 2001, International Business Machines
4
 
*   Corporation and others.  All Rights Reserved.
5
 
*
6
 
***************************************************************************
7
 
*   file name:  colex.cpp
8
 
*
9
 
*   created on: 2001June8
10
 
*   created by: Helena Shih
11
 
*
12
 
*   Sample code for the ICU Search C++ routines.  
13
 
*/
14
 
#include <stdio.h>
15
 
#include "unicode/utypes.h"
16
 
#include "unicode/unistr.h"
17
 
#include "unicode/locid.h"
18
 
 
19
 
#include "strsrch.h"
20
 
 
21
 
int main()
22
 
{
23
 
   UErrorCode status = U_ZERO_ERROR;
24
 
   UnicodeString target("A quick fox jumped over the lazy dog.", "");
25
 
   UnicodeString easyPatterns[] = {"FoX", "CAT", "jump", "under" };
26
 
   int exactOffsets[] = { -1, -1, 12, -1 };
27
 
   int tertiaryOffsets[] = { 8, -1, 12, -1 };
28
 
   UnicodeString monkeyTarget("abcdefgh");
29
 
   UnicodeString monkeyTarget2("ijklmnop");
30
 
 
31
 
   int i, j;
32
 
   int pos = 0; 
33
 
   ::StringSearch *searchIter = new ::StringSearch(easyPatterns[0], target, status);
34
 
   fprintf(stdout, "\n");
35
 
   if (U_FAILURE(status))
36
 
   {
37
 
        fprintf(stderr, "Failed to create a StringSearch object for the default locale.\n");
38
 
   }
39
 
   fprintf(stdout, "Try with default normalization mode and strength.\n");
40
 
   i = 0;
41
 
   while (TRUE)
42
 
   {
43
 
       status = U_ZERO_ERROR;
44
 
       searchIter->reset();
45
 
       pos = searchIter->next();
46
 
       if ( pos != exactOffsets[i] )
47
 
          fprintf(stdout, "Exact match failed at the index %d pattern.\n", i);
48
 
       
49
 
       i ++;
50
 
       if (i == 4) {
51
 
           break;
52
 
       }
53
 
 
54
 
       searchIter->setPattern(easyPatterns[i], status);
55
 
       if (U_FAILURE(status))
56
 
       {
57
 
            fprintf(stderr, "Failed to set a pattern for %d element.\n", i);
58
 
            continue;
59
 
       }
60
 
   }
61
 
   fprintf(stdout, "Try now with strength == primary.\n");
62
 
   status = U_ZERO_ERROR;
63
 
   searchIter->setStrength(Collator::PRIMARY, status);
64
 
   if (U_FAILURE(status))
65
 
   {
66
 
        fprintf(stderr, "Failed to set strength of the string search object.\n");
67
 
   }
68
 
   searchIter->reset();
69
 
   searchIter->setPattern(easyPatterns[0], status);
70
 
   if (U_FAILURE(status))
71
 
   {
72
 
        fprintf(stderr, "Failed to set a pattern for the first element.\n");
73
 
   }
74
 
   pos = searchIter->first();
75
 
   if (pos != tertiaryOffsets[0])
76
 
       fprintf(stdout, "Tertiary match failed at the first pattern.\n");
77
 
   for (i = 1; i < 4; i++)
78
 
   {
79
 
       status = U_ZERO_ERROR;
80
 
       searchIter->setPattern(easyPatterns[i], status);
81
 
       searchIter->reset();
82
 
       pos = searchIter->next();
83
 
       if (pos != tertiaryOffsets[i])
84
 
           fprintf(stdout, "Tertiary match failed at index %d pattern.\n", i);
85
 
   }
86
 
   // Going backwards
87
 
   searchIter->reset();
88
 
   searchIter->setPattern(easyPatterns[--i], status);
89
 
   if (U_FAILURE(status))
90
 
   {
91
 
        fprintf(stderr, "Failed to set a pattern for the last element.\n");
92
 
   }
93
 
   pos = searchIter->last();
94
 
   if (pos != tertiaryOffsets[i])
95
 
       fprintf(stdout, "Tertiary match failed at the last pattern.\n");
96
 
   for (; i >= 1 ; --i)
97
 
   {
98
 
       status = U_ZERO_ERROR;       
99
 
       searchIter->setPattern(easyPatterns[i-1], status);
100
 
       searchIter->reset();
101
 
       pos = searchIter->previous();
102
 
       if (pos != tertiaryOffsets[i-1])
103
 
           fprintf(stdout, "Walking backwards: tertiary match failed at index %d pattern.\n", i);
104
 
   }
105
 
   status = U_ZERO_ERROR;
106
 
  searchIter->setTarget(monkeyTarget);
107
 
  if (U_FAILURE(status))
108
 
  {
109
 
      fprintf(stderr, "Failed to set a pattern for the monkey target.\n");
110
 
      goto cleanup;
111
 
  }
112
 
  searchIter->setStrength(Collator::TERTIARY, status);
113
 
  // change direction again 
114
 
   searchIter->reset();
115
 
   searchIter->setPattern(monkeyTarget, status);
116
 
   if (U_FAILURE(status))
117
 
   {
118
 
        fprintf(stderr, "Failed to set a pattern as monkey test itself.\n");
119
 
   }
120
 
   pos = searchIter->first();
121
 
   if (pos == -1)
122
 
       fprintf(stdout, "Matching monkey test itself failed.\n");
123
 
  for (i = 0; i < monkeyTarget.length() - 1; i++)
124
 
   {
125
 
       // will always find its substring
126
 
       for (j = i+1; j < monkeyTarget.length(); j++)
127
 
       {
128
 
            UnicodeString temp;
129
 
            status = U_ZERO_ERROR;
130
 
            searchIter->reset();
131
 
            monkeyTarget.extract(i, j, temp);
132
 
            searchIter->setPattern(temp, status);
133
 
            if (U_FAILURE(status))
134
 
            {
135
 
                fprintf(stderr, "Failed to set a pattern for the %d -th monkey pattern of length %d.\n", i, j);
136
 
                continue;
137
 
            }
138
 
            pos = searchIter->next();
139
 
            if (pos == -1)
140
 
               fprintf(stdout, "Monkey match failed at index %d in monkey pattern of length %d.\n", i, j);
141
 
       }
142
 
   }
143
 
  status = U_ZERO_ERROR;
144
 
  searchIter->setTarget(monkeyTarget2);
145
 
  if (U_FAILURE(status))
146
 
  {
147
 
      fprintf(stderr, "Failed to set a pattern for the monkey target2.\n");
148
 
      goto cleanup;
149
 
  }
150
 
  for (i = 0; i < monkeyTarget.length() - 1; i++)
151
 
   {
152
 
       // will never find the match
153
 
        UnicodeString temp;
154
 
        status = U_ZERO_ERROR;
155
 
        monkeyTarget.extract(i, monkeyTarget.length(), temp);
156
 
        searchIter->reset();
157
 
        searchIter->setPattern(temp, status);
158
 
        if (U_FAILURE(status))
159
 
        {
160
 
            fprintf(stderr, "Failed to set a pattern for the monkey pattern at offset index %d.\n", i);
161
 
            continue;
162
 
        }
163
 
        pos = searchIter->next();
164
 
        if (pos != -1)
165
 
           fprintf(stdout, "Monkey mismatch failed at index %d in monkey pattern.\n", i);
166
 
   }
167
 
   
168
 
cleanup:
169
 
    delete searchIter;
170
 
    return 0;
171
 
}
172