tests: Add Harmony regex tests

This commit is contained in:
Ilya Matveev
2017-06-07 12:25:26 +07:00
committed by ilmat192
parent 17ed2545ce
commit 4f251003ef
9 changed files with 3585 additions and 0 deletions
@@ -0,0 +1,508 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
import kotlin.text.*
import kotlin.test.*
internal var testPatterns = arrayOf("(a|b)*abb", "(1*2*3*4*)*567", "(a|b|c|d)*aab", "(1|2|3|4|5|6|7|8|9|0)(1|2|3|4|5|6|7|8|9|0)*", "(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)*", "(a|b)*(a|b)*A(a|b)*lice.*", "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)(a|b|c|d|e|f|g|h|" + "i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)*(1|2|3|4|5|6|7|8|9|0)*|while|for|struct|if|do")
internal var groupPatterns = arrayOf("(a|b)*aabb", "((a)|b)*aabb", "((a|b)*)a(abb)", "(((a)|(b))*)aabb", "(((a)|(b))*)aa(b)b", "(((a)|(b))*)a(a(b)b)")
fun testReplaceAll() {
val input = "aabfooaabfooabfoob"
val pattern = "a*b"
val regex = Regex(pattern)
assertEquals("-foo-foo-foo-", regex.replace(input, "-"))
}
fun testReplaceFirst() {
val input = "zzzdogzzzdogzzz"
val pattern = "dog"
val regex = Regex(pattern)
assertEquals("zzzcatzzzdogzzz", regex.replaceFirst(input, "cat"))
}
/*
* Class under test for String group(int)
*/
fun testGroupint() {
val positiveTestString = "ababababbaaabb"
// test IndexOutOfBoundsException
// //
for (i in groupPatterns.indices) {
val regex = Regex(groupPatterns[i])
val result = regex.matchEntire(positiveTestString)!!
try {
// groupPattern <index + 1> equals to number of groups
// of the specified pattern
// //
result.groups[i + 2]
fail("IndexOutBoundsException expected")
result.groups[i + 100]
fail("IndexOutBoundsException expected")
result.groups[-1]
fail("IndexOutBoundsException expected")
result.groups[-100]
fail("IndexOutBoundsException expected")
} catch (e: IndexOutOfBoundsException) {
}
}
val groupResults = arrayOf(
arrayOf("a"),
arrayOf("a", "a"),
arrayOf("ababababba", "a", "abb"),
arrayOf("ababababba", "a", "a", "b"),
arrayOf("ababababba", "a", "a", "b", "b"),
arrayOf("ababababba", "a", "a", "b", "abb", "b")
)
for (i in groupPatterns.indices) {
val regex = Regex(groupPatterns[i])
val result = regex.matchEntire(positiveTestString)!!
for (j in 0..groupResults[i].size - 1) {
assertEquals("i: $i j: $j", groupResults[i][j], result.groupValues[j + 1])
}
}
}
fun testGroup() {
val positiveTestString = "ababababbaaabb"
val negativeTestString = "gjhfgdsjfhgcbv"
for (element in groupPatterns) {
val regex = Regex(element)
val result = regex.matchEntire(positiveTestString)!!
assertEquals(positiveTestString, result.groupValues[0])
assertEquals(positiveTestString, result.groups[0]!!.value)
assertEquals(0 until positiveTestString.length, result.groups[0]!!.range)
}
for (element in groupPatterns) {
val regex = Regex(element)
val result = regex.matchEntire(negativeTestString)
assertEquals(result, null)
}
}
fun testGroupPossessive() {
val regex = Regex("((a)|(b))++c")
assertEquals("a", regex.matchEntire("aac")!!.groupValues[1])
}
fun testMatchesMisc() {
val posSeq = arrayOf(
arrayOf("abb", "ababb", "abababbababb", "abababbababbabababbbbbabb"),
arrayOf("213567", "12324567", "1234567", "213213567", "21312312312567", "444444567"),
arrayOf("abcdaab", "aab", "abaab", "cdaab", "acbdadcbaab"),
arrayOf("213234567", "3458", "0987654", "7689546432", "0398576", "98432", "5"),
arrayOf("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
arrayOf("ababbaAabababblice", "ababbaAliceababab", "ababbAabliceaaa", "abbbAbbbliceaaa", "Alice"),
arrayOf("a123", "bnxnvgds156", "for", "while", "if", "struct"))
for (i in testPatterns.indices) {
val regex = Regex(testPatterns[i])
for (j in 0..posSeq[i].size - 1) {
assertTrue("Incorrect match: " + testPatterns[i] + " vs " + posSeq[i][j], regex.matches(posSeq[i][j]))
}
}
}
fun testMatchesQuantifiers() {
val testPatternsSingles = arrayOf("a{5}", "a{2,4}", "a{3,}")
val testPatternsMultiple = arrayOf("((a)|(b)){1,2}abb", "((a)|(b)){2,4}", "((a)|(b)){3,}")
val stringSingles = arrayOf(
arrayOf("aaaaa", "aaa"),
arrayOf("aa", "a", "aaa", "aaaaaa", "aaaa", "aaaaa"),
arrayOf("aaa", "a", "aaaa", "aa")
)
val stringMultiples = arrayOf(
arrayOf("ababb", "aba"),
arrayOf("ab", "b", "bab", "ababa", "abba", "abababbb"),
arrayOf("aba", "b", "abaa", "ba")
)
for (i in testPatternsSingles.indices) {
val regex = Regex(testPatternsSingles[i])
for (j in 0..stringSingles.size / 2 - 1) {
assertTrue("Match expected, but failed: " + regex.pattern + " : " + stringSingles[i][j],
regex.matches(stringSingles[i][j * 2])
)
assertFalse("Match failure expected, but match succeed: " + regex.pattern + " : " + stringSingles[i][j * 2 + 1],
regex.matches(stringSingles[i][j * 2 + 1])
)
}
}
for (i in testPatternsMultiple.indices) {
val regex = Regex(testPatternsMultiple[i])
for (j in 0..stringMultiples.size / 2 - 1) {
assertTrue("Match expected, but failed: " + regex.pattern + " : " + stringMultiples[i][j],
regex.matches(stringMultiples[i][j * 2])
)
assertFalse("Match failure expected, but match succeed: " + regex.pattern + " : " + stringMultiples[i][j * 2 + 1],
regex.matches(stringMultiples[i][j * 2 + 1])
)
}
}
// Test for the optimized '.+' quantifier node.
assertFalse(Regex(".+abc").matches("abc"))
assertFalse(Regex(".+\nabc", RegexOption.DOT_MATCHES_ALL).matches("\nabc"))
assertFalse(Regex(".+").matches(""))
assertFalse(Regex(".+\n", RegexOption.DOT_MATCHES_ALL).matches("\n"))
assertFalse(Regex(".+abc").containsMatchIn("abc"))
assertFalse(Regex(".+\nabc", RegexOption.DOT_MATCHES_ALL).containsMatchIn("\nabc"))
assertFalse(Regex(".+").containsMatchIn(""))
assertFalse(Regex(".+\n", RegexOption.DOT_MATCHES_ALL).containsMatchIn("\n"))
assertTrue(Regex(".+abc").matches("aabc"))
assertTrue(Regex(".+\nabc", RegexOption.DOT_MATCHES_ALL).matches("a\nabc"))
assertTrue(Regex(".+").matches("a"))
assertTrue(Regex(".+\n", RegexOption.DOT_MATCHES_ALL).matches("a\n"))
assertTrue(Regex(".+abc").containsMatchIn("aabc"))
assertTrue(Regex(".+\nabc", RegexOption.DOT_MATCHES_ALL).containsMatchIn("a\nabc"))
assertTrue(Regex(".+").containsMatchIn("a"))
assertTrue(Regex(".+\n", RegexOption.DOT_MATCHES_ALL).containsMatchIn("a\n"))
}
fun testQuantVsGroup() {
val patternString = "(d{1,3})((a|c)*)(d{1,3})((a|c)*)(d{1,3})"
val testString = "dacaacaacaaddaaacaacaaddd"
val regex = Regex(patternString)
val result = regex.matchEntire(testString)!!
assertEquals("dacaacaacaaddaaacaacaaddd", result.groupValues[0])
assertEquals("d", result.groupValues[1])
assertEquals("acaacaacaa", result.groupValues[2])
assertEquals("dd", result.groupValues[4])
assertEquals("aaacaacaa", result.groupValues[5])
assertEquals("ddd", result.groupValues[7])
}
/*
* Class under test for boolean find()
*/
fun testFind() {
var testPattern = "(abb)"
var testString = "cccabbabbabbabbabb"
val regex = Regex(testPattern)
var result = regex.find(testString)
var start = 3
var end = 6
while (result != null) {
assertEquals(start, result.groups[1]!!.range.start)
assertEquals(end, result.groups[1]!!.range.endInclusive + 1)
start = end
end += 3
result = result.next()
}
testPattern = "(\\d{1,3})"
testString = "aaaa123456789045"
val regex2 = Regex(testPattern)
var result2 = regex2.find(testString)
start = 4
val length = 3
while (result2 != null) {
assertEquals(testString.substring(start, start + length), result2.groupValues[1])
start += length
result2 = result2.next()
}
}
fun testSEOLsymbols() {
val regex = Regex("^a\\(bb\\[$")
assertTrue(regex.matches("a(bb["))
}
fun testGroupCount() {
for (i in groupPatterns.indices) {
val regex = Regex(groupPatterns[i])
val result = regex.matchEntire("ababababbaaabb")!!
assertEquals(i + 1, result.groups.size - 1)
}
}
fun testReluctantQuantifiers() {
val regex = Regex("(ab*)*b")
val result = regex.matchEntire("abbbb")
if (result != null) {
assertEquals("abbb", result.groupValues[1])
} else {
fail("Match expected: (ab*)*b vs abbbb")
}
}
fun testEnhancedFind() {
val input = "foob"
val pattern = "a*b"
val regex = Regex(pattern)
val result = regex.find(input)!!
assertEquals("b", result.groupValues[0])
}
fun testPosCompositeGroup() {
val posExamples = arrayOf("aabbcc", "aacc", "bbaabbcc")
val negExamples = arrayOf("aabb", "bb", "bbaabb")
val posPat = Regex("(aa|bb){1,3}+cc")
val negPat = Regex("(aa|bb){1,3}+bb")
for (element in posExamples) {
assertTrue(posPat.matches(element))
}
for (element in negExamples) {
assertFalse(negPat.matches(element))
}
assertTrue(Regex("(aa|bb){1,3}+bb").matches("aabbaabb"))
}
fun testPosAltGroup() {
val posExamples = arrayOf("aacc", "bbcc", "cc")
val negExamples = arrayOf("bb", "aa")
val posPat = Regex("(aa|bb)?+cc")
val negPat = Regex("(aa|bb)?+bb")
for (element in posExamples) {
assertTrue(posPat.toString() + " vs: " + element, posPat.matches(element))
}
for (element in negExamples) {
assertFalse(negPat.matches(element))
}
assertTrue(Regex("(aa|bb)?+bb").matches("aabb"))
}
fun testRelCompGroup() {
var res = ""
for (i in 0..3) {
val regex = Regex("((aa|bb){$i,3}?).*cc")
val result = regex.matchEntire("aaaaaacc")
assertTrue(regex.toString() + " vs: " + "aaaaaacc", result != null)
assertEquals(res, result!!.groupValues[1])
res += "aa"
}
}
fun testRelAltGroup() {
var regex = Regex("((aa|bb)??).*cc")
var result = regex.matchEntire("aacc")
assertTrue(regex.toString() + " vs: " + "aacc", result != null)
assertEquals("", result!!.groupValues[1])
regex = Regex("((aa|bb)??)cc")
result = regex.matchEntire("aacc")
assertTrue(regex.toString() + " vs: " + "aacc", result != null)
assertEquals("aa", result!!.groupValues[1])
}
fun testIgnoreCase() {
var regex = Regex("(aa|bb)*", RegexOption.IGNORE_CASE)
assertTrue(regex.matches("aAbb"))
regex = Regex("(a|b|c|d|e)*", RegexOption.IGNORE_CASE)
assertTrue(regex.matches("aAebbAEaEdebbedEccEdebbedEaedaebEbdCCdbBDcdcdADa"))
regex = Regex("[a-e]*", RegexOption.IGNORE_CASE)
assertTrue(regex.matches("aAebbAEaEdebbedEccEdebbedEaedaebEbdCCdbBDcdcdADa"))
}
fun testQuoteReplacement() {
assertEquals("\\\\aaCC\\$1", Regex.escapeReplacement("\\aaCC$1"))
}
fun testOverFlow() {
var regex = Regex("(a*)*")
var result = regex.matchEntire("aaa")
assertTrue(result != null)
assertEquals("", result!!.groupValues[1])
assertTrue(Regex("(1+)\\1+").matches("11"))
assertTrue(Regex("(1+)(2*)\\2+").matches("11"))
regex = Regex("(1+)\\1*")
result = regex.matchEntire("11")
assertTrue(result != null)
assertEquals("11", result!!.groupValues[1])
regex = Regex("((1+)|(2+))(\\2+)")
result = regex.matchEntire("11")
assertTrue(result != null)
assertEquals("1", result!!.groupValues[2])
assertEquals("1", result.groupValues[1])
assertEquals("1", result.groupValues[4])
assertEquals("", result.groupValues[3])
}
fun testUnicode() {
assertTrue(Regex("\\x61a").matches("aa"))
assertTrue(Regex("\\u0061a").matches("aa"))
assertTrue(Regex("\\0141a").matches("aa"))
assertTrue(Regex("\\0777").matches("?7"))
}
fun testUnicodeCategory() {
assertTrue(Regex("\\p{Ll}").matches("k")) // Unicode lower case
assertTrue(Regex("\\P{Ll}").matches("K")) // Unicode non-lower
// case
assertTrue(Regex("\\p{Lu}").matches("K")) // Unicode upper case
assertTrue(Regex("\\P{Lu}").matches("k")) // Unicode non-upper
// case combinations
assertTrue(Regex("[\\p{L}&&[^\\p{Lu}]]").matches("k"))
assertTrue(Regex("[\\p{L}&&[^\\p{Ll}]]").matches("K"))
assertFalse(Regex("[\\p{L}&&[^\\p{Lu}]]").matches("K"))
assertFalse(Regex("[\\p{L}&&[^\\p{Ll}]]").matches("k"))
// category/character combinations
assertFalse(Regex("[\\p{L}&&[^a-z]]").matches("k"))
assertTrue(Regex("[\\p{L}&&[^a-z]]").matches("K"))
assertTrue(Regex("[\\p{Lu}a-z]").matches("k"))
assertTrue(Regex("[a-z\\p{Lu}]").matches("k"))
assertFalse(Regex("[\\p{Lu}a-d]").matches("k"))
assertTrue(Regex("[a-d\\p{Lu}]").matches("K"))
assertFalse(Regex("[\\p{L}&&[^\\p{Lu}&&[^G]]]").matches("K"))
}
fun testSplitEmpty() {
val regex = Regex("")
val s = regex.split("", 0)
assertEquals(1, s.size)
assertEquals("", s[0])
}
fun testFindDollar() {
val regex = Regex("a$")
val result = regex.find("a\n")
assertTrue(result != null)
assertEquals("a", result!!.groupValues[0])
}
fun testAllCodePoints() {
// Regression for HARMONY-3145
val codePoint = IntArray(1)
var p = Regex("(\\p{all})+")
var res = true
var cnt = 0
var s: String
for (i in 0..1114111) {
codePoint[0] = i
s = String(codePoint, 0, 1)
if (!s.matches(p.toString().toRegex())) {
cnt++
res = false
}
}
assertTrue(res)
assertEquals(0, cnt)
p = Regex("(\\P{all})+")
res = true
cnt = 0
for (i in 0..1114111) {
codePoint[0] = i
s = String(codePoint, 0, 1)
if (!s.matches(p.toString().toRegex())) {
cnt++
res = false
}
}
assertFalse(res)
assertEquals(0x110000, cnt)
}
/*
* Regression test for HARMONY-674
*/
fun testPatternMatcher() {
assertTrue(Regex("(?:\\d+)(?:pt)").matches("14pt"))
}
/**
* Inspired by HARMONY-3360
*/
fun test3360() {
val str = "!\"#%&'(),-./"
val regex = Regex("\\s")
assertFalse(regex.containsMatchIn(str))
}
/**
* Regression test for HARMONY-3360
*/
fun testGeneralPunctuationCategory() {
val s = arrayOf(",", "!", "\"", "#", "%", "&", "'", "(", ")", "-", ".", "/")
val regexp = "\\p{P}"
for (i in s.indices) {
val regex = Regex(regexp)
assertTrue(regex.containsMatchIn(s[i]))
}
}
fun box() {
testReplaceAll()
testReplaceFirst()
testGroupint()
testGroup()
testGroupPossessive()
testMatchesMisc()
testMatchesQuantifiers()
testQuantVsGroup()
testFind()
testSEOLsymbols()
testGroupCount()
testReluctantQuantifiers()
testEnhancedFind()
testPosCompositeGroup()
testPosAltGroup()
testRelCompGroup()
testRelAltGroup()
testIgnoreCase()
testQuoteReplacement()
testOverFlow()
testUnicode()
testUnicodeCategory()
testSplitEmpty()
testFindDollar()
testAllCodePoints()
testPatternMatcher()
test3360()
testGeneralPunctuationCategory()
}
@@ -0,0 +1,116 @@
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
import kotlin.text.*
import kotlin.test.*
fun testErrorConditions2() {
// Test match cursors in absence of a match
val regex = Regex("(foo[0-9])(bar[a-z])")
var result = regex.find("foo1barzfoo2baryfoozbar5")
Assert.assertTrue(result != null)
Assert.assertEquals(0, result!!.range.start)
Assert.assertEquals(7, result.range.endInclusive)
Assert.assertEquals(0, result.groups[0]!!.range.start)
Assert.assertEquals(7, result.groups[0]!!.range.endInclusive)
Assert.assertEquals(0, result.groups[1]!!.range.start)
Assert.assertEquals(3, result.groups[1]!!.range.endInclusive)
Assert.assertEquals(4, result.groups[2]!!.range.start)
Assert.assertEquals(7, result.groups[2]!!.range.endInclusive)
try {
result.groups[3]
Assert.fail("IndexOutOfBoundsException expected")
} catch (e: IndexOutOfBoundsException) {
}
try {
result.groupValues[3]
Assert.fail("IndexOutOfBoundsException expected")
} catch (e: IndexOutOfBoundsException) {
}
try {
result.groups[-1]
Assert.fail("IndexOutOfBoundsException expected")
} catch (e: IndexOutOfBoundsException) {
}
try {
result.groupValues[-1]
Assert.fail("IndexOutOfBoundsException expected")
} catch (e: IndexOutOfBoundsException) {
}
result = result.next()
Assert.assertTrue(result != null)
Assert.assertEquals(8, result!!.range.start)
Assert.assertEquals(15, result.range.endInclusive)
Assert.assertEquals(8, result.groups[0]!!.range.start)
Assert.assertEquals(15, result.groups[0]!!.range.endInclusive)
Assert.assertEquals(8, result.groups[1]!!.range.start)
Assert.assertEquals(11, result.groups[1]!!.range.endInclusive)
Assert.assertEquals(12, result.groups[2]!!.range.start)
Assert.assertEquals(15, result.groups[2]!!.range.endInclusive)
try {
result.groups[3]
Assert.fail("IndexOutOfBoundsException expected")
} catch (e: IndexOutOfBoundsException) {
}
try {
result.groupValues[3]
Assert.fail("IndexOutOfBoundsException expected")
} catch (e: IndexOutOfBoundsException) {
}
try {
result.groups[-1]
Assert.fail("IndexOutOfBoundsException expected")
} catch (e: IndexOutOfBoundsException) {
}
try {
result.groupValues[-1]
Assert.fail("IndexOutOfBoundsException expected")
} catch (e: IndexOutOfBoundsException) {
}
result = result.next()
Assert.assertFalse(result != null)
}
/*
* Regression test for HARMONY-997
*/
fun testReplacementBackSlash() {
val str = "replace me"
val replacedString = "me"
val substitutionString = "\\"
val regex = Regex(replacedString)
try {
regex.replace(str, substitutionString)
Assert.fail("IllegalArgumentException should be thrown")
} catch (e: IllegalArgumentException) {
}
}
fun box() {
testErrorConditions2()
testReplacementBackSlash()
}
@@ -0,0 +1,116 @@
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
import kotlin.text.*
import kotlin.test.*
/**
* Tests Pattern compilation modes and modes triggered in pattern strings
*/
fun testCase() {
var regex: Regex
var result: MatchResult?
regex = Regex("([a-z]+)[0-9]+")
result = regex.find("cAT123#dog345")
Assert.assertNotNull(result)
Assert.assertEquals("dog", result!!.groupValues[1])
Assert.assertNull(result.next())
regex = Regex("([a-z]+)[0-9]+", RegexOption.IGNORE_CASE)
result = regex.find("cAt123#doG345")
Assert.assertNotNull(result)
Assert.assertEquals("cAt", result!!.groupValues[1])
result = result.next()
Assert.assertNotNull(result)
Assert.assertEquals("doG", result!!.groupValues[1])
Assert.assertNull(result.next())
regex = Regex("(?i)([a-z]+)[0-9]+")
result = regex.find("cAt123#doG345")
Assert.assertNotNull(result)
Assert.assertEquals("cAt", result!!.groupValues[1])
result = result.next()
Assert.assertNotNull(result)
Assert.assertEquals("doG", result!!.groupValues[1])
Assert.assertNull(result.next())
}
fun testMultiline() {
var regex: Regex
var result: MatchResult?
regex = Regex("^foo")
result = regex.find("foobar")
Assert.assertNotNull(result)
Assert.assertTrue(result!!.range.start == 0 && result.range.endInclusive == 2)
Assert.assertTrue(result.groups[0]!!.range.start == 0 && result.groups[0]!!.range.endInclusive == 2)
Assert.assertNull(result.next())
result = regex.find("barfoo")
Assert.assertNull(result)
regex = Regex("foo$")
result = regex.find("foobar")
Assert.assertNull(result)
result = regex.find("barfoo")
Assert.assertNotNull(result)
Assert.assertTrue(result!!.range.start == 3 && result.range.endInclusive == 5)
Assert.assertTrue(result.groups[0]!!.range.start == 3 && result.groups[0]!!.range.endInclusive == 5)
Assert.assertNull(result.next())
regex = Regex("^foo([0-9]*)", RegexOption.MULTILINE)
result = regex.find("foo1bar\nfoo2foo3\nbarfoo4")
Assert.assertNotNull(result)
Assert.assertEquals("1", result!!.groupValues[1])
result = result.next()
Assert.assertNotNull(result)
Assert.assertEquals("2", result!!.groupValues[1])
Assert.assertNull(result.next())
regex = Regex("foo([0-9]*)$", RegexOption.MULTILINE)
result = regex.find("foo1bar\nfoo2foo3\nbarfoo4")
Assert.assertNotNull(result)
Assert.assertEquals("3", result!!.groupValues[1])
result = result.next()
Assert.assertNotNull(result)
Assert.assertEquals("4", result!!.groupValues[1])
Assert.assertNull(result.next())
regex = Regex("(?m)^foo([0-9]*)")
result = regex.find("foo1bar\nfoo2foo3\nbarfoo4")
Assert.assertNotNull(result)
Assert.assertEquals("1", result!!.groupValues[1])
result = result.next()
Assert.assertNotNull(result)
Assert.assertEquals("2", result!!.groupValues[1])
Assert.assertNull(result.next())
regex = Regex("(?m)foo([0-9]*)$")
result = regex.find("foo1bar\nfoo2foo3\nbarfoo4")
Assert.assertNotNull(result)
Assert.assertEquals("3", result!!.groupValues[1])
result = result.next()
Assert.assertNotNull(result)
Assert.assertEquals("4", result!!.groupValues[1])
Assert.assertNull(result.next())
}
fun box() {
testCase()
testMultiline()
}
@@ -0,0 +1,43 @@
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
import kotlin.text.*
import kotlin.test.*
fun testCompileErrors() {
// empty regex string - no exception should be thrown
Regex("")
// note: invalid regex syntax checked in PatternSyntaxExceptionTest
// flags = 0 should raise no exception
Regex("foo", emptySet())
// check that all valid flags accepted without exception
val options = setOf(
RegexOption.UNIX_LINES,
RegexOption.IGNORE_CASE,
RegexOption.MULTILINE,
RegexOption.CANON_EQ,
RegexOption.COMMENTS,
RegexOption.DOT_MATCHES_ALL)
Regex("foo", options)
}
fun box() {
testCompileErrors()
}
@@ -0,0 +1,45 @@
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
import kotlin.text.*
import kotlin.test.*
fun testCase() {
val regex = "("
try {
Regex(regex)
Assert.fail("PatternSyntaxException expected")
} catch (e: PatternSyntaxException) {
// TODO: Check the exception's properties.
}
}
fun testCase2() {
val regex = "[4-"
try {
Regex(regex)
Assert.fail("PatternSyntaxException expected")
} catch (e: PatternSyntaxException) {
}
}
fun box() {
testCase()
testCase2()
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,85 @@
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
import kotlin.text.*
import kotlin.test.*
fun testSimpleReplace() {
val target: String
val pattern: String
val repl: String
target = "foobarfobarfoofo1"
pattern = "fo[^o]"
repl = "xxx"
val regex = Regex(pattern)
Assert.assertEquals("foobarxxxarfoofo1", regex.replaceFirst(target, repl))
Assert.assertEquals("foobarxxxarfooxxx", regex.replace(target, repl))
}
fun testCaptureReplace() {
var target: String
var pattern: String
var repl: String
var s: String
var regex: Regex
target = "[31]foo;bar[42];[99]xyz"
pattern = "\\[([0-9]+)\\]([a-z]+)"
repl = "$2[$1]"
regex = Regex(pattern)
s = regex.replaceFirst(target, repl)
Assert.assertEquals("foo[31];bar[42];[99]xyz", s)
s = regex.replace(target, repl)
Assert.assertEquals("foo[31];bar[42];xyz[99]", s)
target = "[31]foo(42)bar{63}zoo;[12]abc(34)def{56}ghi;{99}xyz[88]xyz(77)xyz;"
pattern = "\\[([0-9]+)\\]([a-z]+)\\(([0-9]+)\\)([a-z]+)\\{([0-9]+)\\}([a-z]+)"
repl = "[$5]$6($3)$4{$1}$2"
regex = Regex(pattern)
s = regex.replaceFirst(target, repl)
Assert.assertEquals("[63]zoo(42)bar{31}foo;[12]abc(34)def{56}ghi;{99}xyz[88]xyz(77)xyz;", s)
s = regex.replace(target, repl)
Assert.assertEquals("[63]zoo(42)bar{31}foo;[56]ghi(34)def{12}abc;{99}xyz[88]xyz(77)xyz;", s)
}
fun testEscapeReplace() {
val target: String
val pattern: String
var repl: String
var s: String
target = "foo'bar''foo"
pattern = "'"
repl = "\\'"
s = target.replace(pattern.toRegex(), repl)
Assert.assertEquals("foo'bar''foo", s)
repl = "\\\\'"
s = target.replace(pattern.toRegex(), repl)
Assert.assertEquals("foo\\'bar\\'\\'foo", s)
repl = "\\$3"
s = target.replace(pattern.toRegex(), repl)
Assert.assertEquals("foo$3bar$3$3foo", s)
}
fun box() {
testSimpleReplace()
testCaptureReplace()
testEscapeReplace()
}
@@ -0,0 +1,157 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
import kotlin.text.*
import kotlin.test.*
fun testSimple() {
val p = Regex("/")
val results = p.split("have/you/done/it/right")
val expected = arrayOf("have", "you", "done", "it", "right")
Assert.assertEquals(expected.size, results.size)
for (i in expected.indices) {
Assert.assertEquals(results[i], expected[i])
}
}
@Throws(PatternSyntaxException::class)
fun testSplit1() {
var p = Regex(" ")
val input = "poodle zoo"
var tokens: List<String>
tokens = p.split(input, 1)
Assert.assertEquals(1, tokens.size)
Assert.assertTrue(tokens[0] == input)
tokens = p.split(input, 2)
Assert.assertEquals(2, tokens.size)
Assert.assertEquals("poodle", tokens[0])
Assert.assertEquals("zoo", tokens[1])
tokens = p.split(input, 5)
Assert.assertEquals(2, tokens.size)
Assert.assertEquals("poodle", tokens[0])
Assert.assertEquals("zoo", tokens[1])
tokens = p.split(input, 0)
Assert.assertEquals(2, tokens.size)
Assert.assertEquals("poodle", tokens[0])
Assert.assertEquals("zoo", tokens[1])
tokens = p.split(input)
Assert.assertEquals(2, tokens.size)
Assert.assertEquals("poodle", tokens[0])
Assert.assertEquals("zoo", tokens[1])
p = Regex("d")
tokens = p.split(input, 1)
Assert.assertEquals(1, tokens.size)
Assert.assertTrue(tokens[0] == input)
tokens = p.split(input, 2)
Assert.assertEquals(2, tokens.size)
Assert.assertEquals("poo", tokens[0])
Assert.assertEquals("le zoo", tokens[1])
tokens = p.split(input, 5)
Assert.assertEquals(2, tokens.size)
Assert.assertEquals("poo", tokens[0])
Assert.assertEquals("le zoo", tokens[1])
tokens = p.split(input, 0)
Assert.assertEquals(2, tokens.size)
Assert.assertEquals("poo", tokens[0])
Assert.assertEquals("le zoo", tokens[1])
tokens = p.split(input)
Assert.assertEquals(2, tokens.size)
Assert.assertEquals("poo", tokens[0])
Assert.assertEquals("le zoo", tokens[1])
p = Regex("o")
tokens = p.split(input, 1)
Assert.assertEquals(1, tokens.size)
Assert.assertTrue(tokens[0] == input)
tokens = p.split(input, 2)
Assert.assertEquals(2, tokens.size)
Assert.assertEquals("p", tokens[0])
Assert.assertEquals("odle zoo", tokens[1])
tokens = p.split(input, 5)
Assert.assertEquals(5, tokens.size)
Assert.assertEquals("p", tokens[0])
Assert.assertTrue(tokens[1] == "")
Assert.assertEquals("dle z", tokens[2])
Assert.assertTrue(tokens[3] == "")
Assert.assertTrue(tokens[4] == "")
tokens = p.split(input, 0)
Assert.assertEquals(5, tokens.size)
Assert.assertEquals("p", tokens[0])
Assert.assertTrue(tokens[1] == "")
Assert.assertEquals("dle z", tokens[2])
Assert.assertTrue(tokens[3] == "")
Assert.assertTrue(tokens[4] == "")
tokens = p.split(input)
Assert.assertEquals(5, tokens.size)
Assert.assertEquals("p", tokens[0])
Assert.assertTrue(tokens[1] == "")
Assert.assertEquals("dle z", tokens[2])
Assert.assertTrue(tokens[3] == "")
Assert.assertTrue(tokens[4] == "")
}
fun testSplit2() {
val p = Regex("")
var s: List<String>
s = p.split("a", 0)
Assert.assertEquals(3, s.size)
Assert.assertEquals("", s[0])
Assert.assertEquals("a", s[1])
Assert.assertEquals("", s[2])
s = p.split("", 0)
Assert.assertEquals(1, s.size)
Assert.assertEquals("", s[0])
s = p.split("abcd", 0)
Assert.assertEquals(6, s.size)
Assert.assertEquals("", s[0])
Assert.assertEquals("a", s[1])
Assert.assertEquals("b", s[2])
Assert.assertEquals("c", s[3])
Assert.assertEquals("d", s[4])
Assert.assertEquals("", s[5])
}
fun testSplitSupplementaryWithEmptyString() {
/*
* See http://www.unicode.org/reports/tr18/#Supplementary_Characters We
* have to treat text as code points not code units.
*/
val p = Regex("")
val s: List<String>
s = p.split("a\ud869\uded6b", 0)
Assert.assertEquals(5, s.size)
Assert.assertEquals("", s[0])
Assert.assertEquals("a", s[1])
Assert.assertEquals("\ud869\uded6", s[2])
Assert.assertEquals("b", s[3])
Assert.assertEquals("", s[4])
}
fun box() {
testSimple()
testSplit1()
testSplit2()
testSplitSupplementaryWithEmptyString()
}