Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Pazderski2019-08-26 12:56:02 +0000
committerPaul Pazderski2019-09-10 18:24:58 +0000
commit28a3067ef86ee5e03a0401f19adf07a318985bd9 (patch)
tree6e650e6bb0bc5c0e063c59a4f5d7acdd5da1efd4 /org.eclipse.text.tests
parent95b3ff3beb9bbaf6e68933385ade1fb721b2a30a (diff)
downloadeclipse.platform.text-28a3067ef86ee5e03a0401f19adf07a318985bd9.tar.gz
eclipse.platform.text-28a3067ef86ee5e03a0401f19adf07a318985bd9.tar.xz
eclipse.platform.text-28a3067ef86ee5e03a0401f19adf07a318985bd9.zip
Bug 550471 - Improve and deprecate TextUtilities.indexOf
Existing TextUtilities.indexOf got a quite ugly return value and some strange behaviour in details (e.g. how it match empty strings). It is also superseded by the new MultiStringMatcher. The implementation is slow (especially for larger inputs) but must be retained at least for some years. This change provides a faster and fully compatible reimplementation based on MultiStringMatcher. Change-Id: I54c471ddd29a6aeb111151116e392dc8b8f8033a Signed-off-by: Paul Pazderski <paul-eclipse@ppazderski.de>
Diffstat (limited to 'org.eclipse.text.tests')
-rw-r--r--org.eclipse.text.tests/src/org/eclipse/text/tests/TextUtilitiesTest.java54
1 files changed, 43 insertions, 11 deletions
diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/TextUtilitiesTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/TextUtilitiesTest.java
index 3a0cc75cc47..76bae96a439 100644
--- a/org.eclipse.text.tests/src/org/eclipse/text/tests/TextUtilitiesTest.java
+++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/TextUtilitiesTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -15,6 +15,7 @@ package org.eclipse.text.tests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Iterator;
@@ -44,7 +45,7 @@ public class TextUtilitiesTest {
private final class DocumentListener implements IDocumentListener {
@Override
- public void documentAboutToBeChanged(DocumentEvent event) {}
+ public void documentAboutToBeChanged(DocumentEvent event) { /* not used */ }
@Override
public void documentChanged(DocumentEvent event) {
fEvents.add(event);
@@ -94,7 +95,7 @@ public class TextUtilitiesTest {
private final class DocumentListener implements IDocumentListener {
@Override
- public void documentAboutToBeChanged(DocumentEvent event) {}
+ public void documentAboutToBeChanged(DocumentEvent event) { /* not used */ }
@Override
public void documentChanged(DocumentEvent event) {
event= new DocumentEvent(event.getDocument(), event.getOffset(), event.getLength(), event.getText());
@@ -138,11 +139,6 @@ public class TextUtilitiesTest {
}
- /**
- * Constructor for UtilitiesTest.
- *
- * @param name the name
- */
private static DocumentEvent createRandomEvent(IDocument document, int maxLength, char character) {
int index0= (int) (Math.random() * (maxLength + 1));
@@ -281,15 +277,51 @@ public class TextUtilitiesTest {
}
@Test
+ @SuppressWarnings("deprecation")
public void testIndexOf() {
int[] result;
- result= TextUtilities.indexOf(new String[] {"a", "ab", "abc"}, "xxxxxxxxxx", 0);
+ result = TextUtilities.indexOf(new String[] { "a", "ab", "abc" }, "xxxxxxxxxx", 0);
assertEquals(-1, result[0]);
assertEquals(-1, result[1]);
- result= TextUtilities.indexOf(new String[] {"a", "ab", "abc"}, "foobarabcd", 0);
+ result = TextUtilities.indexOf(new String[] { "a", "ab", "abc" }, "foobarabcd", 0);
assertEquals(4, result[0]);
assertEquals(0, result[1]);
- }
+ result = TextUtilities.indexOf(new String[] { "ab", "ab" }, "foobarabcd", 0);
+ assertEquals(6, result[0]);
+ assertEquals(0, result[1]);
+
+ result = TextUtilities.indexOf(new String[] { "", "ab", "abc" }, "foobarabcd", 0);
+ assertEquals(6, result[0]);
+ assertEquals(2, result[1]);
+
+ result = TextUtilities.indexOf(new String[] { "arac", "", "fuu" }, "foobarabcd", 0);
+ assertEquals(0, result[0]);
+ assertEquals(1, result[1]);
+
+ result = TextUtilities.indexOf(new String[] { "", "" }, "foobarabcd", 0);
+ assertEquals(0, result[0]);
+ assertEquals(1, result[1]);
+
+ result = TextUtilities.indexOf(new String[] { "" }, "foobarabcd", 5);
+ // looks strange that searching from offset 5 returns match offset 0 but that is
+ // how it was implemented
+ assertEquals(0, result[0]);
+ assertEquals(0, result[1]);
+
+ try {
+ TextUtilities.indexOf(null, "foobarabcd", 0);
+ fail("Exception not thrown");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+
+ try {
+ TextUtilities.indexOf(new String[] { "abc", null }, "foobarabcd", 0);
+ fail("Exception not thrown");
+ } catch (NullPointerException ex) {
+ // expected
+ }
+ }
}

Back to the top