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 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
|
// StringTest.cs - NUnit Test Cases for the System.String class
//
// Authors:
// Jeffrey Stedfast <fejj@ximian.com>
// David Brandt <bucky@keystreams.com>
// Kornél Pál <http://www.kornelpal.hu/>
//
// (C) Ximian, Inc. http://www.ximian.com
// Copyright (C) 2006 Kornél Pál
// Copyright (C) 2006 Novell (http://www.novell.com)
//
using NUnit.Framework;
using System;
using System.Text;
using System.Globalization;
namespace MonoTests.System
{
[TestFixture]
public class StringTest : Assertion
{
[Test]
public unsafe void CharArrayConstructor ()
{
AssertEquals ("char[]", String.Empty, new String ((char[]) null));
AssertEquals (String.Empty, new String (new Char [0]));
AssertEquals ("A", new String (new Char [1] {'A'}));
}
[Test]
public void CharArrayIntIntConstructor ()
{
char[] arr = new char [3] { 'A', 'B', 'C' };
AssertEquals ("BC", new String (arr, 1, 2));
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void CharArray_Null_IntIntConstructor ()
{
new String ((char[])null, 0, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void CharArrayInt_Negative_IntConstructor ()
{
char[] arr = new char [3] { 'A', 'B', 'C' };
new String (arr, -1, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void CharArrayIntInt_Negative_Constructor ()
{
char[] arr = new char [3] { 'A', 'B', 'C' };
new String (arr, 0, -1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void CharArrayIntInt_TooLong_Constructor ()
{
char[] arr = new char [3] { 'A', 'B', 'C' };
new String (arr, 1, 3);
}
[Test]
public void CharIntConstructor ()
{
AssertEquals ("", new String ('A', 0));
AssertEquals ("AAA", new String ('A', 3));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void CharInt_NegativeConstructor ()
{
new String ('A', -1);
}
[Test]
public unsafe void CharPtrConstructor ()
{
AssertEquals ("char*", String.Empty, new String ((char*) null));
AssertEquals ("char*,int,int", String.Empty, new String ((char*) null, 0, 0));
}
public unsafe void TestSbytePtrConstructorASCII ()
{
Encoding encoding = Encoding.ASCII;
String s = "ASCII*\0";
byte[] bytes = encoding.GetBytes (s);
fixed (byte* bytePtr = bytes)
AssertEquals (s, new String ((sbyte*) bytePtr, 0, bytes.Length, encoding));
}
public unsafe void TestSbytePtrConstructorDefault ()
{
Encoding encoding = Encoding.Default;
byte [] bytes = new byte [256];
for (int i = 0; i < 255; i++)
bytes [i] = (byte) (i + 1);
bytes [255] = (byte) 0;
// Ensure that bytes are valid for Encoding.Default
bytes = encoding.GetBytes (encoding.GetChars (bytes));
String s = encoding.GetString(bytes);
// Ensure null terminated array
bytes [bytes.Length - 1] = (byte) 0;
fixed (byte* bytePtr = bytes)
{
AssertEquals (s.Substring (0, s.Length - 1), new String ((sbyte*) bytePtr));
AssertEquals (s, new String ((sbyte*) bytePtr, 0, bytes.Length));
AssertEquals (s, new String ((sbyte*) bytePtr, 0, bytes.Length, null));
AssertEquals (s, new String ((sbyte*) bytePtr, 0, bytes.Length, encoding));
}
}
public unsafe void TestSbytePtrConstructorNull1 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null));
}
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public unsafe void TestSbytePtrConstructorNull2 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 0, 0));
}
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public unsafe void TestSbytePtrConstructorNull3 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 0, 1));
}
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public unsafe void TestSbytePtrConstructorNull4 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 1, 0));
}
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public unsafe void TestSbytePtrConstructorNull5 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 0, 0, null));
}
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public unsafe void TestSbytePtrConstructorNull6 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 0, 1, null));
}
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public unsafe void TestSbytePtrConstructorNull7 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 1, 0, null));
}
public unsafe void TestSbytePtrConstructorNull8 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 0, 0, Encoding.Default));
}
#if NET_2_0
[ExpectedException (typeof (ArgumentOutOfRangeException))]
#else
[ExpectedException (typeof (NullReferenceException))]
#endif
public unsafe void TestSbytePtrConstructorNull9 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 0, 1, Encoding.Default));
}
public unsafe void TestSbytePtrConstructorNull10 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 1, 0, Encoding.Default));
}
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorInvalid1 ()
{
AssertEquals (String.Empty, new String ((sbyte*) (-1)));
}
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorInvalid2 ()
{
AssertEquals (String.Empty, new String ((sbyte*) (-1), 0, 1));
}
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorInvalid3 ()
{
AssertEquals (String.Empty, new String ((sbyte*) (-1), 0, 1, null));
}
#if NET_2_0
[Ignore ("Runtime throws NullReferenceException instead of AccessViolationException")]
[ExpectedException (typeof (AccessViolationException))]
#else
[ExpectedException (typeof (NullReferenceException))]
#endif
public unsafe void TestSbytePtrConstructorInvalid4 ()
{
AssertEquals (String.Empty, new String ((sbyte*) (-1), 0, 1, Encoding.Default));
}
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorNegative1 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, -1, 0));
}
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorNegative2 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 0, -1));
}
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorNegative3 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, -1, 0, null));
}
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorNegative4 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 0, -1, null));
}
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorNegative5 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, -1, 0, Encoding.Default));
}
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorNegative6 ()
{
AssertEquals (String.Empty, new String ((sbyte*) null, 0, -1, Encoding.Default));
}
[Ignore ("Blocked by mcs bug 78899")]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorOverflow1 ()
{
AssertEquals (String.Empty, new String ((sbyte*) (-1), 1, 0));
}
[Ignore ("Blocked by mcs bug 78899")]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorOverflow2 ()
{
AssertEquals (String.Empty, new String ((sbyte*) (-1), 1, 1));
}
[Ignore ("Blocked by mcs bug 78899")]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorOverflow3 ()
{
AssertEquals (String.Empty, new String ((sbyte*) (-1), 1, 0, null));
}
[Ignore ("Blocked by mcs bug 78899")]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorOverflow4 ()
{
AssertEquals (String.Empty, new String ((sbyte*) (-1), 1, 1, null));
}
[Ignore ("Blocked by mcs bug 78899")]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorOverflow5 ()
{
AssertEquals (String.Empty, new String ((sbyte*) (-1), 1, 0, Encoding.Default));
}
[Ignore ("Blocked by mcs bug 78899")]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public unsafe void TestSbytePtrConstructorOverflow6 ()
{
AssertEquals (String.Empty, new String ((sbyte*) (-1), 1, 1, Encoding.Default));
}
public void TestLength ()
{
string str = "test string";
AssertEquals("wrong length", 11, str.Length);
}
[Test]
// http://bugzilla.ximian.com/show_bug.cgi?id=70478
public void CompareNotWorking ()
{
AssertEquals ("A03", String.Compare ("A", "a"), 1);
AssertEquals ("A04", String.Compare ("a", "A"), -1);
}
[Test]
public void CompareNotWorking2 ()
{
string needle = "ab";
string haystack = "abbcbacab";
AssertEquals("basic substring check #9", 0,
String.Compare(needle, 0, haystack, 0, 2, false));
for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
if (i != 7) {
AssertEquals("loop substring check #8/" + i, -1, String.Compare(needle, 0, haystack, i, 2, false));
}
}
}
[Test]
public void Compare ()
{
string lesser = "abc";
string medium = "abcd";
string greater = "xyz";
string caps = "ABC";
AssertEquals(0, String.Compare(null, null));
AssertEquals(1, String.Compare(lesser, null));
Assert (String.Compare (lesser, greater) < 0);
Assert (String.Compare (greater, lesser) > 0);
Assert (String.Compare (lesser, lesser) == 0);
Assert (String.Compare (lesser, medium) < 0);
Assert (String.Compare (lesser, caps, true) == 0);
Assert (String.Compare (lesser, caps, false) != 0);
AssertEquals ("A01", String.Compare ("a", "b"), -1);
AssertEquals ("A02", String.Compare ("b", "a"), 1);
// TODO - test with CultureInfo
string needle = "ab";
string haystack = "abbcbacab";
AssertEquals("basic substring check #1", 0,
String.Compare(needle, 0, haystack, 0, 2));
AssertEquals("basic substring check #2", -1,
String.Compare(needle, 0, haystack, 0, 3));
AssertEquals("basic substring check #3", 0,
String.Compare("ab", 0, "ab", 0, 2));
AssertEquals("basic substring check #4", 0,
String.Compare("ab", 0, "ab", 0, 3));
AssertEquals("basic substring check #5", 0,
String.Compare("abc", 0, "ab", 0, 2));
AssertEquals("basic substring check #6", 1,
String.Compare("abc", 0, "ab", 0, 5));
AssertEquals("basic substring check #7", -1,
String.Compare("ab", 0, "abc", 0, 5));
for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
if (i != 7) {
Assert("loop substring check #1/" + i, String.Compare(needle, 0, haystack, i, 2) != 0);
Assert("loop substring check #2/" + i, String.Compare(needle, 0, haystack, i, 3) != 0);
} else {
AssertEquals("loop substring check #3/" + i, 0, String.Compare(needle, 0, haystack, i, 2));
AssertEquals("loop substring check #4/" + i, 0, String.Compare(needle, 0, haystack, i, 3));
}
}
needle = "AB";
AssertEquals("basic substring check #8", 0,
String.Compare(needle, 0, haystack, 0, 2, true));
for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
if (i != 7) {
Assert("loop substring check #5/" + i, String.Compare(needle, 0, haystack, i, 2, true) != 0);
Assert("loop substring check #6/" + i, String.Compare(needle, 0, haystack, i, 2, false) != 0);
} else {
AssertEquals("loop substring check #7/" + i, 0, String.Compare(needle, 0, haystack, i, 2, true));
}
}
AssertEquals ("Compare with 0 length", 0, String.Compare (needle, 0, haystack, 0, 0));
// TODO - extended format call with CultureInfo
}
public void TestCompareOrdinal ()
{
string lesser = "abc";
string medium = "abcd";
string greater = "xyz";
AssertEquals(0, String.CompareOrdinal(null, null));
AssertEquals(1, String.CompareOrdinal(lesser, null));
Assert ("#1", String.CompareOrdinal (lesser, greater) < 0);
Assert ("#2", String.CompareOrdinal (greater, lesser) > 0);
Assert ("#3", String.CompareOrdinal (lesser, lesser) == 0);
Assert ("#4", String.CompareOrdinal (lesser, medium) < 0);
string needle = "ab";
string haystack = "abbcbacab";
AssertEquals("basic substring check", 0,
String.CompareOrdinal(needle, 0, haystack, 0, 2));
AssertEquals("basic substring miss", -1,
String.CompareOrdinal(needle, 0, haystack, 0, 3));
for (int i = 1; i <= (haystack.Length - needle.Length); i++) {
if (i != 7) {
Assert("loop substring check " + i, String.CompareOrdinal(needle, 0, haystack, i, 2) != 0);
Assert("loop substring check " + i, String.CompareOrdinal(needle, 0, haystack, i, 3) != 0);
} else {
AssertEquals("loop substring check " + i, 0, String.CompareOrdinal(needle, 0, haystack, i, 2));
AssertEquals("loop substring check " + i, 0, String.CompareOrdinal(needle, 0, haystack, i, 3));
}
}
}
public void TestCompareTo ()
{
string lower = "abc";
string greater = "xyz";
string lesser = "abc";
Assert (lower.CompareTo (greater) < 0);
Assert (lower.CompareTo (lower) == 0);
Assert (greater.CompareTo (lesser) > 0);
}
class WeirdToString
{
public override string ToString () { return null; }
}
public void TestConcat ()
{
string string1 = "string1";
string string2 = "string2";
string concat = "string1string2";
Assert (String.Concat (string1, string2) == concat);
AssertEquals (string1, String.Concat (string1, null));
AssertEquals (string1, String.Concat (null, string1));
AssertEquals ("", String.Concat (null, null));
WeirdToString wts = new WeirdToString ();
AssertEquals (string1, String.Concat (string1, wts));
AssertEquals (string1, String.Concat (wts, string1));
AssertEquals ("", String.Concat (wts, wts));
string [] allstr = new string []{ string1, null, string2, concat };
object [] allobj = new object []{ string1, null, string2, concat };
string astr = String.Concat (allstr);
AssertEquals ("string1string2string1string2", astr);
string ostr = String.Concat (allobj);
AssertEquals (astr, ostr);
}
public void TestCopy()
{
string s1 = "original";
string s2 = String.Copy(s1);
AssertEquals("String copy no good", s1, s2);
bool errorThrown = false;
try {
string s = String.Copy(null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null copy shouldn't be good", errorThrown);
}
public void TestCopyTo()
{
string s1 = "original";
bool errorThrown = false;
try {
s1.CopyTo(0, (char[])null, 0, s1.Length);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null CopyTo shouldn't be good", errorThrown);
char[] c1 = new char[s1.Length];
string s2 = new String(c1);
Assert("char string not bad to start", !s1.Equals(s2));
for (int i = 0; i < s1.Length; i++) {
s1.CopyTo(i, c1, i, 1);
}
s2 = new String(c1);
AssertEquals("char-by-char copy bad", s1, s2);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void CopyTo_SourceIndexOverflow ()
{
char[] dest = new char [4];
"Mono".CopyTo (Int32.MaxValue, dest, 0, 4);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void CopyTo_DestinationIndexOverflow ()
{
char[] dest = new char [4];
"Mono".CopyTo (0, dest, Int32.MaxValue, 4);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void CopyTo_CountOverflow ()
{
char[] dest = new char [4];
"Mono".CopyTo (0, dest, 0, Int32.MaxValue);
}
[Test]
public void EndsWith()
{
string s1 = "original";
bool errorThrown = false;
try {
bool huh = s1.EndsWith(null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null EndsWith shouldn't be good", errorThrown);
Assert("should match", s1.EndsWith("l"));
Assert("should match 2", s1.EndsWith("inal"));
Assert("should fail", !s1.EndsWith("ina"));
Assert ("should match 3", s1.EndsWith (""));
}
public void TestEquals()
{
string s1 = "original";
string yes = "original";
object y = yes;
string no = "copy";
string s1s1 = s1 + s1;
Assert("No match for null", !s1.Equals(null));
Assert("Should match object", s1.Equals(y));
Assert("Should match", s1.Equals(yes));
Assert("Shouldn't match", !s1.Equals(no));
Assert("Static nulls should match", String.Equals(null, null));
Assert("Should match", String.Equals(s1, yes));
Assert("Shouldn't match", !String.Equals(s1, no));
AssertEquals ("Equals (object)", false, s1s1.Equals (y));
}
public void TestFormat ()
{
AssertEquals ("Empty format string.", "", String.Format ("", 0));
AssertEquals ("Single argument.", "100", String.Format ("{0}", 100));
AssertEquals ("Single argument, right justified.", "X 37X", String.Format ("X{0,5}X", 37));
AssertEquals ("Single argument, left justified.", "X37 X", String.Format ("X{0,-5}X", 37));
AssertEquals ("Whitespace in specifier", " 7d", String.Format ("{0, 4:x}", 125));
AssertEquals ("Two arguments.", "The 3 wise men.", String.Format ("The {0} wise {1}.", 3, "men"));
AssertEquals ("Three arguments.", "do re me fa so.", String.Format ("{0} re {1} fa {2}.", "do", "me", "so"));
AssertEquals ("Formatted argument.", "###00c0ffee#", String.Format ("###{0:x8}#", 0xc0ffee));
AssertEquals ("Formatted argument, right justified.", "# 033#", String.Format ("#{0,5:x3}#", 0x33));
AssertEquals ("Formatted argument, left justified.", "#033 #", String.Format ("#{0,-5:x3}#", 0x33));
AssertEquals ("Escaped bracket", "typedef struct _MonoObject { ... } MonoObject;", String.Format ("typedef struct _{0} {{ ... }} MonoObject;", "MonoObject"));
AssertEquals
("With Slash", "Could not find file \"a/b\"", String.Format ("Could not find file \"{0}\"", "a/b"));
AssertEquals
("With BackSlash", "Could not find file \"a\\b\"", String.Format ("Could not find file \"{0}\"", "a\\b"));
// TODO test format exceptions
// TODO test argument null exceptions
// This should work, but it doesn't currently.
// I think I broke the spec...
//bool errorThrown = false;
//try {
//string s = String.Format(null, " ");
//} catch (ArgumentNullException) {
//errorThrown = true;
//}
//Assert("error not thrown 1", errorThrown);
//errorThrown = false;
//try {
//string s = String.Format(null, " ", " ");
//} catch (ArgumentNullException) {
//errorThrown = true;
//}
//Assert("error not thrown 2", errorThrown);
//errorThrown = false;
//try {
//string s = String.Format(" ", null);
//} catch (ArgumentNullException) {
//errorThrown = true;
//}
//Assert("error not thrown 3", errorThrown);
}
public void TestGetEnumerator()
{
string s1 = "original";
char[] c1 = new char[s1.Length];
string s2 = new String(c1);
Assert("pre-enumerated string should not match", !s1.Equals(s2));
CharEnumerator en = s1.GetEnumerator();
AssertNotNull("null enumerator", en);
for (int i = 0; i < s1.Length; i++) {
en.MoveNext();
c1[i] = en.Current;
}
s2 = new String(c1);
AssertEquals("enumerated string should match", s1, s2);
}
public void TestGetHashCode()
{
string s1 = "original";
// TODO - weak test, currently. Just verifies determinicity.
AssertEquals("same string, same hash code",
s1.GetHashCode(), s1.GetHashCode());
}
public void TestGetType() {
string s1 = "original";
AssertEquals("String type", "System.String", s1.GetType().ToString());
}
public void TestGetTypeCode() {
string s1 = "original";
Assert(s1.GetTypeCode().Equals(TypeCode.String));
}
public void TestIndexOf() {
string s1 = "original";
bool errorThrown = false;
try {
int i = s1.IndexOf('q', s1.Length + 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range error for char", errorThrown);
errorThrown = false;
try {
int i = s1.IndexOf('q', s1.Length + 1, 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range error for char", errorThrown);
errorThrown = false;
try {
int i = s1.IndexOf("huh", s1.Length + 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range for string", errorThrown);
errorThrown = false;
try {
int i = s1.IndexOf("huh", s1.Length + 1, 3);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range for string", errorThrown);
errorThrown = false;
try {
int i = s1.IndexOf(null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null string error", errorThrown);
errorThrown = false;
try {
int i = s1.IndexOf(null, 0);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null string error", errorThrown);
errorThrown = false;
try {
int i = s1.IndexOf(null, 0, 1);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null string error", errorThrown);
AssertEquals("basic char index", 1, s1.IndexOf('r'));
AssertEquals("basic char index 2", 2, s1.IndexOf('i'));
AssertEquals("basic char index - no", -1, s1.IndexOf('q'));
AssertEquals("basic string index", 1, s1.IndexOf("rig"));
AssertEquals("basic string index 2", 2, s1.IndexOf("i"));
AssertEquals("basic string index 3", 0, "".IndexOf(""));
AssertEquals("basic string index 4", 0, "ABC".IndexOf(""));
AssertEquals("basic string index - no", -1, s1.IndexOf("rag"));
AssertEquals("stepped char index", 1, s1.IndexOf('r', 1));
AssertEquals("stepped char index 2", 2, s1.IndexOf('i', 1));
AssertEquals("stepped char index 3", 4, s1.IndexOf('i', 3));
AssertEquals("stepped char index 4", -1, s1.IndexOf('i', 5));
AssertEquals("stepped char index 5", -1, s1.IndexOf('l', s1.Length));
AssertEquals("stepped limited char index",
1, s1.IndexOf('r', 1, 1));
AssertEquals("stepped limited char index",
-1, s1.IndexOf('r', 0, 1));
AssertEquals("stepped limited char index",
2, s1.IndexOf('i', 1, 3));
AssertEquals("stepped limited char index",
4, s1.IndexOf('i', 3, 3));
AssertEquals("stepped limited char index",
-1, s1.IndexOf('i', 5, 3));
s1 = "original original";
AssertEquals("stepped string index 1",
0, s1.IndexOf("original", 0));
AssertEquals("stepped string index 2",
9, s1.IndexOf("original", 1));
AssertEquals("stepped string index 3",
-1, s1.IndexOf("original", 10));
AssertEquals("stepped string index 4",
3, s1.IndexOf("", 3));
AssertEquals("stepped limited string index 1",
1, s1.IndexOf("rig", 0, 5));
AssertEquals("stepped limited string index 2",
-1, s1.IndexOf("rig", 0, 3));
AssertEquals("stepped limited string index 3",
10, s1.IndexOf("rig", 2, 15));
AssertEquals("stepped limited string index 4",
-1, s1.IndexOf("rig", 2, 3));
AssertEquals("stepped limited string index 5",
2, s1.IndexOf("", 2, 3));
string s2 = "QBitArray::bitarr_data";
AssertEquals ("bug #62160", 9, s2.IndexOf ("::"));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void IndexOf_Char_StartIndexOverflow ()
{
"Mono".IndexOf ('o', Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void IndexOf_Char_LengthOverflow ()
{
"Mono".IndexOf ('o', 1, Int32.MaxValue);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void IndexOf_String_StartIndexOverflow ()
{
"Mono".IndexOf ("no", Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void IndexOf_String_LengthOverflow ()
{
"Mono".IndexOf ("no", 1, Int32.MaxValue);
}
#if NET_2_0
public void IndexOfStringComparison ()
{
string text = "testing123456";
string text2 = "123";
string text3 = "NG";
AssertEquals ("#1", 7, text.IndexOf (text2, StringComparison.Ordinal));
AssertEquals ("#2", 5, text.IndexOf (text3, StringComparison.OrdinalIgnoreCase));
}
#endif
public void TestIndexOfAny() {
string s1 = "abcdefghijklm";
bool errorThrown = false;
try {
int i = s1.IndexOfAny(null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null char[] error", errorThrown);
errorThrown = false;
try {
int i = s1.IndexOfAny(null, 0);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null char[] error", errorThrown);
errorThrown = false;
try {
int i = s1.IndexOfAny(null, 0, 1);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null char[] error", errorThrown);
char[] c1 = {'a', 'e', 'i', 'o', 'u'};
AssertEquals("first vowel", 0, s1.IndexOfAny(c1));
AssertEquals("second vowel", 4, s1.IndexOfAny(c1, 1));
AssertEquals("out of vowels", -1, s1.IndexOfAny(c1, 9));
AssertEquals("second vowel in range",
4, s1.IndexOfAny(c1, 1, 4));
AssertEquals("second vowel out of range",
-1, s1.IndexOfAny(c1, 1, 3));
errorThrown = false;
try {
int i = s1.IndexOfAny(c1, s1.Length+1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("Out of range error", errorThrown);
errorThrown = false;
try {
int i = s1.IndexOfAny(c1, s1.Length+1, 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("Out of range error", errorThrown);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void IndexOfAny_StartIndexOverflow ()
{
"Mono".IndexOfAny (new char [1] { 'o' }, Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void IndexOfAny_LengthOverflow ()
{
"Mono".IndexOfAny (new char [1] { 'o' }, 1, Int32.MaxValue);
}
#if NET_2_0
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Contains_Null () {
"ABC".Contains (null);
}
[Test]
public void TestContains () {
Assert ("ABC".Contains (""));
Assert ("ABC".Contains ("ABC"));
Assert ("ABC".Contains ("AB"));
Assert (!"ABC".Contains ("AD"));
}
[Test]
public void TestIsNullOrEmpty () {
Assert (String.IsNullOrEmpty (null));
Assert (String.IsNullOrEmpty (String.Empty));
Assert (String.IsNullOrEmpty (""));
Assert (!String.IsNullOrEmpty ("A"));
}
#endif
public void TestInsert() {
string s1 = "original";
bool errorThrown = false;
try {
string result = s1.Insert(0, null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("Null arg error", errorThrown);
errorThrown = false;
try {
string result = s1.Insert(s1.Length+1, "Hi!");
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("Out of range error", errorThrown);
AssertEquals("front insert",
"Hi!original", s1.Insert(0, "Hi!"));
AssertEquals("back insert",
"originalHi!", s1.Insert(s1.Length, "Hi!"));
AssertEquals("middle insert",
"origHi!inal", s1.Insert(4, "Hi!"));
}
public void TestIntern() {
bool errorThrown = false;
try {
string s = String.Intern(null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null arg error", errorThrown);
string s1 = "original";
AssertEquals("One string's reps are both the same",
String.Intern(s1), String.Intern(s1));
string s2 = "originally";
Assert("Different strings, different reps",
String.Intern(s1) != String.Intern(s2));
}
public void TestIsInterned() {
bool errorThrown = false;
try {
string s = String.IsInterned(null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null arg error", errorThrown);
// FIXME - it seems like this should work, but no.
// I don't know how it's possible to get a null
// returned from IsInterned.
//Assert("no intern for regular string",
//String.IsInterned("original") == null);
string s1 = "original";
AssertEquals("is interned", s1, String.IsInterned(s1));
}
public void TestJoin() {
bool errorThrown = false;
try {
string s = String.Join(" ", null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null arg error", errorThrown);
string[] chunks = {"this", "is", "a", "test"};
AssertEquals("Basic join", "this is a test",
String.Join(" ", chunks));
AssertEquals("Basic join", "this.is.a.test",
String.Join(".", chunks));
AssertEquals("Subset join", "is a",
String.Join(" ", chunks, 1, 2));
AssertEquals("Subset join", "is.a",
String.Join(".", chunks, 1, 2));
AssertEquals("Subset join", "is a test",
String.Join(" ", chunks, 1, 3));
errorThrown = false;
try {
string s = String.Join(" ", chunks, 2, 3);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range error", errorThrown);
}
[Test]
public void Join_SeparatorNull ()
{
string[] chunks = {"this", "is", "a", "test"};
AssertEquals ("SeparatorNull", "thisisatest", String.Join (null, chunks));
}
[Test]
public void Join_ValuesNull ()
{
string[] chunks1 = {null, "is", "a", null};
AssertEquals ("SomeNull", " is a ", String.Join (" ", chunks1));
string[] chunks2 = {null, "is", "a", null};
AssertEquals ("Some+Sep=Null", "isa", String.Join (null, chunks2));
string[] chunks3 = {null, null, null, null};
AssertEquals ("AllValuesNull", " ", String.Join (" ", chunks3));
}
[Test]
public void Join_AllNull ()
{
string[] chunks = {null, null, null};
AssertEquals ("AllNull", "", String.Join (null, chunks));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Join_StartIndexNegative ()
{
string[] values = { "Mo", "no" };
String.Join ("o", values, -1, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Join_StartIndexOverflow ()
{
string[] values = { "Mo", "no" };
String.Join ("o", values, Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Join_LengthNegative ()
{
string[] values = { "Mo", "no" };
String.Join ("o", values, 1, -1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Join_LengthOverflow ()
{
string[] values = { "Mo", "no" };
String.Join ("o", values, 1, Int32.MaxValue);
}
public void TestLastIndexOf() {
string s1 = "original";
bool errorThrown = false;
try {
int i = s1.LastIndexOf('q', -1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range error for char", errorThrown);
errorThrown = false;
try {
int i = s1.LastIndexOf('q', -1, 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range error for char", errorThrown);
errorThrown = false;
try {
int i = s1.LastIndexOf("huh", s1.Length + 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range for string", errorThrown);
errorThrown = false;
try {
int i = s1.LastIndexOf("huh", s1.Length + 1, 3);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range for string", errorThrown);
errorThrown = false;
try {
int i = s1.LastIndexOf(null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null string error", errorThrown);
errorThrown = false;
try {
int i = s1.LastIndexOf(null, 0);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null string error", errorThrown);
errorThrown = false;
try {
int i = s1.LastIndexOf(null, 0, 1);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null string error", errorThrown);
AssertEquals("basic char index", 1, s1.LastIndexOf('r'));
AssertEquals("basic char index", 4, s1.LastIndexOf('i'));
AssertEquals("basic char index - no", -1, s1.LastIndexOf('q'));
AssertEquals("basic string index", 7, s1.LastIndexOf(""));
AssertEquals("basic string index", 1, s1.LastIndexOf("rig"));
AssertEquals("basic string index", 4, s1.LastIndexOf("i"));
AssertEquals("basic string index - no", -1,
s1.LastIndexOf("rag"));
AssertEquals("stepped char index", 1,
s1.LastIndexOf('r', s1.Length-1));
AssertEquals("stepped char index", 4,
s1.LastIndexOf('i', s1.Length-1));
AssertEquals("stepped char index", 2,
s1.LastIndexOf('i', 3));
AssertEquals("stepped char index", -1,
s1.LastIndexOf('i', 1));
AssertEquals("stepped limited char index",
1, s1.LastIndexOf('r', 1, 1));
AssertEquals("stepped limited char index",
-1, s1.LastIndexOf('r', 0, 1));
AssertEquals("stepped limited char index",
4, s1.LastIndexOf('i', 6, 3));
AssertEquals("stepped limited char index",
2, s1.LastIndexOf('i', 3, 3));
AssertEquals("stepped limited char index",
-1, s1.LastIndexOf('i', 1, 2));
s1 = "original original";
AssertEquals("stepped string index #1",
9, s1.LastIndexOf("original", s1.Length));
AssertEquals("stepped string index #2",
0, s1.LastIndexOf("original", s1.Length-2));
AssertEquals("stepped string index #3",
-1, s1.LastIndexOf("original", s1.Length-11));
AssertEquals("stepped string index #4",
-1, s1.LastIndexOf("translator", 2));
AssertEquals("stepped string index #5",
0, "".LastIndexOf("", 0));
AssertEquals("stepped string index #6",
-1, "".LastIndexOf("A", -1));
AssertEquals("stepped limited string index #1",
10, s1.LastIndexOf("rig", s1.Length-1, 10));
AssertEquals("stepped limited string index #2",
-1, s1.LastIndexOf("rig", s1.Length, 3));
AssertEquals("stepped limited string index #3",
10, s1.LastIndexOf("rig", s1.Length-2, 15));
AssertEquals("stepped limited string index #4",
-1, s1.LastIndexOf("rig", s1.Length-2, 3));
string s2 = "QBitArray::bitarr_data";
AssertEquals ("bug #62160", 9, s2.LastIndexOf ("::"));
string s3 = "test123";
AssertEquals ("bug #77412", 0, s3.LastIndexOf ("test123"));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void LastIndexOf_Char_StartIndexStringLength ()
{
string s = "Mono";
s.LastIndexOf ('n', s.Length, 1);
// this works for string but not for a char
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void LastIndexOf_Char_StartIndexOverflow ()
{
"Mono".LastIndexOf ('o', Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void LastIndexOf_Char_LengthOverflow ()
{
"Mono".LastIndexOf ('o', 1, Int32.MaxValue);
}
[Test]
public void LastIndexOf_String_StartIndexStringLength ()
{
string s = "Mono";
s.LastIndexOf ("n", s.Length, 1);
// this works for string but not for a char
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void LastIndexOf_String_StartIndexStringLength_Plus1 ()
{
string s = "Mono";
s.LastIndexOf ("n", s.Length + 1, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void LastIndexOf_String_StartIndexOverflow ()
{
"Mono".LastIndexOf ("no", Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void LastIndexOf_String_LengthOverflow ()
{
"Mono".LastIndexOf ("no", 1, Int32.MaxValue);
}
public void TestLastIndexOfAny() {
string s1 = ".bcdefghijklm";
bool errorThrown = false;
try {
int i = s1.LastIndexOfAny(null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null char[] error", errorThrown);
errorThrown = false;
try {
int i = s1.LastIndexOfAny(null, s1.Length);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null char[] error", errorThrown);
errorThrown = false;
try {
int i = s1.LastIndexOfAny(null, s1.Length, 1);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null char[] error", errorThrown);
char[] c1 = {'a', 'e', 'i', 'o', 'u'};
AssertEquals("first vowel", 8, s1.LastIndexOfAny(c1));
AssertEquals("second vowel", 4, s1.LastIndexOfAny(c1, 7));
AssertEquals("out of vowels", -1, s1.LastIndexOfAny(c1, 3));
AssertEquals("second vowel in range",
4, s1.LastIndexOfAny(c1, s1.Length-6, 4));
AssertEquals("second vowel out of range",
-1, s1.LastIndexOfAny(c1, s1.Length-6, 3));
errorThrown = false;
try {
int i = s1.LastIndexOfAny(c1, -1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("Out of range error", errorThrown);
errorThrown = false;
try {
int i = s1.LastIndexOfAny(c1, -1, 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("Out of range error", errorThrown);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void LastIndexOfAny_StartIndexOverflow ()
{
"Mono".LastIndexOfAny (new char [1] { 'o' }, Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void LastIndexOfAny_LengthOverflow ()
{
"Mono".LastIndexOfAny (new char [1] { 'o' }, 1, Int32.MaxValue);
}
public void TestPadLeft() {
string s1 = "Hi!";
bool errorThrown = false;
try {
string s = s1.PadLeft(-1);
} catch (ArgumentException) {
errorThrown = true;
}
Assert("Bad argument error", errorThrown);
AssertEquals("Too little padding",
s1, s1.PadLeft(s1.Length-1));
AssertEquals("Some padding",
" Hi!", s1.PadLeft(5));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void PadLeft_Negative ()
{
string s = "Mono".PadLeft (-1);
}
public void TestPadRight() {
string s1 = "Hi!";
bool errorThrown = false;
try {
string s = s1.PadRight(-1);
} catch (ArgumentException) {
errorThrown = true;
}
Assert("Bad argument error", errorThrown);
AssertEquals("Too little padding",
s1, s1.PadRight(s1.Length-1));
AssertEquals("Some padding",
"Hi! ", s1.PadRight(5));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void PadRight_Negative ()
{
string s = "Mono".PadLeft (-1);
}
public void TestRemove() {
string s1 = "original";
bool errorThrown = false;
try {
s1.Remove(-1,1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range error", errorThrown);
errorThrown = false;
try {
s1.Remove(1,-1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range error", errorThrown);
errorThrown = false;
try {
s1.Remove(s1.Length,s1.Length);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("out of range error", errorThrown);
AssertEquals("basic remove", "oinal",
s1.Remove(1, 3));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Remove_StartIndexOverflow ()
{
"Mono".Remove (Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Remove_LengthOverflow ()
{
"Mono".Remove (1, Int32.MaxValue);
}
#if NET_2_0
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TestRemove1_NegativeStartIndex () {
"ABC".Remove (-1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TestRemove1_StartIndexOverflow () {
"ABC".Remove (3);
}
[Test]
public void TestRemove1 () {
AssertEquals ("AB", "ABC".Remove (2));
AssertEquals ("", "ABC".Remove (0));
}
#endif
public void TestReplace() {
string s1 = "original";
AssertEquals("non-hit char", s1, s1.Replace('q','s'));
AssertEquals("single char", "oxiginal", s1.Replace('r', 'x'));
AssertEquals("double char", "orxgxnal", s1.Replace('i', 'x'));
bool errorThrown = false;
try {
string s = s1.Replace(null, "feh");
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("should get null arg exception", errorThrown);
AssertEquals("replace as remove", "ornal",
s1.Replace("igi", null));
AssertEquals("non-hit string", s1, s1.Replace("spam", "eggs"));
AssertEquals("single string", "orirumal",
s1.Replace("gin", "rum"));
AssertEquals("double string", "oreigeinal",
s1.Replace("i", "ei"));
AssertEquals ("result longer", ":!:", "::".Replace ("::", ":!:"));
// Test overlapping matches (bug #54988)
string s2 = "...aaaaaaa.bbbbbbbbb,............ccccccc.u...";
AssertEquals ("..aaaaaaa.bbbbbbbbb,......ccccccc.u..", s2.Replace("..", "."));
// Test replacing null characters (bug #67395)
AssertEquals ("should not strip content after nullchar",
"is this ok ?", "is \0 ok ?".Replace ("\0", "this"));
}
public void TestSplit() {
string s1 = "abcdefghijklm";
char[] c1 = {'q', 'r'};
AssertEquals("No splitters", s1, (s1.Split(c1))[0]);
char[] c2 = {'a', 'e', 'i', 'o', 'u'};
string[] chunks = s1.Split(c2);
AssertEquals("First chunk", "", chunks[0]);
AssertEquals("Second chunk", "bcd", chunks[1]);
AssertEquals("Third chunk", "fgh", chunks[2]);
AssertEquals("Fourth chunk", "jklm", chunks[3]);
{
bool errorThrown = false;
try {
chunks = s1.Split(c2, -1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("Split out of range", errorThrown);
}
chunks = s1.Split(c2, 2);
AssertEquals("Limited chunk", 2, chunks.Length);
AssertEquals("First limited chunk", "", chunks[0]);
AssertEquals("Second limited chunk", "bcdefghijklm", chunks[1]);
string s3 = "1.0";
char[] c3 = {'.'};
chunks = s3.Split(c3,2);
AssertEquals("1.0 split length", 2, chunks.Length);
AssertEquals("1.0 split first chunk", "1", chunks[0]);
AssertEquals("1.0 split second chunk", "0", chunks[1]);
string s4 = "1.0.0";
char[] c4 = {'.'};
chunks = s4.Split(c4,2);
AssertEquals("1.0.0 split length", 2, chunks.Length);
AssertEquals("1.0.0 split first chunk", "1", chunks[0]);
AssertEquals("1.0.0 split second chunk", "0.0", chunks[1]);
string s5 = ".0.0";
char[] c5 = {'.'};
chunks = s5.Split (c5, 2);
AssertEquals(".0.0 split length", 2, chunks.Length);
AssertEquals(".0.0 split first chunk", "", chunks[0]);
AssertEquals(".0.0 split second chunk", "0.0", chunks[1]);
string s6 = ".0";
char[] c6 = {'.'};
chunks = s6.Split (c6, 2);
AssertEquals(".0 split length", 2, chunks.Length);
AssertEquals(".0 split first chunk", "", chunks[0]);
AssertEquals(".0 split second chunk", "0", chunks[1]);
string s7 = "0.";
char[] c7 = {'.'};
chunks = s7.Split (c7, 2);
AssertEquals("0. split length", 2, chunks.Length);
AssertEquals("0. split first chunk", "0", chunks[0]);
AssertEquals("0. split second chunk", "", chunks[1]);
string s8 = "0.0000";
char[] c8 = {'.'};
chunks = s8.Split (c8, 2);
AssertEquals("0.0000/2 split length", 2, chunks.Length);
AssertEquals("0.0000/2 split first chunk", "0", chunks[0]);
AssertEquals("0.0000/2 split second chunk", "0000", chunks[1]);
chunks = s8.Split (c8, 3);
AssertEquals("0.0000/3 split length", 2, chunks.Length);
AssertEquals("0.0000/3 split first chunk", "0", chunks[0]);
AssertEquals("0.0000/3 split second chunk", "0000", chunks[1]);
chunks = s8.Split (c8, 1);
AssertEquals("0.0000/1 split length", 1, chunks.Length);
AssertEquals("0.0000/1 split first chunk", "0.0000", chunks[0]);
chunks = s1.Split(c2, 1);
AssertEquals("Single split", 1, chunks.Length);
AssertEquals("Single chunk", s1, chunks[0]);
chunks = s1.Split(c2, 0);
AssertEquals("Zero split", 0, chunks.Length);
}
[Test]
public void MoreSplit ()
{
string test = "123 456 789";
string [] st = test.Split ();
AssertEquals ("#01", "123", st [0]);
st = test.Split (null);
AssertEquals ("#02", "123", st [0]);
}
[Test]
public void StartsWith() {
string s1 = "original";
bool errorThrown = false;
try {
bool huh = s1.StartsWith(null);
} catch (ArgumentNullException) {
errorThrown = true;
}
Assert("null StartsWith shouldn't be good", errorThrown);
Assert("should match", s1.StartsWith("o"));
Assert("should match 2", s1.StartsWith("orig"));
Assert("should fail", !s1.StartsWith("rig"));
Assert("should match 3", s1.StartsWith(String.Empty));
Assert("should match 4", String.Empty.StartsWith(String.Empty));
Assert("should fail 2", !String.Empty.StartsWith("rig"));
}
public void TestSubstring() {
string s1 = "original";
bool errorThrown = false;
try {
string s = s1.Substring(s1.Length+1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("error not thrown", errorThrown);
errorThrown = false;
try {
string s = s1.Substring(-1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("error not thrown", errorThrown);
errorThrown = false;
try {
string s = s1.Substring(1, -1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("error not thrown", errorThrown);
errorThrown = false;
try {
string s = s1.Substring(-1, 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("error not thrown", errorThrown);
errorThrown = false;
try {
string s = s1.Substring(s1.Length, 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("error not thrown", errorThrown);
errorThrown = false;
try {
string s = s1.Substring(1, s1.Length);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("error not thrown", errorThrown);
AssertEquals("basic substring", "inal",
s1.Substring(4));
AssertEquals("midstring", "igin",
s1.Substring(2, 4));
AssertEquals("at end", "",
s1.Substring(s1.Length, 0));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Substring_StartIndexOverflow ()
{
"Mono".Substring (Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void Substring_LengthOverflow ()
{
"Mono".Substring (1, Int32.MaxValue);
}
public void TestToCharArray() {
string s1 = "original";
char[] c1 = s1.ToCharArray();
AssertEquals("right array size", s1.Length, c1.Length);
AssertEquals("basic char array", s1,
new String(c1));
bool errorThrown = false;
try {
s1.ToCharArray(s1.Length, 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("error not thrown", errorThrown);
errorThrown = false;
try {
s1.ToCharArray(1, s1.Length);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("error not thrown", errorThrown);
errorThrown = false;
try {
s1.ToCharArray(-1, 1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("error not thrown", errorThrown);
errorThrown = false;
try {
s1.ToCharArray(1, -1);
} catch (ArgumentOutOfRangeException) {
errorThrown = true;
}
Assert("error not thrown", errorThrown);
c1 = s1.ToCharArray(0, 3);
AssertEquals("Starting char array", "ori", new String(c1));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void ToCharArray_StartIndexOverflow ()
{
"Mono".ToCharArray (1, Int32.MaxValue);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void ToCharArray_LengthOverflow ()
{
"Mono".ToCharArray (Int32.MaxValue, 1);
}
public void TestToLower() {
string s1 = "OrIgInAl";
AssertEquals("lowercase failed", "original", s1.ToLower());
// TODO - Again, with CultureInfo
}
public void TestToString() {
string s1 = "original";
AssertEquals("ToString failed!", s1, s1.ToString());
}
public void TestToUpper() {
string s1 = "OrIgInAl";
AssertEquals("uppercase failed", "ORIGINAL", s1.ToUpper());
// TODO - Again, with CultureInfo
}
public void TestTrim() {
string s1 = " original\t\n";
AssertEquals("basic trim failed", "original", s1.Trim());
AssertEquals("basic trim failed", "original", s1.Trim(null));
s1 = "original";
AssertEquals("basic trim failed", "original", s1.Trim());
AssertEquals("basic trim failed", "original", s1.Trim(null));
s1 = " \t \n ";
AssertEquals("empty trim failed", "", s1.Trim());
AssertEquals("empty trim failed", "", s1.Trim(null));
s1 = "aaaoriginalbbb";
char[] delims = {'a', 'b'};
AssertEquals("custom trim failed",
"original", s1.Trim(delims));
#if NET_2_0
AssertEquals ("net_2_0 additional char#1", "original", "\u2028original\u2029".Trim ());
AssertEquals ("net_2_0 additional char#2", "original", "\u0085original\u1680".Trim ());
#endif
}
public void TestTrimEnd() {
string s1 = " original\t\n";
AssertEquals("basic TrimEnd failed",
" original", s1.TrimEnd(null));
s1 = " original";
AssertEquals("basic TrimEnd failed",
" original", s1.TrimEnd(null));
s1 = " \t \n \n ";
AssertEquals("empty TrimEnd failed",
"", s1.TrimEnd(null));
s1 = "aaaoriginalbbb";
char[] delims = {'a', 'b'};
AssertEquals("custom TrimEnd failed",
"aaaoriginal", s1.TrimEnd(delims));
}
public void TestTrimStart() {
string s1 = " original\t\n";
AssertEquals("basic TrimStart failed",
"original\t\n", s1.TrimStart(null));
s1 = "original\t\n";
AssertEquals("basic TrimStart failed",
"original\t\n", s1.TrimStart(null));
s1 = " \t \n \n ";
AssertEquals("empty TrimStart failed",
"", s1.TrimStart(null));
s1 = "aaaoriginalbbb";
char[] delims = {'a', 'b'};
AssertEquals("custom TrimStart failed",
"originalbbb", s1.TrimStart(delims));
}
public void TestChars () {
// Check for invalid indexes
bool ok;
string s;
char c;
ok = false;
try {
s = "";
c = s[0];
}
catch (IndexOutOfRangeException) {
ok = true;
}
Assert (ok);
ok = false;
try {
s = "A";
c = s[-1];
}
catch (IndexOutOfRangeException) {
ok = true;
}
Assert (ok);
}
[Test]
public void TestComparePeriod ()
{
// according to bug 63981, this behavior is for all cultures
AssertEquals ("#1", -1, String.Compare ("foo.obj", "foobar.obj", false));
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void LastIndexOfAnyBounds1 ()
{
string mono = "Mono";
char [] k = { 'M' };
mono.LastIndexOfAny (k, mono.Length, 1);
}
#if NET_2_0
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void SplitString_NegativeCount ()
{
"A B".Split (new String [] { "A" }, -1, StringSplitOptions.None);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void SplitString_InvalidOptions ()
{
"A B".Split (new String [] { "A" }, 0, (StringSplitOptions)4096);
}
[Test]
public void StartsWith_WithEmptyCulture ()
{
// This should not crash
string s = "boo";
s.StartsWith ("this", true, null);
}
[Test]
public void SplitString ()
{
String[] res;
// count == 0
res = "A B C".Split (new String [] { "A" }, 0, StringSplitOptions.None);
AssertEquals (0, res.Length);
// empty and RemoveEmpty
res = "".Split (new String [] { "A" }, StringSplitOptions.RemoveEmptyEntries);
AssertEquals (0, res.Length);
// Not found
res = "A B C".Split (new String [] { "D" }, StringSplitOptions.None);
AssertEquals (1, res.Length);
AssertEquals ("A B C", res [0]);
// A normal test
res = "A B C DD E".Split (new String[] { "B", "D" }, StringSplitOptions.None);
AssertEquals (4, res.Length);
AssertEquals ("A ", res [0]);
AssertEquals (" C ", res [1]);
AssertEquals ("", res [2]);
AssertEquals (" E", res [3]);
// Same with RemoveEmptyEntries
res = "A B C DD E".Split (new String[] { "B", "D" }, StringSplitOptions.RemoveEmptyEntries);
AssertEquals (3, res.Length);
AssertEquals ("A ", res [0]);
AssertEquals (" C ", res [1]);
AssertEquals (" E", res [2]);
// Delimiter at the beginning and at the end
res = "B C DD B".Split (new String[] { "B" }, StringSplitOptions.None);
AssertEquals (3, res.Length);
AssertEquals ("", res [0]);
AssertEquals (" C DD ", res [1]);
AssertEquals ("", res [2]);
res = "B C DD B".Split (new String[] { "B" }, StringSplitOptions.RemoveEmptyEntries);
AssertEquals (1, res.Length);
AssertEquals (" C DD ", res [0]);
// count
res = "A B C DD E".Split (new String[] { "B", "D" }, 2, StringSplitOptions.None);
AssertEquals (2, res.Length);
AssertEquals ("A ", res [0]);
AssertEquals (" C DD E", res [1]);
// Ordering
res = "ABCDEF".Split (new String[] { "EF", "BCDE" }, StringSplitOptions.None);
AssertEquals (2, res.Length);
AssertEquals ("A", res [0]);
AssertEquals ("F", res [1]);
res = "ABCDEF".Split (new String[] { "BCD", "BC" }, StringSplitOptions.None);
AssertEquals (2, res.Length);
AssertEquals ("A", res [0]);
AssertEquals ("EF", res [1]);
// Whitespace
res = "A B\nC".Split ((String[])null, StringSplitOptions.None);
AssertEquals (3, res.Length);
AssertEquals ("A", res [0]);
AssertEquals ("B", res [1]);
AssertEquals ("C", res [2]);
res = "A B\nC".Split (new String [0], StringSplitOptions.None);
AssertEquals (3, res.Length);
AssertEquals ("A", res [0]);
AssertEquals ("B", res [1]);
AssertEquals ("C", res [2]);
}
#endif
}
}
|