summaryrefslogtreecommitdiff
path: root/qadevOOo/tests/basic/ifc/i18n/XBreakIterator/i18n_XBreakIterator.xba
blob: 0613d1e96296bf6ecccfa41c596ccca1f5d55781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="i18n_XBreakIterator" script:language="StarBasic">


'*************************************************************************
'
' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
' 
' Copyright 2008 by Sun Microsystems, Inc.
'
' OpenOffice.org - a multi-platform office productivity suite
'
' $RCSfile: i18n_XBreakIterator.xba,v $
'
' $Revision: 1.3 $
'
' This file is part of OpenOffice.org.
'
' OpenOffice.org is free software: you can redistribute it and/or modify
' it under the terms of the GNU Lesser General Public License version 3
' only, as published by the Free Software Foundation.
'
' OpenOffice.org is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Lesser General Public License version 3 for more details
' (a copy is included in the LICENSE file that accompanied this code).
'
' You should have received a copy of the GNU Lesser General Public License
' version 3 along with OpenOffice.org.  If not, see
' <http://www.openoffice.org/license.html>
' for a copy of the LGPLv3 License.
'
'*************************************************************************
'*************************************************************************



' Be sure that all variables are dimensioned:
option explicit

'*************************************************************************
' This Interface/Service test depends on the following GLOBAL variables,
' which must be specified in the object creation:

'     - Global oLocale As Object
'     - Global UnicodeString As String
'     - Global ScriptType(2, 1) As Integer

'*************************************************************************





const arrSize = 300


Sub RunTest()

'*************************************************************************
' INTERFACE: 
' com.sun.star.i18n.XBreakIterator
'*************************************************************************
On Error Goto ErrHndl
    Dim bOK As Boolean
    Dim i As Integer
    Dim nCharacterIteratorMode As Integer
    Dim lDone As Long
    Dim lRes As Long
    Dim wordType As Integer
    Dim Boundary As Variant
    Dim BoundsToTest(arrSize) As new com.sun.star.i18n.Boundary
    Dim Types(arrSize) As Integer
    Dim nBoundsToTest As Integer
    Dim iPos As Integer
    Dim iType As Integer
    Dim bFound As Boolean
    Dim j As Integer
    Dim NextCharacters(2, 3) As Integer
    Dim PreviousCharacters(2, 3) As Integer

    Test.StartMethod("nextCharacters()")
    bOK = true
    nCharacterIteratorMode = com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER

    'Start from position     : Travel ... chars         : Actual position after    : How many chars traveled
    NextCharacters(0, 0) =  1 : NextCharacters(0, 1) =  5000 : NextCharacters(0, 2) = len(UnicodeString) : NextCharacters(0, 3) = len(UnicodeString) - 1
    NextCharacters(1, 0) =  5 : NextCharacters(1, 1) = -5000 : NextCharacters(1, 2) =  0 : NextCharacters(1, 3) = -5
    NextCharacters(2, 0) = 10 : NextCharacters(2, 1) =     6 : NextCharacters(2, 2) = 16 : NextCharacters(2, 3) = 6

    for i = 0 to 2
        lRes = oObj.nextCharacters(UnicodeString, NextCharacters(i, 0), _
                oLocale, nCharacterIteratorMode, NextCharacters(i, 1), lDone)
        Out.Log("Expected result is: lRes = " + NextCharacters(i, 2) + _
                                 "; lDone = " + NextCharacters(i, 3))
        Out.Log("Actual result is: lRes = " + lRes + "; lDone = " + lDone)

        bOK = bOK AND lRes = NextCharacters(i, 2)
        bOK = bOK AND lDone = NextCharacters(i, 3)
    next i

    Test.MethodTested("nextCharacters()", bOK)

    Test.StartMethod("previousCharacters()")
    bOK = true
    nCharacterIteratorMode = com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER

    'Start from position     : Travel ... chars         : Actual position after    : How many chars traveled
    PreviousCharacters(0, 0) = 10 : PreviousCharacters(0, 1) = -5000 : PreviousCharacters(0, 2) = len(UnicodeString) : PreviousCharacters(0, 3) = len(UnicodeString) - 10
    PreviousCharacters(1, 0) =  5 : PreviousCharacters(1, 1) =  5000 : PreviousCharacters(1, 2) = 0 : PreviousCharacters(1, 3) = 5
    PreviousCharacters(2, 0) = 10 : PreviousCharacters(2, 1) =     6 : PreviousCharacters(2, 2) = 4 : PreviousCharacters(2, 3) = 6

    for i = 0 to 2
        lRes = oObj.previousCharacters(UnicodeString, PreviousCharacters(i, 0), _
                oLocale, nCharacterIteratorMode, PreviousCharacters(i, 1), lDone)
        Out.Log("Expected result is: lRes = " + PreviousCharacters(i, 2) + _
                                 "; lDone = " + PreviousCharacters(i, 3))
        Out.Log("Actual result is: lRes = " + lRes + "; lDone = " + lDone)

        bOK = bOK AND lRes = PreviousCharacters(i, 2)
        bOK = bOK AND lDone = PreviousCharacters(i, 3)
    next i

    Test.MethodTested("previousCharacters()", bOK)


    Test.StartMethod("nextWord()")
    Test.StartMethod("previousWord()")
    bOK = true
    wordType = com.sun.star.i18n.WordType.ANYWORD_IGNOREWHITESPACES
    Dim wordCount1 As Integer
    Dim wordCount2 As Integer
    wordCount1 = 0
    wordCount2 = 0
    i = 0
    nBoundsToTest = 0

    while i &lt; len(UnicodeString) - 1
        Boundary = oObj.nextWord(UnicodeString, i, oLocale, wordType)
        i = Boundary.endPos - 1
        wordCount1 = wordCount1 + 1
        if ((nBoundsToTest &lt; arrSize) AND (Boundary.endPos - Boundary.startPos &gt; 3)) then
            BoundsToTest(nBoundsToTest).startPos = Boundary.startPos
            BoundsToTest(nBoundsToTest).endPos = Boundary.endPos  ' Here we find out some words and
            nBoundsToTest = nBoundsToTest + 1       ' get their bounds to test in future
        end if
        Out.Log("Word " + wordCount1 + "(" + Boundary.startPos + "," + Boundary.endPos + "): '" +  _
            mid(UnicodeString, Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'")
    wend
    Out.Log("In text there are " + wordCount1 + " words, if count from left to right")

    while i &gt; 0
        Boundary = oObj.previousWord(UnicodeString, i, oLocale, wordType)
        i = Boundary.startPos
        wordCount2 = wordCount2 + 1
        Out.Log("Word " + wordCount2 + "(" + Boundary.startPos + "," + Boundary.endPos + "): '" + _
            mid(UnicodeString,  Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'")
    wend
    Out.Log("In text there are " + wordCount2 + " words, if count from right to left")

    bOK = bOK AND wordCount1 = wordCount2
    Test.MethodTested("previousWord()", bOK)
    Test.MethodTested("nextWord()", bOK)

    Test.StartMethod("getWordBoundary()")
    bOK = true
    if (nBoundsToTest = 0) then
        Out.Log("Couldn't find words longer than 3 letters.")
        bOK = false
    end if

    for i = 0 to nBoundsToTest - 1
        iPos = (BoundsToTest(i).endPos - BoundsToTest(i).startPos) / 2 + BoundsToTest(i).startPos ' calculate middle of the word
        Boundary = oObj.getWordBoundary(UnicodeString, iPos, oLocale, wordType, true)
        Out.Log("Expected result is: startPos = " + BoundsToTest(i).startPos + _
                                 "; endPos = " + BoundsToTest(i).endPos)
        Out.Log("Actual result is: startPos = " + Boundary.startPos + "; endPos = " + Boundary.endPos + _
               " Word is: '" + mid(UnicodeString, Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'")

        bOK = bOK AND BoundsToTest(i).startPos = Boundary.startPos
        bOK = bOK AND BoundsToTest(i).endPos = Boundary.endPos
    next i

    Test.MethodTested("getWordBoundary()", bOK)

    Test.StartMethod("getWordType()")
    bOK = true
    for i = 0 to nBoundsToTest - 1
        iPos = (BoundsToTest(i).endPos - BoundsToTest(i).startPos) / 2 + BoundsToTest(i).startPos ' calculate middle of the word
        wordType = oObj.getWordType(UnicodeString, iPos, oLocale)
        bOK = bOK AND wordType &gt;= 0 AND wordType &lt;= 2
    next i
    Test.MethodTested("getWordType()", bOK)

    Test.StartMethod("isBeginWord()")
    bOK = true
    if (nBoundsToTest = 0) then
        Out.Log("Couldn't find words longer than 3 letters.")
        bOK = false
    end if

    for i = 0 to nBoundsToTest - 1
        bOK = bOK AND oObj.isBeginWord(UnicodeString, BoundsToTest(i).startPos, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
        bOK = bOK AND NOT oObj.isBeginWord(UnicodeString, BoundsToTest(i).startPos + 1, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
    next i
    Test.MethodTested("isBeginWord()", bOK)

    Test.StartMethod("isEndWord()")
    bOK = true
    if (nBoundsToTest = 0) then
        Out.Log("Couldn't find words longer than 3 letters.")
        bOK = false
    end if

    for i = 0 to nBoundsToTest - 1
        bOK = bOK AND oObj.isEndWord(UnicodeString, BoundsToTest(i).endPos, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
        bOK = bOK AND NOT oObj.isEndWord(UnicodeString, BoundsToTest(i).endPos - 1, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
    next i
    Test.MethodTested("isEndWord()", bOK)

    Test.StartMethod("beginOfSentence()")
    Test.StartMethod("endOfSentence()")
    Dim sStart As Integer
    Dim sEnd As Integer

    bOK = true
    iPos = 0
    while (iPos &lt;= len(UnicodeString))' AND bOK)
        sStart = oObj.beginOfSentence(UnicodeString, iPos, oLocale)
        sEnd = oObj.endOfSentence(UnicodeString, iPos, oLocale)
        if (iPos &lt; sStart OR iPos &gt; sEnd) then
            bOK = false
            Out.Log("From position " + iPos + " sentence range is [" + sStart + ", " + sEnd + "]")
            Out.Log("'" + mid(UnicodeString, sStart + 1, sEnd - sStart) + "'")
        end if
        iPos = iPos + 1
    wend

    if (bOK) then
        bOK = oObj.beginOfSentence(UnicodeString, -10, oLocale) = -1
        bOK = oObj.endOfSentence(UnicodeString, -10, oLocale) = -1
        bOK = oObj.beginOfSentence(UnicodeString, len(UnicodeString) + 1, oLocale) = -1
        bOK = oObj.endOfSentence(UnicodeString, len(UnicodeString) + 1, oLocale) = -1
        if (NOT bOK) then Out.Log("When invalid position, returned range is NOT [-1, -1]")
    end if

    Test.MethodTested("beginOfSentence()", bOK)
    Test.MethodTested("endOfSentence()", bOK)

    Test.StartMethod("getLineBreak()")
    bOK = true
    Dim LineBreakResults As Variant
    Dim _LineBreakHyphenationOptions As new com.sun.star.i18n.LineBreakHyphenationOptions
    Dim _LineBreakUserOptions As new com.sun.star.i18n.LineBreakUserOptions
    Dim breakPos As Long, pos As Long
    _LineBreakUserOptions.applyForbiddenRules = false
    _LineBreakUserOptions.allowHyphenateEnglish = false

    breakPos = 0  : pos = 0
    
    while breakPos = 0 AND pos &lt; len(UnicodeString)
        LineBreakResults = oObj.getLineBreak(UnicodeString, pos, oLocale, 0, _LineBreakHyphenationOptions, _LineBreakUserOptions)
        breakPos = LineBreakResults.BreakIndex
        pos = pos + 1
    wend 
    
    ' finally the position of break must be found in the middle and 
    ' it must be before the break position specified
    bOK = (breakPos &lt;= pos AND breakPos > 0)

    if (NOT bOK) then
        Out.Log("The last position was: " + pos + ", and the break position was: " + breakPos)
    end if

    Test.MethodTested("getLineBreak()", bOK)


    Test.StartMethod("beginOfScript()")
    Test.StartMethod("endOfScript()")
    bOK = true
    Out.Log("Searching for scripts...")
    iPos = 0
    nBoundsToTest = 0

    while iPos &lt; len(UnicodeString) AND iPos &gt; -1 AND nBoundsToTest &lt; arrSize
        iType = oObj.getScriptType(UnicodeString, iPos)
        BoundsToTest(nBoundsToTest).startPos = oObj.beginOfScript(UnicodeString, iPos, iType)
        BoundsToTest(nBoundsToTest).endPos = oObj.endOfScript(UnicodeString, iPos, iType)
        Types(nBoundsToTest) = iType
        iPos = BoundsToTest(nBoundsToTest).endPos
        Out.Log("Type: " + iType + "; Bounds: [" + BoundsToTest(nBoundsToTest).startPos + "," + BoundsToTest(nBoundsToTest).endPos + "]")
        nBoundsToTest = nBoundsToTest + 1
    wend
    bOK = bOK AND nBoundsToTest &gt; 0

    Test.MethodTested("beginOfScript()", bOK)
    Test.MethodTested("endOfScript()", bOK)


    Test.StartMethod("nextScript()")
    bOK = true
    i = 1
    iPos = -1
    while i &lt; nBoundsToTest AND bOK
        iPos = oObj.nextScript(UnicodeString, iPos, Types(i))
        Out.Log("Expected index for Type " + Types(i) + " for nextScript is " + BoundsToTest(i).startPos)
        bOK = bOK AND iPos = BoundsToTest(i).startPos
        Out.Log("NextScript returned " + iPos + ";")
        i = i + 1
    wend
    Test.MethodTested("nextScript()", bOK)

    Test.StartMethod("previousScript()")
    bOK = true
    i = nBoundsToTest - 2
    iPos = len(UnicodeString) + 1
    while i &gt;= 0  AND bOK
        iPos = oObj.previousScript(UnicodeString, iPos, Types(i))
        Out.Log("Expected index for Type " + Types(i) + " for previousScript is " + BoundsToTest(i).startPos)
        bOK = bOK AND iPos = BoundsToTest(i).startPos
        Out.Log("PreviousScript returned " + iPos + ";")
        i = i - 1
    wend
    Test.MethodTested("previousScript()", bOK)

    Test.StartMethod("getScriptType()")
    bOK = true
    for i = 0 to 2
        iPos = oObj.getScriptType(UnicodeString, ScriptType(i, 0))
        Out.Log("Expected result is: ScriptType = " + ScriptType(i, 1))
        Out.Log("Actual result is: ScriptType = " + iPos)
        bOK = bOK AND iPos = ScriptType(i, 1)
    next i
    Test.MethodTested("getScriptType()", bOK)

    Test.StartMethod("beginOfCharBlock()")
    Test.StartMethod("endOfCharBlock()")
    bOK = true
    Out.Log("getting CharBlocks bounds...")
    iPos = 0
    nBoundsToTest = 0

    while iPos &lt; len(UnicodeString) AND iPos &gt; -1 AND nBoundsToTest &lt; arrSize
        iType = getCharBlockType(iPos)
        BoundsToTest(nBoundsToTest).startPos = oObj.beginOfCharBlock(UnicodeString, iPos, oLocale, iType)
        BoundsToTest(nBoundsToTest).endPos = oObj.endOfCharBlock(UnicodeString, iPos, oLocale, iType)
        iPos = BoundsToTest(nBoundsToTest).endPos
        Out.Log("" + nBoundsToTest + "). Bounds: [" + BoundsToTest(nBoundsToTest).startPos + "," + BoundsToTest(nBoundsToTest).endPos + "]; Type = " + iType)
        Types(nBoundsToTest) = iType
        nBoundsToTest = nBoundsToTest + 1
    wend

    for i = 0 to nBoundsToTest - 2
        bOK = bOK AND BoundsToTest(i).endPos = BoundsToTest(i + 1).startPos
    next
    Out.Log("Testing for no intersections : " + bOK)
    bOK = bOK AND BoundsToTest(0).startPos = 0
    bOK = bOK AND BoundsToTest(nBoundsToTest - 1).endPos = len(UnicodeString)
    Out.Log("Regions should starts with 0 and ends with " + len(UnicodeString))

    Test.MethodTested("beginOfCharBlock()", bOK)
    Test.MethodTested("endOfCharBlock()", bOK)

    Test.StartMethod("nextCharBlock()")
    bOK = true
    for i = 0 to nBoundsToTest - 1
        iPos = oObj.nextCharBlock(UnicodeString, BoundsToTest(i).startPos - 1, oLocale, Types(i))
        if (iPos &lt;&gt; BoundsToTest(i).startPos) then
            bOK = false
            Out.Log("nextCharBlock(UnicodeString, " + BoundsToTest(i).startPos - 1 + ", oLocale, " + Types(i) + ") should return " + BoundsToTest(i).startPos)
            Out.Log("... and actual value is " + iPos)
        end if
    next i
    Test.MethodTested("nextCharBlock()", bOK)

    Test.StartMethod("previousCharBlock()")
    bOK = true
    for i = 0 to nBoundsToTest - 1
        iPos = oObj.previousCharBlock(UnicodeString, BoundsToTest(i).endPos + 1, oLocale, Types(i))
        if (iPos &lt;&gt; BoundsToTest(i).startPos) then
            bOK = false
            Out.Log("previousCharBlock(UnicodeString, " + BoundsToTest(i).startPos - 1 +", oLocale, " + Types(i) + ") should return " + BoundsToTest(i).startPos)
            Out.Log("... and actual value is " + iPos)
        end if
    next i
    Test.MethodTested("previousCharBlock()", bOK)

Exit Sub
ErrHndl:
    Test.Exception()
    bOK = false
    resume next
End Sub
Function getCharBlockType(pos As Integer) As Integer
    Dim i As Integer
    Dim cType As Integer

    cType = 0
    i = 1
    while (i &lt; 31)
        if (oObj.beginOfCharBlock(UnicodeString, pos, oLocale, i) &lt;&gt; -1) then
            cType = i
            i = 100
        end if
        i = i + 1
    wend

    getCharBlockType = cType
End Function
</script:module>