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
|
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
|
-
+
-
-
+
+
-
-
-
+
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
-
+
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
-
-
+
-
-
-
-
-
+
+
+
+
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
+
-
-
-
+
+
+
-
+
|
@end
@implementation TestsAppDelegate (OFDictionaryTests)
- (void)dictionaryTestsWithClass: (Class)dictionaryClass
mutableClass: (Class)mutableDictionaryClass
{
void *pool = objc_autoreleasePoolPush();
OFMutableDictionary *mutDict = [mutableDictionaryClass dictionary];
OFMutableDictionary *mutableDict = [mutableDictionaryClass dictionary];
OFDictionary *dict;
OFEnumerator *keyEnumerator, *objectEnumerator;
OFArray *keysArray, *valuesArray;
[mutDict setObject: values[0] forKey: keys[0]];
[mutDict setValue: values[1] forKey: keys[1]];
[mutableDict setObject: values[0] forKey: keys[0]];
[mutableDict setValue: values[1] forKey: keys[1]];
TEST(@"-[objectForKey:]",
[[mutDict objectForKey: keys[0]] isEqual: values[0]] &&
[[mutDict objectForKey: keys[1]] isEqual: values[1]] &&
[mutDict objectForKey: @"key3"] == nil)
[[mutableDict objectForKey: keys[0]] isEqual: values[0]] &&
[[mutableDict objectForKey: keys[1]] isEqual: values[1]] &&
[mutableDict objectForKey: @"key3"] == nil)
TEST(@"-[valueForKey:]",
[[mutDict valueForKey: keys[0]] isEqual: values[0]] &&
[[mutDict valueForKey: @"@count"] isEqual:
[[mutableDict valueForKey: keys[0]] isEqual: values[0]] &&
[[mutableDict valueForKey: @"@count"] isEqual:
[OFNumber numberWithInt: 2]])
EXPECT_EXCEPTION(@"Catching -[setValue:forKey:] on immutable "
@"dictionary", OFUndefinedKeyException,
[[dictionaryClass dictionary] setValue: @"x" forKey: @"x"])
TEST(@"-[containsObject:]",
[mutDict containsObject: values[0]] &&
![mutDict containsObject: @"nonexistent"])
[mutableDict containsObject: values[0]] &&
![mutableDict containsObject: @"nonexistent"])
TEST(@"-[containsObjectIdenticalTo:]",
[mutDict containsObjectIdenticalTo: values[0]] &&
![mutDict containsObjectIdenticalTo:
[mutableDict containsObjectIdenticalTo: values[0]] &&
![mutableDict containsObjectIdenticalTo:
[OFString stringWithString: values[0]]])
TEST(@"-[description]",
[[mutDict description] isEqual:
[[mutableDict description] isEqual:
@"{\n\tkey1 = value1;\n\tkey2 = value2;\n}"])
TEST(@"-[allKeys]",
[[mutDict allKeys] isEqual: [OFArray arrayWithObjects: keys[0],
keys[1], nil]])
[[mutableDict allKeys] isEqual:
[OFArray arrayWithObjects: keys[0], keys[1], nil]])
TEST(@"-[allObjects]",
[[mutDict allObjects] isEqual: [OFArray arrayWithObjects: values[0],
values[1], nil]])
[[mutableDict allObjects] isEqual:
[OFArray arrayWithObjects: values[0], values[1], nil]])
TEST(@"-[keyEnumerator]", (keyEnumerator = [mutDict keyEnumerator]))
TEST(@"-[keyEnumerator]", (keyEnumerator = [mutableDict keyEnumerator]))
TEST(@"-[objectEnumerator]",
(objectEnumerator = [mutDict objectEnumerator]))
(objectEnumerator = [mutableDict objectEnumerator]))
TEST(@"OFEnumerator's -[nextObject]",
[[keyEnumerator nextObject] isEqual: keys[0]] &&
[[objectEnumerator nextObject] isEqual: values[0]] &&
[[keyEnumerator nextObject] isEqual: keys[1]] &&
[[objectEnumerator nextObject] isEqual: values[1]] &&
[keyEnumerator nextObject] == nil &&
[objectEnumerator nextObject] == nil)
[mutDict removeObjectForKey: keys[0]];
[mutableDict removeObjectForKey: keys[0]];
EXPECT_EXCEPTION(@"Detection of mutation during enumeration",
OFEnumerationMutationException, [keyEnumerator nextObject]);
[mutDict setObject: values[0] forKey: keys[0]];
[mutableDict setObject: values[0] forKey: keys[0]];
size_t i = 0;
bool ok = true;
for (OFString *key in mutDict) {
for (OFString *key in mutableDict) {
if (i > 1 || ![key isEqual: keys[i]]) {
ok = false;
break;
}
[mutableDict setObject: [mutableDict objectForKey: key]
[mutDict setObject: [mutDict objectForKey: key] forKey: key];
forKey: key];
i++;
}
TEST(@"Fast Enumeration", ok)
ok = false;
@try {
for (OFString *key in mutDict) {
for (OFString *key in mutableDict) {
(void)key;
[mutDict setObject: @"" forKey: @""];
[mutableDict setObject: @"" forKey: @""];
}
} @catch (OFEnumerationMutationException *e) {
ok = true;
}
TEST(@"Detection of mutation during Fast Enumeration", ok)
[mutDict removeObjectForKey: @""];
[mutableDict removeObjectForKey: @""];
TEST(@"-[stringByURLEncoding]",
[[[OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar",
@"q&x", @"q=x", nil]
stringByURLEncoding] isEqual: @"q%26x=q%3Dx&foo=bar"])
#ifdef OF_HAVE_BLOCKS
{
__block size_t j = 0;
__block bool blockOk = true;
[mutDict enumerateKeysAndObjectsUsingBlock:
[mutableDict enumerateKeysAndObjectsUsingBlock:
^ (id key, id object, bool *stop) {
if (j > 1 || ![key isEqual: keys[j]]) {
blockOk = false;
*stop = true;
return;
}
[mutDict setObject: [mutDict objectForKey: key]
forKey: key];
[mutableDict setObject: [mutableDict objectForKey: key]
forKey: key];
j++;
}];
TEST(@"Enumeration using blocks", blockOk)
blockOk = false;
@try {
[mutDict enumerateKeysAndObjectsUsingBlock:
[mutableDict enumerateKeysAndObjectsUsingBlock:
^ (id key, id object, bool *stop) {
[mutDict setObject: @""
[mutableDict setObject: @"" forKey: @""];
forKey: @""];
}];
} @catch (OFEnumerationMutationException *e) {
blockOk = true;
}
TEST(@"Detection of mutation during enumeration using blocks",
blockOk)
[mutDict removeObjectForKey: @""];
[mutableDict removeObjectForKey: @""];
}
TEST(@"-[replaceObjectsUsingBlock:]",
R([mutDict replaceObjectsUsingBlock: ^ id (id key, id object) {
if ([key isEqual: keys[0]])
return @"value_1";
if ([key isEqual: keys[1]])
return @"value_2";
R([mutableDict replaceObjectsUsingBlock: ^ id (id key, id object) {
if ([key isEqual: keys[0]])
return @"value_1";
if ([key isEqual: keys[1]])
return @"value_2";
return nil;
}]) && [[mutDict objectForKey: keys[0]] isEqual: @"value_1"] &&
[[mutDict objectForKey: keys[1]] isEqual: @"value_2"])
return nil;
}]) && [[mutableDict objectForKey: keys[0]] isEqual: @"value_1"] &&
[[mutableDict objectForKey: keys[1]] isEqual: @"value_2"])
TEST(@"-[mappedDictionaryUsingBlock:]",
[[[mutDict mappedDictionaryUsingBlock: ^ id (id key, id object) {
if ([key isEqual: keys[0]])
return @"val1";
if ([key isEqual: keys[1]])
return @"val2";
[[[mutableDict mappedDictionaryUsingBlock:
^ id (id key, id object) {
if ([key isEqual: keys[0]])
return @"val1";
if ([key isEqual: keys[1]])
return @"val2";
return nil;
}] description] isEqual: @"{\n\tkey1 = val1;\n\tkey2 = val2;\n}"])
TEST(@"-[filteredDictionaryUsingBlock:]",
[[[mutDict filteredDictionaryUsingBlock:
^ bool (id key, id object) {
return [key isEqual: keys[0]];
[[[mutableDict filteredDictionaryUsingBlock:
^ bool (id key, id object) {
return [key isEqual: keys[0]];
}] description] isEqual: @"{\n\tkey1 = value_1;\n}"])
#endif
TEST(@"-[count]", mutDict.count == 2)
TEST(@"-[count]", mutableDict.count == 2)
TEST(@"+[dictionaryWithKeysAndObjects:]",
(dict = [dictionaryClass dictionaryWithKeysAndObjects:
@"foo", @"bar", @"baz", @"qux", nil]) &&
[[dict objectForKey: @"foo"] isEqual: @"bar"] &&
[[dict objectForKey: @"baz"] isEqual: @"qux"])
|
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
|
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
|
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
+
+
+
+
+
+
|
TEST(@"-[copy]",
(dict = [[dict copy] autorelease]) &&
[[dict objectForKey: keys[0]] isEqual: values[0]] &&
[[dict objectForKey: keys[1]] isEqual: values[1]])
TEST(@"-[mutableCopy]",
(mutDict = [[dict mutableCopy] autorelease]) &&
mutDict.count == dict.count &&
[[mutDict objectForKey: keys[0]] isEqual: values[0]] &&
[[mutDict objectForKey: keys[1]] isEqual: values[1]] &&
R([mutDict setObject: @"value3" forKey: @"key3"]) &&
[[mutDict objectForKey: @"key3"] isEqual: @"value3"] &&
[[mutDict objectForKey: keys[0]] isEqual: values[0]] &&
R([mutDict setObject: @"foo" forKey: keys[0]]) &&
[[mutDict objectForKey: keys[0]] isEqual: @"foo"])
(mutableDict = [[dict mutableCopy] autorelease]) &&
mutableDict.count == dict.count &&
[[mutableDict objectForKey: keys[0]] isEqual: values[0]] &&
[[mutableDict objectForKey: keys[1]] isEqual: values[1]] &&
R([mutableDict setObject: @"value3" forKey: @"key3"]) &&
[[mutableDict objectForKey: @"key3"] isEqual: @"value3"] &&
[[mutableDict objectForKey: keys[0]] isEqual: values[0]] &&
R([mutableDict setObject: @"foo" forKey: keys[0]]) &&
[[mutableDict objectForKey: keys[0]] isEqual: @"foo"])
TEST(@"-[removeObjectForKey:]",
R([mutDict removeObjectForKey: keys[0]]) &&
[mutDict objectForKey: keys[0]] == nil)
R([mutableDict removeObjectForKey: keys[0]]) &&
[mutableDict objectForKey: keys[0]] == nil)
[mutDict setObject: @"foo" forKey: keys[0]];
TEST(@"-[isEqual:]", ![mutDict isEqual: dict] &&
R([mutDict removeObjectForKey: @"key3"]) &&
![mutDict isEqual: dict] &&
R([mutDict setObject: values[0] forKey: keys[0]]) &&
[mutDict isEqual: dict])
[mutableDict setObject: @"foo" forKey: keys[0]];
TEST(@"-[isEqual:]", ![mutableDict isEqual: dict] &&
R([mutableDict removeObjectForKey: @"key3"]) &&
![mutableDict isEqual: dict] &&
R([mutableDict setObject: values[0] forKey: keys[0]]) &&
[mutableDict isEqual: dict])
objc_autoreleasePoolPop(pool);
}
- (void)dictionaryTests
{
module = @"OFDictionary";
|