Differences From Artifact [f58673a4b1]:
- File tests/OFArrayTests.m — part of check-in [eb0cfa6ff9] at 2021-04-17 14:13:56 on branch new-naming-convention — Always prefix functions with the type (user: js, size: 11734) [annotate] [blame] [check-ins using] [more...]
To Artifact [a384e7dc9e]:
- File
tests/OFArrayTests.m
— part of check-in
[14f1e22d79]
at
2021-05-08 23:22:59
on branch trunk
— tests: Align more with ObjFW style
ObjFW's style changed over the years, but the tests were never adjusted
to it. (user: js, size: 12816) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
14 15 16 17 18 19 20 | */ #include "config.h" #import "TestsAppDelegate.h" static OFString *module = nil; | | | 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | */ #include "config.h" #import "TestsAppDelegate.h" static OFString *module = nil; static OFString *const cArray[] = { @"Foo", @"Bar", @"Baz" }; @interface SimpleArray: OFArray { |
︙ | ︙ | |||
123 124 125 126 127 128 129 | @end @implementation TestsAppDelegate (OFArrayTests) - (void)arrayTestsWithClass: (Class)arrayClass mutableClass: (Class)mutableArrayClass { void *pool = objc_autoreleasePoolPush(); | | | | | > | | | | | > | | | > | > | | | | | | | | | | | | | | | | | | | | | | | > | | | | | | | | > | > | | > > | | | > | | | | < < > > > | | | > | | | | | | | | | | | | | | | | | | | | > | | | | | | | | | | | | | | | | | | | | | | | | > | | | | | | | | | | | | | > | | | | | | | | | > | | | | | | | | > | | | | | | | | 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 | @end @implementation TestsAppDelegate (OFArrayTests) - (void)arrayTestsWithClass: (Class)arrayClass mutableClass: (Class)mutableArrayClass { void *pool = objc_autoreleasePoolPush(); OFArray *array1, *array2; OFMutableArray *mutableArray1, *mutableArray2; OFEnumerator *enumerator; id object; bool ok; size_t i; TEST(@"+[array]", (mutableArray1 = [mutableArrayClass array])) TEST(@"+[arrayWithObjects:]", (array1 = [arrayClass arrayWithObjects: @"Foo", @"Bar", @"Baz", nil])) TEST(@"+[arrayWithObjects:count:]", (array2 = [arrayClass arrayWithObjects: cArray count: 3]) && [array2 isEqual: array1]) TEST(@"-[description]", [array1.description isEqual: @"(\n\tFoo,\n\tBar,\n\tBaz\n)"]) TEST(@"-[addObject:]", R([mutableArray1 addObject: cArray[0]]) && R([mutableArray1 addObject: cArray[2]])) TEST(@"-[insertObject:atIndex:]", R([mutableArray1 insertObject: cArray[1] atIndex: 1])) TEST(@"-[count]", mutableArray1.count == 3 && array1.count == 3 && array2.count == 3) TEST(@"-[isEqual:]", [mutableArray1 isEqual: array1] && [array1 isEqual: array2]) TEST(@"-[objectAtIndex:]", [[mutableArray1 objectAtIndex: 0] isEqual: cArray[0]] && [[mutableArray1 objectAtIndex: 1] isEqual: cArray[1]] && [[mutableArray1 objectAtIndex: 2] isEqual: cArray[2]] && [[array1 objectAtIndex: 0] isEqual: cArray[0]] && [[array1 objectAtIndex: 1] isEqual: cArray[1]] && [[array1 objectAtIndex: 2] isEqual: cArray[2]] && [[array2 objectAtIndex: 0] isEqual: cArray[0]] && [[array2 objectAtIndex: 1] isEqual: cArray[1]] && [[array2 objectAtIndex: 2] isEqual: cArray[2]]) TEST(@"-[containsObject:]", [array1 containsObject: cArray[1]] && ![array1 containsObject: @"nonexistent"]) TEST(@"-[containsObjectIdenticalTo:]", [array1 containsObjectIdenticalTo: cArray[1]] && ![array1 containsObjectIdenticalTo: [OFString stringWithString: cArray[1]]]) TEST(@"-[indexOfObject:]", [array1 indexOfObject: cArray[1]] == 1) TEST(@"-[indexOfObjectIdenticalTo:]", [array2 indexOfObjectIdenticalTo: cArray[1]] == 1) TEST(@"-[objectsInRange:]", [[array1 objectsInRange: OFRangeMake(1, 2)] isEqual: [arrayClass arrayWithObjects: cArray[1], cArray[2], nil]]) TEST(@"-[replaceObject:withObject:]", R([mutableArray1 replaceObject: cArray[1] withObject: cArray[0]]) && [[mutableArray1 objectAtIndex: 0] isEqual: cArray[0]] && [[mutableArray1 objectAtIndex: 1] isEqual: cArray[0]] && [[mutableArray1 objectAtIndex: 2] isEqual: cArray[2]]) TEST(@"-[replaceObject:identicalTo:]", R([mutableArray1 replaceObjectIdenticalTo: cArray[0] withObject: cArray[1]]) && [[mutableArray1 objectAtIndex: 0] isEqual: cArray[1]] && [[mutableArray1 objectAtIndex: 1] isEqual: cArray[0]] && [[mutableArray1 objectAtIndex: 2] isEqual: cArray[2]]) TEST(@"-[replaceObjectAtIndex:withObject:]", R([mutableArray1 replaceObjectAtIndex: 0 withObject: cArray[0]]) && [[mutableArray1 objectAtIndex: 0] isEqual: cArray[0]] && [[mutableArray1 objectAtIndex: 1] isEqual: cArray[0]] && [[mutableArray1 objectAtIndex: 2] isEqual: cArray[2]]) TEST(@"-[removeObject:]", R([mutableArray1 removeObject: cArray[0]]) && mutableArray1.count == 2) TEST(@"-[removeObjectIdenticalTo:]", R([mutableArray1 removeObjectIdenticalTo: cArray[2]]) && mutableArray1.count == 1) mutableArray2 = [[array1 mutableCopy] autorelease]; TEST(@"-[removeObjectAtIndex:]", R([mutableArray2 removeObjectAtIndex: 1]) && mutableArray2.count == 2 && [[mutableArray2 objectAtIndex: 1] isEqual: cArray[2]]) mutableArray2 = [[array1 mutableCopy] autorelease]; TEST(@"-[removeObjectsInRange:]", R([mutableArray2 removeObjectsInRange: OFRangeMake(0, 2)]) && mutableArray2.count == 1 && [[mutableArray2 objectAtIndex: 0] isEqual: cArray[2]]) mutableArray2 = [[array1 mutableCopy] autorelease]; [mutableArray2 addObject: @"qux"]; [mutableArray2 addObject: @"last"]; TEST(@"-[reverse]", R([mutableArray2 reverse]) && [mutableArray2 isEqual: [arrayClass arrayWithObjects: @"last", @"qux", @"Baz", @"Bar", @"Foo", nil]]) mutableArray2 = [[array1 mutableCopy] autorelease]; [mutableArray2 addObject: @"qux"]; [mutableArray2 addObject: @"last"]; TEST(@"-[reversedArray]", [[mutableArray2 reversedArray] isEqual: [arrayClass arrayWithObjects: @"last", @"qux", @"Baz", @"Bar", @"Foo", nil]]) mutableArray2 = [[array1 mutableCopy] autorelease]; [mutableArray2 addObject: @"0"]; [mutableArray2 addObject: @"z"]; TEST(@"-[sortedArray]", [[mutableArray2 sortedArray] isEqual: [arrayClass arrayWithObjects: @"0", @"Bar", @"Baz", @"Foo", @"z", nil]] && [[mutableArray2 sortedArrayUsingSelector: @selector(compare:) options: OFArraySortDescending] isEqual: [arrayClass arrayWithObjects: @"z", @"Foo", @"Baz", @"Bar", @"0", nil]]) EXPECT_EXCEPTION(@"Detect out of range in -[objectAtIndex:]", OFOutOfRangeException, [array1 objectAtIndex: array1.count]) EXPECT_EXCEPTION(@"Detect out of range in -[removeObjectsInRange:]", OFOutOfRangeException, [mutableArray1 removeObjectsInRange: OFRangeMake(0, mutableArray1.count + 1)]) TEST(@"-[componentsJoinedByString:]", (array2 = [arrayClass arrayWithObjects: @"", @"a", @"b", @"c", nil]) && [[array2 componentsJoinedByString: @" "] isEqual: @" a b c"] && (array2 = [arrayClass arrayWithObject: @"foo"]) && [[array2 componentsJoinedByString: @" "] isEqual: @"foo"]) TEST(@"-[componentsJoinedByString:options]", (array2 = [arrayClass arrayWithObjects: @"", @"foo", @"", @"", @"bar", @"", nil]) && [[array2 componentsJoinedByString: @" " options: OFArraySkipEmptyComponents] isEqual: @"foo bar"]) mutableArray1 = [[array1 mutableCopy] autorelease]; ok = true; i = 0; TEST(@"-[objectEnumerator]", (enumerator = [mutableArray1 objectEnumerator])) while ((object = [enumerator nextObject]) != nil) { if (![object isEqual: cArray[i]]) ok = false; [mutableArray1 replaceObjectAtIndex: i withObject: @""]; i++; } if (mutableArray1.count != i) ok = false; TEST(@"OFEnumerator's -[nextObject]", ok) [mutableArray1 removeObjectAtIndex: 0]; EXPECT_EXCEPTION(@"Detection of mutation during enumeration", OFEnumerationMutationException, [enumerator nextObject]) mutableArray1 = [[array1 mutableCopy] autorelease]; ok = true; i = 0; for (OFString *string in mutableArray1) { if (![string isEqual: cArray[i]]) ok = false; [mutableArray1 replaceObjectAtIndex: i withObject: @""]; i++; } if (mutableArray1.count != i) ok = false; TEST(@"Fast Enumeration", ok) [mutableArray1 replaceObjectAtIndex: 0 withObject: cArray[0]]; [mutableArray1 replaceObjectAtIndex: 1 withObject: cArray[1]]; [mutableArray1 replaceObjectAtIndex: 2 withObject: cArray[2]]; ok = false; i = 0; @try { for (OFString *string in mutableArray1) { (void)string; if (i == 0) [mutableArray1 addObject: @""]; i++; } } @catch (OFEnumerationMutationException *e) { ok = true; } TEST(@"Detection of mutation during Fast Enumeration", ok) [mutableArray1 removeLastObject]; #ifdef OF_HAVE_BLOCKS { __block bool blockOK = true; __block size_t count = 0; OFArray *compareArray = array1; OFMutableArray *mutableArray3; mutableArray1 = [[array1 mutableCopy] autorelease]; [mutableArray1 enumerateObjectsUsingBlock: ^ (id object_, size_t idx, bool *stop) { count++; if (![object_ isEqual: [compareArray objectAtIndex: idx]]) blockOK = false; }]; if (count != compareArray.count) blockOK = false; TEST(@"Enumeration using blocks", blockOK) blockOK = false; mutableArray3 = mutableArray1; @try { [mutableArray3 enumerateObjectsUsingBlock: ^ (id object_, size_t idx, bool *stop) { [mutableArray3 removeObjectAtIndex: idx]; }]; } @catch (OFEnumerationMutationException *e) { blockOK = true; } @catch (OFOutOfRangeException *e) { /* * Out of bounds access due to enumeration not being * detected. */ } TEST(@"Detection of mutation during enumeration using blocks", blockOK) } TEST(@"-[replaceObjectsUsingBlock:]", R([mutableArray1 replaceObjectsUsingBlock: ^ id (id object_, size_t idx) { switch (idx) { case 0: return @"foo"; case 1: return @"bar"; } return nil; }]) && [mutableArray1.description isEqual: @"(\n\tfoo,\n\tbar\n)"]) TEST(@"-[mappedArrayUsingBlock:]", [[mutableArray1 mappedArrayUsingBlock: ^ id (id object_, size_t idx) { switch (idx) { case 0: return @"foobar"; case 1: return @"qux"; } return nil; }].description isEqual: @"(\n\tfoobar,\n\tqux\n)"]) TEST(@"-[filteredArrayUsingBlock:]", [[mutableArray1 filteredArrayUsingBlock: ^ bool (id object_, size_t idx) { return [object_ isEqual: @"foo"]; }].description isEqual: @"(\n\tfoo\n)"]) TEST(@"-[foldUsingBlock:]", [[arrayClass arrayWithObjects: [OFMutableString string], @"foo", @"bar", @"baz", nil] foldUsingBlock: ^ id (id left, id right) { [left appendString: right]; return left; }]) #endif TEST(@"-[valueForKey:]", [[[arrayClass arrayWithObjects: @"foo", @"bar", @"quxqux", nil] valueForKey: @"length"] isEqual: [arrayClass arrayWithObjects: [OFNumber numberWithInt: 3], [OFNumber numberWithInt: 3], [OFNumber numberWithInt: 6], nil]] && [[[arrayClass arrayWithObjects: @"1", @"2", nil] valueForKey: @"@count"] isEqual: [OFNumber numberWithInt: 2]]) mutableArray1 = [mutableArrayClass arrayWithObjects: [OFMutableURL URLWithString: @"http://foo.bar/"], [OFMutableURL URLWithString: @"http://bar.qux/"], [OFMutableURL URLWithString: @"http://qux.quxqux/"], nil]; TEST(@"-[setValue:forKey:]", R([mutableArray1 setValue: [OFNumber numberWithShort: 1234] forKey: @"port"]) && [mutableArray1 isEqual: [arrayClass arrayWithObjects: [OFURL URLWithString: @"http://foo.bar:1234/"], [OFURL URLWithString: @"http://bar.qux:1234/"], [OFURL URLWithString: @"http://qux.quxqux:1234/"], nil]]) objc_autoreleasePoolPop(pool); } |
︙ | ︙ |