Differences From Artifact [a25483183f]:
- File
tests/OFDictionaryTests.m
— part of check-in
[25c985fec1]
at
2020-08-25 00:11:24
on branch trunk
— OFNumber: Remove (u)int{8,16,32,64} methods
Since C guarantees minimum sizes for char, short, int, long and long
long, these can be used instead. (user: js, size: 10075) [annotate] [blame] [check-ins using] [more...]
To Artifact [daa54d882b]:
- File tests/OFDictionaryTests.m — part of check-in [1b82d3bf4f] at 2021-03-07 20:25:21 on branch trunk — *.m: Fold methods into one line where it fits (user: js, size: 9912) [annotate] [blame] [check-ins using] [more...]
1 | /* | < < | | 1 2 3 4 5 6 7 8 9 | /* * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * |
︙ | ︙ | |||
53 54 55 56 57 58 59 | [self release]; @throw e; } return self; } | | < | 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | [self release]; @throw e; } return self; } - (instancetype)initWithKey: (id)key arguments: (va_list)arguments { self = [super init]; @try { _dictionary = [[OFMutableDictionary alloc] initWithKey: key arguments: arguments]; |
︙ | ︙ | |||
119 120 121 122 123 124 125 | @implementation SimpleMutableDictionary + (void)initialize { if (self == [SimpleMutableDictionary class]) [self inheritMethodsFromClass: [SimpleDictionary class]]; } | | < | < | 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | @implementation SimpleMutableDictionary + (void)initialize { if (self == [SimpleMutableDictionary class]) [self inheritMethodsFromClass: [SimpleDictionary class]]; } - (void)setObject: (id)object forKey: (id)key { bool existed = ([_dictionary objectForKey: key] == nil); [_dictionary setObject: object forKey: key]; if (existed) _mutations++; } - (void)removeObjectForKey: (id)key { |
︙ | ︙ | |||
165 166 167 168 169 170 171 | { void *pool = objc_autoreleasePoolPush(); OFMutableDictionary *mutDict = [mutableDictionaryClass dictionary]; OFDictionary *dict; OFEnumerator *keyEnumerator, *objectEnumerator; OFArray *keysArray, *valuesArray; | | < | < | < | 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 | { void *pool = objc_autoreleasePoolPush(); OFMutableDictionary *mutDict = [mutableDictionaryClass dictionary]; OFDictionary *dict; OFEnumerator *keyEnumerator, *objectEnumerator; OFArray *keysArray, *valuesArray; [mutDict setObject: values[0] forKey: keys[0]]; [mutDict 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) TEST(@"-[valueForKey:]", [[mutDict valueForKey: keys[0]] isEqual: values[0]] && [[mutDict 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"]) TEST(@"-[containsObjectIdenticalTo:]", [mutDict containsObjectIdenticalTo: values[0]] && |
︙ | ︙ | |||
223 224 225 226 227 228 229 | [objectEnumerator nextObject] == nil) [mutDict removeObjectForKey: keys[0]]; EXPECT_EXCEPTION(@"Detection of mutation during enumeration", OFEnumerationMutationException, [keyEnumerator nextObject]); | | < | < < | < | 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 | [objectEnumerator nextObject] == nil) [mutDict removeObjectForKey: keys[0]]; EXPECT_EXCEPTION(@"Detection of mutation during enumeration", OFEnumerationMutationException, [keyEnumerator nextObject]); [mutDict setObject: values[0] forKey: keys[0]]; size_t i = 0; bool ok = true; for (OFString *key in mutDict) { if (i > 1 || ![key isEqual: keys[i]]) { ok = false; break; } [mutDict setObject: [mutDict objectForKey: key] forKey: key]; i++; } TEST(@"Fast Enumeration", ok) ok = false; @try { for (OFString *key in mutDict) { (void)key; [mutDict setObject: @"" forKey: @""]; } } @catch (OFEnumerationMutationException *e) { ok = true; } TEST(@"Detection of mutation during Fast Enumeration", ok) |
︙ | ︙ | |||
359 360 361 362 363 364 365 | [[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]] && | | < | < | < | < | 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 | [[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"]) TEST(@"-[removeObjectForKey:]", R([mutDict removeObjectForKey: keys[0]]) && [mutDict 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]) objc_autoreleasePoolPop(pool); } - (void)dictionaryTests { |
︙ | ︙ |