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 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
|
/*
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 4313885 4926319 4927634 5032610 5032622 5049968 5059533 6223711 6277261 6269946 6288823
* 8072722 8139414 8166261 8172695
* @summary Basic tests of java.util.Scanner methods
* @key randomness
* @modules jdk.localedata
* @run main/othervm ScanTest
*/
import java.io.*;
import java.math.*;
import java.nio.*;
import java.text.*;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.regex.*;
import java.util.stream.*;
public class ScanTest {
private static boolean failure = false;
private static int failCount = 0;
private static int NUM_SOURCE_TYPES = 2;
private static File inputFile = new File(System.getProperty("test.src", "."), "input.txt");
public static void main(String[] args) throws Exception {
Locale defaultLocale = Locale.getDefault();
try {
// Before we have resource to improve the test to be ready for
// arbitrary locale, force the default locale to be ROOT for now.
Locale.setDefault(Locale.US);
skipTest();
findInLineTest();
findWithinHorizonTest();
findInEmptyLineTest();
removeTest();
fromFileTest();
ioExceptionTest();
matchTest();
delimiterTest();
boundaryDelimTest();
useLocaleTest();
closeTest();
cacheTest();
cacheTest2();
nonASCIITest();
resetTest();
streamCloseTest();
streamComodTest();
outOfRangeRadixTest();
for (int j = 0; j < NUM_SOURCE_TYPES; j++) {
hasNextTest(j);
nextTest(j);
hasNextPatternTest(j);
nextPatternTest(j);
booleanTest(j);
byteTest(j);
shortTest(j);
intTest(j);
longTest(j);
floatTest(j);
doubleTest(j);
integerPatternTest(j);
floatPatternTest(j);
bigIntegerPatternTest(j);
bigDecimalPatternTest(j);
hasNextLineTest(j);
nextLineTest(j);
singleDelimTest(j);
}
// Examples
//example1();
//example2();
//example3();
// Usage cases
useCase1();
useCase2();
useCase3();
useCase4();
useCase5();
if (failure)
throw new RuntimeException("Failure in the scanning tests.");
else
System.err.println("OKAY: All tests passed.");
} finally {
// restore the default locale
Locale.setDefault(defaultLocale);
}
}
public static void useCase1() throws Exception {
try (Scanner sc = new Scanner(inputFile)) {
sc.findWithinHorizon("usage case 1", 0);
String[] names = new String[4];
for (int i=0; i<4; i++) {
while (sc.hasNextFloat())
sc.nextFloat();
names[i] = sc.next();
sc.nextLine();
}
if (!names[0].equals("Frank"))
failCount++;
if (!names[1].equals("Joe"))
failCount++;
if (!names[2].equals("Mary"))
failCount++;
if (!names[3].equals("Michelle"))
failCount++;
}
report("Use case 1");
}
public static void useCase2() throws Exception {
try (Scanner sc = new Scanner(inputFile).useDelimiter("-")) {
String testDataTag = sc.findWithinHorizon("usage case 2\n", 0);
if (!testDataTag.equals("usage case 2\n"))
failCount++;
if (!sc.next().equals("cat"))
failCount++;
if (sc.nextInt() != 9)
failCount++;
if (!sc.next().equals("dog"))
failCount++;
if (sc.nextInt() != 6)
failCount++;
if (!sc.next().equals("pig"))
failCount++;
if (sc.nextInt() != 2)
failCount++;
if (!sc.next().equals(""))
failCount++;
if (sc.nextInt() != 5)
failCount++;
}
report("Use case 2");
}
public static void useCase3() throws Exception {
try (Scanner sc = new Scanner(inputFile)) {
String testDataTag = sc.findWithinHorizon("usage case 3\n", 0);
if (!testDataTag.equals("usage case 3\n"))
failCount++;
Pattern tagPattern = Pattern.compile("@[a-z]+");
Pattern endPattern = Pattern.compile("\\*\\/");
String tag;
String end = sc.findInLine(endPattern);
while (end == null) {
if ((tag = sc.findInLine(tagPattern)) != null) {
String text = sc.nextLine();
text = text.substring(0, text.length() - 1);
//System.out.println(text);
} else {
sc.nextLine();
}
end = sc.findInLine(endPattern);
}
}
report("Use case 3");
}
public static void useCase4() throws Exception {
try (Scanner sc = new Scanner(inputFile)) {
String testDataTag = sc.findWithinHorizon("usage case 4\n", 0);
if (!testDataTag.equals("usage case 4\n"))
failCount++;
// Read some text parts of four hrefs
String[] expected = { "Diffs", "Sdiffs", "Old", "New" };
for (int i=0; i<4; i++) {
sc.findWithinHorizon("<a href", 1000);
sc.useDelimiter("[<>\n]+");
sc.next();
String textOfRef = sc.next();
if (!textOfRef.equals(expected[i]))
failCount++;
}
// Read some html tags using < and > as delimiters
if (!sc.next().equals("/a"))
failCount++;
if (!sc.next().equals("b"))
failCount++;
// Scan some html tags using skip and next
Pattern nonTagStart = Pattern.compile("[^<]+");
Pattern tag = Pattern.compile("<[^>]+?>");
Pattern spotAfterTag = Pattern.compile("(?<=>)");
String[] expected2 = { "</b>", "<p>", "<ul>", "<li>" };
sc.useDelimiter(spotAfterTag);
int tagsFound = 0;
while (tagsFound < 4) {
if (!sc.hasNext(tag)) {
// skip text between tags
sc.skip(nonTagStart);
}
String tagContents = sc.next(tag);
if (!tagContents.equals(expected2[tagsFound]))
failCount++;
tagsFound++;
}
}
report("Use case 4");
}
public static void useCase5() throws Exception {
try (Scanner sc = new Scanner(inputFile)) {
String testDataTag = sc.findWithinHorizon("usage case 5\n", 0);
if (!testDataTag.equals("usage case 5\n"))
failCount++;
sc.findWithinHorizon("Share Definitions", 0);
sc.nextLine();
sc.next("\\[([a-z]+)\\]");
String shareName = sc.match().group(1);
if (!shareName.equals("homes"))
failCount++;
String[] keys = { "comment", "browseable", "writable", "valid users" };
String[] vals = { "Home Directories", "no", "yes", "%S" };
for (int i=0; i<4; i++) {
sc.useDelimiter("=");
String key = sc.next().trim();
if (!key.equals(keys[i]))
failCount++;
sc.skip("[ =]+");
sc.useDelimiter("\n");
String value = sc.next();
if (!value.equals(vals[i]))
failCount++;
sc.nextLine();
}
}
report("Use case 5");
}
public static void nonASCIITest() throws Exception {
String yourBasicTibetanNumberZero = "\u0f20";
String yourBasicTibetanFloatingNumber = "\u0f23.\u0f27";
String weirdMixtureOfTibetanAndASCII = "\u0f23.7";
String weirdMixtureOfASCIIAndTibetan = "3.\u0f27";
Scanner sc = new Scanner(yourBasicTibetanNumberZero);
int i = sc.nextInt();
if (i != 0)
failCount++;
sc = new Scanner(yourBasicTibetanFloatingNumber);
float f = sc.nextFloat();
if (f != Float.parseFloat("3.7"))
failCount++;
sc = new Scanner(weirdMixtureOfTibetanAndASCII);
f = sc.nextFloat();
if (f != Float.parseFloat("3.7"))
failCount++;
sc = new Scanner(weirdMixtureOfASCIIAndTibetan);
f = sc.nextFloat();
if (f != Float.parseFloat("3.7"))
failCount++;
report("Scanning non ASCII digits");
}
public static void findWithinHorizonTest() throws Exception {
// Test with a string source
Scanner sc = new Scanner("dog cat cat dog cat");
try {
sc.findWithinHorizon("dog", -1);
failCount++;
} catch (IllegalArgumentException iae) {
// Correct result
}
if (sc.findWithinHorizon("dog", 2) != null)
failCount++;
if (!sc.findWithinHorizon("dog", 3).equals("dog"))
failCount++;
if (sc.findWithinHorizon("cat", 4) != null)
failCount++;
if (!sc.findWithinHorizon("cat", 5).equals("cat"))
failCount++;
if (sc.findWithinHorizon("cat", 7) != null)
failCount++;
if (sc.findWithinHorizon("dog", 7) != null)
failCount++;
if (!sc.findWithinHorizon("cat", 0).equals("cat"))
failCount++;
if (!sc.findWithinHorizon("dog", 0).equals("dog"))
failCount++;
if (!sc.findWithinHorizon("cat", 0).equals("cat"))
failCount++;
// Test with a stream source
StutteringInputStream stutter = new StutteringInputStream();
for (int index=0; index<stutter.length(); index++) {
//System.out.println("index is now "+index);
sc = new Scanner(stutter);
String word = stutter.wordInIndex(index);
if (word != null) {
String result = sc.findWithinHorizon(word, index);
if ((result == null) || (!result.equals(word)))
failCount++;
}
stutter.reset();
word = stutter.wordBeyondIndex(index);
sc = new Scanner(stutter);
String result = sc.findWithinHorizon(word, index);
if ((result != null) && (index > 0))
failCount++;
stutter.reset();
}
// We must loop to let StutteringInputStream do its magic
for (int j=0; j<10; j++) {
// An anchor at the end of stream should work
stutter.reset();
sc = new Scanner(stutter);
String result = sc.findWithinHorizon("phant$", 0);
if (!result.equals("phant"))
failCount++;
stutter.reset();
sc = new Scanner(stutter);
result = sc.findWithinHorizon("phant$", 54);
if (!result.equals("phant"))
failCount++;
// An anchor at the end of horizon should not
stutter.reset();
sc = new Scanner(stutter);
result = sc.findWithinHorizon("brummer$", 7);
if (result != null)
failCount++;
// An anchor at start should work
stutter.reset();
sc = new Scanner(stutter);
result = sc.findWithinHorizon("^brummer", 0);
if (!result.equals("brummer"))
failCount++;
}
report("Find to horizon test");
}
// StutteringInputStream returns 1 to 3 characters at a time
static class StutteringInputStream implements Readable {
StutteringInputStream() {
text = "brummer hisser tort zardzard rantrant caimagator phant";
datalen = 54;
}
StutteringInputStream(String text) {
this.text = text;
datalen = text.length();
}
Random generator = new Random();
String text;
int datalen;
int index = 0;
public int length() {
return datalen;
}
public void reset() {
index = 0;
}
public String wordInIndex(int index) {
if (index < 7) return null;
if (index < 14) return "brummer";
if (index < 19) return "hisser";
if (index < 28) return "tort";
if (index < 37) return "zardzard";
if (index < 48) return "rantrant";
return "caimagator";
}
public String wordBeyondIndex(int index) {
if (index < 7) return "brummer";
if (index < 14) return "hisser";
if (index < 19) return "tort";
if (index < 28) return "zardzard";
if (index < 37) return "rantrant";
if (index < 48) return "caimagator";
return "phantphant";
}
public int read(java.nio.CharBuffer target) throws IOException {
if (index > datalen-1)
return -1; // EOS
int len = target.remaining();
if (len > 4) // return 1 to 3 characters
len = generator.nextInt(3) + 1;
while ((index + len) > datalen)
len--;
for (int i=0; i<len; i++)
target.put(text.charAt(index++));
return len;
}
}
public static void hasNextLineTest(int sourceType) throws Exception {
Scanner sc = scannerFor("1\n2\n3 3\r\n4 4 4\r5", sourceType);
if (!sc.hasNextLine()) failCount++;
if (!sc.nextLine().equals("1")) failCount++;
if (!sc.hasNextLine()) failCount++;
if (sc.nextInt() != 2) failCount++;
if (!sc.hasNextLine()) failCount++;
if (!sc.nextLine().equals("")) failCount++;
if (!sc.hasNextLine()) failCount++;
if (sc.nextInt() != 3) failCount++;
if (!sc.hasNextLine()) failCount++;
if (!sc.nextLine().equals(" 3")) failCount++;
if (!sc.hasNextLine()) failCount++;
if (sc.nextInt() != 4) failCount++;
if (!sc.hasNextLine()) failCount++;
if (sc.nextInt() != 4) failCount++;
if (!sc.hasNextLine()) failCount++;
if (!sc.nextLine().equals(" 4")) failCount++;
if (!sc.hasNextLine()) failCount++;
if (!sc.nextLine().equals("5")) failCount++;
if (sc.hasNextLine()) failCount++;
sc = new Scanner("blah blah blah blah blah blah");
if (!sc.hasNextLine()) failCount++;
if (!sc.nextLine().equals("blah blah blah blah blah blah"))
failCount++;
if (sc.hasNextLine()) failCount++;
// Go through all the lines in a file
try (Scanner sc2 = new Scanner(inputFile)) {
String lastLine = "blah";
while (sc2.hasNextLine())
lastLine = sc2.nextLine();
if (!lastLine.equals("# Data for usage case 6")) failCount++;
}
report("Has next line test");
}
public static void nextLineTest(int sourceType) throws Exception {
Scanner sc = scannerFor("1\n2\n3 3\r\n4 4 4\r5", sourceType);
if (!sc.nextLine().equals("1"))
failCount++;
if (sc.nextInt() != 2)
failCount++;
if (!sc.nextLine().equals(""))
failCount++;
if (sc.nextInt() != 3)
failCount++;
if (!sc.nextLine().equals(" 3"))
failCount++;
if (sc.nextInt() != 4)
failCount++;
if (sc.nextInt() != 4)
failCount++;
if (!sc.nextLine().equals(" 4"))
failCount++;
if (!sc.nextLine().equals("5"))
failCount++;
sc = new Scanner("blah blah blah blah blah blah");
if (!sc.nextLine().equals("blah blah blah blah blah blah"))
failCount++;
report("Next line test");
}
public static void singleDelimTest(int sourceType) throws Exception {
Scanner sc = scannerFor("12 13 14 15 16 17 ",
sourceType);
sc.useDelimiter(" ");
for (int i=0; i<6; i++) {
int j = sc.nextInt();
if (j != 12 + i)
failCount++;
for (int k=0; k<i; k++) {
String empty = sc.next();
if (!empty.equals(""))
failCount++;
}
}
report("Single delim test");
}
private static void append(StringBuilder sb, char c, int n) {
for (int i = 0; i < n; i++) {
sb.append(c);
}
}
public static void boundaryDelimTest() throws Exception {
// 8139414
int i = 1019;
StringBuilder sb = new StringBuilder();
sb.append("--;");
for (int j = 0; j < 1019; ++j) {
sb.append(j%10);
}
sb.append("-;-");
String text = sb.toString();
try (Scanner scanner = new Scanner(text)) {
scanner.useDelimiter("-;(-)?");
while (scanner.hasNext()) {
scanner.next();
}
} catch (NoSuchElementException e) {
System.out.println("Caught NoSuchElementException " + e);
e.printStackTrace();
failCount++;
}
report("delim at boundary test");
}
/*
* The hasNextPattern caches a match of a pattern called the regular cache
* The hasNextType caches a match of that type called the type cache
* Any next must clear the caches whether it uses them or not, because
* it advances past a token thus invalidating any cached token; any
* hasNext must set a cache to what it finds.
*/
public static void cacheTest() throws Exception {
// Test clearing of the type cache
Scanner scanner = new Scanner("777 dog");
scanner.hasNextInt();
scanner.findInLine("777");
try {
scanner.nextInt();
System.out.println("type cache not cleared by find");
failCount++;
} catch (InputMismatchException ime) {
// Correct
}
scanner = new Scanner("777 dog");
scanner.hasNextInt();
scanner.skip("777");
try {
scanner.nextInt();
System.out.println("type cache not cleared by skip");
failCount++;
} catch (InputMismatchException ime) {
// Correct
}
// Test clearing of the regular cache
scanner = new Scanner("777 dog");
scanner.hasNext("777");
scanner.findInLine("777");
try {
scanner.next("777");
System.out.println("regular cache not cleared by find");
failCount++;
} catch (InputMismatchException ime) {
// Correct
}
// Test two primitive next clearing of type cache
scanner = new Scanner("777 dog");
scanner.hasNextInt();
scanner.nextLong();
try {
scanner.nextInt();
System.out.println("type cache not cleared by primitive next");
failCount++;
} catch (InputMismatchException ime) {
// Correct
}
// Test using both of them, type first
scanner = new Scanner("777 dog");
scanner.hasNext("777");
scanner.nextInt();
try {
scanner.next("777");
System.out.println("regular cache not cleared by primitive next");
failCount++;
} catch (InputMismatchException ime) {
// Correct
}
// Test using both of them, regular first
scanner = new Scanner("777 dog");
scanner.hasNext("777");
scanner.hasNextInt();
scanner.next("777");
try {
scanner.nextInt();
System.out.println("type cache not cleared by regular next");
failCount++;
} catch (InputMismatchException ime) {
// Correct
}
report("Cache test");
}
/*
* The hasNext<IntegerType>(radix) method caches a matched integer type
* with specified radix for the next next<IntegerType>(radix) invoke.
* The cache value should not be used if the next<IntegerType>(radix)
* has different radix value with the last hasNext<IntegerType>(radix).
*/
public static void cacheTest2() throws Exception {
// Test clearing of the type cache
Scanner scanner = new Scanner("10");
scanner.hasNextByte(16);
if (scanner.nextByte(10) != 10) {
System.out.println("wrong radix cache is used");
failCount++;
}
scanner = new Scanner("10");
scanner.hasNextShort(16);
if (scanner.nextShort(10) != 10) {
System.out.println("wrong radix cache is used");
failCount++;
}
scanner = new Scanner("10");
scanner.hasNextInt(16);
if (scanner.nextInt(10) != 10) {
System.out.println("wrong radix cache is used");
failCount++;
}
scanner = new Scanner("10");
scanner.hasNextLong(16);
if (scanner.nextLong(10) != 10) {
System.out.println("wrong radix cache is used");
failCount++;
}
scanner = new Scanner("10");
scanner.hasNextBigInteger(16);
if (scanner.nextBigInteger(10).intValue() != 10) {
System.out.println("wrong radix cache is used");
failCount++;
}
report("Cache test2");
}
public static void closeTest() throws Exception {
Scanner sc = new Scanner("testing");
sc.close();
sc.ioException();
sc.delimiter();
sc.useDelimiter("blah");
sc.useDelimiter(Pattern.compile("blah"));
for (Consumer<Scanner> method : methodList) {
try {
method.accept(sc);
failCount++;
} catch (IllegalStateException ise) {
// Correct
}
}
report("Close test");
}
static List<Consumer<Scanner>> methodList = Arrays.asList(
Scanner::hasNext,
Scanner::next,
sc -> sc.hasNext(Pattern.compile("blah")),
sc -> sc.next(Pattern.compile("blah")),
Scanner::hasNextBoolean,
Scanner::nextBoolean,
Scanner::hasNextByte,
Scanner::nextByte,
Scanner::hasNextShort,
Scanner::nextShort,
Scanner::hasNextInt,
Scanner::nextInt,
Scanner::hasNextLong,
Scanner::nextLong,
Scanner::hasNextFloat,
Scanner::nextFloat,
Scanner::hasNextDouble,
Scanner::nextDouble,
Scanner::hasNextBigInteger,
Scanner::nextBigInteger,
Scanner::hasNextBigDecimal,
Scanner::nextBigDecimal,
Scanner::hasNextLine,
Scanner::tokens,
sc -> sc.findAll(Pattern.compile("blah")),
sc -> sc.findAll("blah")
);
public static void removeTest() throws Exception {
Scanner sc = new Scanner("testing");
try {
sc.remove();
failCount++;
} catch (UnsupportedOperationException uoe) {
// Correct result
}
report("Remove test");
}
public static void delimiterTest() throws Exception {
Scanner sc = new Scanner("blah");
Pattern test = sc.delimiter();
if (!test.toString().equals("\\p{javaWhitespace}+"))
failCount++;
sc.useDelimiter("a");
test = sc.delimiter();
if (!test.toString().equals("a"))
failCount++;
sc.useDelimiter(Pattern.compile("b"));
test = sc.delimiter();
if (!test.toString().equals("b"))
failCount++;
report("Delimiter test");
}
public static void ioExceptionTest() throws Exception {
Readable thrower = new ThrowingReadable();
Scanner sc = new Scanner(thrower);
try {
sc.nextInt();
failCount++;
} catch (NoSuchElementException nsee) {
// Correct result
}
Exception thrown = sc.ioException();
String detail = thrown.getMessage();
if (!detail.equals("ThrowingReadable always throws"))
failCount++;
report("IOException test");
}
public static void bigIntegerPatternTest(int sourceType) throws Exception {
Scanner sc = scannerFor("23 9223372036854775817", sourceType);
if (!sc.nextBigInteger().equals(BigInteger.valueOf(23)))
failCount++;
if (!sc.nextBigInteger().equals(new BigInteger(
"9223372036854775817", 10)))
failCount++;
// Test another radix
sc = new Scanner("4a4 4A4").useRadix(16);
if (!sc.nextBigInteger().equals(new BigInteger("4a4", 16)))
failCount++;
if (!sc.nextBigInteger().equals(new BigInteger("4A4", 16)))
failCount++;
// Test alternating radices
sc = new Scanner("12 4a4 14 4f4");
if (!sc.nextBigInteger(10).equals(new BigInteger("12", 10)))
failCount++;
if (!sc.nextBigInteger(16).equals(new BigInteger("4a4", 16)))
failCount++;
if (!sc.nextBigInteger(10).equals(new BigInteger("14", 10)))
failCount++;
if (!sc.nextBigInteger(16).equals(new BigInteger("4f4", 16)))
failCount++;
// Test use of a lot of radices
for (int i=2; i<17; i++) {
sc = new Scanner("1111");
if (!sc.nextBigInteger(i).equals(new BigInteger("1111", i)))
failCount++;
}
report("BigInteger pattern");
}
public static void bigDecimalPatternTest(int sourceType) throws Exception {
Scanner sc = scannerFor("23 45.99 -45,067.444 3.4e10", sourceType);
if (!sc.nextBigDecimal().equals(BigDecimal.valueOf(23)))
failCount++;
if (!sc.nextBigDecimal().equals(new BigDecimal("45.99")))
failCount++;
if (!sc.nextBigDecimal().equals(new BigDecimal("-45067.444")))
failCount++;
if (!sc.nextBigDecimal().equals(new BigDecimal("3.4e10")))
failCount++;
report("BigDecimal pattern");
}
public static void integerPatternTest(int sourceType) throws Exception {
String input =
"1 22 f FF Z -3 -44 123 1,200 -123 -3,400,000 5,40 ,500 ";
Scanner sc = scannerFor(input, sourceType);
integerPatternBody(sc);
CharBuffer cb = CharBuffer.wrap(input);
sc = new Scanner(cb);
integerPatternBody(sc);
report("Integer pattern");
}
public static void integerPatternBody(Scanner sc) throws Exception {
if (sc.nextInt() != 1) failCount++;
if (sc.nextShort() != 22) failCount++;
if (sc.nextShort(16) != 15) failCount++;
if (sc.nextShort(16) != 255) failCount++;
if (sc.nextShort(36) != 35) failCount++;
if (!sc.hasNextInt()) failCount++;
if (sc.nextInt() != -3) failCount++;
if (sc.nextInt() != -44) failCount++;
if (sc.nextLong() != 123) failCount++;
if (!sc.hasNextInt()) failCount++;
if (sc.nextInt() != 1200) failCount++;
if (sc.nextInt() != -123) failCount++;
if (sc.nextInt() != -3400000) failCount++;
try {
sc.nextInt();
failCount++;
} catch (InputMismatchException ime) {
// Correct result
}
sc.next();
try {
sc.nextLong();
failCount++;
} catch (InputMismatchException ime) {
// Correct result
}
sc.next();
try {
sc.next();
failCount++;
} catch (InputMismatchException ime) {
failCount++;
} catch (NoSuchElementException nse) {
// Correct result
}
}
public static void floatPatternTest(int sourceType) throws Exception {
String input =
"090.090 1 22.0 -3 -44.05 +.123 -.1234 -3400000 56,566.6 " +
"Infinity +Infinity -Infinity NaN -NaN +NaN 5.4.0 5-.00 ++6.07";
Scanner sc = scannerFor(input, sourceType);
floatPatternBody(sc);
CharBuffer cb = CharBuffer.wrap(input);
sc = new Scanner(cb);
floatPatternBody(sc);
report("Float pattern");
}
public static void floatPatternBody(Scanner sc) throws Exception {
if (sc.nextFloat() != 090.090f) failCount++;
if (sc.nextFloat() != 1f) failCount++;
if (sc.nextFloat() != 22.0f) failCount++;
if (sc.nextDouble() != -3d) failCount++;
if (sc.nextDouble() != -44.05d) failCount++;
if (sc.nextFloat() != .123f) failCount++;
if (sc.nextFloat() != -.1234f) failCount++;
if (sc.nextDouble() != -3400000d) failCount++;
if (sc.nextDouble() != 56566.6d) failCount++;
if (sc.nextDouble() != Double.POSITIVE_INFINITY) failCount++;
if (sc.nextDouble() != Double.POSITIVE_INFINITY) failCount++;
if (sc.nextDouble() != Double.NEGATIVE_INFINITY) failCount++;
if (!Double.valueOf(sc.nextDouble()).isNaN()) failCount++;
if (!Double.valueOf(sc.nextDouble()).isNaN()) failCount++;
if (!Double.valueOf(sc.nextDouble()).isNaN()) failCount++;
try {
sc.nextFloat();
failCount++;
} catch (NoSuchElementException nse) {
// Correct result
}
try {
sc.nextDouble();
failCount++;
} catch (NoSuchElementException nse) {
// Correct result
}
try {
sc.nextDouble();
failCount++;
} catch (NoSuchElementException nse) {
// Correct result
}
}
public static void fromFileTest() throws Exception {
File f = new File(System.getProperty("test.src", "."), "input.txt");
try (Scanner sc = new Scanner(f)) {
sc.useDelimiter("\n+");
String testDataTag = sc.findWithinHorizon("fromFileTest", 0);
if (!testDataTag.equals("fromFileTest"))
failCount++;
int count = 0;
while (sc.hasNextLong()) {
long blah = sc.nextLong();
count++;
}
if (count != 7)
failCount++;
}
report("From file");
}
private static void example1() throws Exception {
Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
s.useDelimiter("\\s*fish\\s*");
List <String> results = new ArrayList<String>();
while (s.hasNext())
results.add(s.next());
System.out.println(results);
}
private static void example2() throws Exception {
Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
s.useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
}
private static void example3() throws Exception {
Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
for (int i=1; i<=s.match().groupCount(); i++)
System.out.println(s.match().group(i));
}
private static void findInLineTest() throws Exception {
Scanner s = new Scanner("abc def ghi jkl mno");
Pattern letters = Pattern.compile("[a-z]+");
Pattern frogs = Pattern.compile("frogs");
String str = s.findInLine(letters);
if (!str.equals("abc"))
failCount++;
if (!s.hasNext(letters))
failCount++;
try {
str = s.findInLine(frogs);
} catch (NoSuchElementException nsee) {
// Correct
}
if (!s.hasNext())
failCount++;
if (!s.hasNext(letters))
failCount++;
str = s.findInLine(letters);
if (!str.equals("def"))
failCount++;
report("Find patterns");
}
private static void findInEmptyLineTest() throws Exception {
String eol = System.getProperty("line.separator");
Scanner s = new Scanner("line 1" + eol + "" + eol + "line 3" + eol);
int lineNo = 0;
while (s.hasNextLine()) {
lineNo++;
s.findInLine("3");
s.nextLine();
}
if (lineNo != 3)
failCount++;
report("findInEmptyLine test");
}
private static void matchTest() throws Exception {
Scanner s = new Scanner("1 fish 2 fish red fish blue fish");
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
if (!result.group(1).equals("1"))
failCount++;
if (!result.group(2).equals("2"))
failCount++;
if (!result.group(3).equals("red"))
failCount++;
if (!result.group(4).equals("blue"))
failCount++;
report("Match patterns");
}
private static void skipTest() throws Exception {
Scanner s = new Scanner("abc def ghi jkl mno");
Pattern letters = Pattern.compile("[a-z]+");
Pattern spaceLetters = Pattern.compile(" [a-z]+");
Pattern frogs = Pattern.compile("frogs");
try {
s.skip(letters);
} catch (NoSuchElementException ime) {
failCount++;
}
String token = s.next(letters);
if (!token.equals("def")) {
System.out.println("expected def");
System.out.println("I found "+token);
failCount++;
}
try {
s.skip(letters);
failCount++;
} catch (NoSuchElementException ime) {
// Correct result
}
token = s.next(letters);
if (!token.equals("ghi")) {
System.out.println("expected ghi");
System.out.println("I found "+token);
failCount++;
}
try {
s.skip(letters);
failCount++;
} catch (NoSuchElementException ime) {
// Correct result because skip ignores delims
}
try {
s.skip(spaceLetters);
} catch (NoSuchElementException ime) {
failCount++;
}
token = s.next(letters);
if (!token.equals("mno")) {
System.out.println("expected mno");
System.out.println("I found "+token);
failCount++;
}
try {
s.skip(letters);
failCount++;
} catch (NoSuchElementException ime) {
// Correct result
}
report("Skip patterns");
}
private static void byteTest(int sourceType) throws Exception {
String input = " 3 0 00 b -B 012 44 -55 12 127 129 -131 dog 0x12";
Scanner s = scannerFor(input, sourceType);
if (!s.hasNextByte()) failCount++;
if (s.nextByte() != (byte)3) failCount++;
if (!s.hasNextByte()) failCount++;
if (s.nextByte() != (byte)0) failCount++;
if (!s.hasNextByte()) failCount++;
if (s.nextByte() != (byte)0) failCount++;
if (!s.hasNextByte(16)) failCount++;
if (s.nextByte(16) != (byte)11)failCount++;
if (!s.hasNextByte(16)) failCount++;
if (s.nextByte(16) != (byte)-11) failCount++;
if (!s.hasNextByte()) failCount++;
if (s.nextByte() != (byte)12) failCount++;
if (!s.hasNextByte()) failCount++;
if (s.nextByte() != (byte)44) failCount++;
if (!s.hasNextByte()) failCount++;
if (s.nextByte() != (byte)-55) failCount++;
if (!s.hasNextByte()) failCount++;
if (s.nextByte() != (byte)12) failCount++;
if (!s.hasNextByte()) failCount++;
if (s.nextByte() != (byte)127) failCount++;
if (s.hasNextByte()) failCount++;
try {
s.nextByte();
failCount++;
} catch (InputMismatchException ime) {
// Correct result
}
if (s.hasNextByte()) failCount++;
if (s.nextInt() != 129) failCount++;
if (s.hasNextByte()) failCount++;
try {
s.nextByte();
failCount++;
} catch (InputMismatchException ime) {
// Correct result
}
if (s.nextInt() != -131) failCount++;
if (s.hasNextByte()) failCount++;
try {
s.nextByte();
failCount++;
} catch (InputMismatchException ime) {
// Correct result
}
s.next(Pattern.compile("\\w+"));
if (s.hasNextByte())
failCount++;
try {
s.nextByte();
failCount++;
} catch (NoSuchElementException nsee) {
// Correct result
}
s.next();
if (s.hasNextByte())
failCount++;
try {
byte bb = s.nextByte();
failCount++;
} catch (NoSuchElementException nsee) {
// Correct result
}
report("Scan bytes");
}
private static void shortTest(int sourceType) throws Exception {
String input = " 017 22 00E -34 44,333 -53999 0x19 dog";
Scanner s = scannerFor(input, sourceType);
if (!s.hasNextShort()) failCount++;
if (s.nextShort() != (short)17) failCount++;
if (!s.hasNextShort()) failCount++;
if (s.nextShort() != (short)22) failCount++;
if (!s.hasNextShort(16)) failCount++;
if (s.nextShort(16) != (short)14) failCount++;
if (!s.hasNextShort()) failCount++;
if (s.nextShort() != (short)-34) failCount++;
for (int i=0; i<4; i++) {
if (s.hasNextShort())
failCount++;
try {
s.nextShort();
failCount++;
} catch (InputMismatchException ime) {
// Correct result
}
s.next();
}
try {
s.next();
failCount++;
} catch (InputMismatchException ime) {
failCount++;
} catch (NoSuchElementException nse) {
// Correct result
}
report("Scan shorts");
}
private static void intTest(int sourceType) throws Exception {
Scanner s = scannerFor(
"22 022 C -34 0x80000000 -2147483649 dog ", sourceType);
if (!s.hasNextInt()) failCount++;
if (s.nextInt() != 22) failCount++;
if (!s.hasNextInt()) failCount++;
if (s.nextInt() != 22) failCount++;
if (!s.hasNextInt(16)) failCount++;
if (s.nextInt(16) != 12) failCount++;
if (!s.hasNextInt()) failCount++;
if (s.nextInt() != -34) failCount++;
for (int i=0; i<3; i++) {
if (s.hasNextInt())
failCount++;
try {
s.nextInt();
failCount++;
} catch (InputMismatchException ime) {
// Correct result
}
s.next();
}
try {
s.next();
failCount++;
} catch (InputMismatchException ime) {
failCount++;
} catch (NoSuchElementException nse) {
// Correct result
}
report("Scan ints");
}
private static void longTest(int sourceType) throws Exception {
Scanner s = scannerFor(
"022 9223372036854775807 0x8000000000000000 9223372036854775808 dog ",
sourceType);
if (!s.hasNextLong()) failCount++;
if (s.nextLong() != (long)22) failCount++;
if (!s.hasNextLong()) failCount++;
if (s.nextLong() != 9223372036854775807L) failCount++;
for (int i=0; i<3; i++) {
if (s.hasNextLong())
failCount++;
try {
s.nextLong();
failCount++;
} catch (InputMismatchException ime) {
// Correct result
}
s.next();
}
try {
s.next();
failCount++;
} catch (InputMismatchException ime) {
failCount++;
} catch (NoSuchElementException nse) {
// Correct result
}
report("Scan longs");
}
private static void floatTest(int sourceType) throws Exception {
Scanner s = scannerFor(
"0 0. 0.0 2 2. 2.0 2.3 -2 -2.0 -2.3 -. 2-. 2..3", sourceType);
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != 0f) failCount++;
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != 0f) failCount++;
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != 0f) failCount++;
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != 2f) failCount++;
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != 2f) failCount++;
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != 2f) failCount++;
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != 2.3f) failCount++;
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != -2f) failCount++;
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != -2f) failCount++;
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != -2.3f) failCount++;
for (int i=0; i<3; i++) {
if (s.hasNextLong())
failCount++;
try {
s.nextFloat();
failCount++;
} catch (InputMismatchException ime) {
// Correct result
}
s.next();
}
try {
s.next();
failCount++;
} catch (InputMismatchException ime) {
failCount++;
} catch (NoSuchElementException nse) {
// Correct result
}
report("Scan floats");
}
private static void doubleTest(int sourceType) throws Exception {
Scanner s = scannerFor(
"0 0. 0.0 2 2. 2.0 2.3 -2 -2.0 -2.3 -. 2-. 2..3", sourceType);
if (!s.hasNextDouble()) failCount++;
if (s.nextDouble() != 0d) failCount++;
if (!s.hasNextDouble()) failCount++;
if (s.nextDouble() != 0d) failCount++;
if (!s.hasNextDouble()) failCount++;
if (s.nextDouble() != 0d) failCount++;
if (!s.hasNextDouble()) failCount++;
if (s.nextDouble() != 2d) failCount++;
if (!s.hasNextDouble()) failCount++;
if (s.nextDouble() != 2d) failCount++;
if (!s.hasNextDouble()) failCount++;
if (s.nextDouble() != 2d) failCount++;
if (!s.hasNextDouble()) failCount++;
if (s.nextDouble() != 2.3d) failCount++;
if (!s.hasNextDouble()) failCount++;
if (s.nextDouble() != -2d) failCount++;
if (!s.hasNextDouble()) failCount++;
if (s.nextDouble() != -2d) failCount++;
if (!s.hasNextDouble()) failCount++;
if (s.nextDouble() != -2.3d) failCount++;
for (int i=0; i<3; i++) {
if (s.hasNextLong())
failCount++;
try {
s.nextDouble();
failCount++;
} catch (InputMismatchException ime) {
// Correct result
}
s.next();
}
try {
s.next();
failCount++;
} catch (InputMismatchException ime) {
failCount++;
} catch (NoSuchElementException nse) {
// Correct result
}
report("Scan doubles");
}
private static void booleanTest(int sourceType) throws Exception {
Scanner s = scannerFor(
" true false\t \r\n true FaLse \n True Tru", sourceType);
if (!s.nextBoolean()) failCount++;
if (!s.hasNextBoolean()) failCount++;
if (s.nextBoolean()) failCount++;
if (!s.nextBoolean()) failCount++;
if (s.nextBoolean()) failCount++;
if (!s.nextBoolean()) failCount++;
if (s.hasNextBoolean()) failCount++;
try {
s.nextBoolean();
failCount++;
} catch (NoSuchElementException nsee) {
// Expected result
}
report("Scan booleans");
}
private static void hasNextTest(int sourceType) throws Exception {
Scanner s = scannerFor(
" blah blech\t blather alongblatherindeed", sourceType);
if (!s.hasNext()) failCount++;
if (!s.hasNext()) failCount++;
String result = s.next();
if (!result.equals("blah")) failCount++;
if (!s.hasNext()) failCount++;
if (!s.hasNext()) failCount++;
result = s.next();
if (!result.equals("blech")) failCount++;
if (!s.hasNext()) failCount++;
result = s.next();
if (!result.equals("blather")) failCount++;
if (!s.hasNext()) failCount++;
if (!s.hasNext()) failCount++;
result = s.next();
if (!result.equals("alongblatherindeed")) failCount++;
if (s.hasNext()) failCount++;
try {
result = s.next();
failCount++;
} catch (NoSuchElementException nsee) {
// Correct result
}
report("Has next test");
}
private static void nextTest(int sourceType) throws Exception {
Scanner s = scannerFor(
" blah blech\t blather alongblatherindeed", sourceType);
String result = (String)s.next();
if (!result.equals("blah")) failCount++;
result = (String)s.next();
if (!result.equals("blech")) failCount++;
result = (String)s.next();
if (!result.equals("blather")) failCount++;
result = (String)s.next();
if (!result.equals("alongblatherindeed"))
failCount++;
try {
result = (String)s.next();
failCount++;
} catch (NoSuchElementException nsee) {
// Correct result
}
report("Next test");
}
private static void hasNextPatternTest(int sourceType) throws Exception {
Scanner s = scannerFor(
" blah blech\t blather alongblatherindeed", sourceType);
Pattern p1 = Pattern.compile("\\w+");
Pattern p2 = Pattern.compile("blech");
if (!s.hasNext(p1)) failCount++;
if (!s.hasNext(p1)) failCount++;
if (s.hasNext(p2)) failCount++;
String result = (String)s.next();
if (!result.equals("blah")) failCount++;
if (!s.hasNext(p1)) failCount++;
if (!s.hasNext(p2)) failCount++;
result = (String)s.next();
if (!result.equals("blech")) failCount++;
if (!s.hasNext(p1)) failCount++;
if (s.hasNext(p2)) failCount++;
result = (String)s.next();
if (!result.equals("blather")) failCount++;
if (!s.hasNext(p1)) failCount++;
if (s.hasNext(p2)) failCount++;
result = (String)s.next();
if (!result.equals("alongblatherindeed")) failCount++;
if (s.hasNext(p1)) failCount++;
if (s.hasNext(p2)) failCount++;
report("Has Next Pattern test");
}
private static void nextPatternTest(int sourceType) throws Exception {
Scanner s = scannerFor(
" blah blech\t blather alongblatherindeed", sourceType);
Pattern p1 = Pattern.compile("blah");
Pattern p2 = Pattern.compile("blech");
Pattern p3 = Pattern.compile("blather");
Pattern p4 = Pattern.compile("alongblatherindeed");
String result = null;
try {
result = (String)s.next(p2);
failCount++;
} catch (NoSuchElementException nsee) {
// Correct result
}
result = (String)s.next(p1);
if (!result.equals("blah"))
failCount++;
try {
result = (String)s.next(p1);
failCount++;
} catch (NoSuchElementException nsee) {
// Correct result
}
result = (String)s.next(p2);
if (!result.equals("blech"))
failCount++;
try {
result = (String)s.next(p4);
failCount++;
} catch (NoSuchElementException nsee) {
// Correct result
}
result = (String)s.next(p3);
if (!result.equals("blather"))
failCount++;
try {
result = (String)s.next(p3);
failCount++;
} catch (NoSuchElementException nsee) {
// Correct result
}
result = (String)s.next(p4);
if (!result.equals("alongblatherindeed"))
failCount++;
try {
result = (String)s.next();
failCount++;
} catch (NoSuchElementException nsee) {
// Correct result
}
report("Next pattern test");
}
private static void useLocaleTest() throws Exception {
Scanner s = new Scanner("334.65").useLocale(Locale.ENGLISH);
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != 334.65f) failCount++;
s = new Scanner("334,65").useLocale(Locale.FRENCH);
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != 334.65f) failCount++;
s = new Scanner("4.334,65").useLocale(Locale.GERMAN);
if (!s.hasNextFloat()) failCount++;
if (s.nextFloat() != 4334.65f) failCount++;
// Test case reported from India
try {
String Message = "123978.90 $";
Locale locale = new Locale("hi","IN");
NumberFormat form = NumberFormat.getInstance(locale);
double myNumber = 1902.09;
Scanner scanner = new Scanner(form.format(myNumber).toString());
scanner.useLocale(locale);
double d = scanner.nextDouble();
} catch (InputMismatchException ime) {
failCount++;
}
report("Use locale test");
}
public static void resetTest() throws Exception {
Scanner sc = new Scanner("");
int radix = sc.radix();
Locale locale = sc.locale();
Pattern delimiter = sc.delimiter();
Pattern a = Pattern.compile("A");
sc.useDelimiter(a);
Locale dummy = new Locale("en", "US", "dummy");
sc.useLocale(dummy);
sc.useRadix(16);
if (sc.radix() != 16 ||
!sc.locale().equals(dummy) ||
!sc.delimiter().pattern().equals(a.pattern())) {
failCount++;
} else {
sc.reset();
if (sc.radix() != radix ||
!sc.locale().equals(locale) ||
!sc.delimiter().pattern().equals(delimiter.pattern())) {
failCount++;
}
}
sc.close();
report("Reset test");
}
static List<BiConsumer <Scanner, Integer>> methodWRList = Arrays.asList(
(s, r) -> s.hasNextByte(r),
(s, r) -> s.nextByte(r),
(s, r) -> s.hasNextShort(r),
(s, r) -> s.nextShort(r),
(s, r) -> s.hasNextInt(r),
(s, r) -> s.nextInt(r),
(s, r) -> s.hasNextLong(r),
(s, r) -> s.nextLong(r),
(s, r) -> s.hasNextBigInteger(r),
(s, r) -> s.nextBigInteger(r)
);
/*
* Test that setting the radix to an out of range value triggers
* an IllegalArgumentException
*/
public static void outOfRangeRadixTest() throws Exception {
int[] bad = new int[] { -1, 0, 1, 37, 38 };
int[] good = IntStream.rangeClosed(Character.MIN_RADIX, Character.MAX_RADIX)
.toArray();
methodWRList.stream().forEach( m -> {
for (int r : bad) {
try (Scanner sc = new Scanner("10 10 10 10")) {
m.accept(sc, r);
failCount++;
} catch (IllegalArgumentException ise) {}
}
});
methodWRList.stream().forEach( m -> {
for (int r : good) {
try (Scanner sc = new Scanner("10 10 10 10")) {
m.accept(sc, r);
} catch (Exception x) {
failCount++;
}
}
});
report("Radix out of range test");
}
/*
* Test that closing the stream also closes the underlying Scanner.
* The cases of attempting to open streams on a closed Scanner are
* covered by closeTest().
*/
public static void streamCloseTest() throws Exception {
Scanner sc;
Scanner sc1 = new Scanner("xyzzy");
sc1.tokens().close();
try {
sc1.hasNext();
failCount++;
} catch (IllegalStateException ise) {
// Correct result
}
Scanner sc2 = new Scanner("a b c d e f");
try {
sc2.tokens()
.peek(s -> sc2.close())
.count();
} catch (IllegalStateException ise) {
// Correct result
}
Scanner sc3 = new Scanner("xyzzy");
sc3.findAll("q").close();
try {
sc3.hasNext();
failCount++;
} catch (IllegalStateException ise) {
// Correct result
}
try (Scanner sc4 = new Scanner(inputFile)) {
sc4.findAll("[0-9]+")
.peek(s -> sc4.close())
.count();
failCount++;
} catch (IllegalStateException ise) {
// Correct result
}
report("Streams Close test");
}
/*
* Test ConcurrentModificationException
*/
public static void streamComodTest() {
try {
Scanner sc = new Scanner("a b c d e f");
sc.tokens()
.peek(s -> sc.hasNext())
.count();
failCount++;
} catch (ConcurrentModificationException cme) {
// Correct result
}
try {
Scanner sc = new Scanner("a b c d e f");
Iterator<String> it = sc.tokens().iterator();
it.next();
sc.next();
it.next();
failCount++;
} catch (ConcurrentModificationException cme) {
// Correct result
}
try {
String input = IntStream.range(0, 100)
.mapToObj(String::valueOf)
.collect(Collectors.joining(" "));
Scanner sc = new Scanner(input);
sc.findAll("[0-9]+")
.peek(s -> sc.hasNext())
.count();
failCount++;
} catch (ConcurrentModificationException cme) {
// Correct result
}
try {
String input = IntStream.range(0, 100)
.mapToObj(String::valueOf)
.collect(Collectors.joining(" "));
Scanner sc = new Scanner(input);
Iterator<MatchResult> it = sc.findAll("[0-9]+").iterator();
it.next();
sc.next();
it.next();
failCount++;
} catch (ConcurrentModificationException cme) {
// Correct result
}
report("Streams Comod test");
}
private static void report(String testName) {
System.err.printf("%-30s: %s%n", testName,
(failCount == 0) ? "Passed" : String.format("Failed(%d)", failCount));
if (failCount > 0)
failure = true;
failCount = 0;
}
static Scanner scannerFor(String input, int sourceType) {
if (sourceType == 1)
return new Scanner(input);
else
return new Scanner(new StutteringInputStream(input));
}
static class ThrowingReadable implements Readable {
ThrowingReadable() {
}
public int read(java.nio.CharBuffer cb) throws IOException {
throw new IOException("ThrowingReadable always throws");
}
}
}
|