Menu

[r1]: / trunk / Source / unitTests / multithreading.m  Maximize  Restore  History

Download this file

2268 lines (1748 with data), 132.8 kB

   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
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
//
// multithreading.m
// RegexKit
//
#import "multithreading.h"
#include <sys/time.h>
#include <sys/resource.h>
#define RKPrettyObjectMethodString(stringArg, ...) ([NSString stringWithFormat:[NSString stringWithFormat:@"%p [%@ %@]: %@", self, NSStringFromClass([(id)self class]), NSStringFromSelector(_cmd), stringArg], ##__VA_ARGS__])
@implementation multithreading
NSString *RKThreadWillExitNotification = @"RKThreadWillExitNotification";
- (id) initWithInvocation:(NSInvocation *) anInvocation
{
[self autorelease]; // In case anything goes wrong (ie, exception), we're guaranteed to be in the autorelease pool. On successful initialization, we send ourselves a retain.
// If we create any ivars that are not autoreleased, they should release when the autorelease pool releases us and in turn we dealloc, releasing those resources.
if(isInitialized == NO) {
if((self = [super initWithInvocation:anInvocation]) == NULL) { goto errorExit; }
isInitialized = YES;
srandomdev();
startAutoreleasedObjects = [NSAutoreleasePool totalAutoreleasedObjects];
int pthread_err = 0;
if((pthread_err = pthread_mutex_init(&globalThreadLock, NULL)) != 0) { NSLog(@"pthread_mutex_init on globalThreadLock returned %d", pthread_err); exit(0); }
if((pthread_err = pthread_mutex_init(&globalThreadConditionLock, NULL)) != 0) { NSLog(@"pthread_mutex_init on globalThreadConditionLock returned %d", pthread_err); exit(0); }
if((pthread_err = pthread_cond_init(&globalThreadCondition, NULL)) != 0) { NSLog(@"pthread_cond_init on globalThreadCondition returned %d", pthread_err); exit(0); }
if((pthread_err = pthread_mutex_init(&globalLogLock, NULL)) != 0) { NSLog(@"pthread_mutex_init on globalLogLock returned %d", pthread_err); exit(0); }
if((pthread_err = pthread_mutex_init(&threadExitLock, NULL)) != 0) { NSLog(@"pthread_mutex_init on threadExitLock returned %d", pthread_err); exit(0); }
logDateFormatter = [[NSDateFormatter alloc] initWithDateFormat:@"%Y/%m/%d %H:%M:%S.%F" allowNaturalLanguage:NO];
globalLogString = [[NSMutableString alloc] init];
globalLogArray = [[NSMutableArray alloc] init];
timingResultsArray = [[NSMutableArray alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptNotification:) name:RKThreadWillExitNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptNotification:) name:NSWillBecomeMultiThreadedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptNotification:) name:SenTestSuiteDidStartNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptNotification:) name:SenTestSuiteDidStopNotification object:nil];
[[RKRegex regexCache] clearCache];
[[RKRegex regexCache] clearCounters];
if(getenv("DEBUG") != NULL) { debugEnvString = [[NSString alloc] initWithCString:getenv("DEBUG") encoding:NSUTF8StringEncoding]; }
if(getenv("LEAK_CHECK") != NULL) { leakEnvString = [[NSString alloc] initWithCString:getenv("LEAK_CHECK") encoding:NSUTF8StringEncoding]; }
if(getenv("TIMING") != NULL) { timingEnvString = [[NSString alloc] initWithCString:getenv("TIMING") encoding:NSUTF8StringEncoding]; }
if(getenv("MULTITHREADING") != NULL) { multithreadingEnvString = [[NSString alloc] initWithCString:getenv("MULTITHREADING") encoding:NSUTF8StringEncoding]; }
NSLog(@"LEAK_CHECK = %@, DEBUG = %@, TIMING = %@, MULTITHREADING = %@", leakEnvString, debugEnvString, timingEnvString, multithreadingEnvString);
[[RKRegex regexCache] setDebug:YES];
iterations = 500;
if(([leakEnvString intValue] > 0) || ([debugEnvString intValue] > 0)) { iterations = 200; }
if(([leakEnvString intValue] > 0)) {
NSString *leakString = [[NSString alloc] initWithFormat:@"This string should leak from [%@ %@] on purpose", NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
NSLog(@"leakString @ %p: %@", leakString, leakString);
leakString = (NSString *)0xdeadbeef;
if([leakEnvString intValue] > 1) {
NSString *leaksCommandString = [NSString stringWithFormat:@"/usr/bin/leaks -exclude \"+[%@ %@]\" -exclude \"+[NSTitledFrame initialize]\" -exclude \"+[NSLanguage initialize]\" -exclude \"NSPrintAutoreleasePools\" -exclude \"+[NSWindowBinder initialize]\" -exclude \"+[NSCollator initialize]\" -exclude \"+[NSCollatorElement initialize]\" %d", NSStringFromClass([self class]), NSStringFromSelector(_cmd), getpid()];
[NSAutoreleasePool showPools];
NSLog(@"starting autoreleased objects: %u Now: %u Diff: %u", startAutoreleasedObjects, [NSAutoreleasePool totalAutoreleasedObjects], [NSAutoreleasePool totalAutoreleasedObjects] - startAutoreleasedObjects);
NSLog(@"autoreleasedObjectCount: %u", [NSAutoreleasePool autoreleasedObjectCount]);
NSLog(@"topAutoreleasePoolCount: %u", [NSAutoreleasePool topAutoreleasePoolCount]);
NSLog(@"Executing '%@'", leaksCommandString);
system([leaksCommandString UTF8String]);
}
}
loggingTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(flushLog) userInfo:nil repeats:YES] retain];
testStartCPUTime = [NSDate cpuTimeUsed];
}
return([self retain]); // We have successfully initialized, so rescue ourselves from the autorelease pool.
errorExit: // Catch point in case any clean up needs to be done. Currently, none is neccesary.
// We are autoreleased at the start, any objects/resources we created will be handled by dealloc
NSLog(@"Unable to initalize");
return(nil);
}
- (void)releaseResources
{
NSLog(RKPrettyObjectMethodString(@"Cache status:\n%@", [RKRegex regexCache]));
testEndCPUTime = [NSDate cpuTimeUsed];
testElapsedCPUTime = [NSDate differenceOfStartingTime:testStartCPUTime endingTime:testEndCPUTime];
NSString *leaksCommandString = nil;
NSSet *regexCacheSet = [[RKRegex regexCache] cacheSet];
NSLog(@"Cache set count: %d", [regexCacheSet count]);
NSAutoreleasePool *cachePool = [[NSAutoreleasePool alloc] init];
[cachePool release]; cachePool = NULL;
NSLog(@"RKRegex cache flushed");
if(([leakEnvString intValue] > 0)) {
leaksCommandString = [[NSString alloc] initWithFormat:@"/usr/bin/leaks -exclude \"+[%@ %@]\" -exclude \"+[NSTitledFrame initialize]\" -exclude \"+[NSLanguage initialize]\" -exclude \"NSPrintAutoreleasePools\" -exclude \"+[NSWindowBinder initialize]\" -exclude \"+[NSCollator initialize]\" -exclude \"+[NSCollatorElement initialize]\" %d", NSStringFromClass([self class]), NSStringFromSelector(_cmd), getpid()];
if([leakEnvString intValue] > 2) {
[NSAutoreleasePool showPools];
NSLog(@"starting autoreleased objects: %u Now: %u Diff: %u", startAutoreleasedObjects, [NSAutoreleasePool totalAutoreleasedObjects], [NSAutoreleasePool totalAutoreleasedObjects] - startAutoreleasedObjects);
NSLog(@"autoreleasedObjectCount: %u", [NSAutoreleasePool autoreleasedObjectCount]);
NSLog(@"topAutoreleasePoolCount: %u", [NSAutoreleasePool topAutoreleasePoolCount]);
}
NSLog(@"Executing '%@'", leaksCommandString);
system([leaksCommandString UTF8String]);
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
if(loggingTimer != nil) {
NSLog(@"invalidating timer..");
[loggingTimer invalidate];
[loggingTimer release];
loggingTimer = nil;
NSLog(@"The logging timer has been terminated.");
}
if(timingResultsArray != nil) {
if([timingResultsArray count] > 0) {
NSLog(@"Timing results:\n");
NSEnumerator *timingEnumerator = [timingResultsArray objectEnumerator];
NSString *timingString = nil;
while((timingString = [timingEnumerator nextObject]) != nil) {
fprintf(stderr, "%s\n", [timingString UTF8String]);
}
fprintf(stderr, "\n");
}
}
int pthread_err = 0;
if((pthread_err = pthread_mutex_destroy(&globalThreadLock)) != 0) { NSLog(@"pthread_mutex_destroy on globalThreadLock returned %d", pthread_err); }
if((pthread_err = pthread_mutex_destroy(&globalThreadConditionLock)) != 0) { NSLog(@"pthread_mutex_destroy on globalThreadConditionLock returned %d", pthread_err); }
if((pthread_err = pthread_cond_destroy(&globalThreadCondition)) != 0) { NSLog(@"pthread_cond_destroy on globalThreadCondition returned %d", pthread_err); }
if((pthread_err = pthread_mutex_destroy(&globalLogLock)) != 0) { NSLog(@"pthread_mutex_destroy on globalLogLock returned %d", pthread_err); }
if((pthread_err = pthread_mutex_destroy(&threadExitLock)) != 0) { NSLog(@"pthread_mutex_destroy on threadExitLock returned %d", pthread_err); }
if(globalLogString != nil) {
if([globalLogString length] > 0) {
fprintf(stderr, "\n---------\nThe global log string @ %p:\n%s\n", globalLogString, [globalLogString UTF8String]);
fflush(stderr);
}
}
if(globalLogString != nil) { [globalLogString release]; globalLogString = nil; }
if(globalLogArray != nil) { [globalLogArray release]; globalLogArray = nil; }
if(timingResultsArray != nil) { [timingResultsArray release]; timingResultsArray = nil; }
if(loggingTimer != nil) { [loggingTimer release]; loggingTimer = nil; }
if(logDateFormatter != nil) { [logDateFormatter release]; logDateFormatter = nil; }
if(leakEnvString != nil) { [leakEnvString release]; leakEnvString = nil; }
if(debugEnvString != nil) { [debugEnvString release]; debugEnvString = nil; }
if(timingEnvString != nil) { [timingEnvString release]; timingEnvString = nil; }
NSLog(@"starting autoreleased objects: %u Now: %u Diff: %u", startAutoreleasedObjects, [NSAutoreleasePool totalAutoreleasedObjects], [NSAutoreleasePool totalAutoreleasedObjects] - startAutoreleasedObjects);
NSLog(@"Elapsed CPU time: %@", [NSDate stringFromCPUTime:testElapsedCPUTime]);
NSLog(@"Elapsed CPU time: %@", [NSDate microSecondsStringFromCPUTime:testElapsedCPUTime]);
fflush(stdout); fflush(stderr);
NSLog(@"Completed dealloc");
NSLog(RKPrettyObjectMethodString(@"Teardown complete\n"));
fflush(stdout); fflush(stderr);
fprintf(stderr, "-----------------------------------------\n\n");
fflush(stdout); fflush(stderr);
}
- (void)dealloc
{
[self releaseResources];
[super dealloc];
}
- (void)setUp
{
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f/10.0f]];
[self flushLog];
[[RKRegex regexCache] clearCache];
[[RKRegex regexCache] clearCounters];
}
- (void)tearDown
{
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f/10.0f]];
[self flushLog];
[self releaseResources];
}
- (void)acceptNotification:(NSNotification *)theNotification
{
if(isInitialized == NO) { NSLog(@"Notification: %@", [theNotification name]); goto exitNow; }
[self thread:0 log:[NSString stringWithFormat:@"Notification: %@\n", [theNotification name]]];
if([[theNotification name] isEqualToString:RKThreadWillExitNotification]) {
int pthread_err = 0;
if((pthread_err = pthread_mutex_lock(&threadExitLock)) != 0) { NSLog(@"pthread_mutex_lock on threadExitLock returned %d", pthread_err); }
threadExitCount++;
if((pthread_err = pthread_mutex_unlock(&threadExitLock)) != 0) { NSLog(@"pthread_mutex_unlock on threadExitLock returned %d", pthread_err); }
}
exitNow:
return;
}
- (void)flushLog
{
if(isInitialized == NO) { NSLog(@"flushLog called but not initialized"); return; }
pthread_mutex_lock(&globalLogLock);
// vvvvvvvvvvvv BEGIN LOCK CRITICAL PATH vvvvvvvvvvvv
NSString *logString = [NSString stringWithString:globalLogString];
[globalLogString setString:@""];
// ^^^^^^^^^^^^^ END LOCK CRITICAL PATH ^^^^^^^^^^^^^
pthread_mutex_unlock(&globalLogLock);
if([logString length] > 0) { fprintf(stderr, "%s", [logString UTF8String]); }
}
- (void)thread:(int)threadID log:(NSString *)logString
{
if(isInitialized == NO) { NSLog(@"logger called but not initialized: ID #%d : %@", threadID, logString); return; }
pthread_mutex_lock(&globalLogLock);
// vvvvvvvvvvvv BEGIN LOCK CRITICAL PATH vvvvvvvvvvvv
[globalLogString appendString:[NSString stringWithFormat:@"[%@ <#%2d>] %@", [logDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]], threadID, logString]];
// ^^^^^^^^^^^^^ END LOCK CRITICAL PATH ^^^^^^^^^^^^^
pthread_mutex_unlock(&globalLogLock);
}
- (void)testSimpleMultiThreading
{
if(isInitialized == NO) { STFail(@"[%@ %@] is not initialized!", [self className], NSStringFromSelector(_cmd)); return; }
int pthread_err = 0, totalThreads = 13;
[self thread:0 log:[NSString stringWithFormat:@"%@ is initializing.\n", NSStringFromSelector(_cmd)]];
[self flushLog];
if([multithreadingEnvString intValue] < 1) { [self thread:0 log:@"Multithreading testing was not requested\n"]; return; }
setpriority(PRIO_PROCESS, 0, 20);
int x = 0;
for(x = 1; x < totalThreads + 1; x++) {
[NSThread detachNewThreadSelector:@selector(threadEntry:) toTarget:self withObject:[NSNumber numberWithInt:x]];
[self flushLog];
}
[self thread:0 log:[NSString stringWithFormat:@"Threads launched\n"]];
[self flushLog];
[self thread:0 log:[NSString stringWithFormat:@"Quiescent for 1 second in run loop\n"]];
[self flushLog];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f]];
[self thread:0 log:[NSString stringWithFormat:@"Broadcasting start condition\n"]];
[self flushLog];
if((pthread_err = pthread_cond_broadcast(&globalThreadCondition)) != 0) { [self thread:0 log:[NSString stringWithFormat:@"pthread_cond_broadcast on globalThreadCondition returned %d\n", pthread_err]]; }
[self thread:0 log:[NSString stringWithFormat:@"Will now run the run loop until I see %d threads exit.\n", totalThreads]];
[self flushLog];
NSTimeInterval startedAt = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval lastRelease = startedAt;
int lastCount = 0, thisCount = 0;
double resolution = 1.0/10.0;
BOOL endRunLoop = NO, isRunning = YES;
while((isRunning == YES) && (endRunLoop == NO)) {
NSAutoreleasePool *threadPool = [[NSAutoreleasePool alloc] init];
NSDate *next = [NSDate dateWithTimeIntervalSinceNow:resolution];
isRunning = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:next];
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
if(((currentTime - lastRelease) > 1.0)) {
[self thread:0 log:[NSString stringWithFormat:@"Cache status: %@\n", [[RKRegex regexCache] status]]];
[[RKRegex regexCache] clearCache];
[threadPool release];
sched_yield();
threadPool = [[NSAutoreleasePool alloc] init];
lastRelease = currentTime;
}
if((pthread_err = pthread_mutex_lock(&threadExitLock)) != 0) { [self thread:0 log:[NSString stringWithFormat:@"pthread_mutex_lock on threadExitLock returned %d\n", pthread_err]]; }
thisCount = threadExitCount;
if((pthread_err = pthread_mutex_unlock(&threadExitLock)) != 0) { [self thread:0 log:[NSString stringWithFormat:@"pthread_mutex_unlock on threadExitLock returned %d\n", pthread_err]]; }
if(thisCount != lastCount) { [self thread:0 log:[NSString stringWithFormat:@"%d of %d threads exited. Waiting for %d more.\n", thisCount, totalThreads, totalThreads - thisCount]]; lastCount = thisCount; }
if(thisCount == totalThreads) { [self thread:0 log:[NSString stringWithFormat:@"All threads are now accounted for, will stop the run loop\n"]]; endRunLoop = YES; }
[threadPool release];
}
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0f/10.0f]];
setpriority(PRIO_PROCESS, 0, 0);
return;
}
- (int)threadEntry:(id)threadArgument
{
NSAutoreleasePool *threadPool = [[NSAutoreleasePool alloc] init];
int pthread_err = 0;
int threadID = [threadArgument intValue];
NSTimeInterval sleepInterval = 0.0;
switch(threadID) {
case 1: sleepInterval = 0.0003; break;
case 2: sleepInterval = 0.0005; break;
case 3: sleepInterval = 0.0007; break;
case 4: sleepInterval = 0.0011; break;
case 5: sleepInterval = 0.0013; break;
case 6: sleepInterval = 0.0017; break;
case 7: sleepInterval = 0.0019; break;
case 8: sleepInterval = 0.0023; break;
case 9: sleepInterval = 0.0029; break;
case 10: sleepInterval = 0.0031; break;
case 11: sleepInterval = 0.0037; break;
case 12: sleepInterval = 0.0041; break;
case 13: sleepInterval = 0.0043; break;
case 14: sleepInterval = 0.0047; break;
case 15: sleepInterval = 0.0053; break;
case 16: sleepInterval = 0.0059; break;
default: sleepInterval = 0.0; break;
}
[self thread:threadID log:[NSString stringWithFormat:@"Thread %d, pthread 0x%8.8x spun up and waiting for launch command. Frame address is 0x%8.8x\n", threadID, pthread_self(), __builtin_frame_address(0)]];
if((pthread_err = pthread_mutex_lock(&globalThreadConditionLock)) != 0) { [self thread:threadID log:[NSString stringWithFormat:@"pthread_mutex_lock on globalThreadConditionLock returned %d\n", pthread_err]]; }
if((pthread_err = pthread_cond_wait(&globalThreadCondition, &globalThreadConditionLock)) != 0) { [self thread:threadID log:[NSString stringWithFormat:@"pthread_cond_wait on globalThreadCondition returned %d\n", pthread_err]]; }
if((pthread_err = pthread_mutex_unlock(&globalThreadConditionLock)) != 0) { [self thread:threadID log:[NSString stringWithFormat:@"pthread_mutex_unlock on globalThreadConditionLock returned %d\n", pthread_err]]; }
double rndTime = ((double)random()/(double)INT_MAX)*5.0;
[self thread:threadID log:[NSString stringWithFormat:@"Thread %d received launch signal, starting tests after %5.3f delay\n", threadID, rndTime]];
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:rndTime]];
int x = 0, y = 0;
for(x = 0; x < 13; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
[self thread:threadID log:[NSString stringWithFormat:@"Thread %3d, round %3d starting.. cacheCount: %3u cacheClearedCount: %5u read busy: %5u spin: %5u write busy: %5u totalAutoreleasedObjects: %8u\n", threadID, x, [[RKRegex regexCache] cacheCount], [[RKRegex regexCache] cacheClearedCount], [[RKRegex regexCache] readBusyCount], [[RKRegex regexCache] readSpinCount], [[RKRegex regexCache] writeBusyCount], [NSAutoreleasePool totalAutoreleasedObjects]]];
for(y = 0; y < 46; y++) {
[self executeTest:y];
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:sleepInterval]];
}
if(loopPool != nil) { [loopPool release]; loopPool = nil; }
}
[self thread:threadID log:[NSString stringWithFormat:@"Thread %d completed all tests, will now exit.\n", threadID]];
// We post our own thread exit notification with our unique thread id to prevent coalescing in the notification center.
NSValue *threadValue = [NSValue valueWithPointer:pthread_self()];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:RKThreadWillExitNotification object:threadValue userInfo:[NSDictionary dictionaryWithObject:threadValue forKey:@"threadValue"]]];
[threadPool release];
return(0);
}
- (BOOL)executeTest:(unsigned int)testNumber
{
NSAutoreleasePool *testPool = [[NSAutoreleasePool alloc] init];
switch(testNumber) {
case 0: [self mt_test_1]; break;
case 1: [self mt_test_2]; break;
case 2: [self mt_test_3]; break;
case 3: [self mt_test_4]; break;
case 4: [self mt_cache_4]; break;
case 5: [self mt_test_6]; break;
case 6: [self mt_test_7]; break;
case 7: [self mt_cache_1]; break;
case 8: [self mt_test_8]; break;
case 9: [self mt_test_9]; break;
case 10: [self mt_test_10]; break;
case 11: [self mt_cache_5]; break;
case 12: [self mt_test_12]; break;
case 13: [self mt_test_13]; break;
case 14: [self mt_test_14]; break;
case 15: [self mt_cache_2]; break;
case 16: [self mt_test_15]; break;
case 17: [self mt_test_16]; break;
case 18: [self mt_time_1]; break;
case 19: [self mt_cache_6]; break;
case 20: [self mt_time_3]; break;
case 21: [self mt_time_4]; break;
case 22: [self mt_cache_4]; break;
case 23: [self mt_time_5]; break;
case 24: [self mt_time_6]; break;
case 25: [self mt_time_7]; break;
case 26: [self mt_time_8]; break;
case 27: [self mt_cache_3]; break;
case 28: [self mt_time_9]; break;
case 29: [self mt_time_10]; break;
case 30: [self mt_time_11]; break;
case 31: [self mt_test_5]; break;
case 32: [self mt_test_11]; break;
case 33: [self mt_time_2]; break;
case 34: [self mt_time_12]; break;
case 35: [self mt_test_17]; break;
case 36: [self mt_test_18]; break;
case 37: [self mt_test_19]; break;
case 38: [self mt_test_20]; break;
case 39: [self mt_test_21]; break;
case 40: [self mt_test_22]; break;
case 41: [self mt_test_23]; break;
case 42: [self mt_test_24]; break;
case 43: [self mt_test_25]; break;
case 44: [self mt_test_26]; break;
case 45: [self mt_test_27]; break;
default: [self mt_time_3]; break;
}
[testPool release];
return(NO);
}
- (void)mt_cache_1
{
BOOL lookupEnabled = [[RKRegex regexCache] isCacheLookupEnabled];
if(lookupEnabled == NO) { [[RKRegex regexCache] setCacheLookupEnabled:YES]; } //else { [[RKRegex regexCache] setCacheLookupEnabled:NO]; }
}
- (void)mt_cache_2
{
BOOL addingEnabled = [[RKRegex regexCache] isCacheAddingEnabled];
if(addingEnabled == NO) { [[RKRegex regexCache] setCacheAddingEnabled:YES]; } //else { [[RKRegex regexCache] setCacheAddingEnabled:NO]; }
}
- (void)mt_cache_3
{
NSAutoreleasePool *clearCachePool = [[NSAutoreleasePool alloc] init];
[[RKRegex regexCache] clearCache];
[clearCachePool release];
sched_yield();
}
- (void)mt_cache_4
{
NSSet *regexCacheSet = [[RKRegex regexCache] cacheSet];
regexCacheSet = nil;
}
- (void)mt_cache_5
{
RKRegex *regex = nil;
STAssertNotNil((regex = [RKRegex regexWithRegexString:@"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))" options:RKCompileDupNames]), nil);
[[RKRegex regexCache] removeObjectFromCache:regex];
STAssertNotNil((regex = [RKRegex regexWithRegexString:@".* (\\w+) .*" options:0]), nil);
[[RKRegex regexCache] removeObjectFromCache:regex];
STAssertNotNil((regex = [RKRegex regexWithRegexString:@"^(Match)\\s+the\\s+(MAGIC)" options:0]), nil);
[[RKRegex regexCache] removeObjectFromCache:regex];
STAssertNotNil((regex = [RKRegex regexWithRegexString:@"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) )" options:0]), nil);
[[RKRegex regexCache] removeObjectFromCache:regex];
STAssertNotNil((regex = [RKRegex regexWithRegexString:@"(?<pn> \\( ( (?>[^()]+) | (?&pn) )* \\) )" options:0]), nil);
[[RKRegex regexCache] removeObjectFromCache:regex];
STAssertNotNil((regex = [RKRegex regexWithRegexString:@"http://www.google.com/this/neat/url/index.html" options:0]), nil);
[[RKRegex regexCache] removeObjectFromCache:regex];
STAssertNotNil((regex = [RKRegex regexWithRegexString:@" http://www.google.com/this/neat/url/index.html " options:0]), nil);
[[RKRegex regexCache] removeObjectFromCache:regex];
}
- (void)mt_cache_6
{
RKRegex *regex = nil;
STAssertTrue((regex = [RKRegex regexWithRegexString:@"\\( ( ( ([^()];+) | (?R) )* ) \\)" options:0]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"\\( ( ( ([^()];+) | (?R) )* ) \\)" options:0]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"http://www.yahoo.com/this/neat/url/index.html" options:0]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"http://www.yahoo.com/this/neat/url/index.html" options:RKCompileDupNames]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"http://www.yahoo.com/this/neat/url/index.html" options:RKCompileUTF8]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"http://www.yahoo.com/this/neat/url/index.html" options:(RKCompileDupNames | RKCompileUTF8)]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"http://www.google.com/this/neat/url/index.html" options:0]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"http://www.google.com/this/neat/url/index.html" options:RKCompileDupNames]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"http://www.google.com/this/neat/url/index.html" options:RKCompileUTF8]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"http://www.google.com/this/neat/url/index.html" options:(RKCompileDupNames | RKCompileUTF8)]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@" http://www.yahoo.com/this/neat/url/index.html " options:0]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@" http://www.yahoo.com/this/neat/url/index.html " options:RKCompileDupNames]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@" http://www.yahoo.com/this/neat/url/index.html " options:RKCompileUTF8]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@" http://www.yahoo.com/this/neat/url/index.html " options:(RKCompileDupNames | RKCompileUTF8)]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@" http://www.google.com/this/neat/url/index.html " options:0]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@" http://www.google.com/this/neat/url/index.html " options:RKCompileDupNames]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@" http://www.google.com/this/neat/url/index.html " options:RKCompileUTF8]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@" http://www.google.com/this/neat/url/index.html " options:(RKCompileDupNames | RKCompileUTF8)]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"(?<pn> \\( ( (?>[^()];+) | (?&pn) )* \\) )" options:0]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"\\( ( ( (?>[^()];+) | (?R) )* ) \\)" options:0]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"\\( ( ( (?>[^()];+) | (?R) )* ) \\)" options:0]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
STAssertTrue((regex = [RKRegex regexWithRegexString:@"\\( ( ( ([^()];+) | (?R) )* ) \\)" options:0]) != nil, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
regex = nil;
STAssertThrowsSpecificNamed((regex = [RKRegex regexWithRegexString:@"^(Match)\\s+the\\s+((MAGIC)$" options:0]), NSException, RKRegexSyntaxErrorException, nil);
STAssertNil(regex, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
regex = nil;
STAssertThrowsSpecificNamed((regex = [RKRegex regexWithRegexString:@"(?<pn> \\( ( (?>[^()];+) | (?&xq) )* \\) )" options:0]), NSException, RKRegexSyntaxErrorException, nil);
STAssertNil(regex, nil);
[[RKRegex regexCache] addObjectToCache:regex withHash:[regex hash]];
}
//- (void)testSimpleStringAdditions
- (void)mt_test_1
{
NSString *subString0 = nil, *subString1 = nil, *subString2 = nil;
NSString *subjectString = @"This web address, http://www.foad.org/no_way/it really/does/match/ is really neat!";
//NSString *regexString = @"http://([^/]+)(/.*/)"; // Stupid xcode seems to choke on the last 3-4 characters when they're together. It looses it's indentation mind.
NSString *regexString = [@"http://([^/]+)(/.*" stringByAppendingString:@"/)"];
#ifdef not_now
NSArray *matchesArray = [subjectString stringsWithRegex:regexString];
STAssertNotNil(matchesArray, nil);
STAssertTrue([matchesArray count] == 3, nil);
STAssertTrue([[matchesArray objectAtIndex:0] isEqualToString:@"http://www.foad.org/no_way/it really/does/match/"], nil);
STAssertTrue([[matchesArray objectAtIndex:1] isEqualToString:@"www.foad.org"], nil);
STAssertTrue([[matchesArray objectAtIndex:2] isEqualToString:@"/no_way/it really/does/match/"], nil);
#endif
STAssertTrue(([subjectString getCapturesWithRegexAndReferences:regexString, @"${2}", &subString2, nil]) , nil ) ;
STAssertTrue([subString2 isEqualToString:@"/no_way/it really/does/match/"], @"len %d %@ = '%s'", [subString2 length], subString2, [subString2 UTF8String]);
NSString *namedSubjectString = @" 1999 - 12 - 01 / 55 ";
NSString *namedRegexString = @"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))";
NSString *subStringDate = nil, *subStringDay = nil, *subStringYear = nil;
subString0 = nil, subString1 = nil, subString2 = nil;
#ifdef not_now
matchesArray = [namedSubjectString stringsWithRegex:namedRegexString];
STAssertNotNil(matchesArray, nil);
STAssertTrue([matchesArray count] == 7, nil);
STAssertTrue([[matchesArray objectAtIndex:0] isEqualToString:@" 1999 - 12 - 01 / 55"], nil);
STAssertTrue([[matchesArray objectAtIndex:1] isEqualToString:@" 1999 - 12 - 01 / 55"], nil);
STAssertTrue([[matchesArray objectAtIndex:2] isEqualToString:@"1999"], nil);
STAssertTrue([[matchesArray objectAtIndex:3] isEqualToString:@"19"], nil);
STAssertTrue([[matchesArray objectAtIndex:4] isEqualToString:@"12"], nil);
STAssertTrue([[matchesArray objectAtIndex:5] isEqualToString:@"01"], nil);
STAssertTrue([[matchesArray objectAtIndex:6] isEqualToString:@"55"], nil);
#endif
STAssertTrue(([namedSubjectString getCapturesWithRegexAndReferences:namedRegexString, @"${day}", &subStringDay, @"${date}", &subStringDate, @"${2}", &subString2, @"${1}", &subString1, @"${year}", &subStringYear, nil]), nil);
STAssertTrue([subString1 isEqualToString:@" 1999 - 12 - 01 / 55"], nil);
STAssertTrue([subString2 isEqualToString:@"1999"], nil);
STAssertTrue([subStringDate isEqualToString:@" 1999 - 12 - 01 / 55"], @"got %@ %d", subStringDate, [subStringDate isEqualToString:@" 1999 - 12 - 01 / 55"]);
STAssertTrue([subStringDay isEqualToString:@"01"], @"got %@ %d", subStringDay, [subStringDay isEqualToString:@"01"]);
STAssertTrue([subStringYear isEqualToString:@"1999"], @"got %@ %d", subStringYear, [subStringYear isEqualToString:@"1999"]);
}
//- (void)testSimpleInit
- (void)mt_test_2
{
NSAutoreleasePool *initPool = [[NSAutoreleasePool alloc] init];
RKRegex *regex = nil;
NSMutableSet *regexSet = [NSMutableSet set];
STAssertNotNil(regex = [[[RKRegex alloc] initWithRegexString:@".* (\\w+) .*" options:0] autorelease], nil);
[regexSet addObject:regex];
STAssertThrowsSpecificNamed([[[RKRegex alloc] initWithRegexString:nil options:0] autorelease], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([[[RKRegex alloc] initWithRegexString:@".* (\\w+) .*" options:0xffffffff] autorelease], NSException, RKRegexSyntaxErrorException, nil);
STAssertNotNil(regex = [RKRegex regexWithRegexString:@".* (\\w+) .*" options:0], nil);
[regexSet addObject:regex];
STAssertThrowsSpecificNamed([RKRegex regexWithRegexString:nil options:0], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([RKRegex regexWithRegexString:@".* (\\w+) .*" options:0xffffffff], NSException, RKRegexSyntaxErrorException, nil);
regex = [RKRegex regexWithRegexString:@".* (\\w+) .*" options:0];
[regexSet addObject:regex];
regex = [RKRegex regexWithRegexString:@".* (\\w+) .*" options:RKCompileDupNames];
[regexSet addObject:regex];
regex = [RKRegex regexWithRegexString:@".* (\\w+) .*" options:RKCompileDupNames];
[regexSet addObject:regex];
if([RKRegex PCREBuildConfig] & RKBuildConfigUTF8) {
regex = [RKRegex regexWithRegexString:@".* (\\w+) .*" options:RKCompileDupNames | RKCompileUTF8];
[regexSet addObject:regex];
regex = [RKRegex regexWithRegexString:@".* (\\w+) .*" options:RKCompileDupNames | RKCompileUTF8];
[regexSet addObject:regex];
regex = [RKRegex regexWithRegexString:@".* (\\W+) .*" options:RKCompileDupNames | RKCompileUTF8];
[regexSet addObject:regex];
regex = [RKRegex regexWithRegexString:@".* (\\W+) .*" options:RKCompileDupNames | RKCompileUTF8];
[regexSet addObject:regex];
}
[initPool release];
}
//- (void)testTrivialInitStringEncoding
- (void)mt_test_3
{
// Copied from somewhere, converted to UTF8, french
char utf8Ptr[] = {0x4c, 0x65, 0x20, 0x6d, 0xe2, 0x94, 0x9c, 0xc2, 0xac, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x78, 0x74, 0x65, 0x20, 0x65, 0x6e, 0x20, 0x66, 0x72, 0x61, 0x6e, 0xe2, 0x94, 0x9c, 0xc2, 0xaf, 0x61, 0x69, 0x73, 0x00};
NSString *initString = [NSString stringWithUTF8String:utf8Ptr];
STAssertNotNil((id)initString, nil);
RKRegex *regex = nil;
STAssertNoThrow(regex = [RKRegex regexWithRegexString:(id)initString options:0], nil);
STAssertNotNil(regex, nil);
}
//- (void)testBuildConfig
- (void)mt_test_4
{
RKRegex *regex = [RKRegex regexWithRegexString:@".* (\\w+) .*" options:0];
STAssertNotNil(regex, nil); if(regex == nil) { return; }
RKBuildConfig regexBuildConfig = [RKRegex PCREBuildConfig];
RKBuildConfig pcreBuildConfig = 0;
RKCompileErrorCode initErrorCode = RKCompileErrorNoError;
int tempConfigInt = 0;
STAssertTrue((initErrorCode = pcre_config(PCRE_CONFIG_UTF8, &tempConfigInt)) == RKCompileErrorNoError, @"initErrorCode = %d", initErrorCode);
if(tempConfigInt == 1) { pcreBuildConfig |= RKBuildConfigUTF8; }
STAssertTrue((initErrorCode = pcre_config(PCRE_CONFIG_UNICODE_PROPERTIES, &tempConfigInt)) == RKCompileErrorNoError, @"initErrorCode = %d", initErrorCode);
if(tempConfigInt == 1) { pcreBuildConfig |= RKBuildConfigUnicodeProperties; }
STAssertTrue((initErrorCode = pcre_config(PCRE_CONFIG_NEWLINE, &tempConfigInt)) == RKCompileErrorNoError, @"initErrorCode = %d", initErrorCode);
switch(tempConfigInt) {
case -1: pcreBuildConfig |= RKBuildConfigNewlineAny; break;
#if PCRE_MAJOR >= 7 && PCRE_MINOR >= 1
case -2: pcreBuildConfig |= RKBuildConfigNewlineAnyCRLF; break;
#endif
case 10: pcreBuildConfig |= RKBuildConfigNewlineLF; break;
case 13: pcreBuildConfig |= RKBuildConfigNewlineCR; break;
case 3338: pcreBuildConfig |= RKBuildConfigNewlineCRLF; break;
default: STAssertTrue(tempConfigInt == tempConfigInt, @"Unknown new line configuration encountered. %u 0x%8.8x", tempConfigInt, tempConfigInt); break;
}
#if PCRE_MAJOR >= 7 && PCRE_MINOR >= 4
STAssertTrue(((initErrorCode = pcre_config(PCRE_CONFIG_BSR, &tempConfigInt)) == RKMatchErrorNoError), @"initErrorCode = %d", initErrorCode);
switch(tempConfigInt) {
case 0: pcreBuildConfig |= RKBuildConfigBackslashRUnicode; break;
case 1: pcreBuildConfig |= RKBuildConfigBackslashRAnyCRLR; break;
default: STAssertTrue(1==0, @"Unknown PCRE_CONFIG_BSR encountered. %u 0x%8.8x", tempConfigInt, tempConfigInt); break;
}
#endif // >= 7.4
STAssertTrue(pcreBuildConfig == regexBuildConfig, @"regex config: %u pcre config: %u", regexBuildConfig, pcreBuildConfig);
}
//- (void)testSimpleGetRanges
- (void)mt_test_5
{
NSString *regexString = @"^(Match)\\s+the\\s+(MAGIC)";
NSString *subjectString = @"Match the MAGIC in this string";
unsigned int subjectLength = [subjectString length], captureCount = 0, x = 0;
NSRange *matchRanges = NULL;
RKRegex *regex = [RKRegex regexWithRegexString:regexString options:0];
STAssertNotNil(regex, nil); if(regex == nil) { return; }
captureCount = [regex captureCount];
STAssertTrue(captureCount == 3, @"captureCount is %u", captureCount); if(captureCount != 3) { return; }
matchRanges = alloca(captureCount * sizeof(NSRange));
STAssertTrue(matchRanges != NULL, nil); if(matchRanges == NULL) { return; }
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); }
RKMatchErrorCode errorCode = [regex getRanges:matchRanges withCharacters:[subjectString UTF8String] length:subjectLength inRange:NSMakeRange(0, subjectLength) options:0];
STAssertTrue(errorCode == 3, @"errorCode = %d", errorCode);
STAssertTrue(NSEqualRanges(NSMakeRange(0, 15), matchRanges[0]), @"matchRange[0] = %@", NSStringFromRange(matchRanges[0]));
STAssertTrue(NSEqualRanges(NSMakeRange(0, 5), matchRanges[1]), @"matchRange[1] = %@", NSStringFromRange(matchRanges[1]));
STAssertTrue(NSEqualRanges(NSMakeRange(10, 5), matchRanges[2]), @"matchRange[2] = %@", NSStringFromRange(matchRanges[2]));
}
//- (void)testSimpleGetRangesNoMatch
- (void)mt_test_6
{
NSString *regexString = @"^(Match)\\s+the\\s+(MAGIC)$";
NSString *subjectString = @"Match the MAGIC in this string";
unsigned int subjectLength = [subjectString length], captureCount = 0, x = 0;
NSRange *matchRanges = NULL;
RKRegex *regex = [RKRegex regexWithRegexString:regexString options:0];
STAssertNotNil(regex, nil); if(regex == nil) { return; }
captureCount = [regex captureCount];
STAssertTrue(captureCount == 3, @"captureCount is %u", captureCount); if(captureCount != 3) { return; }
matchRanges = alloca(captureCount * sizeof(NSRange));
STAssertTrue(matchRanges != NULL, nil); if(matchRanges == NULL) { return; }
RKMatchErrorCode errorCode = RKMatchErrorNoError;
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); }
errorCode = [regex getRanges:matchRanges withCharacters:[subjectString UTF8String] length:subjectLength inRange:NSMakeRange(0, subjectLength) options:0];
STAssertTrue(errorCode == RKMatchErrorNoMatch, @"errorCode = %d", errorCode);
for(x = 0; x < captureCount; x++) { STAssertTrue(NSEqualRanges(matchRanges[x], NSMakeRange(0xdeadbeef, 0x0badc0de)), @"matchRange[%d] = %@", x, NSStringFromRange(matchRanges[x])); }
}
//- (void)testGetRangesExceptionsAndCornerCases
- (void)mt_test_7
{
NSString *regexString = @"^(Match)\\s+the\\s+(MAGIC)";
NSString *subjectString = @"Match the MAGIC in this string";
unsigned int subjectLength = [subjectString length], captureCount = 0, x = 0;
NSRange *matchRanges = NULL;
RKRegex *regex = [RKRegex regexWithRegexString:regexString options:0];
STAssertNotNil(regex, nil); if(regex == nil) { return; }
captureCount = [regex captureCount];
STAssertTrue(captureCount == 3, @"captureCount is %u", captureCount); if(captureCount != 3) { return; }
matchRanges = alloca(captureCount * sizeof(NSRange));
STAssertTrue(matchRanges != NULL, nil); if(matchRanges == NULL) { return; }
RKMatchErrorCode errorCode = RKMatchErrorNoError;
// First check if it works.
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); } errorCode = RKMatchErrorNoError;
errorCode = [regex getRanges:matchRanges withCharacters:[subjectString UTF8String] length:subjectLength inRange:NSMakeRange(0, subjectLength) options:0];
STAssertTrue(errorCode == 3, @"errorCode = %d", errorCode);
STAssertTrue(NSEqualRanges(NSMakeRange(0, 15), matchRanges[0]), @"matchRange[0] = %@", NSStringFromRange(matchRanges[0]));
STAssertTrue(NSEqualRanges(NSMakeRange(0, 5), matchRanges[1]), @"matchRange[1] = %@", NSStringFromRange(matchRanges[1]));
STAssertTrue(NSEqualRanges(NSMakeRange(10, 5), matchRanges[2]), @"matchRange[2] = %@", NSStringFromRange(matchRanges[2]));
/// getRanges NULL
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); } errorCode = RKMatchErrorNoError;
STAssertThrowsSpecificNamed(errorCode = [regex getRanges:NULL withCharacters:[subjectString UTF8String] length:subjectLength inRange:NSMakeRange(0, subjectLength) options:0], NSException, NSInvalidArgumentException, nil);
STAssertTrue(errorCode == RKMatchErrorNoError, @"errorCode = %d", errorCode);
for(x = 0; x < captureCount; x++) { STAssertTrue(NSEqualRanges(matchRanges[x], NSMakeRange(0xdeadbeef, 0x0badc0de)), @"matchRange[%d] = %@", x, NSStringFromRange(matchRanges[x])); }
/// withCharacters NULL
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); } errorCode = RKMatchErrorNoError;
STAssertThrowsSpecificNamed(errorCode = [regex getRanges:matchRanges withCharacters:NULL length:subjectLength inRange:NSMakeRange(0, subjectLength) options:0], NSException, NSInvalidArgumentException, nil);
STAssertTrue(errorCode == RKMatchErrorNoError, @"errorCode = %d", errorCode);
for(x = 0; x < captureCount; x++) { STAssertTrue(NSEqualRanges(matchRanges[x], NSMakeRange(0xdeadbeef, 0x0badc0de)), @"matchRange[%d] = %@", x, NSStringFromRange(matchRanges[x])); }
/// Bad options
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); } errorCode = RKMatchErrorNoError;
STAssertNoThrow(errorCode = [regex getRanges:matchRanges withCharacters:[subjectString UTF8String] length:subjectLength inRange:NSMakeRange(0, subjectLength) options:0xffffffff], nil);
STAssertTrue(errorCode == RKMatchErrorBadOption, @"errorCode = %d", errorCode);
for(x = 0; x < captureCount; x++) { STAssertTrue(NSEqualRanges(matchRanges[x], NSMakeRange(0xdeadbeef, 0x0badc0de)), @"matchRange[%d] = %@", x, NSStringFromRange(matchRanges[x])); }
/// inRange start location exactly at the end of buffer, with a length of 0
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); } errorCode = RKMatchErrorNoError;
STAssertNoThrow(errorCode = [regex getRanges:matchRanges withCharacters:[subjectString UTF8String] length:subjectLength inRange:NSMakeRange(subjectLength, 0) options:0], nil);
STAssertTrue(errorCode == RKMatchErrorNoMatch, @"errorCode = %d", errorCode);
for(x = 0; x < captureCount; x++) { STAssertTrue(NSEqualRanges(matchRanges[x], NSMakeRange(0xdeadbeef, 0x0badc0de)), @"matchRange[%d] = %@", x, NSStringFromRange(matchRanges[x])); }
/// inRange start location exactly at the end of buffer, with a length of 1
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); } errorCode = RKMatchErrorNoError;
STAssertThrowsSpecificNamed(errorCode = [regex getRanges:matchRanges withCharacters:[subjectString UTF8String] length:subjectLength inRange:NSMakeRange(subjectLength, 1) options:0], NSException, NSRangeException, nil);
STAssertTrue(errorCode == RKMatchErrorNoError, @"errorCode = %d", errorCode);
for(x = 0; x < captureCount; x++) { STAssertTrue(NSEqualRanges(matchRanges[x], NSMakeRange(0xdeadbeef, 0x0badc0de)), @"matchRange[%d] = %@", x, NSStringFromRange(matchRanges[x])); }
/// inRange start location > buffer length
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); } errorCode = RKMatchErrorNoError;
STAssertThrowsSpecificNamed(errorCode = [regex getRanges:matchRanges withCharacters:[subjectString UTF8String] length:subjectLength inRange:NSMakeRange(subjectLength + 1, 1) options:0], NSException, NSRangeException, nil);
STAssertTrue(errorCode == RKMatchErrorNoError, @"errorCode = %d", errorCode);
for(x = 0; x < captureCount; x++) { STAssertTrue(NSEqualRanges(matchRanges[x], NSMakeRange(0xdeadbeef, 0x0badc0de)), @"matchRange[%d] = %@", x, NSStringFromRange(matchRanges[x])); }
/// inRange start location at one character before end of buffer, with a length of 2 exceeding buffer length
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); } errorCode = RKMatchErrorNoError;
STAssertThrowsSpecificNamed(errorCode = [regex getRanges:matchRanges withCharacters:[subjectString UTF8String] length:subjectLength inRange:NSMakeRange(subjectLength - 1, 2) options:0], NSException, NSRangeException, nil);
STAssertTrue(errorCode == RKMatchErrorNoError, @"errorCode = %d", errorCode);
for(x = 0; x < captureCount; x++) { STAssertTrue(NSEqualRanges(matchRanges[x], NSMakeRange(0xdeadbeef, 0x0badc0de)), @"matchRange[%d] = %@", x, NSStringFromRange(matchRanges[x])); }
/// inRange start location at one character before end of buffer, with a length of 1 within buffer
for(x = 0; x < captureCount; x++) { matchRanges[x] = NSMakeRange(0xdeadbeef, 0x0badc0de); } errorCode = RKMatchErrorNoError;
STAssertNoThrow(errorCode = [regex getRanges:matchRanges withCharacters:[subjectString UTF8String] length:subjectLength inRange:NSMakeRange(subjectLength - 1, 1) options:0], nil);
STAssertTrue(errorCode == RKMatchErrorNoMatch, @"errorCode = %d", errorCode);
for(x = 0; x < captureCount; x++) { STAssertTrue(NSEqualRanges(matchRanges[x], NSMakeRange(0xdeadbeef, 0x0badc0de)), @"matchRange[%d] = %@", x, NSStringFromRange(matchRanges[x])); }
}
//- (void)testSimpleCaptureNameException
- (void)mt_test_8
{
NSString *regexString = @"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) )";
RKRegex *regex = [RKRegex regexWithRegexString:regexString options:0];
STAssertThrowsSpecificNamed([regex captureIndexForCaptureName:nil], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([regex captureIndexForCaptureName:@"Doesn't exist"], NSException, NSInvalidArgumentException, nil);
}
//- (void)testSimpleCaptureName
- (void)mt_test_9
{
NSString *regexString = @"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) )";
RKRegex *regex = [RKRegex regexWithRegexString:regexString options:0];
STAssertNotNil(regex, nil); if(regex == nil) { return; }
STAssertTrue([regex captureCount] == 6, @"count: %u", [regex captureCount]);
NSArray *regexCaptureNameArray = [regex captureNameArray];
STAssertNotNil(regexCaptureNameArray, nil);
STAssertTrue([regexCaptureNameArray count] == 6, @"count: %u", [regexCaptureNameArray count]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:0] isEqualTo:[NSNull null]], @"== %@", [regexCaptureNameArray objectAtIndex:0]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:1] isEqualToString:@"date"], @"== %@", [regexCaptureNameArray objectAtIndex:1]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:2] isEqualToString:@"year"], @"== %@", [regexCaptureNameArray objectAtIndex:2]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:3] isEqualTo:[NSNull null]], @"== %@", [regexCaptureNameArray objectAtIndex:3]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:4] isEqualToString:@"month"], @"== %@", [regexCaptureNameArray objectAtIndex:4]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:5] isEqualToString:@"day"], @"== %@", [regexCaptureNameArray objectAtIndex:5]);
STAssertTrue([regex captureIndexForCaptureName:@"date"] == 1, @"value: %u", [regex captureIndexForCaptureName:@"date"]);
STAssertTrue([regex captureIndexForCaptureName:@"year"] == 2, @"value: %u", [regex captureIndexForCaptureName:@"year"]);
STAssertTrue([regex captureIndexForCaptureName:@"month"] == 4, @"value: %u", [regex captureIndexForCaptureName:@"month"]);
STAssertTrue([regex captureIndexForCaptureName:@"day"] == 5, @"value: %u", [regex captureIndexForCaptureName:@"day"]);
STAssertThrows([regex captureIndexForCaptureName:@"UNKNOWN"], nil);
STAssertThrows([regex captureIndexForCaptureName:nil], nil);
}
//- (void)testDupCaptureName
- (void)mt_test_10
{
NSString *regexString = @"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))";
STAssertThrowsSpecificNamed([RKRegex regexWithRegexString:regexString options:0], NSException, RKRegexSyntaxErrorException, nil); // needs to have dup names option
RKRegex *regex = [RKRegex regexWithRegexString:regexString options:RKCompileDupNames];
STAssertNotNil(regex, nil); if(regex == nil) { return; }
STAssertTrue([regex captureCount] == 7, @"count: %u", [regex captureCount]);
NSArray *regexCaptureNameArray = [regex captureNameArray];
STAssertNotNil(regexCaptureNameArray, nil);
STAssertTrue([regexCaptureNameArray count] == 7, @"count: %u", [regexCaptureNameArray count]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:0] isEqualTo:[NSNull null]], @"== %@", [regexCaptureNameArray objectAtIndex:0]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:1] isEqualToString:@"date"], @"== %@", [regexCaptureNameArray objectAtIndex:1]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:2] isEqualToString:@"year"], @"== %@", [regexCaptureNameArray objectAtIndex:2]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:3] isEqualTo:[NSNull null]], @"== %@", [regexCaptureNameArray objectAtIndex:3]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:4] isEqualToString:@"month"], @"== %@", [regexCaptureNameArray objectAtIndex:4]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:5] isEqualToString:@"day"], @"== %@", [regexCaptureNameArray objectAtIndex:5]);
STAssertTrue([[regexCaptureNameArray objectAtIndex:6] isEqualToString:@"month"], @"== %@", [regexCaptureNameArray objectAtIndex:6]);
STAssertTrue([regex captureIndexForCaptureName:@"date"] == 1, @"value: %u", [regex captureIndexForCaptureName:@"date"]);
STAssertTrue([regex captureIndexForCaptureName:@"year"] == 2, @"value: %u", [regex captureIndexForCaptureName:@"year"]);
STAssertTrue([regex captureIndexForCaptureName:@"month"] == 4, @"value: %u", [regex captureIndexForCaptureName:@"month"]); // Only the lowest index is returned
STAssertTrue([regex captureIndexForCaptureName:@"day"] == 5, @"value: %u", [regex captureIndexForCaptureName:@"day"]);
STAssertThrows([regex captureIndexForCaptureName:@"UNKNOWN"], nil);
STAssertThrows([regex captureIndexForCaptureName:nil], nil);
}
//- (void)testRegexString
- (void)mt_test_11
{
RKRegex *regex = [RKRegex regexWithRegexString:@"123" options:0];
STAssertTrue([[regex regexString] isEqualToString:@"123"], nil);
regex = [RKRegex regexWithRegexString:@"^(Match)\\s+the\\s+(MAGIC)$" options:0];
STAssertTrue([[regex regexString] isEqualToString:@"^(Match)\\s+the\\s+(MAGIC)$"], nil);
}
//- (void)testValidRegexString
- (void)mt_test_12
{
STAssertTrue([RKRegex isValidRegexString:@"123" options:0], nil);
STAssertTrue([RKRegex isValidRegexString:@"^(Match)\\s+the\\s+(MAGIC)$" options:0], nil);
STAssertTrue([RKRegex isValidRegexString:@"(?<pn> \\( ( (?>[^()]+) | (?&pn) )* \\) )" options:0], nil);
STAssertTrue([RKRegex isValidRegexString:@"\\( ( ( (?>[^()]+) | (?R) )* ) \\)" options:0], nil);
STAssertTrue([RKRegex isValidRegexString:@"\\( ( ( (?>[^()]+) | (?R) )* ) \\)" options:0], nil);
STAssertTrue([RKRegex isValidRegexString:@"\\( ( ( ([^()]+) | (?R) )* ) \\)" options:0], nil);
STAssertThrowsSpecificNamed([RKRegex regexWithRegexString:@"^(Match)\\s+the\\s+((MAGIC)$" options:0xffffffff], NSException, RKRegexSyntaxErrorException, nil);
STAssertNoThrow([RKRegex isValidRegexString:@"(?<pn> \\( ( (?>[^()]+) | (?&xq) )* \\) )" options:0], nil);
STAssertNoThrow([RKRegex isValidRegexString:nil options:0], nil);
STAssertFalse([RKRegex isValidRegexString:nil options:0], nil);
STAssertNoThrow([RKRegex isValidRegexString:@"\\( ( ( ([^()]+) | (?R) )* ) \\)" options:0xffffffff], nil);
STAssertFalse([RKRegex isValidRegexString:@"\\( ( ( ([^()]+) | (?R) )* ) \\)" options:0xffffffff], nil);
}
//- (void)testSimpleMatches
- (void)mt_test_13
{
STAssertTrue([@" 012345 " isMatchedByRegex:[RKRegex regexWithRegexString:@"123" options:0]], nil);
STAssertTrue([@" 012345 " isMatchedByRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(2, 3)], nil);
STAssertTrue([[RKRegex regexWithRegexString:@"123" options:0] matchesCharacters:" 012345 " length:strlen(" 012345 ") inRange:NSMakeRange(2, 3) options:0], nil);
STAssertFalse([@" 012345 " isMatchedByRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(1, 2)], nil);
STAssertFalse([@" 012345 " isMatchedByRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(3, 2)], nil);
STAssertFalse([[RKRegex regexWithRegexString:@"123" options:0] matchesCharacters:" 012345 " length:strlen(" 012345 ") inRange:NSMakeRange(1, 2) options:0], nil);
STAssertFalse([[RKRegex regexWithRegexString:@"123" options:0] matchesCharacters:" 012345 " length:strlen(" 012345 ") inRange:NSMakeRange(3, 2) options:0], nil);
STAssertThrowsSpecificNamed([@" 012345 " isMatchedByRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(400, 16)], NSException, NSRangeException, nil);
STAssertThrowsSpecificNamed([[RKRegex regexWithRegexString:@"123" options:0] matchesCharacters:" 012345 " length:strlen(" 012345 ") inRange:NSMakeRange(400, 16) options:0], NSException, NSRangeException, nil);
STAssertThrowsSpecificNamed([[RKRegex regexWithRegexString:@"123" options:0] matchesCharacters:" 012345 " length:0 inRange:NSMakeRange(0, 8) options:0], NSException, NSRangeException, nil);
STAssertThrowsSpecificNamed([[RKRegex regexWithRegexString:@"123" options:0] matchesCharacters:NULL length:strlen(" 012345 ") inRange:NSMakeRange(400, 16) options:0], NSException, NSInvalidArgumentException, nil);
}
//- (void)testSimpleRangeForCharacters
- (void)mt_test_14
{
NSRange matchRange = NSMakeRange(0, 0);
STAssertTrue(NSEqualRanges((matchRange = [@" 012345 " rangeOfRegex:[RKRegex regexWithRegexString:@"123" options:0]]), NSMakeRange(2, 3)), @"matchRange = %@", NSStringFromRange(matchRange));
STAssertTrue(NSEqualRanges((matchRange = [@" 012345 " rangeOfRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(2, 3) capture:0]), NSMakeRange(2, 3)), @"matchRange = %@", NSStringFromRange(matchRange));
STAssertTrue(NSEqualRanges((matchRange = [@" 012345 " rangeOfRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(1,2) capture:0]), NSMakeRange(NSNotFound, 0)), @"matchRange = %@", NSStringFromRange(matchRange));
STAssertTrue(NSEqualRanges((matchRange = [@" 012345 " rangeOfRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(3,2) capture:0]), NSMakeRange(NSNotFound, 0)), @"matchRange = %@", NSStringFromRange(matchRange));
STAssertThrowsSpecificNamed([@" 012345 " rangeOfRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(400, 16) capture:0], NSException, NSRangeException, nil);
STAssertThrowsSpecificNamed([[RKRegex regexWithRegexString:@"123" options:0] rangeForCharacters:nil length:strlen(" 012345 ") inRange:NSMakeRange(400, 16) captureIndex:23 options:RKMatchNoOptions], NSException, NSInvalidArgumentException, nil);
}
//- (void)testSimpleRangesData
- (void)mt_test_15
{
NSRange *matchRanges = NULL;
matchRanges = NULL;
STAssertNoThrow(matchRanges = [@" 012345 " rangesOfRegex:[RKRegex regexWithRegexString:@"123" options:0]], nil);
STAssertTrue(matchRanges != NULL, nil); if(matchRanges == NULL) { return; }
STAssertTrue(NSEqualRanges(matchRanges[0], NSMakeRange(2, 3)), @"matchRanges[0] = %@", NSStringFromRange(matchRanges[0]));
matchRanges = NULL;
STAssertNoThrow(matchRanges = [@" 012345 " rangesOfRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(2, 3)], nil);
STAssertTrue(matchRanges != NULL, nil); if(matchRanges == NULL) { return; }
STAssertTrue(NSEqualRanges(matchRanges[0], NSMakeRange(2, 3)), @"matchRanges[0] = %@", NSStringFromRange(matchRanges[0]));
matchRanges = NULL;
STAssertNoThrow(matchRanges = [[RKRegex regexWithRegexString:@"123" options:0] rangesForCharacters:" 012345 " length:strlen(" 012345 ") inRange:NSMakeRange(2, 3) options:0], nil);
STAssertTrue(matchRanges != NULL, nil); if(matchRanges == NULL) { return; }
STAssertTrue(NSEqualRanges(matchRanges[0], NSMakeRange(2, 3)), @"matchRanges[0] = %@", NSStringFromRange(matchRanges[0]));
matchRanges = NULL;
STAssertNoThrow(matchRanges = [@" 012345 " rangesOfRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(1, 2)], nil);
STAssertTrue(matchRanges == NULL, nil);
matchRanges = NULL;
STAssertNoThrow(matchRanges = [@" 012345 " rangesOfRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(3, 2)], nil);
STAssertTrue(matchRanges == NULL, nil);
matchRanges = NULL;
STAssertNoThrow(matchRanges = [[RKRegex regexWithRegexString:@"123" options:0] rangesForCharacters:" 012345 " length:strlen(" 012345 ") inRange:NSMakeRange(1, 2) options:0], nil);
STAssertTrue(matchRanges == NULL, nil);
matchRanges = NULL;
STAssertNoThrow(matchRanges = [[RKRegex regexWithRegexString:@"123" options:0] rangesForCharacters:" 012345 " length:strlen(" 012345 ") inRange:NSMakeRange(3, 2) options:0], nil);
STAssertTrue(matchRanges == NULL, nil);
matchRanges = NULL;
STAssertThrowsSpecificNamed(matchRanges = [@" 012345 " rangesOfRegex:[RKRegex regexWithRegexString:@"123" options:0] inRange:NSMakeRange(400, 16)], NSException, NSRangeException, nil);
STAssertTrue(matchRanges == NULL, nil);
matchRanges = NULL;
STAssertThrowsSpecificNamed(matchRanges = [[RKRegex regexWithRegexString:@"123" options:0] rangesForCharacters:" 012345 " length:strlen(" 012345 ") inRange:NSMakeRange(400, 16) options:0], NSException, NSRangeException, nil);
STAssertTrue(matchRanges == NULL, nil);
matchRanges = NULL;
STAssertThrowsSpecificNamed(matchRanges = [[RKRegex regexWithRegexString:@"123" options:0] rangesForCharacters:" 012345 " length:0 inRange:NSMakeRange(0, 8) options:0], NSException, NSRangeException, nil);
STAssertTrue(matchRanges == NULL, nil);
matchRanges = NULL;
STAssertThrowsSpecificNamed(matchRanges = [[RKRegex regexWithRegexString:@"123" options:0] rangesForCharacters:NULL length:strlen(" 012345 ") inRange:NSMakeRange(0, 8) options:0], NSException, NSInvalidArgumentException, nil);
STAssertTrue(matchRanges == NULL, nil);
}
- (void)mt_test_16
{
//Old range value tests.
}
//- (void)testCaptureReferenceBadSyntaxTests
- (void)mt_test_17
{
NSString *subjectString = nil, *regexString = nil;//, *captureString = nil;
int intType = 7;
subjectString = @"12345";
regexString = @"(\\d+)";
STAssertTrue(intType == 7, nil);
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"${1:d}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"$:d}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"$1:d}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"${1:d", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"1:d", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"1:%", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"1:", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"2", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"${a:d}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"$a:d}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"${a:d", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"a:d}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"a:", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"a", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"${1:%}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"$1:%}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"1:%}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"${1:%", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"1:%", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"${1:@}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"$1:@}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"1:@}", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"${1:@", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"1:@", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"1:@z", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"$$1:", &intType, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(intType == 7, @"int value is: %d", intType); intType = 7;
}
//- (void)testNumericCaptureReferenceConversionSimpleTests
- (void)mt_test_18
{
NSString *subjectString = nil, *regexString = nil, *captureString = nil;
int intType = 7;
subjectString = @"12345";
regexString = @"(\\d+)";
STAssertTrue(intType == 7, nil);
captureString = nil;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"${zip:%d}", &captureString, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(captureString == nil, nil);
STAssertNoThrow(([subjectString getCapturesWithRegexAndReferences:regexString, @"${1:%d}", &intType, nil]), nil);
STAssertTrue(intType == 12345, nil);
captureString = nil;
STAssertTrue(captureString == nil, nil);
STAssertNoThrow(([subjectString getCapturesWithRegexAndReferences:regexString, @"${1}", &captureString, nil]), nil);
STAssertTrue(captureString != nil, nil);
STAssertTrue([captureString isEqualToString:@"12345"], nil);
}
//- (void)testNamedCaptureReferenceConversionSimpleTests
- (void)mt_test_19
{
NSString *subjectString = nil, *regexString = nil, *captureString = nil;
int intType = 7;
subjectString = @"12345";
regexString = @"(?<num>\\d+)";
STAssertTrue(intType == 7, nil);
captureString = nil;
STAssertThrowsSpecificNamed(([subjectString getCapturesWithRegexAndReferences:regexString, @"${zip:%d}", &captureString, nil]), NSException, RKRegexCaptureReferenceException, nil);
STAssertTrue(captureString == nil, nil);
STAssertNoThrow(([subjectString getCapturesWithRegexAndReferences:regexString, @"${num:%d}", &intType, nil]), nil);
STAssertTrue(intType == 12345, nil);
captureString = nil;
STAssertTrue(captureString == nil, nil);
STAssertNoThrow(([subjectString getCapturesWithRegexAndReferences:regexString, @"${num}", &captureString, nil]), nil);
STAssertTrue(captureString != nil, nil);
STAssertTrue([captureString isEqualToString:@"12345"], nil);
}
//- (void)testNumericCaptureReferenceNSNumberConversionSimpleTests
- (void)mt_test_20
{
NSNumber *number1 = nil, *number2 = nil;
STAssertNoThrow(([@"123, 456" getCapturesWithRegexAndReferences:@"(\\d+), (\\d+)", @"${1:@n}", &number1, @"${2:@n}", &number2, nil]), nil);
STAssertTrue(number1 != nil, nil); STAssertTrue(number2 != nil, nil);
number1 = number2 = nil;
STAssertNoThrow(([@"$123, 99.5%" getCapturesWithRegexAndReferences:@"([^,]*), (.*)", @"${1:@$n}", &number1, @"${2:@%n}", &number2, nil]), nil);
STAssertTrue(number1 != nil, nil); STAssertTrue(number2 != nil, nil);
number1 = number2 = nil;
STAssertNoThrow(([@"forty-two, 100,954,123.92" getCapturesWithRegexAndReferences:@"([^,]*), (.*)", @"${1:@wn}", &number1, @"${2:@.n}", &number2, nil]), nil);
STAssertTrue(number1 != nil, nil); STAssertTrue(number2 != nil, nil);
number1 = number2 = nil;
STAssertNoThrow(([@"2.657066311614524e+78, $5,917.23" getCapturesWithRegexAndReferences:@"([^,]*), (.*)", @"${1:@sn}", &number1, @"${2:@$n}", &number2, nil]), nil);
STAssertTrue(number1 != nil, nil); STAssertTrue(number2 != nil, nil);
}
//- (void)testNamedCaptureReferenceNSNumberConversionSimpleTests
- (void)mt_test_21
{
NSNumber *number1 = nil, *number2 = nil;
STAssertNoThrow(([@"123, 456" getCapturesWithRegexAndReferences:@"(?<num1>\\d+), (?<num2>\\d+)", @"${num1:@n}", &number1, @"${num2:@n}", &number2, nil]), nil);
STAssertTrue(number1 != nil, nil); STAssertTrue(number2 != nil, nil);
number1 = number2 = nil;
STAssertNoThrow(([@"$123, 99.5%" getCapturesWithRegexAndReferences:@"(?<num1>[^,]*), (?<num2>.*)", @"${num1:@$n}", &number1, @"${num2:@%n}", &number2, nil]), nil);
STAssertTrue(number1 != nil, nil); STAssertTrue(number2 != nil, nil);
number1 = number2 = nil;
STAssertNoThrow(([@"forty-two, 100,954,123.92" getCapturesWithRegexAndReferences:@"(?<num1>[^,]*), (?<num2>.*)", @"${num1:@wn}", &number1, @"${num2:@.n}", &number2, nil]), nil);
STAssertTrue(number1 != nil, nil); STAssertTrue(number2 != nil, nil);
number1 = number2 = nil;
STAssertNoThrow(([@"2.657066311614524e+78, $5,917.23" getCapturesWithRegexAndReferences:@"(?<num1>[^,]*), (?<num2>.*)", @"${num1:@sn}", &number1, @"${num2:@$n}", &number2, nil]), nil);
STAssertTrue(number1 != nil, nil); STAssertTrue(number2 != nil, nil);
}
//- (void)testNumericCaptureReferenceNSDateFormatterConversionSimpleTests
- (void)mt_test_22
{
id dateCapture = nil;
STAssertNoThrow(([@"07/20/2007" getCapturesWithRegexAndReferences:@"(.*)", @"${1:@d}", &dateCapture, nil]), nil);
STAssertTrue(dateCapture != nil, nil);
STAssertTrue([dateCapture monthOfYear] == 7, [NSString stringWithFormat:@"monthOfYear == %d object ='%@'", [dateCapture monthOfYear], dateCapture]);
STAssertTrue([dateCapture dayOfMonth] == 20, [NSString stringWithFormat:@"dayOfMonth == %d object ='%@'", [dateCapture dayOfMonth], dateCapture]);
STAssertTrue([dateCapture yearOfCommonEra] == 2007, [NSString stringWithFormat:@"yearOfCommonEra == %d object ='%@'", [dateCapture yearOfCommonEra], dateCapture]);
dateCapture = nil;
STAssertNoThrow(([@"6:44 PM" getCapturesWithRegexAndReferences:@"(.*)", @"${1:@d}", &dateCapture, nil]), nil);
STAssertTrue(dateCapture != nil, nil);
STAssertTrue([dateCapture hourOfDay] == 18, [NSString stringWithFormat:@"hourOfDay == %d object = '%@'", [dateCapture hourOfDay], dateCapture]);
STAssertTrue([dateCapture minuteOfHour] == 44, [NSString stringWithFormat:@"minuteOfHour == %d object ='%@'", [dateCapture minuteOfHour], dateCapture]);
dateCapture = nil;
STAssertNoThrow(([@"Feb 5th" getCapturesWithRegexAndReferences:@"(.*)", @"${1:@d}", &dateCapture, nil]), nil);
STAssertTrue(dateCapture != nil, nil);
STAssertTrue([dateCapture monthOfYear] == 2, [NSString stringWithFormat:@"monthOfYear == %d object ='%@'", [dateCapture monthOfYear], dateCapture]);
STAssertTrue([dateCapture dayOfMonth] == 5, [NSString stringWithFormat:@"dayOfMonth == %d object ='%@'", [dateCapture dayOfMonth], dateCapture]);
dateCapture = nil;
STAssertNoThrow(([@"6/20/2007, 11:34PM EDT" getCapturesWithRegexAndReferences:@"(.*)", @"${1:@d}", &dateCapture, nil]), nil);
STAssertTrue(dateCapture != nil, nil);
STAssertTrue([dateCapture monthOfYear] == 6, [NSString stringWithFormat:@"monthOfYear == %d object ='%@'", [dateCapture monthOfYear], dateCapture]);
STAssertTrue([dateCapture dayOfMonth] == 20, [NSString stringWithFormat:@"dayOfMonth == %d object ='%@'", [dateCapture dayOfMonth], dateCapture]);
STAssertTrue([dateCapture yearOfCommonEra] == 2007, [NSString stringWithFormat:@"yearOfCommonEra == %d object ='%@'", [dateCapture yearOfCommonEra], dateCapture]);
STAssertTrue([dateCapture hourOfDay] == 23, [NSString stringWithFormat:@"hourOfDay == %d object ='%@'", [dateCapture hourOfDay], dateCapture]);
STAssertTrue([dateCapture minuteOfHour] == 34, [NSString stringWithFormat:@"minuteOfHour == %d object ='%@'", [dateCapture minuteOfHour], dateCapture]);
STAssertTrue([[dateCapture timeZone] isEqualToTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EDT"]] == YES, [NSString stringWithFormat:@"timeZone name: %@, abbreviation: %@", [[dateCapture timeZone] name], [[dateCapture timeZone] abbreviation]]);
}
//- (void)testNamedCaptureReferenceNSDateFormatterConversionSimpleTests
- (void)mt_test_23
{
id dateCapture = nil;
STAssertNoThrow(([@"07/20/2007" getCapturesWithRegexAndReferences:@"(.*)", @"${1:@d}", &dateCapture, nil]), nil);
STAssertTrue(dateCapture != nil, nil);
STAssertTrue([dateCapture monthOfYear] == 7, [NSString stringWithFormat:@"monthOfYear == %d object ='%@'", [dateCapture monthOfYear], dateCapture]);
STAssertTrue([dateCapture dayOfMonth] == 20, [NSString stringWithFormat:@"dayOfMonth == %d object ='%@'", [dateCapture dayOfMonth], dateCapture]);
STAssertTrue([dateCapture yearOfCommonEra] == 2007, [NSString stringWithFormat:@"yearOfCommonEra == %d object ='%@'", [dateCapture yearOfCommonEra], dateCapture]);
dateCapture = nil;
STAssertNoThrow(([@"6:44 PM" getCapturesWithRegexAndReferences:@"(.*)", @"${1:@d}", &dateCapture, nil]), nil);
STAssertTrue(dateCapture != nil, nil);
STAssertTrue([dateCapture hourOfDay] == 18, [NSString stringWithFormat:@"hourOfDay == %d object = '%@'", [dateCapture hourOfDay], dateCapture]);
STAssertTrue([dateCapture minuteOfHour] == 44, [NSString stringWithFormat:@"minuteOfHour == %d object ='%@'", [dateCapture minuteOfHour], dateCapture]);
dateCapture = nil;
STAssertNoThrow(([@"Feb 5th" getCapturesWithRegexAndReferences:@"(.*)", @"${1:@d}", &dateCapture, nil]), nil);
STAssertTrue(dateCapture != nil, nil);
STAssertTrue([dateCapture monthOfYear] == 2, [NSString stringWithFormat:@"monthOfYear == %d object ='%@'", [dateCapture monthOfYear], dateCapture]);
STAssertTrue([dateCapture dayOfMonth] == 5, [NSString stringWithFormat:@"dayOfMonth == %d object ='%@'", [dateCapture dayOfMonth], dateCapture]);
dateCapture = nil;
STAssertNoThrow(([@"6/20/2007, 11:34PM EDT" getCapturesWithRegexAndReferences:@"(.*)", @"${1:@d}", &dateCapture, nil]), nil);
STAssertTrue(dateCapture != nil, nil);
STAssertTrue([dateCapture monthOfYear] == 6, [NSString stringWithFormat:@"monthOfYear == %d object ='%@'", [dateCapture monthOfYear], dateCapture]);
STAssertTrue([dateCapture dayOfMonth] == 20, [NSString stringWithFormat:@"dayOfMonth == %d object ='%@'", [dateCapture dayOfMonth], dateCapture]);
STAssertTrue([dateCapture yearOfCommonEra] == 2007, [NSString stringWithFormat:@"yearOfCommonEra == %d object ='%@'", [dateCapture yearOfCommonEra], dateCapture]);
STAssertTrue([dateCapture hourOfDay] == 23, [NSString stringWithFormat:@"hourOfDay == %d object ='%@'", [dateCapture hourOfDay], dateCapture]);
STAssertTrue([dateCapture minuteOfHour] == 34, [NSString stringWithFormat:@"minuteOfHour == %d object ='%@'", [dateCapture minuteOfHour], dateCapture]);
STAssertTrue([[dateCapture timeZone] isEqualToTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EDT"]] == YES, [NSString stringWithFormat:@"timeZone name: %@, abbreviation: %@", [[dateCapture timeZone] name], [[dateCapture timeZone] abbreviation]]);
}
//- (void)testArrayExtensions
- (void)mt_test_24
{
NSArray *testArray = NULL, *matchedArray = NULL;
NSMutableArray *mutableArray = NULL;
STAssertTrueNoThrow((testArray = [NSArray arrayWithObjects:@"zero 0 aaa", @"one 1 aab", @"two 2 ab", @"three 3 bab", @"four 4 aba ", @"five 5 baa", @"six 6 bbb", nil]) != NULL, nil);
STAssertTrueNoThrow((matchedArray = [testArray arrayByMatchingObjectsWithRegex:@"ab"]) != NULL, nil);
//ab array : ("one 1 aab", "two 2 ab", "three 3 bab", "four 4 aba ")
STAssertTrue([matchedArray count] == 4, @"Count is %u", [matchedArray count]);
STAssertTrue([[matchedArray objectAtIndex:0] isEqualToString:@"one 1 aab"], nil);
STAssertTrue([[matchedArray objectAtIndex:1] isEqualToString:@"two 2 ab"], nil);
STAssertTrue([[matchedArray objectAtIndex:2] isEqualToString:@"three 3 bab"], nil);
STAssertTrue([[matchedArray objectAtIndex:3] isEqualToString:@"four 4 aba "], nil);
STAssertTrueNoThrow((matchedArray = [testArray arrayByMatchingObjectsWithRegex:@"aa"]) != NULL, nil);
//aa array : ("zero 0 aaa", "one 1 aab", "five 5 baa")
STAssertTrue([matchedArray count] == 3, @"Count is %u", [matchedArray count]);
STAssertTrue([[matchedArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[matchedArray objectAtIndex:1] isEqualToString:@"one 1 aab"], nil);
STAssertTrue([[matchedArray objectAtIndex:2] isEqualToString:@"five 5 baa"], nil);
STAssertTrueNoThrow((matchedArray = [testArray arrayByMatchingObjectsWithRegex:@"ba"]) != NULL, nil);
//ba array : ("three 3 bab", "four 4 aba ", "five 5 baa")
STAssertTrue([matchedArray count] == 3, @"Count is %u", [matchedArray count]);
STAssertTrue([[matchedArray objectAtIndex:0] isEqualToString:@"three 3 bab"], nil);
STAssertTrue([[matchedArray objectAtIndex:1] isEqualToString:@"four 4 aba "], nil);
STAssertTrue([[matchedArray objectAtIndex:2] isEqualToString:@"five 5 baa"], nil);
STAssertTrueNoThrow((matchedArray = [testArray arrayByMatchingObjectsWithRegex:@"o"]) != NULL, nil);
//o array : ("zero 0 aaa", "one 1 aab", "two 2 ab", "four 4 aba ")
STAssertTrue([matchedArray count] == 4, @"Count is %u", [matchedArray count]);
STAssertTrue([[matchedArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[matchedArray objectAtIndex:1] isEqualToString:@"one 1 aab"], nil);
STAssertTrue([[matchedArray objectAtIndex:2] isEqualToString:@"two 2 ab"], nil);
STAssertTrue([[matchedArray objectAtIndex:3] isEqualToString:@"four 4 aba "], nil);
STAssertTrueNoThrow((matchedArray = [testArray arrayByMatchingObjectsWithRegex:@"2|4|6"]) != NULL, nil);
//2|4|6 array : ("two 2 ab", "four 4 aba ", "six 6 bbb")
STAssertTrue([matchedArray count] == 3, @"Count is %u", [matchedArray count]);
STAssertTrue([[matchedArray objectAtIndex:0] isEqualToString:@"two 2 ab"], nil);
STAssertTrue([[matchedArray objectAtIndex:1] isEqualToString:@"four 4 aba "], nil);
STAssertTrue([[matchedArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
STAssertTrueNoThrow((mutableArray = [NSMutableArray arrayWithArray:testArray]) != NULL, nil);
//mutable array: ("zero 0 aaa", "one 1 aab", "two 2 ab", "three 3 bab", "four 4 aba ", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableArray count] == 7, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"one 1 aab"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"two 2 ab"], nil);
STAssertTrue([[mutableArray objectAtIndex:3] isEqualToString:@"three 3 bab"], nil);
STAssertTrue([[mutableArray objectAtIndex:4] isEqualToString:@"four 4 aba "], nil);
STAssertTrue([[mutableArray objectAtIndex:5] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:6] isEqualToString:@"six 6 bbb"], nil);
STAssertNoThrow([mutableArray addObjectsFromArray:testArray matchingRegex:@"ab"], nil);
//mutable array: ("zero 0 aaa", "one 1 aab", "two 2 ab", "three 3 bab", "four 4 aba ", "five 5 baa", "six 6 bbb", "one 1 aab", "two 2 ab", "three 3 bab", "four 4 aba ")
STAssertTrue([mutableArray count] == 11, @"Count is %u", [mutableArray count]);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"ab"] == 8, @"Count == %u", [mutableArray countOfObjectsMatchingRegex:@"ab"]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"one 1 aab"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"two 2 ab"], nil);
STAssertTrue([[mutableArray objectAtIndex:3] isEqualToString:@"three 3 bab"], nil);
STAssertTrue([[mutableArray objectAtIndex:4] isEqualToString:@"four 4 aba "], nil);
STAssertTrue([[mutableArray objectAtIndex:5] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:6] isEqualToString:@"six 6 bbb"], nil);
STAssertTrue([[mutableArray objectAtIndex:7] isEqualToString:@"one 1 aab"], nil);
STAssertTrue([[mutableArray objectAtIndex:8] isEqualToString:@"two 2 ab"], nil);
STAssertTrue([[mutableArray objectAtIndex:9] isEqualToString:@"three 3 bab"], nil);
STAssertTrue([[mutableArray objectAtIndex:10] isEqualToString:@"four 4 aba "], nil);
STAssertTrueNoThrow([mutableArray indexOfObjectMatchingRegex:@"^four"] == 4, @"Index is %u", [mutableArray indexOfObjectMatchingRegex:@"^four"]);
STAssertTrueNoThrow([mutableArray indexOfObjectMatchingRegex:@"^four" inRange:NSMakeRange(3, 7)] == 4, @"Index is %u", [mutableArray indexOfObjectMatchingRegex:@"^four" inRange:NSMakeRange(3, 7)]);
STAssertTrueNoThrow([mutableArray indexOfObjectMatchingRegex:@"^four" inRange:NSMakeRange(5, 5)] == NSNotFound, @"Index is %u", [mutableArray indexOfObjectMatchingRegex:@"^four" inRange:NSMakeRange(5, 5)]);
STAssertTrueNoThrow([mutableArray indexOfObjectMatchingRegex:@"^four" inRange:NSMakeRange(5, 6)] == 10, @"Index is %u", [mutableArray indexOfObjectMatchingRegex:@"^four" inRange:NSMakeRange(5, 6)]);
STAssertThrowsSpecificNamed([mutableArray indexOfObjectMatchingRegex:@"^four" inRange:NSMakeRange(5, 7)], NSException, NSRangeException, nil);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"^four"] == 2, @"Count is %u", [mutableArray countOfObjectsMatchingRegex:@"^four"]);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"^four" inRange:NSMakeRange(3, 8)] == 2, @"Count is %u", [mutableArray countOfObjectsMatchingRegex:@"^four" inRange:NSMakeRange(3, 8)]);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"^four" inRange:NSMakeRange(4, 7)] == 2, @"Count is %u", [mutableArray countOfObjectsMatchingRegex:@"^four" inRange:NSMakeRange(4, 7)]);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"^four" inRange:NSMakeRange(5, 6)] == 1, @"Count is %u", [mutableArray countOfObjectsMatchingRegex:@"^four" inRange:NSMakeRange(5, 6)]);
STAssertThrowsSpecificNamed([mutableArray countOfObjectsMatchingRegex:@"^four" inRange:NSMakeRange(5, 7)], NSException, NSRangeException, nil);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"ab" inRange:NSMakeRange(3, 5)] == 3, @"Count == %u", [mutableArray countOfObjectsMatchingRegex:@"ab" inRange:NSMakeRange(3, 5)]);
STAssertNoThrow([mutableArray removeObjectsMatchingRegex:@"ab" inRange:NSMakeRange(3, 5)], nil);
// mutable array: ("zero 0 aaa", "one 1 aab", "two 2 ab", "five 5 baa", "six 6 bbb", "two 2 ab", "three 3 bab", "four 4 aba ")
STAssertTrue([mutableArray count] == 8, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"one 1 aab"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"two 2 ab"], nil);
STAssertTrue([[mutableArray objectAtIndex:3] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:4] isEqualToString:@"six 6 bbb"], nil);
STAssertTrue([[mutableArray objectAtIndex:5] isEqualToString:@"two 2 ab"], nil);
STAssertTrue([[mutableArray objectAtIndex:6] isEqualToString:@"three 3 bab"], nil);
STAssertTrue([[mutableArray objectAtIndex:7] isEqualToString:@"four 4 aba "], nil);
STAssertNoThrow([mutableArray removeObjectsMatchingRegex:@"ab"], nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
// Same thing, should remain the same.
STAssertNoThrow([mutableArray removeObjectsMatchingRegex:@"ab"], nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
STAssertThrowsSpecificNamed([mutableArray containsObjectMatchingRegex:@"ab" inRange:NSMakeRange(23,42)], NSException, NSRangeException, nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
STAssertThrowsSpecificNamed([mutableArray removeObjectsMatchingRegex:@".*" inRange:NSMakeRange(13, 17)], NSException, NSRangeException, nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
STAssertNoThrow([mutableArray removeObjectsMatchingRegex:@".*" inRange:NSMakeRange(0, 0)], nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
STAssertNoThrow([mutableArray removeObjectsMatchingRegex:@".*" inRange:NSMakeRange(1, 0)], nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
STAssertNoThrow([mutableArray removeObjectsMatchingRegex:@".*" inRange:NSMakeRange(2, 0)], nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
STAssertNoThrow([mutableArray removeObjectsMatchingRegex:@".*" inRange:NSMakeRange(3, 0)], nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
STAssertNoThrow([mutableArray removeObjectsInRange:NSMakeRange(3, 0)], nil);
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
STAssertThrowsSpecificNamed([mutableArray removeObjectsMatchingRegex:@".*" inRange:NSMakeRange(4, 0)], NSException, NSRangeException, nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([[mutableArray objectAtIndex:0] isEqualToString:@"zero 0 aaa"], nil);
STAssertTrue([[mutableArray objectAtIndex:1] isEqualToString:@"five 5 baa"], nil);
STAssertTrue([[mutableArray objectAtIndex:2] isEqualToString:@"six 6 bbb"], nil);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"(?:a|b)" inRange:NSMakeRange(1, 1)] == 1, nil);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"^(?:five|six)" inRange:NSMakeRange(0, 2)] == 1, nil);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"^(?:five|six)" inRange:NSMakeRange(1, 2)] == 2, nil);
STAssertThrowsSpecificNamed([mutableArray removeObjectsMatchingRegex:@"^(?:five|six)" inRange:NSMakeRange(2, 2)], NSException, NSRangeException, nil);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"^(?:five|six)" inRange:NSMakeRange(2, 1)] == 1, nil);
STAssertTrueNoThrow([mutableArray countOfObjectsMatchingRegex:@"^(?:five|six)" inRange:NSMakeRange(0, 3)] == 2, nil);
STAssertTrueNoThrow((matchedArray = [mutableArray arrayByMatchingObjectsWithRegex:@"I don't wanna match"]) != NULL, nil);
STAssertTrue([mutableArray count] == 3, @"Count is %u", [mutableArray count]);
STAssertTrue([matchedArray count] == 0, @"Count is %u", [matchedArray count]);
STAssertThrowsSpecificNamed([mutableArray removeObjectsMatchingRegex:NULL inRange:NSMakeRange(0, 1)], NSException, NSInvalidArgumentException, nil);
}
//- (void)testDictionaryExtensions
- (void)mt_test_25
{
NSMutableDictionary *testDict = [NSMutableDictionary dictionary];
NSDictionary *regexDictionary = NULL, *staticDictionary = NULL;
NSArray *testObjects = NULL, *regexObjects = NULL;
STAssertNotNil(testDict, nil);
[testDict setObject:[NSNumber numberWithInt:0] forKey:@"zero aaa"];
[testDict setObject:[NSNumber numberWithInt:1] forKey:@"one aab"];
[testDict setObject:[NSNumber numberWithInt:2] forKey:@"two aba"];
[testDict setObject:[NSNumber numberWithInt:3] forKey:@"three baa"];
[testDict setObject:[NSNumber numberWithInt:4] forKey:@"four bab"];
STAssertTrue([testDict count] == 5, @"Count = %u", [testDict count]);
STAssertTrueNoThrow((staticDictionary = [NSDictionary dictionaryWithDictionary:testDict]) != NULL, nil);
STAssertTrueNoThrow((regexDictionary = [testDict dictionaryByMatchingKeysWithRegex:@"No! They're stealing my regex!"]) != NULL, nil);
STAssertTrue([regexDictionary count] == 0, @"Count = %u", [regexDictionary count]);
STAssertTrueNoThrow((regexDictionary = [testDict dictionaryByMatchingKeysWithRegex:@".*"]) != NULL, nil);
STAssertTrue([regexDictionary count] == 5, @"Count = %u", [regexDictionary count]);
STAssertTrueNoThrow((regexObjects = [regexDictionary allKeys]) != NULL, nil);
STAssertTrue([regexObjects containsObject:@"zero aaa"], nil);
STAssertTrue([regexObjects containsObject:@"one aab"], nil);
STAssertTrue([regexObjects containsObject:@"two aba"], nil);
STAssertTrue([regexObjects containsObject:@"three baa"], nil);
STAssertTrue([regexObjects containsObject:@"four bab"], nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary allValues]) != NULL, nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:0]], nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:1]], nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:2]], nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:3]], nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:4]], nil);
STAssertTrue([[regexDictionary objectForKey:@"zero aaa"] isEqualToNumber:[NSNumber numberWithInt:0]], nil);
STAssertTrue([[regexDictionary objectForKey:@"one aab"] isEqualToNumber:[NSNumber numberWithInt:1]], nil);
STAssertTrue([[regexDictionary objectForKey:@"two aba"] isEqualToNumber:[NSNumber numberWithInt:2]], nil);
STAssertTrue([[regexDictionary objectForKey:@"three baa"] isEqualToNumber:[NSNumber numberWithInt:3]], nil);
STAssertTrue([[regexDictionary objectForKey:@"four bab"] isEqualToNumber:[NSNumber numberWithInt:4]], nil);
STAssertTrueNoThrow((regexDictionary = [testDict dictionaryByMatchingObjectsWithRegex:@".*"]) != NULL, nil);
STAssertTrue([regexDictionary count] == 5, @"Count = %u", [regexDictionary count]);
STAssertTrueNoThrow((regexObjects = [regexDictionary allKeys]) != NULL, nil);
STAssertTrue([regexObjects containsObject:@"zero aaa"], nil);
STAssertTrue([regexObjects containsObject:@"one aab"], nil);
STAssertTrue([regexObjects containsObject:@"two aba"], nil);
STAssertTrue([regexObjects containsObject:@"three baa"], nil);
STAssertTrue([regexObjects containsObject:@"four bab"], nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary allValues]) != NULL, nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:0]], nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:1]], nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:2]], nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:3]], nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:4]], nil);
STAssertTrue([[regexDictionary objectForKey:@"zero aaa"] isEqualToNumber:[NSNumber numberWithInt:0]], nil);
STAssertTrue([[regexDictionary objectForKey:@"one aab"] isEqualToNumber:[NSNumber numberWithInt:1]], nil);
STAssertTrue([[regexDictionary objectForKey:@"two aba"] isEqualToNumber:[NSNumber numberWithInt:2]], nil);
STAssertTrue([[regexDictionary objectForKey:@"three baa"] isEqualToNumber:[NSNumber numberWithInt:3]], nil);
STAssertTrue([[regexDictionary objectForKey:@"four bab"] isEqualToNumber:[NSNumber numberWithInt:4]], nil);
STAssertTrueNoThrow((regexDictionary = [testDict dictionaryByMatchingObjectsWithRegex:@"1|3|4"]) != NULL, nil);
STAssertTrue([regexDictionary count] == 3, @"Count = %u", [regexDictionary count]);
STAssertTrue([[regexDictionary allKeys] containsObject:@"one aab"], nil);
STAssertTrue([[regexDictionary allKeys] containsObject:@"three baa"], nil);
STAssertTrue([[regexDictionary allKeys] containsObject:@"four bab"], nil);
STAssertTrue([[regexDictionary allValues] containsObject:[NSNumber numberWithInt:1]], nil);
STAssertTrue([[regexDictionary allValues] containsObject:[NSNumber numberWithInt:3]], nil);
STAssertTrue([[regexDictionary allValues] containsObject:[NSNumber numberWithInt:4]], nil);
STAssertTrue([[regexDictionary objectForKey:@"one aab"] isEqualToNumber:[NSNumber numberWithInt:1]], nil);
STAssertTrue([[regexDictionary objectForKey:@"three baa"] isEqualToNumber:[NSNumber numberWithInt:3]], nil);
STAssertTrue([[regexDictionary objectForKey:@"four bab"] isEqualToNumber:[NSNumber numberWithInt:4]], nil);
STAssertTrueNoThrow((testObjects = [testDict objectsMatchingRegex:@"2|3"]) != NULL, nil);
STAssertTrue([testObjects count] == 2, @"Count = %u", [testObjects count]);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:2]], nil);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:3]], nil);
STAssertTrue([regexDictionary containsObjectMatchingRegex:@"0|2"] == NO, nil);
STAssertTrue([regexDictionary containsObjectMatchingRegex:@"0|1|2"] == YES, nil);
STAssertTrue([regexDictionary containsObjectMatchingRegex:@"0|1"] == YES, nil);
STAssertTrue([regexDictionary containsObjectMatchingRegex:@"1|2"] == YES, nil);
STAssertTrue([regexDictionary containsObjectMatchingRegex:@"0"] == NO, nil);
STAssertTrue([regexDictionary containsObjectMatchingRegex:@"3"] == YES, nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary objectsMatchingRegex:@"0|1|2"]) != NULL, nil);
STAssertTrue([regexObjects count] == 1, @"Count = %u", [regexObjects count]);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:1]], nil);
STAssertTrueNoThrow((testObjects = [testDict objectsForKeysMatchingRegex:@"o a"]) != NULL, nil);
STAssertTrue([testObjects count] == 2, @"Count = %u", [regexObjects count]);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:0]], nil);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:2]], nil);
STAssertTrueNoThrow((testObjects = [testDict objectsMatchingRegex:@"CantMatchThis"]) != NULL, nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary objectsMatchingRegex:@"CanTooMatchThat"]) != NULL, nil);
STAssertTrue([testObjects count] == 0, @"Count = %u", [testObjects count]);
STAssertTrue([regexObjects count] == 0, @"Count = %u", [regexObjects count]);
STAssertTrueNoThrow((regexDictionary = [testDict dictionaryByMatchingKeysWithRegex:@"o a"]) != NULL, nil);
STAssertTrue([regexDictionary count] == 2, @"Count = %u", [regexDictionary count]);
STAssertTrue([[regexDictionary allKeys] containsObject:@"zero aaa"], nil);
STAssertTrue([[regexDictionary allKeys] containsObject:@"two aba"], nil);
STAssertTrue([[regexDictionary allValues] containsObject:[NSNumber numberWithInt:0]], nil);
STAssertTrue([[regexDictionary allValues] containsObject:[NSNumber numberWithInt:2]], nil);
STAssertTrue([[regexDictionary objectForKey:@"zero aaa"] isEqualToNumber:[NSNumber numberWithInt:0]], nil);
STAssertTrue([[regexDictionary objectForKey:@"two aba"] isEqualToNumber:[NSNumber numberWithInt:2]], nil);
STAssertTrueNoThrow([testDict containsKeyMatchingRegex:@"aaa"], nil);
STAssertTrueNoThrow([regexDictionary containsKeyMatchingRegex:@"aaa"], nil);
STAssertTrueNoThrow([testDict containsKeyMatchingRegex:@"aab"], nil);
STAssertFalseNoThrow([regexDictionary containsKeyMatchingRegex:@"aab"], nil);
STAssertTrueNoThrow((testObjects = [testDict keysMatchingRegex:@"CantMatchThis"]) != NULL, nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary keysMatchingRegex:@"CanTooMatchThat"]) != NULL, nil);
STAssertTrue([testObjects count] == 0, @"Count = %u", [testObjects count]);
STAssertTrue([regexObjects count] == 0, @"Count = %u", [regexObjects count]);
STAssertTrueNoThrow((testObjects = [testDict keysMatchingRegex:@".*"]) != NULL, nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary keysMatchingRegex:@".*"]) != NULL, nil);
STAssertTrue([testObjects count] == 5, @"Count = %u", [testObjects count]);
STAssertTrue([regexObjects count] == 2, @"Count = %u", [regexObjects count]);
STAssertTrue([testObjects containsObject:@"zero aaa"], nil);
STAssertTrue([testObjects containsObject:@"one aab"], nil);
STAssertTrue([testObjects containsObject:@"two aba"], nil);
STAssertTrue([testObjects containsObject:@"three baa"], nil);
STAssertTrue([testObjects containsObject:@"four bab"], nil);
STAssertTrue([regexObjects containsObject:@"zero aaa"], nil);
STAssertTrue([regexObjects containsObject:@"two aba"], nil);
STAssertTrueNoThrow((testObjects = [testDict keysMatchingRegex:@"ba"]) != NULL, nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary keysMatchingRegex:@"ba"]) != NULL, nil);
STAssertTrue([testObjects count] == 3, @"Count = %u", [testObjects count]);
STAssertTrue([regexObjects count] == 1, @"Count = %u", [regexObjects count]);
STAssertTrue([testObjects containsObject:@"two aba"], nil);
STAssertTrue([testObjects containsObject:@"three baa"], nil);
STAssertTrue([testObjects containsObject:@"four bab"], nil);
STAssertTrue([regexObjects containsObject:@"two aba"], nil);
STAssertTrueNoThrow((testObjects = [testDict objectsForKeysMatchingRegex:@"ab"]) != NULL, nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary objectsForKeysMatchingRegex:@"ab"]) != NULL, nil);
STAssertTrue([testObjects count] == 3, @"Count = %u", [testObjects count]);
STAssertTrue([regexObjects count] == 1, @"Count = %u", [regexObjects count]);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:1]], nil);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:2]], nil);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:4]], nil);
STAssertTrue([regexObjects containsObject:[NSNumber numberWithInt:2]], nil);
STAssertTrueNoThrow((testObjects = [testDict objectsForKeysMatchingRegex:@"bab"]) != NULL, nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary objectsForKeysMatchingRegex:@"bab"]) != NULL, nil);
STAssertTrue([testObjects count] == 1, @"Count = %u", [testObjects count]);
STAssertTrue([regexObjects count] == 0, @"Count = %u", [regexObjects count]);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:4]], nil);
STAssertNotNil((regexDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"five aca", [NSNumber numberWithInt:6], @"six dad", nil]), nil);
STAssertTrue([regexDictionary count] == 2, nil);
STAssertTrue([testDict count] == 5, nil);
STAssertNoThrow([testDict addEntriesFromDictionary:regexDictionary withKeysMatchingRegex:@"^six"], nil);
STAssertTrue([testDict count] == 6, nil);
STAssertTrue([regexDictionary count] == 2, nil);
STAssertTrueNoThrow((testObjects = [testDict objectsForKeysMatchingRegex:@"dad$"]) != NULL, nil);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:6]], nil);
STAssertNoThrow([testDict removeObjectsForKeysMatchingRegex:@"^(\\w+)\\s+(?:a|b)a(?:a|b)$"], nil);
STAssertTrue([testDict count] == 2, nil);
STAssertTrueNoThrow((testObjects = [testDict objectsForKeysMatchingRegex:@"^(\\w+)\\s+([abd]{3})"]) != NULL, nil);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:2]], nil);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:6]], nil);
STAssertThrowsSpecificNamed([testDict objectsForKeysMatchingRegex:@"^(\\w+)(?\\s+)([abd]{3})"], NSException, RKRegexSyntaxErrorException, nil);
STAssertThrows([(NSMutableDictionary *)regexDictionary removeObjectsForKeysMatchingRegex:@"e a"], nil);
STAssertThrows([(NSMutableDictionary *)regexDictionary addEntriesFromDictionary:testDict withKeysMatchingRegex:@".*"], nil);
STAssertNotNil((regexDictionary = [NSDictionary dictionary]), nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary objectsForKeysMatchingRegex:@"^No Match I!$"]) != NULL, nil);
STAssertTrue([regexObjects count] == 0, nil);
STAssertNotNil((regexDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"five aca", [NSNumber numberWithInt:6], @"six dad", nil]), nil);
STAssertTrue([regexDictionary count] == 2, nil);
STAssertNotNil((testDict = [NSMutableDictionary dictionary]), nil);
STAssertTrue([testDict count] == 0, nil);
STAssertNoThrow([testDict addEntriesFromDictionary:regexDictionary withKeysMatchingRegex:@"^No Match I!$"], nil);
STAssertTrue([testDict count] == 0, nil);
STAssertNoThrow([testDict addEntriesFromDictionary:regexDictionary withObjectsMatchingRegex:@"^No Match I!$"], nil);
STAssertTrue([testDict count] == 0, nil);
STAssertNoThrow([testDict addEntriesFromDictionary:regexDictionary withKeysMatchingRegex:@".*"], nil);
STAssertTrue([testDict count] == 2, nil);
STAssertNoThrow([testDict addEntriesFromDictionary:testDict withKeysMatchingRegex:@".*"], nil);
STAssertTrue([testDict count] == 2, @"Count is %u", [testDict count]);
STAssertNoThrow([testDict addEntriesFromDictionary:testDict withKeysMatchingRegex:@"^six"], nil);
STAssertTrue([testDict count] == 2, @"Count is %u", [testDict count]);
STAssertNoThrow([testDict addEntriesFromDictionary:testDict withKeysMatchingRegex:@"^six"], nil);
STAssertTrue([testDict count] == 2, @"Count is %u", [testDict count]);
STAssertNotNil((testDict = [NSMutableDictionary dictionary]), nil);
STAssertTrue([testDict count] == 0, nil);
STAssertNotNil((regexDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"five aca", [NSNumber numberWithInt:6], @"six dad", nil]), nil);
STAssertTrue([regexDictionary count] == 2, nil);
STAssertNoThrow([testDict setDictionary:staticDictionary], nil);
STAssertTrue([testDict count] == 5, nil);
STAssertNoThrow([testDict addEntriesFromDictionary:regexDictionary withObjectsMatchingRegex:@"6\\d*"], nil);
STAssertTrue([testDict count] == 6, nil);
STAssertTrue([regexDictionary count] == 2, nil);
STAssertTrueNoThrow((testObjects = [testDict objectsMatchingRegex:@"\\d*6"]) != NULL, nil);
STAssertTrue([testObjects containsObject:[NSNumber numberWithInt:6]], nil);
STAssertNoThrow([testDict removeObjectsMatchingRegex:@"^[^26]*$"], nil);
STAssertTrue([testDict count] == 2, nil);
STAssertTrueNoThrow((testObjects = [testDict keysForObjectsMatchingRegex:@"\\d*[26]\\d*"]) != NULL, nil);
STAssertTrue([testObjects containsObject:@"two aba"], nil);
STAssertTrue([testObjects containsObject:@"six dad"], nil);
STAssertThrowsSpecificNamed([testDict keysForObjectsMatchingRegex:@"^(\\d+)(\\S*)(?[246]{3})"], NSException, RKRegexSyntaxErrorException, nil);
STAssertThrows([(NSMutableDictionary *)regexDictionary removeObjectsMatchingRegex:@"\\d+"], nil);
STAssertThrows([(NSMutableDictionary *)regexDictionary addEntriesFromDictionary:testDict withObjectsMatchingRegex:@".*"], nil);
STAssertNotNil((regexDictionary = [NSDictionary dictionary]), nil);
STAssertTrueNoThrow((regexObjects = [regexDictionary keysForObjectsMatchingRegex:@"^No Match You!$"]) != NULL, nil);
STAssertTrue([regexObjects count] == 0, nil);
STAssertNoThrow([testDict removeObjectsMatchingRegex:@"\\d+"], nil);
STAssertTrue([testDict count] == 0, nil);
STAssertNotNil((regexDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"five aca", [NSNumber numberWithInt:6], @"six dad", nil]), nil);
STAssertTrue([regexDictionary count] == 2, nil);
STAssertNoThrow([testDict addEntriesFromDictionary:regexDictionary withKeysMatchingRegex:@"^No Match Zuul!$"], nil);
STAssertTrue([testDict count] == 0, nil);
STAssertNoThrow([testDict addEntriesFromDictionary:regexDictionary withObjectsMatchingRegex:@"^No Match Zuul\\?$"], nil);
STAssertTrue([testDict count] == 0, nil);
STAssertNoThrow([testDict addEntriesFromDictionary:regexDictionary withKeysMatchingRegex:@".*"], nil);
STAssertTrue([testDict count] == 2, nil);
STAssertNoThrow([testDict addEntriesFromDictionary:testDict withObjectsMatchingRegex:@".*"], nil);
STAssertTrue([testDict count] == 2, @"Count is %u", [testDict count]);
STAssertNoThrow([testDict addEntriesFromDictionary:testDict withObjectsMatchingRegex:@"\\d+"], nil);
STAssertTrue([testDict count] == 2, @"Count is %u", [testDict count]);
STAssertNoThrow([testDict addEntriesFromDictionary:testDict withObjectsMatchingRegex:@"6\\d*"], nil);
STAssertTrue([testDict count] == 2, @"Count is %u", [testDict count]);
}
//- (void)testSetExtensions
- (void)mt_test_26
{
NSSet *testSet = NULL, *matchedSet = NULL;
NSMutableSet *mutableSet = NULL;
STAssertTrueNoThrow((testSet = [NSSet setWithObjects:@"zero 0 aaa", @"one 1 aab", @"two 2 ab", @"three 3 bab", @"four 4 aba ", @"five 5 baa", @"six 6 bbb", nil]) != NULL, nil);
STAssertTrueNoThrow((matchedSet = [testSet setByMatchingObjectsWithRegex:@"ab"]) != NULL, nil);
//ab array : ("one 1 aab", "two 2 ab", "three 3 bab", "four 4 aba ")
STAssertTrue([matchedSet count] == 4, @"Count is %u", [matchedSet count]);
STAssertTrue([matchedSet containsObject:@"one 1 aab"], nil);
STAssertTrue([matchedSet containsObject:@"two 2 ab"], nil);
STAssertTrue([matchedSet containsObject:@"three 3 bab"], nil);
STAssertTrue([matchedSet containsObject:@"four 4 aba "], nil);
STAssertTrueNoThrow((matchedSet = [testSet setByMatchingObjectsWithRegex:@"aa"]) != NULL, nil);
//aa array : ("zero 0 aaa", "one 1 aab", "five 5 baa")
STAssertTrue([matchedSet count] == 3, @"Count is %u", [matchedSet count]);
STAssertTrue([matchedSet containsObject:@"zero 0 aaa"], nil);
STAssertTrue([matchedSet containsObject:@"one 1 aab"], nil);
STAssertTrue([matchedSet containsObject:@"five 5 baa"], nil);
STAssertTrueNoThrow((matchedSet = [testSet setByMatchingObjectsWithRegex:@"ba"]) != NULL, nil);
//ba array : ("three 3 bab", "four 4 aba ", "five 5 baa")
STAssertTrue([matchedSet count] == 3, @"Count is %u", [matchedSet count]);
STAssertTrue([matchedSet containsObject:@"three 3 bab"], nil);
STAssertTrue([matchedSet containsObject:@"four 4 aba "], nil);
STAssertTrue([matchedSet containsObject:@"five 5 baa"], nil);
STAssertTrueNoThrow((matchedSet = [testSet setByMatchingObjectsWithRegex:@"o"]) != NULL, nil);
//o array : ("zero 0 aaa", "one 1 aab", "two 2 ab", "four 4 aba ")
STAssertTrue([matchedSet count] == 4, @"Count is %u", [matchedSet count]);
STAssertTrue([matchedSet containsObject:@"zero 0 aaa"], nil);
STAssertTrue([matchedSet containsObject:@"one 1 aab"], nil);
STAssertTrue([matchedSet containsObject:@"two 2 ab"], nil);
STAssertTrue([matchedSet containsObject:@"four 4 aba "], nil);
STAssertTrueNoThrow((matchedSet = [testSet setByMatchingObjectsWithRegex:@"2|4|6"]) != NULL, nil);
//2|4|6 array : ("two 2 ab", "four 4 aba ", "six 6 bbb")
STAssertTrue([matchedSet count] == 3, @"Count is %u", [matchedSet count]);
STAssertTrue([matchedSet containsObject:@"two 2 ab"], nil);
STAssertTrue([matchedSet containsObject:@"four 4 aba "], nil);
STAssertTrue([matchedSet containsObject:@"six 6 bbb"], nil);
STAssertTrueNoThrow((mutableSet = [NSMutableSet setWithSet:testSet]) != NULL, nil);
//mutable array: ("zero 0 aaa", "one 1 aab", "two 2 ab", "three 3 bab", "four 4 aba ", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableSet count] == 7, @"Count is %u", [mutableSet count]);
STAssertTrue([mutableSet containsObject:@"zero 0 aaa"], nil);
STAssertTrue([mutableSet containsObject:@"one 1 aab"], nil);
STAssertTrue([mutableSet containsObject:@"two 2 ab"], nil);
STAssertTrue([mutableSet containsObject:@"three 3 bab"], nil);
STAssertTrue([mutableSet containsObject:@"four 4 aba "], nil);
STAssertTrue([mutableSet containsObject:@"five 5 baa"], nil);
STAssertTrue([mutableSet containsObject:@"six 6 bbb"], nil);
STAssertTrueNoThrow([mutableSet countOfObjectsMatchingRegex:@"ab"] == 4, @"Count == %u", [mutableSet countOfObjectsMatchingRegex:@"ab"]);
STAssertNoThrow([mutableSet addObjectsFromSet:testSet matchingRegex:@"ab"], nil);
//mutable array: ("zero 0 aaa", "one 1 aab", "two 2 ab", "three 3 bab", "four 4 aba ", "five 5 baa", "six 6 bbb"
STAssertTrue([mutableSet count] == 7, @"Count is %u", [mutableSet count]);
STAssertTrueNoThrow([mutableSet countOfObjectsMatchingRegex:@"ab"] == 4, @"Count == %u", [mutableSet countOfObjectsMatchingRegex:@"ab"]);
STAssertTrue([mutableSet containsObject:@"zero 0 aaa"], nil);
STAssertTrue([mutableSet containsObject:@"one 1 aab"], nil);
STAssertTrue([mutableSet containsObject:@"two 2 ab"], nil);
STAssertTrue([mutableSet containsObject:@"three 3 bab"], nil);
STAssertTrue([mutableSet containsObject:@"four 4 aba "], nil);
STAssertTrue([mutableSet containsObject:@"five 5 baa"], nil);
STAssertTrue([mutableSet containsObject:@"six 6 bbb"], nil);
STAssertTrueNoThrow([mutableSet countOfObjectsMatchingRegex:@"ba"] == 3, @"Count == %u", [mutableSet countOfObjectsMatchingRegex:@"ba"]);
STAssertNoThrow([mutableSet addObjectsFromArray:[testSet allObjects] matchingRegex:@"ba"], nil);
//mutable array: ("zero 0 aaa", "one 1 aab", "two 2 ab", "three 3 bab", "four 4 aba ", "five 5 baa", "six 6 bbb"
STAssertTrue([mutableSet count] == 7, @"Count is %u", [mutableSet count]);
STAssertTrueNoThrow([mutableSet countOfObjectsMatchingRegex:@"ba"] == 3, @"Count == %u", [mutableSet countOfObjectsMatchingRegex:@"ba"]);
STAssertTrue([mutableSet containsObject:@"zero 0 aaa"], nil);
STAssertTrue([mutableSet containsObject:@"one 1 aab"], nil);
STAssertTrue([mutableSet containsObject:@"two 2 ab"], nil);
STAssertTrue([mutableSet containsObject:@"three 3 bab"], nil);
STAssertTrue([mutableSet containsObject:@"four 4 aba "], nil);
STAssertTrue([mutableSet containsObject:@"five 5 baa"], nil);
STAssertTrue([mutableSet containsObject:@"six 6 bbb"], nil);
STAssertTrueNoThrow([mutableSet countOfObjectsMatchingRegex:@"^four"] == 1, @"Count is %u", [mutableSet countOfObjectsMatchingRegex:@"^four"]);
STAssertNoThrow([mutableSet removeObjectsMatchingRegex:@"ab"], nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableSet count] == 3, @"Count is %u", [mutableSet count]);
STAssertTrue([mutableSet containsObject:@"zero 0 aaa"], nil);
STAssertTrue([mutableSet containsObject:@"five 5 baa"], nil);
STAssertTrue([mutableSet containsObject:@"six 6 bbb"], nil);
// Same thing, should remain the same.
STAssertNoThrow([mutableSet removeObjectsMatchingRegex:@"ab"], nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableSet count] == 3, @"Count is %u", [mutableSet count]);
STAssertTrue([mutableSet containsObject:@"zero 0 aaa"], nil);
STAssertTrue([mutableSet containsObject:@"five 5 baa"], nil);
STAssertTrue([mutableSet containsObject:@"six 6 bbb"], nil);
// Add it back in...
STAssertTrueNoThrow([mutableSet countOfObjectsMatchingRegex:@"ab"] == 0, @"Count == %u", [mutableSet countOfObjectsMatchingRegex:@"ab"]);
STAssertNoThrow([mutableSet addObjectsFromSet:testSet matchingRegex:@"(b|a)"], nil);
//mutable array: ("zero 0 aaa", "one 1 aab", "two 2 ab", "three 3 bab", "four 4 aba ", "five 5 baa", "six 6 bbb"
STAssertTrue([mutableSet count] == 7, @"Count is %u", [mutableSet count]);
STAssertTrueNoThrow([mutableSet countOfObjectsMatchingRegex:@"ab"] == 4, @"Count == %u", [mutableSet countOfObjectsMatchingRegex:@"ab"]);
STAssertTrue([mutableSet containsObject:@"zero 0 aaa"], nil);
STAssertTrue([mutableSet containsObject:@"one 1 aab"], nil);
STAssertTrue([mutableSet containsObject:@"two 2 ab"], nil);
STAssertTrue([mutableSet containsObject:@"three 3 bab"], nil);
STAssertTrue([mutableSet containsObject:@"four 4 aba "], nil);
STAssertTrue([mutableSet containsObject:@"five 5 baa"], nil);
STAssertTrue([mutableSet containsObject:@"six 6 bbb"], nil);
// Remove and try with an array
STAssertNoThrow([mutableSet removeObjectsMatchingRegex:@"ab"], nil);
//mutable array: ("zero 0 aaa", "five 5 baa", "six 6 bbb")
STAssertTrue([mutableSet count] == 3, @"Count is %u", [mutableSet count]);
STAssertTrue([mutableSet containsObject:@"zero 0 aaa"], nil);
STAssertTrue([mutableSet containsObject:@"five 5 baa"], nil);
STAssertTrue([mutableSet containsObject:@"six 6 bbb"], nil);
STAssertTrueNoThrow([mutableSet countOfObjectsMatchingRegex:@"ba"] == 1, @"Count == %u", [mutableSet countOfObjectsMatchingRegex:@"ba"]);
STAssertNoThrow([mutableSet addObjectsFromArray:[testSet allObjects] matchingRegex:@"ba"], nil);
//mutable set: (five 5 baa, six 6 bbb, three 3 bab, zero 0 aaa, four 4 aba )
STAssertTrue([mutableSet count] == 5, @"Count is %u", [mutableSet count]);
STAssertTrueNoThrow([mutableSet countOfObjectsMatchingRegex:@"ba"] == 3, @"Count == %u", [mutableSet countOfObjectsMatchingRegex:@"ba"]);
STAssertTrue([mutableSet containsObject:@"zero 0 aaa"], nil);
STAssertTrue([mutableSet containsObject:@"three 3 bab"], nil);
STAssertTrue([mutableSet containsObject:@"four 4 aba "], nil);
STAssertTrue([mutableSet containsObject:@"five 5 baa"], nil);
STAssertTrue([mutableSet containsObject:@"six 6 bbb"], nil);
STAssertTrueNoThrow((matchedSet = [mutableSet setByMatchingObjectsWithRegex:@"I don't wanna match"]) != NULL, nil);
STAssertTrue([mutableSet count] == 5, @"Count is %u", [mutableSet count]);
STAssertTrue([matchedSet count] == 0, @"Count is %u", [matchedSet count]);
STAssertThrowsSpecificNamed([mutableSet addObjectsFromArray:NULL matchingRegex:@".*"], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([mutableSet addObjectsFromSet:NULL matchingRegex:@".*"], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([mutableSet addObjectsFromArray:[testSet allObjects] matchingRegex:NULL], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([mutableSet addObjectsFromSet:testSet matchingRegex:NULL], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([mutableSet removeObjectsMatchingRegex:NULL], NSException, NSInvalidArgumentException, nil);
STAssertTrueNoThrow([mutableSet countOfObjectsMatchingRegex:@"I don't wanna match"] == 0, nil);
STAssertTrueNoThrow([mutableSet containsObjectMatchingRegex:@"\\s\\d\\s"] == YES, nil);
STAssertTrueNoThrow([mutableSet containsObjectMatchingRegex:@"No more matching!"] == NO, nil);
STAssertTrueNoThrow([mutableSet anyObjectMatchingRegex:@"\\s\\d\\s"] != NULL, nil);
STAssertTrueNoThrow([mutableSet anyObjectMatchingRegex:@"^Really! I've had enough of your pattern matching (ways|plays)!$"] == NULL, nil);
}
//- (void)testRKEnumeratorInit
- (void)mt_test_27
{
NSAutoreleasePool *tempPool = NULL;
RKEnumerator *enumerator = NULL;
NSString *subjectString = @" blarg You think? Nope.. ";
STAssertTrueNoThrow((enumerator = [[[RKEnumerator alloc] initWithRegex:@"\\s*\\S+\\s+" string:subjectString inRange:NSMakeRange(0, [subjectString length])] autorelease]) != NULL, nil);
STAssertTrue([subjectString isEqualToString:[enumerator string]], nil);
STAssertTrue([[RKRegex regexWithRegexString:@"\\s*\\S+\\s+" options:RKCompileNoOptions] isEqualTo:[enumerator regex]], nil);
tempPool = [[NSAutoreleasePool alloc] init];
enumerator = NULL;
STAssertTrueNoThrow((enumerator = [[RKEnumerator alloc] initWithRegex:[RKRegex regexWithRegexString:@"\\s*\\S+\\s+" options:RKCompileDupNames] string:subjectString]) != NULL, nil);
STAssertTrue([subjectString isEqualToString:[enumerator string]], nil);
[tempPool release]; tempPool = NULL;
[enumerator autorelease];
STAssertTrue([[RKRegex regexWithRegexString:@"\\s*\\S+\\s+" options:RKCompileDupNames] isEqualTo:[enumerator regex]], nil);
enumerator = NULL;
STAssertTrueNoThrow((enumerator = [[[RKEnumerator alloc] initWithRegex:@"\\s*\\S+\\s+" string:subjectString] autorelease]) != NULL, nil);
STAssertTrue([subjectString isEqualToString:[enumerator string]], nil);
STAssertTrue([[RKRegex regexWithRegexString:@"\\s*\\S+\\s+" options:RKCompileNoOptions] isEqualTo:[enumerator regex]], nil);
enumerator = NULL;
STAssertTrueNoThrow((enumerator = [RKEnumerator enumeratorWithRegex:@"\\s*\\S+\\s+" string:subjectString inRange:NSMakeRange(0, [subjectString length])]) != NULL, nil);
STAssertTrue([subjectString isEqualToString:[enumerator string]], nil);
STAssertTrue([[RKRegex regexWithRegexString:@"\\s*\\S+\\s+" options:RKCompileNoOptions] isEqualTo:[enumerator regex]], nil);
enumerator = NULL;
STAssertTrueNoThrow((enumerator = [RKEnumerator enumeratorWithRegex:@"\\s*\\S+\\s+" string:subjectString]) != NULL, nil);
STAssertTrue([subjectString isEqualToString:[enumerator string]], nil);
STAssertTrue([[RKRegex regexWithRegexString:@"\\s*\\S+\\s+" options:RKCompileNoOptions] isEqualTo:[enumerator regex]], nil);
STAssertThrowsSpecificNamed([[[RKEnumerator alloc] initWithRegex:@"\\s{1,6}\\S+\\s+" string:subjectString inRange:NSMakeRange(0, [subjectString length] + 1)] autorelease], NSException, NSRangeException, nil);
STAssertThrowsSpecificNamed([[[RKEnumerator alloc] initWithRegex:@"\\s*\\S{3}\\s+" string:subjectString inRange:NSMakeRange([subjectString length] + 1, 100)] autorelease], NSException, NSRangeException, nil);
STAssertThrowsSpecificNamed([RKEnumerator enumeratorWithRegex:@"\\s*\\S+\\s{9,}" string:subjectString inRange:NSMakeRange(2, [subjectString length] + 3)], NSException, NSRangeException, nil);
STAssertThrowsSpecificNamed([RKEnumerator enumeratorWithRegex:@"\\s{3,}\\S+\\s+" string:subjectString inRange:NSMakeRange([subjectString length] - 2, 61)], NSException, NSRangeException, nil);
STAssertThrowsSpecificNamed([[[RKEnumerator alloc] initWithRegex:@"\\s{1,6}\\S+\\s+" string:NULL inRange:NSMakeRange(0, [subjectString length])] autorelease], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([[[RKEnumerator alloc] initWithRegex:@"\\s{6,12}\\S+\\s+" string:NULL] autorelease], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([RKEnumerator enumeratorWithRegex:@"\\s*\\S+\\s{9,}" string:NULL inRange:NSMakeRange(0, [subjectString length])], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([RKEnumerator enumeratorWithRegex:@"\\s*\\S{4,5}\\s{9,}" string:NULL], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([[[RKEnumerator alloc] initWithRegex:NULL string:subjectString inRange:NSMakeRange(0, [subjectString length])] autorelease], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([[[RKEnumerator alloc] initWithRegex:NULL string:subjectString] autorelease], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([RKEnumerator enumeratorWithRegex:NULL string:subjectString inRange:NSMakeRange(0, [subjectString length])], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([RKEnumerator enumeratorWithRegex:NULL string:subjectString], NSException, NSInvalidArgumentException, nil);
enumerator = NULL;
STAssertTrueNoThrow((enumerator = [subjectString matchEnumeratorWithRegex:@"\\s{5}\\S+?\\s?" inRange:NSMakeRange(0, [subjectString length])]) != NULL, nil);
STAssertTrue([subjectString isEqualToString:[enumerator string]], nil);
STAssertTrue([[RKRegex regexWithRegexString:@"\\s{5}\\S+?\\s?" options:RKCompileNoOptions] isEqualTo:[enumerator regex]], nil);
enumerator = NULL;
STAssertTrueNoThrow((enumerator = [subjectString matchEnumeratorWithRegex:@"\\s{6}\\S+?\\s?"]) != NULL, nil);
STAssertTrue([subjectString isEqualToString:[enumerator string]], nil);
STAssertTrue([[RKRegex regexWithRegexString:@"\\s{6}\\S+?\\s?" options:RKCompileNoOptions] isEqualTo:[enumerator regex]], nil);
STAssertThrowsSpecificNamed([subjectString matchEnumeratorWithRegex:@"\\s{7}\\S+?\\s?" inRange:NSMakeRange(0, [subjectString length] + 1)], NSException, NSRangeException, nil);
STAssertThrowsSpecificNamed([subjectString matchEnumeratorWithRegex:@"\\s{8}\\S+?\\s?" inRange:NSMakeRange([subjectString length], 1)], NSException, NSRangeException, nil);
STAssertThrowsSpecificNamed([subjectString matchEnumeratorWithRegex:NULL inRange:NSMakeRange(2, [subjectString length] + 1)], NSException, NSInvalidArgumentException, nil);
STAssertThrowsSpecificNamed([subjectString matchEnumeratorWithRegex:NULL], NSException, NSInvalidArgumentException, nil);
}
//- (void)testZInitSimpleTiming
- (BOOL)mt_time_1
{
unsigned int x = 0;
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
RKRegex *regex = [[RKRegex alloc] initWithRegexString:@".* (\\w+) .*" options:0];
STAssertNotNil(regex, nil);
[regex release];
[loopPool release];
}
return(NO);
}
//- (void)testZConvienenceInitSimpleTiming
- (BOOL)mt_time_2
{
unsigned int x = 0;
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
STAssertNotNil([RKRegex regexWithRegexString:@".* (\\w+) .*" options:0], nil);
[loopPool release];
}
return(NO);
}
//- (void)testZStringAdditionsNoNamedCapturesSimpleTiming
- (BOOL)mt_time_3
{
unsigned int x = 0;
NSString *namedSubjectString = @" 1999 - 12 - 01 / 55 ";
NSString *namedRegexString = @"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))";
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
NSString *subString0 = nil, *subString1 = nil, *subString2 = nil, *subString3 = nil, *subString4 = nil;
STAssertTrue(([namedSubjectString getCapturesWithRegexAndReferences:namedRegexString, @"${2}", &subString2, @"${4}", &subString4, @"${1}", &subString1, @"${0}", &subString0, @"${3}", &subString3, nil] == YES), nil);
STAssertNotNil(subString0, nil);
STAssertNotNil(subString1, nil);
STAssertNotNil(subString2, nil);
STAssertNotNil(subString3, nil);
STAssertNotNil(subString4, nil);
[loopPool release];
}
return(NO);
}
//- (void)testZStringAdditionsHeavyNamedCaptureSimpleTiming
- (BOOL)mt_time_4
{
unsigned int x = 0;
NSString *namedSubjectString = @" 1999 - 12 - 01 / 55 ";
NSString *namedRegexString = @"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))";
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
NSString *subStringDate = nil, *subStringDay = nil, *subStringYear = nil;
NSString /**subString0 = @"$0",*/ *subString1 = nil, *subString2 = nil;
STAssertTrue(([namedSubjectString getCapturesWithRegexAndReferences:namedRegexString, @"${day}", &subStringDay, @"${date}", &subStringDate, @"${2}", &subString2, @"${1}", &subString1, @"${year}", &subStringYear, nil] == YES), nil);
STAssertNotNil(subStringDate, nil);
STAssertNotNil(subStringYear, nil);
STAssertNotNil(subStringDay, nil);
STAssertNotNil(subString1, nil);
STAssertNotNil(subString2, nil);
[loopPool release];
}
return(NO);
}
//- (void)testZInitWithCaptureNamesTiming
- (BOOL)mt_time_5
{
unsigned int x = 0;
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
RKRegex *regex = [[RKRegex alloc] initWithRegexString:@"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))" options:RKCompileDupNames];
STAssertNotNil(regex, nil);
[regex release];
[loopPool release];
}
return(NO);
}
//- (void)testZConvienceInitWithCaptureNamesTiming
- (BOOL)mt_time_6
{
unsigned int x = 0;
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
RKRegex *regex = [RKRegex regexWithRegexString:@"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))" options:RKCompileDupNames];
STAssertNotNil(regex, nil);
[loopPool release];
}
return(NO);
}
//- (void)testZrangesForTiming
- (BOOL)mt_time_7
{
unsigned int x = 0;
NSString *subjectString = @" 1999 - 12 - 01 / 55 ";
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
RKRegex *regex = [RKRegex regexWithRegexString:@"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))" options:RKCompileDupNames];
NSRange *ranges = [subjectString rangesOfRegex:regex];
STAssertNotNil(regex, nil);
STAssertTrue(ranges != NULL, nil);
[loopPool release];
}
return(NO);
}
//- (void)testZPCRERKRegexEquivTiming
- (BOOL)mt_time_8
{
unsigned int x = 0;
const char *subjectCharacters = " 1999 - 12 - 01 / 55 ";
int subjectLength = strlen(subjectCharacters);
NSRange subjectRange = NSMakeRange(0, subjectLength);
NSRange matchRanges[4096];
RKRegex *regex = [RKRegex regexWithRegexString:@"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))" options:RKCompileDupNames];
sched_yield();
STAssertNotNil(regex, nil);
if(regex == nil) { return(YES); }
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
[regex getRanges:&matchRanges[0] withCharacters:subjectCharacters length:subjectLength inRange:subjectRange options:0];
[loopPool release];
}
return(NO);
}
//- (void)testZPCREbaseCaptureNameTiming
- (BOOL)mt_time_9
{
unsigned int x = 0;
const char *subjectCharacters = " 1999 - 12 - 01 / 55 ";
int subjectLength = strlen(subjectCharacters);
const char *regexCharacters = "(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))";
BOOL returnValue = NO;
int ovectors[4096];
void *_compiledPCRE = NULL;
void *_extraPCRE = NULL;
int compileErrorOffset = 0;
const char *errorCharPtr = NULL;
RKCompileErrorCode initErrorCode = RKCompileErrorNoError;
RKMatchErrorCode matchErrorCode = RKMatchErrorNoError;
_compiledPCRE = pcre_compile2(regexCharacters, RKCompileDupNames, (int *)&initErrorCode, &errorCharPtr, &compileErrorOffset, NULL);
if((initErrorCode != RKCompileErrorNoError) || (_compiledPCRE == NULL)) { returnValue = YES; goto exitNow; }
_extraPCRE = pcre_study(_compiledPCRE, 0, &errorCharPtr);
if((_extraPCRE == NULL) && (errorCharPtr != NULL)) { returnValue = YES; goto exitNow; }
for(x = 0; x < iterations; x++) {
matchErrorCode = (RKMatchErrorCode)pcre_exec(_compiledPCRE, _extraPCRE, subjectCharacters, subjectLength, 0, 0, &ovectors[0], 256);
if(matchErrorCode <= 0) { returnValue = YES; goto exitNow; }
}
exitNow:
if(_compiledPCRE) { pcre_free(_compiledPCRE); _compiledPCRE = NULL; }
sched_yield();
if(_extraPCRE) { pcre_free(_extraPCRE); _extraPCRE = NULL; }
sched_yield();
return(returnValue);
}
//- (void)testZrangesForNoMatchTiming
- (BOOL)mt_time_10
{
unsigned int x = 0;
NSString *subjectString = @" 1999 - 12 - 01 / ab ";
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
RKRegex *regex = [RKRegex regexWithRegexString:@"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<month>\\d\\d))" options:RKCompileDupNames];
NSRange *ranges = [subjectString rangesOfRegex:regex];
STAssertNotNil(regex, nil);
STAssertTrue(ranges == NULL, nil);
[loopPool release];
}
return(NO);
}
//- (void)testZSimplerangesForTiming
- (BOOL)mt_time_11
{
unsigned int x = 0;
NSString *regexString = @"^(Match)\\s+the\\s+(MAGIC)";
NSString *subjectString = @"Match the MAGIC in this string";
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
RKRegex *regex = [RKRegex regexWithRegexString:regexString options:RKCompileDupNames];
NSRange *ranges = [subjectString rangesOfRegex:regex];
STAssertNotNil(regex, nil);
STAssertTrue(ranges != NULL, nil);
[loopPool release];
}
return(NO);
}
- (BOOL)mt_time_12
{
unsigned int x = 0;
NSString *namedSubjectString = @" 1999 - 12 - 01 / 55 ";
NSString *namedRegexString = @"(?<date> (?<year>(\\d\\d)?\\d\\d) - (?<month>\\d\\d) - (?<day>\\d\\d) / (?<monthb>\\d\\d))";
for(x = 0; x < iterations; x++) {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
NSString *subString0 = nil, *subString1 = nil, *subString2 = nil, *subString3 = nil, *subString4 = nil;
STAssertTrue(([namedSubjectString getCapturesWithRegexAndReferences:namedRegexString, @"${2}", &subString2, @"${4}", &subString4, @"${1}", &subString1, @"${0}", &subString0, @"${3}", &subString3, nil] == YES), nil);
[[RKRegex regexCache] removeObjectFromCache:[[[RKRegex alloc] initWithRegexString:namedRegexString options:RKCompileDupNames] autorelease]];
STAssertNotNil(subString0, nil);
STAssertNotNil(subString1, nil);
STAssertNotNil(subString2, nil);
STAssertNotNil(subString3, nil);
STAssertNotNil(subString4, nil);
[loopPool release];
}
return(NO);
}
@end
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.