ObjFW  Diff

Differences From Artifact [0918022e86]:

To Artifact [6cb38442bb]:

  • File tests/OFDictionaryTests.m — part of check-in [cfd54bd090] at 2017-05-01 13:51:41 on branch trunk — Add tests for generic OF(Mutable)Dictionary

    These are usually not used, as OFDictionary_hashtable and
    OFMutableDictionary_hashtable are used instead. However, they are used
    if someone creates their own subclass of OFDictionary /
    OFMutableDictionary. As they are rarely used in production, it is
    important to run all tests on them. (user: js, size: 9537) [annotate] [blame] [check-ins using]


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
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"
#import "OFUndefinedKeyException.h"

#import "TestsAppDelegate.h"

static OFString *module = @"OFDictionary";
static OFString *keys[] = {
	@"key1",
	@"key2"
};
static OFString *values[] = {
	@"value1",
	@"value2"
};

























































































































@implementation TestsAppDelegate (OFDictionaryTests)
- (void)dictionaryTests

{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableDictionary *dict = [OFMutableDictionary dictionary];
	OFDictionary *idict;
	OFEnumerator *key_enum, *obj_enum;
	OFArray *akeys, *avalues;

	[dict setObject: values[0]
		 forKey: keys[0]];
	[dict setValue: values[1]







|








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|
>


|







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
#import "OFAutoreleasePool.h"

#import "OFEnumerationMutationException.h"
#import "OFUndefinedKeyException.h"

#import "TestsAppDelegate.h"

static OFString *module = nil;
static OFString *keys[] = {
	@"key1",
	@"key2"
};
static OFString *values[] = {
	@"value1",
	@"value2"
};

@interface SimpleDictionary: OFDictionary
{
	OFMutableDictionary *_dictionary;
}
@end

@interface SimpleMutableDictionary: OFMutableDictionary
{
	OFMutableDictionary *_dictionary;
}
@end

@implementation SimpleDictionary
- init
{
	self = [super init];

	@try {
		_dictionary = [[OFMutableDictionary alloc] init];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithKey: (id)key
    arguments: (va_list)arguments
{
	self = [super init];

	@try {
		_dictionary = [[OFMutableDictionary alloc]
		    initWithKey: key
		      arguments: arguments];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithObjects: (const id*)objects
	  forKeys: (const id*)keys
	    count: (size_t)count
{
	self = [super init];

	@try {
		_dictionary = [[OFMutableDictionary alloc]
		    initWithObjects: objects
			    forKeys: keys
			      count: count];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_dictionary release];

	[super dealloc];
}

- (id)objectForKey: (id)key
{
	return [_dictionary objectForKey: key];
}

- (size_t)count
{
	return [_dictionary count];
}

- (OFEnumerator*)keyEnumerator
{
	return [_dictionary keyEnumerator];
}

- (OFEnumerator*)objectEnumerator
{
	return [_dictionary objectEnumerator];
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count
{
	return [_dictionary countByEnumeratingWithState: state
						objects: objects
						  count: count];
}
@end

@implementation SimpleMutableDictionary
+ (void)initialize
{
	if (self == [SimpleMutableDictionary class])
		[self inheritMethodsFromClass: [SimpleDictionary class]];
}

- (void)setObject: (id)object
	   forKey: (id)key
{
	[_dictionary setObject: object
			forKey: key];
}

- (void)removeObjectForKey: (id)key
{
	[_dictionary removeObjectForKey: key];
}
@end

@implementation TestsAppDelegate (OFDictionaryTests)
- (void)dictionaryTestsWithClass: (Class)dictionaryClass
		    mutableClass: (Class)mutableDictionaryClass
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFMutableDictionary *dict = [mutableDictionaryClass dictionary];
	OFDictionary *idict;
	OFEnumerator *key_enum, *obj_enum;
	OFArray *akeys, *avalues;

	[dict setObject: values[0]
		 forKey: keys[0]];
	[dict setValue: values[1]
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	TEST(@"-[valueForKey:]",
	    [[dict valueForKey: keys[0]] isEqual: values[0]] &&
	    [[dict valueForKey: @"@count"] isEqual:
	    [OFNumber numberWithSize: 2]])

	EXPECT_EXCEPTION(@"Catching -[setValue:forKey:] on immutable "
	    @"dictionary", OFUndefinedKeyException,
	    [[OFDictionary dictionary] setValue: @"x"
					 forKey: @"x"])

	TEST(@"-[containsObject:]",
	    [dict containsObject: values[0]] &&
	    ![dict containsObject: @"nonexistant"])

	TEST(@"-[containsObjectIdenticalTo:]",
	    [dict containsObjectIdenticalTo: values[0]] &&







|
|







180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
	TEST(@"-[valueForKey:]",
	    [[dict valueForKey: keys[0]] isEqual: values[0]] &&
	    [[dict valueForKey: @"@count"] isEqual:
	    [OFNumber numberWithSize: 2]])

	EXPECT_EXCEPTION(@"Catching -[setValue:forKey:] on immutable "
	    @"dictionary", OFUndefinedKeyException,
	    [[dictionaryClass dictionary] setValue: @"x"
					    forKey: @"x"])

	TEST(@"-[containsObject:]",
	    [dict containsObject: values[0]] &&
	    ![dict containsObject: @"nonexistant"])

	TEST(@"-[containsObjectIdenticalTo:]",
	    [dict containsObjectIdenticalTo: values[0]] &&
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
		return [key isEqual: keys[0]];
	    }] description] isEqual: @"{\n\tkey1 = value_1;\n}"])
#endif

	TEST(@"-[count]", [dict count] == 2)

	TEST(@"+[dictionaryWithKeysAndObjects:]",
	    (idict = [OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar",
								 @"baz", @"qux",
								 nil]) &&
	    [[idict objectForKey: @"foo"] isEqual: @"bar"] &&
	    [[idict objectForKey: @"baz"] isEqual: @"qux"])

	TEST(@"+[dictionaryWithObject:forKey:]",
	    (idict = [OFDictionary dictionaryWithObject: @"bar"
						 forKey: @"foo"]) &&
	    [[idict objectForKey: @"foo"] isEqual: @"bar"])

	akeys = [OFArray arrayWithObjects: keys[0], keys[1], nil];
	avalues = [OFArray arrayWithObjects: values[0], values[1], nil];
	TEST(@"+[dictionaryWithObjects:forKeys:]",
	    (idict = [OFDictionary dictionaryWithObjects: avalues
						 forKeys: akeys]) &&
	    [[idict objectForKey: keys[0]] isEqual: values[0]] &&
	    [[idict objectForKey: keys[1]] isEqual: values[1]])

	TEST(@"-[copy]",
	    (idict = [[idict copy] autorelease]) &&
	    [[idict objectForKey: keys[0]] isEqual: values[0]] &&
	    [[idict objectForKey: keys[1]] isEqual: values[1]])







|
|
<




|
|





|
|







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
		return [key isEqual: keys[0]];
	    }] description] isEqual: @"{\n\tkey1 = value_1;\n}"])
#endif

	TEST(@"-[count]", [dict count] == 2)

	TEST(@"+[dictionaryWithKeysAndObjects:]",
	    (idict = [dictionaryClass dictionaryWithKeysAndObjects:
	    @"foo", @"bar", @"baz", @"qux", nil]) &&

	    [[idict objectForKey: @"foo"] isEqual: @"bar"] &&
	    [[idict objectForKey: @"baz"] isEqual: @"qux"])

	TEST(@"+[dictionaryWithObject:forKey:]",
	    (idict = [dictionaryClass dictionaryWithObject: @"bar"
						    forKey: @"foo"]) &&
	    [[idict objectForKey: @"foo"] isEqual: @"bar"])

	akeys = [OFArray arrayWithObjects: keys[0], keys[1], nil];
	avalues = [OFArray arrayWithObjects: values[0], values[1], nil];
	TEST(@"+[dictionaryWithObjects:forKeys:]",
	    (idict = [dictionaryClass dictionaryWithObjects: avalues
						    forKeys: akeys]) &&
	    [[idict objectForKey: keys[0]] isEqual: values[0]] &&
	    [[idict objectForKey: keys[1]] isEqual: values[1]])

	TEST(@"-[copy]",
	    (idict = [[idict copy] autorelease]) &&
	    [[idict objectForKey: keys[0]] isEqual: values[0]] &&
	    [[idict objectForKey: keys[1]] isEqual: values[1]])
253
254
255
256
257
258
259











260
	    ![dict isEqual: idict] &&
	    R([dict setObject: values[0]
		       forKey: keys[0]]) &&
	    [dict isEqual: idict])

	[pool drain];
}











@end







>
>
>
>
>
>
>
>
>
>
>

373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
	    ![dict isEqual: idict] &&
	    R([dict setObject: values[0]
		       forKey: keys[0]]) &&
	    [dict isEqual: idict])

	[pool drain];
}

- (void)dictionaryTests
{
	module = @"OFDictionary";
	[self dictionaryTestsWithClass: [SimpleDictionary class]
			  mutableClass: [SimpleMutableDictionary class]];

	module = @"OFDictionary_hashtable";
	[self dictionaryTestsWithClass: [OFDictionary class]
			  mutableClass: [OFMutableDictionary class]];
}
@end