ICU4J AlphabeticIndex tests to improve coverage.
Bug: 22023363
Change-Id: I25f92448713c407195c951e74ea528da65365c0d
diff --git a/android_icu4j/cts-coverage/src/main/tests/android/icu/cts/coverage/text/AlphabeticIndexTest.java b/android_icu4j/cts-coverage/src/main/tests/android/icu/cts/coverage/text/AlphabeticIndexTest.java
new file mode 100644
index 0000000..ed45929
--- /dev/null
+++ b/android_icu4j/cts-coverage/src/main/tests/android/icu/cts/coverage/text/AlphabeticIndexTest.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.icu.cts.coverage.text;
+
+import android.icu.text.AlphabeticIndex;
+import android.icu.util.ULocale;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.util.Locale;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+@RunWith(JUnit4.class)
+public class AlphabeticIndexTest {
+ @Test
+ public void testAddLabels_Locale() {
+ AlphabeticIndex<?> ulocaleIndex = new AlphabeticIndex<>(ULocale.CANADA);
+ AlphabeticIndex<?> localeIndex = new AlphabeticIndex<>(Locale.CANADA);
+
+ ulocaleIndex.addLabels(ULocale.SIMPLIFIED_CHINESE);
+ localeIndex.addLabels(Locale.SIMPLIFIED_CHINESE);
+
+ assertEquals(ulocaleIndex.getBucketLabels(), localeIndex.getBucketLabels());
+ }
+
+ @Test
+ public void testGetRecordCount_empty() {
+ assertEquals(0, new AlphabeticIndex<>(ULocale.CANADA).getRecordCount());
+ }
+
+ @Test
+ public void testGetRecordCount_withRecords() {
+ assertEquals(1, new AlphabeticIndex<>(ULocale.CANADA).addRecord("foo", null)
+ .getRecordCount());
+ }
+
+ /**
+ * Check that setUnderflowLabel/setOverflowLabel/setInflowLabel correctly influence the name of
+ * generated labels.
+ */
+ @Test
+ public void testFlowLabels() {
+ AlphabeticIndex<?> index = new AlphabeticIndex<>(ULocale.ENGLISH)
+ .addLabels(ULocale.forLanguageTag("ru"));
+ index.setUnderflowLabel("underflow");
+ index.setOverflowLabel("overflow");
+ index.setInflowLabel("inflow");
+ index.addRecord("!", null);
+ index.addRecord("\u03B1", null); // GREEK SMALL LETTER ALPHA
+ index.addRecord("\uab70", null); // CHEROKEE SMALL LETTER A
+
+
+ AlphabeticIndex.Bucket<?> underflowBucket = null;
+ AlphabeticIndex.Bucket<?> overflowBucket = null;
+ AlphabeticIndex.Bucket<?> inflowBucket = null;
+ for (AlphabeticIndex.Bucket<?> bucket : index) {
+ switch (bucket.getLabelType()) {
+ case UNDERFLOW:
+ assertNull(underflowBucket);
+ underflowBucket = bucket;
+ break;
+ case OVERFLOW:
+ assertNull(overflowBucket);
+ overflowBucket = bucket;
+ break;
+ case INFLOW:
+ assertNull(inflowBucket);
+ inflowBucket = bucket;
+ break;
+ }
+ }
+ assertNotNull(underflowBucket);
+ assertEquals("underflow", underflowBucket.getLabel());
+ assertEquals(1, underflowBucket.size());
+
+ assertNotNull(overflowBucket);
+ assertEquals("overflow", overflowBucket.getLabel());
+ assertEquals(1, overflowBucket.size());
+
+ assertNotNull(inflowBucket);
+ assertEquals("inflow", inflowBucket.getLabel());
+ assertEquals(1, inflowBucket.size());
+ }
+}
diff --git a/android_icu4j/cts-coverage/src/main/tests/android/icu/cts/coverage/text/TestAll.java b/android_icu4j/cts-coverage/src/main/tests/android/icu/cts/coverage/text/TestAll.java
index 5938896..e4f1bb2 100644
--- a/android_icu4j/cts-coverage/src/main/tests/android/icu/cts/coverage/text/TestAll.java
+++ b/android_icu4j/cts-coverage/src/main/tests/android/icu/cts/coverage/text/TestAll.java
@@ -24,6 +24,7 @@
@RunWith(Suite.class)
// Add classes in alphabetical order with a trailing comma even if it's the last entry.
@Suite.SuiteClasses({
+ AlphabeticIndexTest.class,
RelativeDateTimeFormatterTest.class,
TimeZoneNamesTest.class,
})