1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
|
/*
* Copyright (C) 2008 The Guava Authors
*
* 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 com.google.common.base;
import static com.google.common.base.CharMatcher.anyOf;
import static com.google.common.base.CharMatcher.breakingWhitespace;
import static com.google.common.base.CharMatcher.forPredicate;
import static com.google.common.base.CharMatcher.inRange;
import static com.google.common.base.CharMatcher.is;
import static com.google.common.base.CharMatcher.isNot;
import static com.google.common.base.CharMatcher.noneOf;
import static com.google.common.base.CharMatcher.whitespace;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.Sets;
import com.google.common.testing.NullPointerTester;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
/**
* Unit test for {@link CharMatcher}.
*
* @author Kevin Bourrillion
*/
@GwtCompatible(emulated = true)
public class CharMatcherTest extends TestCase {
@GwtIncompatible // NullPointerTester
public void testStaticNullPointers() throws Exception {
NullPointerTester tester = new NullPointerTester();
tester.testAllPublicStaticMethods(CharMatcher.class);
tester.testAllPublicInstanceMethods(CharMatcher.any());
tester.testAllPublicInstanceMethods(CharMatcher.anyOf("abc"));
}
private static final CharMatcher WHATEVER =
new CharMatcher() {
@Override
public boolean matches(char c) {
throw new AssertionFailedError("You weren't supposed to actually invoke me!");
}
};
public void testAnyAndNone_logicalOps() throws Exception {
// These are testing behavior that's never promised by the API, but since
// we're lucky enough that these do pass, it saves us from having to write
// more excruciating tests! Hooray!
assertSame(CharMatcher.any(), CharMatcher.none().negate());
assertSame(CharMatcher.none(), CharMatcher.any().negate());
assertSame(WHATEVER, CharMatcher.any().and(WHATEVER));
assertSame(CharMatcher.any(), CharMatcher.any().or(WHATEVER));
assertSame(CharMatcher.none(), CharMatcher.none().and(WHATEVER));
assertSame(WHATEVER, CharMatcher.none().or(WHATEVER));
}
// The rest of the behavior of ANY and DEFAULT will be covered in the tests for
// the text processing methods below.
public void testWhitespaceBreakingWhitespaceSubset() throws Exception {
for (int c = 0; c <= Character.MAX_VALUE; c++) {
if (breakingWhitespace().matches((char) c)) {
assertTrue(Integer.toHexString(c), whitespace().matches((char) c));
}
}
}
// The next tests require ICU4J and have, at least for now, been sliced out
// of the open-source view of the tests.
@GwtIncompatible // Character.isISOControl
public void testJavaIsoControl() {
for (int c = 0; c <= Character.MAX_VALUE; c++) {
assertEquals(
"" + c, Character.isISOControl(c), CharMatcher.javaIsoControl().matches((char) c));
}
}
// Omitting tests for the rest of the JAVA_* constants as these are defined
// as extremely straightforward pass-throughs to the JDK methods.
// We're testing the is(), isNot(), anyOf(), noneOf() and inRange() methods
// below by testing their text-processing methods.
// The organization of this test class is unusual, as it's not done by
// method, but by overall "scenario". Also, the variety of actual tests we
// do borders on absurd overkill. Better safe than sorry, though?
@GwtIncompatible // java.util.BitSet
public void testSetBits() {
doTestSetBits(CharMatcher.any());
doTestSetBits(CharMatcher.none());
doTestSetBits(is('a'));
doTestSetBits(isNot('a'));
doTestSetBits(anyOf(""));
doTestSetBits(anyOf("x"));
doTestSetBits(anyOf("xy"));
doTestSetBits(anyOf("CharMatcher"));
doTestSetBits(noneOf("CharMatcher"));
doTestSetBits(inRange('n', 'q'));
doTestSetBits(forPredicate(Predicates.equalTo('c')));
doTestSetBits(CharMatcher.ascii());
doTestSetBits(CharMatcher.digit());
doTestSetBits(CharMatcher.invisible());
doTestSetBits(CharMatcher.whitespace());
doTestSetBits(inRange('A', 'Z').and(inRange('F', 'K').negate()));
}
@GwtIncompatible // java.util.BitSet
private void doTestSetBits(CharMatcher matcher) {
BitSet bitset = new BitSet();
matcher.setBits(bitset);
for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {
assertEquals(matcher.matches((char) i), bitset.get(i));
}
}
public void testEmpty() throws Exception {
doTestEmpty(CharMatcher.any());
doTestEmpty(CharMatcher.none());
doTestEmpty(is('a'));
doTestEmpty(isNot('a'));
doTestEmpty(anyOf(""));
doTestEmpty(anyOf("x"));
doTestEmpty(anyOf("xy"));
doTestEmpty(anyOf("CharMatcher"));
doTestEmpty(noneOf("CharMatcher"));
doTestEmpty(inRange('n', 'q'));
doTestEmpty(forPredicate(Predicates.equalTo('c')));
}
@GwtIncompatible // NullPointerTester
public void testNull() throws Exception {
doTestNull(CharMatcher.any());
doTestNull(CharMatcher.none());
doTestNull(is('a'));
doTestNull(isNot('a'));
doTestNull(anyOf(""));
doTestNull(anyOf("x"));
doTestNull(anyOf("xy"));
doTestNull(anyOf("CharMatcher"));
doTestNull(noneOf("CharMatcher"));
doTestNull(inRange('n', 'q'));
doTestNull(forPredicate(Predicates.equalTo('c')));
}
private void doTestEmpty(CharMatcher matcher) throws Exception {
reallyTestEmpty(matcher);
reallyTestEmpty(matcher.negate());
reallyTestEmpty(matcher.precomputed());
}
private void reallyTestEmpty(CharMatcher matcher) throws Exception {
assertEquals(-1, matcher.indexIn(""));
assertEquals(-1, matcher.indexIn("", 0));
try {
matcher.indexIn("", 1);
fail();
} catch (IndexOutOfBoundsException expected) {
}
try {
matcher.indexIn("", -1);
fail();
} catch (IndexOutOfBoundsException expected) {
}
assertEquals(-1, matcher.lastIndexIn(""));
assertFalse(matcher.matchesAnyOf(""));
assertTrue(matcher.matchesAllOf(""));
assertTrue(matcher.matchesNoneOf(""));
assertEquals("", matcher.removeFrom(""));
assertEquals("", matcher.replaceFrom("", 'z'));
assertEquals("", matcher.replaceFrom("", "ZZ"));
assertEquals("", matcher.trimFrom(""));
assertEquals(0, matcher.countIn(""));
}
@GwtIncompatible // NullPointerTester
private static void doTestNull(CharMatcher matcher) throws Exception {
NullPointerTester tester = new NullPointerTester();
tester.testAllPublicInstanceMethods(matcher);
}
public void testNoMatches() {
doTestNoMatches(CharMatcher.none(), "blah");
doTestNoMatches(is('a'), "bcde");
doTestNoMatches(isNot('a'), "aaaa");
doTestNoMatches(anyOf(""), "abcd");
doTestNoMatches(anyOf("x"), "abcd");
doTestNoMatches(anyOf("xy"), "abcd");
doTestNoMatches(anyOf("CharMatcher"), "zxqy");
doTestNoMatches(noneOf("CharMatcher"), "ChMa");
doTestNoMatches(inRange('p', 'x'), "mom");
doTestNoMatches(forPredicate(Predicates.equalTo('c')), "abe");
doTestNoMatches(inRange('A', 'Z').and(inRange('F', 'K').negate()), "F1a");
doTestNoMatches(CharMatcher.digit(), "\tAz()");
doTestNoMatches(CharMatcher.javaDigit(), "\tAz()");
doTestNoMatches(CharMatcher.digit().and(CharMatcher.ascii()), "\tAz()");
doTestNoMatches(CharMatcher.singleWidth(), "\u05bf\u3000");
}
private void doTestNoMatches(CharMatcher matcher, String s) {
reallyTestNoMatches(matcher, s);
reallyTestAllMatches(matcher.negate(), s);
reallyTestNoMatches(matcher.precomputed(), s);
reallyTestAllMatches(matcher.negate().precomputed(), s);
reallyTestAllMatches(matcher.precomputed().negate(), s);
reallyTestNoMatches(forPredicate(matcher), s);
reallyTestNoMatches(matcher, new StringBuilder(s));
}
public void testAllMatches() {
doTestAllMatches(CharMatcher.any(), "blah");
doTestAllMatches(isNot('a'), "bcde");
doTestAllMatches(is('a'), "aaaa");
doTestAllMatches(noneOf("CharMatcher"), "zxqy");
doTestAllMatches(anyOf("x"), "xxxx");
doTestAllMatches(anyOf("xy"), "xyyx");
doTestAllMatches(anyOf("CharMatcher"), "ChMa");
doTestAllMatches(inRange('m', 'p'), "mom");
doTestAllMatches(forPredicate(Predicates.equalTo('c')), "ccc");
doTestAllMatches(CharMatcher.digit(), "0123456789\u0ED0\u1B59");
doTestAllMatches(CharMatcher.javaDigit(), "0123456789");
doTestAllMatches(CharMatcher.digit().and(CharMatcher.ascii()), "0123456789");
doTestAllMatches(CharMatcher.singleWidth(), "\t0123ABCdef~\u00A0\u2111");
}
private void doTestAllMatches(CharMatcher matcher, String s) {
reallyTestAllMatches(matcher, s);
reallyTestNoMatches(matcher.negate(), s);
reallyTestAllMatches(matcher.precomputed(), s);
reallyTestNoMatches(matcher.negate().precomputed(), s);
reallyTestNoMatches(matcher.precomputed().negate(), s);
reallyTestAllMatches(forPredicate(matcher), s);
reallyTestAllMatches(matcher, new StringBuilder(s));
}
private void reallyTestNoMatches(CharMatcher matcher, CharSequence s) {
assertFalse(matcher.matches(s.charAt(0)));
assertEquals(-1, matcher.indexIn(s));
assertEquals(-1, matcher.indexIn(s, 0));
assertEquals(-1, matcher.indexIn(s, 1));
assertEquals(-1, matcher.indexIn(s, s.length()));
try {
matcher.indexIn(s, s.length() + 1);
fail();
} catch (IndexOutOfBoundsException expected) {
}
try {
matcher.indexIn(s, -1);
fail();
} catch (IndexOutOfBoundsException expected) {
}
assertEquals(-1, matcher.lastIndexIn(s));
assertFalse(matcher.matchesAnyOf(s));
assertFalse(matcher.matchesAllOf(s));
assertTrue(matcher.matchesNoneOf(s));
assertEquals(s.toString(), matcher.removeFrom(s));
assertEquals(s.toString(), matcher.replaceFrom(s, 'z'));
assertEquals(s.toString(), matcher.replaceFrom(s, "ZZ"));
assertEquals(s.toString(), matcher.trimFrom(s));
assertEquals(0, matcher.countIn(s));
}
private void reallyTestAllMatches(CharMatcher matcher, CharSequence s) {
assertTrue(matcher.matches(s.charAt(0)));
assertEquals(0, matcher.indexIn(s));
assertEquals(0, matcher.indexIn(s, 0));
assertEquals(1, matcher.indexIn(s, 1));
assertEquals(-1, matcher.indexIn(s, s.length()));
assertEquals(s.length() - 1, matcher.lastIndexIn(s));
assertTrue(matcher.matchesAnyOf(s));
assertTrue(matcher.matchesAllOf(s));
assertFalse(matcher.matchesNoneOf(s));
assertEquals("", matcher.removeFrom(s));
assertEquals(Strings.repeat("z", s.length()), matcher.replaceFrom(s, 'z'));
assertEquals(Strings.repeat("ZZ", s.length()), matcher.replaceFrom(s, "ZZ"));
assertEquals("", matcher.trimFrom(s));
assertEquals(s.length(), matcher.countIn(s));
}
public void testGeneral() {
doTestGeneral(is('a'), 'a', 'b');
doTestGeneral(isNot('a'), 'b', 'a');
doTestGeneral(anyOf("x"), 'x', 'z');
doTestGeneral(anyOf("xy"), 'y', 'z');
doTestGeneral(anyOf("CharMatcher"), 'C', 'z');
doTestGeneral(noneOf("CharMatcher"), 'z', 'C');
doTestGeneral(inRange('p', 'x'), 'q', 'z');
}
private void doTestGeneral(CharMatcher matcher, char match, char noMatch) {
doTestOneCharMatch(matcher, "" + match);
doTestOneCharNoMatch(matcher, "" + noMatch);
doTestMatchThenNoMatch(matcher, "" + match + noMatch);
doTestNoMatchThenMatch(matcher, "" + noMatch + match);
}
private void doTestOneCharMatch(CharMatcher matcher, String s) {
reallyTestOneCharMatch(matcher, s);
reallyTestOneCharNoMatch(matcher.negate(), s);
reallyTestOneCharMatch(matcher.precomputed(), s);
reallyTestOneCharNoMatch(matcher.negate().precomputed(), s);
reallyTestOneCharNoMatch(matcher.precomputed().negate(), s);
}
private void doTestOneCharNoMatch(CharMatcher matcher, String s) {
reallyTestOneCharNoMatch(matcher, s);
reallyTestOneCharMatch(matcher.negate(), s);
reallyTestOneCharNoMatch(matcher.precomputed(), s);
reallyTestOneCharMatch(matcher.negate().precomputed(), s);
reallyTestOneCharMatch(matcher.precomputed().negate(), s);
}
private void doTestMatchThenNoMatch(CharMatcher matcher, String s) {
reallyTestMatchThenNoMatch(matcher, s);
reallyTestNoMatchThenMatch(matcher.negate(), s);
reallyTestMatchThenNoMatch(matcher.precomputed(), s);
reallyTestNoMatchThenMatch(matcher.negate().precomputed(), s);
reallyTestNoMatchThenMatch(matcher.precomputed().negate(), s);
}
private void doTestNoMatchThenMatch(CharMatcher matcher, String s) {
reallyTestNoMatchThenMatch(matcher, s);
reallyTestMatchThenNoMatch(matcher.negate(), s);
reallyTestNoMatchThenMatch(matcher.precomputed(), s);
reallyTestMatchThenNoMatch(matcher.negate().precomputed(), s);
reallyTestMatchThenNoMatch(matcher.precomputed().negate(), s);
}
@SuppressWarnings("deprecation") // intentionally testing apply() method
private void reallyTestOneCharMatch(CharMatcher matcher, String s) {
assertTrue(matcher.matches(s.charAt(0)));
assertTrue(matcher.apply(s.charAt(0)));
assertEquals(0, matcher.indexIn(s));
assertEquals(0, matcher.indexIn(s, 0));
assertEquals(-1, matcher.indexIn(s, 1));
assertEquals(0, matcher.lastIndexIn(s));
assertTrue(matcher.matchesAnyOf(s));
assertTrue(matcher.matchesAllOf(s));
assertFalse(matcher.matchesNoneOf(s));
assertEquals("", matcher.removeFrom(s));
assertEquals("z", matcher.replaceFrom(s, 'z'));
assertEquals("ZZ", matcher.replaceFrom(s, "ZZ"));
assertEquals("", matcher.trimFrom(s));
assertEquals(1, matcher.countIn(s));
}
@SuppressWarnings("deprecation") // intentionally testing apply() method
private void reallyTestOneCharNoMatch(CharMatcher matcher, String s) {
assertFalse(matcher.matches(s.charAt(0)));
assertFalse(matcher.apply(s.charAt(0)));
assertEquals(-1, matcher.indexIn(s));
assertEquals(-1, matcher.indexIn(s, 0));
assertEquals(-1, matcher.indexIn(s, 1));
assertEquals(-1, matcher.lastIndexIn(s));
assertFalse(matcher.matchesAnyOf(s));
assertFalse(matcher.matchesAllOf(s));
assertTrue(matcher.matchesNoneOf(s));
assertSame(s, matcher.removeFrom(s));
assertSame(s, matcher.replaceFrom(s, 'z'));
assertSame(s, matcher.replaceFrom(s, "ZZ"));
assertSame(s, matcher.trimFrom(s));
assertEquals(0, matcher.countIn(s));
}
private void reallyTestMatchThenNoMatch(CharMatcher matcher, String s) {
assertEquals(0, matcher.indexIn(s));
assertEquals(0, matcher.indexIn(s, 0));
assertEquals(-1, matcher.indexIn(s, 1));
assertEquals(-1, matcher.indexIn(s, 2));
assertEquals(0, matcher.lastIndexIn(s));
assertTrue(matcher.matchesAnyOf(s));
assertFalse(matcher.matchesAllOf(s));
assertFalse(matcher.matchesNoneOf(s));
assertEquals(s.substring(1), matcher.removeFrom(s));
assertEquals("z" + s.substring(1), matcher.replaceFrom(s, 'z'));
assertEquals("ZZ" + s.substring(1), matcher.replaceFrom(s, "ZZ"));
assertEquals(s.substring(1), matcher.trimFrom(s));
assertEquals(1, matcher.countIn(s));
}
private void reallyTestNoMatchThenMatch(CharMatcher matcher, String s) {
assertEquals(1, matcher.indexIn(s));
assertEquals(1, matcher.indexIn(s, 0));
assertEquals(1, matcher.indexIn(s, 1));
assertEquals(-1, matcher.indexIn(s, 2));
assertEquals(1, matcher.lastIndexIn(s));
assertTrue(matcher.matchesAnyOf(s));
assertFalse(matcher.matchesAllOf(s));
assertFalse(matcher.matchesNoneOf(s));
assertEquals(s.substring(0, 1), matcher.removeFrom(s));
assertEquals(s.substring(0, 1) + "z", matcher.replaceFrom(s, 'z'));
assertEquals(s.substring(0, 1) + "ZZ", matcher.replaceFrom(s, "ZZ"));
assertEquals(s.substring(0, 1), matcher.trimFrom(s));
assertEquals(1, matcher.countIn(s));
}
/**
* Checks that expected is equals to out, and further, if in is equals to expected, then out is
* successfully optimized to be identical to in, i.e. that "in" is simply returned.
*/
private void assertEqualsSame(String expected, String in, String out) {
if (expected.equals(in)) {
assertSame(in, out);
} else {
assertEquals(expected, out);
}
}
// Test collapse() a little differently than the rest, as we really want to
// cover lots of different configurations of input text
public void testCollapse() {
// collapsing groups of '-' into '_' or '-'
doTestCollapse("-", "_");
doTestCollapse("x-", "x_");
doTestCollapse("-x", "_x");
doTestCollapse("--", "_");
doTestCollapse("x--", "x_");
doTestCollapse("--x", "_x");
doTestCollapse("-x-", "_x_");
doTestCollapse("x-x", "x_x");
doTestCollapse("---", "_");
doTestCollapse("--x-", "_x_");
doTestCollapse("--xx", "_xx");
doTestCollapse("-x--", "_x_");
doTestCollapse("-x-x", "_x_x");
doTestCollapse("-xx-", "_xx_");
doTestCollapse("x--x", "x_x");
doTestCollapse("x-x-", "x_x_");
doTestCollapse("x-xx", "x_xx");
doTestCollapse("x-x--xx---x----x", "x_x_xx_x_x");
doTestCollapseWithNoChange("");
doTestCollapseWithNoChange("x");
doTestCollapseWithNoChange("xx");
}
private void doTestCollapse(String in, String out) {
// Try a few different matchers which all match '-' and not 'x'
// Try replacement chars that both do and do not change the value.
for (char replacement : new char[] {'_', '-'}) {
String expected = out.replace('_', replacement);
assertEqualsSame(expected, in, is('-').collapseFrom(in, replacement));
assertEqualsSame(expected, in, is('-').collapseFrom(in, replacement));
assertEqualsSame(expected, in, is('-').or(is('#')).collapseFrom(in, replacement));
assertEqualsSame(expected, in, isNot('x').collapseFrom(in, replacement));
assertEqualsSame(expected, in, is('x').negate().collapseFrom(in, replacement));
assertEqualsSame(expected, in, anyOf("-").collapseFrom(in, replacement));
assertEqualsSame(expected, in, anyOf("-#").collapseFrom(in, replacement));
assertEqualsSame(expected, in, anyOf("-#123").collapseFrom(in, replacement));
}
}
private void doTestCollapseWithNoChange(String inout) {
assertSame(inout, is('-').collapseFrom(inout, '_'));
assertSame(inout, is('-').or(is('#')).collapseFrom(inout, '_'));
assertSame(inout, isNot('x').collapseFrom(inout, '_'));
assertSame(inout, is('x').negate().collapseFrom(inout, '_'));
assertSame(inout, anyOf("-").collapseFrom(inout, '_'));
assertSame(inout, anyOf("-#").collapseFrom(inout, '_'));
assertSame(inout, anyOf("-#123").collapseFrom(inout, '_'));
assertSame(inout, CharMatcher.none().collapseFrom(inout, '_'));
}
public void testCollapse_any() {
assertEquals("", CharMatcher.any().collapseFrom("", '_'));
assertEquals("_", CharMatcher.any().collapseFrom("a", '_'));
assertEquals("_", CharMatcher.any().collapseFrom("ab", '_'));
assertEquals("_", CharMatcher.any().collapseFrom("abcd", '_'));
}
public void testTrimFrom() {
// trimming -
doTestTrimFrom("-", "");
doTestTrimFrom("x-", "x");
doTestTrimFrom("-x", "x");
doTestTrimFrom("--", "");
doTestTrimFrom("x--", "x");
doTestTrimFrom("--x", "x");
doTestTrimFrom("-x-", "x");
doTestTrimFrom("x-x", "x-x");
doTestTrimFrom("---", "");
doTestTrimFrom("--x-", "x");
doTestTrimFrom("--xx", "xx");
doTestTrimFrom("-x--", "x");
doTestTrimFrom("-x-x", "x-x");
doTestTrimFrom("-xx-", "xx");
doTestTrimFrom("x--x", "x--x");
doTestTrimFrom("x-x-", "x-x");
doTestTrimFrom("x-xx", "x-xx");
doTestTrimFrom("x-x--xx---x----x", "x-x--xx---x----x");
// additional testing using the doc example
assertEquals("cat", anyOf("ab").trimFrom("abacatbab"));
}
private void doTestTrimFrom(String in, String out) {
// Try a few different matchers which all match '-' and not 'x'
assertEquals(out, is('-').trimFrom(in));
assertEquals(out, is('-').or(is('#')).trimFrom(in));
assertEquals(out, isNot('x').trimFrom(in));
assertEquals(out, is('x').negate().trimFrom(in));
assertEquals(out, anyOf("-").trimFrom(in));
assertEquals(out, anyOf("-#").trimFrom(in));
assertEquals(out, anyOf("-#123").trimFrom(in));
}
public void testTrimLeadingFrom() {
// trimming -
doTestTrimLeadingFrom("-", "");
doTestTrimLeadingFrom("x-", "x-");
doTestTrimLeadingFrom("-x", "x");
doTestTrimLeadingFrom("--", "");
doTestTrimLeadingFrom("x--", "x--");
doTestTrimLeadingFrom("--x", "x");
doTestTrimLeadingFrom("-x-", "x-");
doTestTrimLeadingFrom("x-x", "x-x");
doTestTrimLeadingFrom("---", "");
doTestTrimLeadingFrom("--x-", "x-");
doTestTrimLeadingFrom("--xx", "xx");
doTestTrimLeadingFrom("-x--", "x--");
doTestTrimLeadingFrom("-x-x", "x-x");
doTestTrimLeadingFrom("-xx-", "xx-");
doTestTrimLeadingFrom("x--x", "x--x");
doTestTrimLeadingFrom("x-x-", "x-x-");
doTestTrimLeadingFrom("x-xx", "x-xx");
doTestTrimLeadingFrom("x-x--xx---x----x", "x-x--xx---x----x");
// additional testing using the doc example
assertEquals("catbab", anyOf("ab").trimLeadingFrom("abacatbab"));
}
private void doTestTrimLeadingFrom(String in, String out) {
// Try a few different matchers which all match '-' and not 'x'
assertEquals(out, is('-').trimLeadingFrom(in));
assertEquals(out, is('-').or(is('#')).trimLeadingFrom(in));
assertEquals(out, isNot('x').trimLeadingFrom(in));
assertEquals(out, is('x').negate().trimLeadingFrom(in));
assertEquals(out, anyOf("-#").trimLeadingFrom(in));
assertEquals(out, anyOf("-#123").trimLeadingFrom(in));
}
public void testTrimTrailingFrom() {
// trimming -
doTestTrimTrailingFrom("-", "");
doTestTrimTrailingFrom("x-", "x");
doTestTrimTrailingFrom("-x", "-x");
doTestTrimTrailingFrom("--", "");
doTestTrimTrailingFrom("x--", "x");
doTestTrimTrailingFrom("--x", "--x");
doTestTrimTrailingFrom("-x-", "-x");
doTestTrimTrailingFrom("x-x", "x-x");
doTestTrimTrailingFrom("---", "");
doTestTrimTrailingFrom("--x-", "--x");
doTestTrimTrailingFrom("--xx", "--xx");
doTestTrimTrailingFrom("-x--", "-x");
doTestTrimTrailingFrom("-x-x", "-x-x");
doTestTrimTrailingFrom("-xx-", "-xx");
doTestTrimTrailingFrom("x--x", "x--x");
doTestTrimTrailingFrom("x-x-", "x-x");
doTestTrimTrailingFrom("x-xx", "x-xx");
doTestTrimTrailingFrom("x-x--xx---x----x", "x-x--xx---x----x");
// additional testing using the doc example
assertEquals("abacat", anyOf("ab").trimTrailingFrom("abacatbab"));
}
private void doTestTrimTrailingFrom(String in, String out) {
// Try a few different matchers which all match '-' and not 'x'
assertEquals(out, is('-').trimTrailingFrom(in));
assertEquals(out, is('-').or(is('#')).trimTrailingFrom(in));
assertEquals(out, isNot('x').trimTrailingFrom(in));
assertEquals(out, is('x').negate().trimTrailingFrom(in));
assertEquals(out, anyOf("-#").trimTrailingFrom(in));
assertEquals(out, anyOf("-#123").trimTrailingFrom(in));
}
public void testTrimAndCollapse() {
// collapsing groups of '-' into '_' or '-'
doTestTrimAndCollapse("", "");
doTestTrimAndCollapse("x", "x");
doTestTrimAndCollapse("-", "");
doTestTrimAndCollapse("x-", "x");
doTestTrimAndCollapse("-x", "x");
doTestTrimAndCollapse("--", "");
doTestTrimAndCollapse("x--", "x");
doTestTrimAndCollapse("--x", "x");
doTestTrimAndCollapse("-x-", "x");
doTestTrimAndCollapse("x-x", "x_x");
doTestTrimAndCollapse("---", "");
doTestTrimAndCollapse("--x-", "x");
doTestTrimAndCollapse("--xx", "xx");
doTestTrimAndCollapse("-x--", "x");
doTestTrimAndCollapse("-x-x", "x_x");
doTestTrimAndCollapse("-xx-", "xx");
doTestTrimAndCollapse("x--x", "x_x");
doTestTrimAndCollapse("x-x-", "x_x");
doTestTrimAndCollapse("x-xx", "x_xx");
doTestTrimAndCollapse("x-x--xx---x----x", "x_x_xx_x_x");
}
private void doTestTrimAndCollapse(String in, String out) {
// Try a few different matchers which all match '-' and not 'x'
for (char replacement : new char[] {'_', '-'}) {
String expected = out.replace('_', replacement);
assertEqualsSame(expected, in, is('-').trimAndCollapseFrom(in, replacement));
assertEqualsSame(expected, in, is('-').or(is('#')).trimAndCollapseFrom(in, replacement));
assertEqualsSame(expected, in, isNot('x').trimAndCollapseFrom(in, replacement));
assertEqualsSame(expected, in, is('x').negate().trimAndCollapseFrom(in, replacement));
assertEqualsSame(expected, in, anyOf("-").trimAndCollapseFrom(in, replacement));
assertEqualsSame(expected, in, anyOf("-#").trimAndCollapseFrom(in, replacement));
assertEqualsSame(expected, in, anyOf("-#123").trimAndCollapseFrom(in, replacement));
}
}
public void testReplaceFrom() {
assertEquals("yoho", is('a').replaceFrom("yaha", 'o'));
assertEquals("yh", is('a').replaceFrom("yaha", ""));
assertEquals("yoho", is('a').replaceFrom("yaha", "o"));
assertEquals("yoohoo", is('a').replaceFrom("yaha", "oo"));
assertEquals("12 > 5", is('>').replaceFrom("12 > 5", ">"));
}
public void testPrecomputedOptimizations() {
// These are testing behavior that's never promised by the API.
// Some matchers are so efficient that it is a waste of effort to
// build a precomputed version.
CharMatcher m1 = is('x');
assertSame(m1, m1.precomputed());
assertEquals(m1.toString(), m1.precomputed().toString());
CharMatcher m2 = anyOf("Az");
assertSame(m2, m2.precomputed());
assertEquals(m2.toString(), m2.precomputed().toString());
CharMatcher m3 = inRange('A', 'Z');
assertSame(m3, m3.precomputed());
assertEquals(m3.toString(), m3.precomputed().toString());
assertSame(CharMatcher.none(), CharMatcher.none().precomputed());
assertSame(CharMatcher.any(), CharMatcher.any().precomputed());
}
@GwtIncompatible // java.util.BitSet
private static BitSet bitSet(String chars) {
return bitSet(chars.toCharArray());
}
@GwtIncompatible // java.util.BitSet
private static BitSet bitSet(char[] chars) {
BitSet tmp = new BitSet();
for (char c : chars) {
tmp.set(c);
}
return tmp;
}
@GwtIncompatible // java.util.Random, java.util.BitSet
public void testSmallCharMatcher() {
CharMatcher len1 = SmallCharMatcher.from(bitSet("#"), "#");
CharMatcher len2 = SmallCharMatcher.from(bitSet("ab"), "ab");
CharMatcher len3 = SmallCharMatcher.from(bitSet("abc"), "abc");
CharMatcher len4 = SmallCharMatcher.from(bitSet("abcd"), "abcd");
assertTrue(len1.matches('#'));
assertFalse(len1.matches('!'));
assertTrue(len2.matches('a'));
assertTrue(len2.matches('b'));
for (char c = 'c'; c < 'z'; c++) {
assertFalse(len2.matches(c));
}
assertTrue(len3.matches('a'));
assertTrue(len3.matches('b'));
assertTrue(len3.matches('c'));
for (char c = 'd'; c < 'z'; c++) {
assertFalse(len3.matches(c));
}
assertTrue(len4.matches('a'));
assertTrue(len4.matches('b'));
assertTrue(len4.matches('c'));
assertTrue(len4.matches('d'));
for (char c = 'e'; c < 'z'; c++) {
assertFalse(len4.matches(c));
}
Random rand = new Random(1234);
for (int testCase = 0; testCase < 100; testCase++) {
char[] chars = randomChars(rand, rand.nextInt(63) + 1);
CharMatcher m = SmallCharMatcher.from(bitSet(chars), new String(chars));
checkExactMatches(m, chars);
}
}
static void checkExactMatches(CharMatcher m, char[] chars) {
Set<Character> positive = Sets.newHashSetWithExpectedSize(chars.length);
for (char c : chars) {
positive.add(c);
}
for (int c = 0; c <= Character.MAX_VALUE; c++) {
assertFalse(positive.contains(new Character((char) c)) ^ m.matches((char) c));
}
}
static char[] randomChars(Random rand, int size) {
Set<Character> chars = new HashSet<>(size);
for (int i = 0; i < size; i++) {
char c;
do {
c = (char) rand.nextInt(Character.MAX_VALUE - Character.MIN_VALUE + 1);
} while (chars.contains(c));
chars.add(c);
}
char[] retValue = new char[chars.size()];
int i = 0;
for (char c : chars) {
retValue[i++] = c;
}
Arrays.sort(retValue);
return retValue;
}
public void testToString() {
assertToStringWorks("CharMatcher.none()", CharMatcher.anyOf(""));
assertToStringWorks("CharMatcher.is('\\u0031')", CharMatcher.anyOf("1"));
assertToStringWorks("CharMatcher.isNot('\\u0031')", CharMatcher.isNot('1'));
assertToStringWorks("CharMatcher.anyOf(\"\\u0031\\u0032\")", CharMatcher.anyOf("12"));
assertToStringWorks("CharMatcher.anyOf(\"\\u0031\\u0032\\u0033\")", CharMatcher.anyOf("321"));
assertToStringWorks("CharMatcher.inRange('\\u0031', '\\u0033')", CharMatcher.inRange('1', '3'));
}
private static void assertToStringWorks(String expected, CharMatcher matcher) {
assertEquals(expected, matcher.toString());
assertEquals(expected, matcher.precomputed().toString());
assertEquals(expected, matcher.negate().negate().toString());
assertEquals(expected, matcher.negate().precomputed().negate().toString());
assertEquals(expected, matcher.negate().precomputed().negate().precomputed().toString());
}
}
|