1 /*
 2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 4401726
27  * @author John O'Conner
28  * @library /java/text/testlib
29  * @summary Regression tests for Collation and associated classes
30  */
31 
32 
33 import java.text.*;
34 import java.util.Locale;
35 import java.util.Vector;
36 
37 public class Test4401726 extends CollatorTest {
38 
39     public static void main(String[] args) throws Exception {
40         new Test4401726().run(args);
41     }
42 
43    public void TestSetOffSet() {
44 
45        int[] expected = {0, -1, 65536};
46        int[] actual = new int[expected.length];
47 
48         try {
49             String rule = "< a, A < d; D";
50 
51             RuleBasedCollator rbc = new RuleBasedCollator(rule);
52             String str = "aD";
53             CollationElementIterator iterator =
54                 rbc.getCollationElementIterator(str);
55 
56             iterator.setOffset(0);
57             actual[0] = iterator.getOffset();
58             actual[1] = iterator.previous();
59             iterator.setOffset(0);
60             actual[2] = iterator.next();
61 
62             if (compareArray(expected, actual) == false) {
63                 errln("Failed.");
64             }
65 
66             str = "a";
67             iterator = rbc.getCollationElementIterator(str);
68             iterator.setOffset(0);
69             actual[0] = iterator.getOffset();
70             actual[1] = iterator.previous();
71             iterator.setOffset(0);
72             actual[2] = iterator.next();
73 
74             if (compareArray(expected, actual) == false) {
75                 errln("Failed.");
76             }
77 
78         } catch (ParseException e) {
79             errln("Unexpected ParseException: " + e);
80         }
81 
82 
83     }
84 
85     boolean compareArray(int[] expected, int[] actual) {
86         boolean retVal = false;
87         if (expected.length == actual.length) {
88             int errors = 0;
89             for(int x=0; x< expected.length; ++x) {
90                 if (expected[x] != actual[x]) {
91                     ++errors;
92                 }
93             }
94             if (errors == 0) retVal = true;
95         }
96         return retVal;
97     }
98 }