add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / reverse / TestReverseStringFilter.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.analysis.reverse;
19
20 import java.io.StringReader;
21
22 import org.apache.lucene.analysis.MockTokenizer;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
25
26 public class TestReverseStringFilter extends BaseTokenStreamTestCase {
27   public void testFilter() throws Exception {
28     TokenStream stream = new MockTokenizer(new StringReader("Do have a nice day"),
29         MockTokenizer.WHITESPACE, false);     // 1-4 length string
30     ReverseStringFilter filter = new ReverseStringFilter(TEST_VERSION_CURRENT, stream);
31     assertTokenStreamContents(filter, new String[] { "oD", "evah", "a", "ecin", "yad" });
32   }
33   
34   public void testFilterWithMark() throws Exception {
35     TokenStream stream = new MockTokenizer(new StringReader("Do have a nice day"),
36         MockTokenizer.WHITESPACE, false); // 1-4 length string
37     ReverseStringFilter filter = new ReverseStringFilter(TEST_VERSION_CURRENT, stream, '\u0001');
38     assertTokenStreamContents(filter, 
39         new String[] { "\u0001oD", "\u0001evah", "\u0001a", "\u0001ecin", "\u0001yad" });
40   }
41
42   public void testReverseString() throws Exception {
43     assertEquals( "A", ReverseStringFilter.reverse(TEST_VERSION_CURRENT, "A" ) );
44     assertEquals( "BA", ReverseStringFilter.reverse(TEST_VERSION_CURRENT, "AB" ) );
45     assertEquals( "CBA", ReverseStringFilter.reverse(TEST_VERSION_CURRENT, "ABC" ) );
46   }
47   
48   public void testReverseChar() throws Exception {
49     char[] buffer = { 'A', 'B', 'C', 'D', 'E', 'F' };
50     ReverseStringFilter.reverse(TEST_VERSION_CURRENT, buffer, 2, 3 );
51     assertEquals( "ABEDCF", new String( buffer ) );
52   }
53   
54   /**
55    * Test the broken 3.0 behavior, for back compat
56    */
57   public void testBackCompat() throws Exception {
58     assertEquals("\uDF05\uD866\uDF05\uD866", ReverseStringFilter.reverse("𩬅𩬅"));
59   }
60   
61   public void testReverseSupplementary() throws Exception {
62     // supplementary at end
63     assertEquals("𩬅艱鍟䇹愯瀛", ReverseStringFilter.reverse(TEST_VERSION_CURRENT, "瀛愯䇹鍟艱𩬅"));
64     // supplementary at end - 1
65     assertEquals("a𩬅艱鍟䇹愯瀛", ReverseStringFilter.reverse(TEST_VERSION_CURRENT, "瀛愯䇹鍟艱𩬅a"));
66     // supplementary at start
67     assertEquals("fedcba𩬅", ReverseStringFilter.reverse(TEST_VERSION_CURRENT, "𩬅abcdef"));
68     // supplementary at start + 1
69     assertEquals("fedcba𩬅z", ReverseStringFilter.reverse(TEST_VERSION_CURRENT, "z𩬅abcdef"));
70     // supplementary medial
71     assertEquals("gfe𩬅dcba", ReverseStringFilter.reverse(TEST_VERSION_CURRENT, "abcd𩬅efg"));
72   }
73
74   public void testReverseSupplementaryChar() throws Exception {
75     // supplementary at end
76     char[] buffer = "abc瀛愯䇹鍟艱𩬅".toCharArray();
77     ReverseStringFilter.reverse(TEST_VERSION_CURRENT, buffer, 3, 7);
78     assertEquals("abc𩬅艱鍟䇹愯瀛", new String(buffer));
79     // supplementary at end - 1
80     buffer = "abc瀛愯䇹鍟艱𩬅d".toCharArray();
81     ReverseStringFilter.reverse(TEST_VERSION_CURRENT, buffer, 3, 8);
82     assertEquals("abcd𩬅艱鍟䇹愯瀛", new String(buffer));
83     // supplementary at start
84     buffer = "abc𩬅瀛愯䇹鍟艱".toCharArray();
85     ReverseStringFilter.reverse(TEST_VERSION_CURRENT, buffer, 3, 7);
86     assertEquals("abc艱鍟䇹愯瀛𩬅", new String(buffer));
87     // supplementary at start + 1
88     buffer = "abcd𩬅瀛愯䇹鍟艱".toCharArray();
89     ReverseStringFilter.reverse(TEST_VERSION_CURRENT, buffer, 3, 8);
90     assertEquals("abc艱鍟䇹愯瀛𩬅d", new String(buffer));
91     // supplementary medial
92     buffer = "abc瀛愯𩬅def".toCharArray();
93     ReverseStringFilter.reverse(TEST_VERSION_CURRENT, buffer, 3, 7);
94     assertEquals("abcfed𩬅愯瀛", new String(buffer));
95   }
96 }