View Javadoc
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.commons.codec.language.bm;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertThat;
22  
23  import org.hamcrest.BaseMatcher;
24  import org.hamcrest.Description;
25  import org.junit.Test;
26  
27  /**
28   * Tests Rule.
29   *
30   * @since 1.6
31   */
32  public class RuleTest {
33      private static class NegativeIntegerBaseMatcher extends BaseMatcher<Integer> {
34          @Override
35          public void describeTo(final Description description) {
36              description.appendText("value should be negative");
37          }
38  
39          @Override
40          public boolean matches(final Object item) {
41              return ((Integer) item).intValue() < 0;
42          }
43      }
44  
45      private Rule.Phoneme[][] makePhonemes() {
46          final String[][] words = {
47                  { "rinD", "rinDlt", "rina", "rinalt", "rino", "rinolt", "rinu", "rinult" },
48                  { "dortlaj", "dortlej", "ortlaj", "ortlej", "ortlej-dortlaj" } };
49          final Rule.Phoneme[][] phonemes = new Rule.Phoneme[words.length][];
50  
51          for (int i = 0; i < words.length; i++) {
52              final String[] words_i = words[i];
53              final Rule.Phoneme[] phonemes_i = phonemes[i] = new Rule.Phoneme[words_i.length];
54              for (int j = 0; j < words_i.length; j++) {
55                  phonemes_i[j] = new Rule.Phoneme(words_i[j], Languages.NO_LANGUAGES);
56              }
57          }
58  
59          return phonemes;
60      }
61  
62      @Test
63      public void testPhonemeComparedToLaterIsNegative() {
64          for (final Rule.Phoneme[] phs : makePhonemes()) {
65              for (int i = 0; i < phs.length; i++) {
66                  for (int j = i + 1; j < phs.length; j++) {
67                      final int c = Rule.Phoneme.COMPARATOR.compare(phs[i], phs[j]);
68  
69                      assertThat("Comparing " + phs[i].getPhonemeText() + " to " + phs[j].getPhonemeText() + " should be negative", Integer.valueOf(c),
70                              new NegativeIntegerBaseMatcher());
71                  }
72              }
73          }
74      }
75  
76      @Test
77      public void testPhonemeComparedToSelfIsZero() {
78          for (final Rule.Phoneme[] phs : makePhonemes()) {
79              for (final Rule.Phoneme ph : phs) {
80                  assertEquals("Phoneme compared to itself should be zero: " + ph.getPhonemeText(), 0,
81                          Rule.Phoneme.COMPARATOR.compare(ph, ph));
82              }
83          }
84      }
85  
86      @Test
87      public void testSubSequenceWorks() {
88          // AppendableCharSequence is private to Rule. We can only make it through a Phoneme.
89  
90          final Rule.Phoneme a = new Rule.Phoneme("a", null);
91          final Rule.Phoneme b = new Rule.Phoneme("b", null);
92          final Rule.Phoneme cd = new Rule.Phoneme("cd", null);
93          final Rule.Phoneme ef = new Rule.Phoneme("ef", null);
94          final Rule.Phoneme ghi = new Rule.Phoneme("ghi", null);
95          final Rule.Phoneme jkl = new Rule.Phoneme("jkl", null);
96  
97          assertEquals('a', a.getPhonemeText().charAt(0));
98          assertEquals('b', b.getPhonemeText().charAt(0));
99          assertEquals('c', cd.getPhonemeText().charAt(0));
100         assertEquals('d', cd.getPhonemeText().charAt(1));
101         assertEquals('e', ef.getPhonemeText().charAt(0));
102         assertEquals('f', ef.getPhonemeText().charAt(1));
103         assertEquals('g', ghi.getPhonemeText().charAt(0));
104         assertEquals('h', ghi.getPhonemeText().charAt(1));
105         assertEquals('i', ghi.getPhonemeText().charAt(2));
106         assertEquals('j', jkl.getPhonemeText().charAt(0));
107         assertEquals('k', jkl.getPhonemeText().charAt(1));
108         assertEquals('l', jkl.getPhonemeText().charAt(2));
109 
110         final Rule.Phoneme a_b = new Rule.Phoneme(a, b);
111         assertEquals('a', a_b.getPhonemeText().charAt(0));
112         assertEquals('b', a_b.getPhonemeText().charAt(1));
113         assertEquals("ab", a_b.getPhonemeText().subSequence(0, 2).toString());
114         assertEquals("a", a_b.getPhonemeText().subSequence(0, 1).toString());
115         assertEquals("b", a_b.getPhonemeText().subSequence(1, 2).toString());
116 
117         final Rule.Phoneme cd_ef = new Rule.Phoneme(cd, ef);
118         assertEquals('c', cd_ef.getPhonemeText().charAt(0));
119         assertEquals('d', cd_ef.getPhonemeText().charAt(1));
120         assertEquals('e', cd_ef.getPhonemeText().charAt(2));
121         assertEquals('f', cd_ef.getPhonemeText().charAt(3));
122         assertEquals("c", cd_ef.getPhonemeText().subSequence(0, 1).toString());
123         assertEquals("d", cd_ef.getPhonemeText().subSequence(1, 2).toString());
124         assertEquals("e", cd_ef.getPhonemeText().subSequence(2, 3).toString());
125         assertEquals("f", cd_ef.getPhonemeText().subSequence(3, 4).toString());
126         assertEquals("cd", cd_ef.getPhonemeText().subSequence(0, 2).toString());
127         assertEquals("de", cd_ef.getPhonemeText().subSequence(1, 3).toString());
128         assertEquals("ef", cd_ef.getPhonemeText().subSequence(2, 4).toString());
129         assertEquals("cde", cd_ef.getPhonemeText().subSequence(0, 3).toString());
130         assertEquals("def", cd_ef.getPhonemeText().subSequence(1, 4).toString());
131         assertEquals("cdef", cd_ef.getPhonemeText().subSequence(0, 4).toString());
132 
133         final Rule.Phoneme a_b_cd = new Rule.Phoneme(new Rule.Phoneme(a, b), cd);
134         assertEquals('a', a_b_cd.getPhonemeText().charAt(0));
135         assertEquals('b', a_b_cd.getPhonemeText().charAt(1));
136         assertEquals('c', a_b_cd.getPhonemeText().charAt(2));
137         assertEquals('d', a_b_cd.getPhonemeText().charAt(3));
138         assertEquals("a", a_b_cd.getPhonemeText().subSequence(0, 1).toString());
139         assertEquals("b", a_b_cd.getPhonemeText().subSequence(1, 2).toString());
140         assertEquals("c", a_b_cd.getPhonemeText().subSequence(2, 3).toString());
141         assertEquals("d", a_b_cd.getPhonemeText().subSequence(3, 4).toString());
142         assertEquals("ab", a_b_cd.getPhonemeText().subSequence(0, 2).toString());
143         assertEquals("bc", a_b_cd.getPhonemeText().subSequence(1, 3).toString());
144         assertEquals("cd", a_b_cd.getPhonemeText().subSequence(2, 4).toString());
145         assertEquals("abc", a_b_cd.getPhonemeText().subSequence(0, 3).toString());
146         assertEquals("bcd", a_b_cd.getPhonemeText().subSequence(1, 4).toString());
147         assertEquals("abcd", a_b_cd.getPhonemeText().subSequence(0, 4).toString());
148     }
149 }