001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.codec.language.bm;
019
020 import static org.junit.Assert.assertEquals;
021 import static org.junit.Assert.assertThat;
022
023 import org.hamcrest.BaseMatcher;
024 import org.hamcrest.Description;
025 import org.junit.Test;
026
027 /**
028 * Tests Rule.
029 *
030 * @since 1.6
031 */
032 public class RuleTest {
033 private static class NegativeIntegerBaseMatcher extends BaseMatcher<Integer> {
034 @Override
035 public void describeTo(final Description description) {
036 description.appendText("value should be negative");
037 }
038
039 @Override
040 public boolean matches(final Object item) {
041 return ((Integer) item).intValue() < 0;
042 }
043 }
044
045 private Rule.Phoneme[][] makePhonemes() {
046 final String[][] words = {
047 { "rinD", "rinDlt", "rina", "rinalt", "rino", "rinolt", "rinu", "rinult" },
048 { "dortlaj", "dortlej", "ortlaj", "ortlej", "ortlej-dortlaj" } };
049 final Rule.Phoneme[][] phonemes = new Rule.Phoneme[words.length][];
050
051 for (int i = 0; i < words.length; i++) {
052 final String[] words_i = words[i];
053 final Rule.Phoneme[] phonemes_i = phonemes[i] = new Rule.Phoneme[words_i.length];
054 for (int j = 0; j < words_i.length; j++) {
055 phonemes_i[j] = new Rule.Phoneme(words_i[j], Languages.NO_LANGUAGES);
056 }
057 }
058
059 return phonemes;
060 }
061
062 @Test
063 public void testPhonemeComparedToLaterIsNegative() {
064 for (final Rule.Phoneme[] phs : makePhonemes()) {
065 for (int i = 0; i < phs.length; i++) {
066 for (int j = i + 1; j < phs.length; j++) {
067 final int c = Rule.Phoneme.COMPARATOR.compare(phs[i], phs[j]);
068
069 assertThat("Comparing " + phs[i].getPhonemeText() + " to " + phs[j].getPhonemeText() + " should be negative", Integer.valueOf(c),
070 new NegativeIntegerBaseMatcher());
071 }
072 }
073 }
074 }
075
076 @Test
077 public void testPhonemeComparedToSelfIsZero() {
078 for (final Rule.Phoneme[] phs : makePhonemes()) {
079 for (final Rule.Phoneme ph : phs) {
080 assertEquals("Phoneme compared to itself should be zero: " + ph.getPhonemeText(), 0,
081 Rule.Phoneme.COMPARATOR.compare(ph, ph));
082 }
083 }
084 }
085
086 @Test
087 public void testSubSequenceWorks() {
088 // AppendableCharSequence is private to Rule. We can only make it through a Phoneme.
089
090 final Rule.Phoneme a = new Rule.Phoneme("a", null);
091 final Rule.Phoneme b = new Rule.Phoneme("b", null);
092 final Rule.Phoneme cd = new Rule.Phoneme("cd", null);
093 final Rule.Phoneme ef = new Rule.Phoneme("ef", null);
094 final Rule.Phoneme ghi = new Rule.Phoneme("ghi", null);
095 final Rule.Phoneme jkl = new Rule.Phoneme("jkl", null);
096
097 assertEquals('a', a.getPhonemeText().charAt(0));
098 assertEquals('b', b.getPhonemeText().charAt(0));
099 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 = a.append(b.getPhonemeText());
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 = cd.append(ef.getPhonemeText());
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 = a.append(b.getPhonemeText()).append(cd.getPhonemeText());
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 }