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
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
/*
* Copyright (c) 2008 - 2009
* Jonathan Schleifer <js@webkeks.org>
*
* All rights reserved.
*
* This file is part of libobjfw. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE included in
* the packaging of this file.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#import "OFAutoreleasePool.h"
#import "OFDictionary.h"
#import "OFString.h"
#import "OFExceptions.h"
#define TESTS 14
int
main()
{
int i = 0;
OFDictionary *dict = [OFMutableDictionary dictionary];
OFDictionary *dict2;
OFArray *keys, *objs;
OFIterator *iter;
of_iterator_pair_t pair[2];
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFString *key1 = [OFString stringWithString: @"key1"];
OFString *key2 = [OFString stringWithString: @"key2"];
OFString *value1 = [OFString stringWithString: @"value1"];
OFString *value2 = [OFString stringWithString: @"value2"];
[dict setObject: value1
forKey: key1];
[dict setObject: value2
forKey: key2];
[pool release];
i++;
if (strcmp([[dict objectForKey: @"key1"] cString], "value1")) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
if (strcmp([[dict objectForKey: key2] cString], "value2")) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
iter = [dict iterator];
pair[0] = [iter nextKeyObjectPair];
pair[1] = [iter nextKeyObjectPair];
if (![pair[0].key isEqual: @"key1"] ||
![pair[0].object isEqual: @"value1"] ||
![pair[1].key isEqual: @"key2"] ||
![pair[1].object isEqual: @"value2"]) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
if ([dict count] != 2) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
if ([iter nextKeyObjectPair].object != nil) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
if ([dict objectForKey: @"key3"] != nil) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
[dict release];
dict = [OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar",
@"baz", @"qux",
nil];
if (![[dict objectForKey: @"foo"] isEqual: @"bar"]) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
if (![[dict objectForKey: @"baz"] isEqual: @"qux"]) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
[dict release];
dict = [OFDictionary dictionaryWithObject: @"bar"
forKey: @"foo"];
if (![[dict objectForKey: @"foo"] isEqual: @"bar"]) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
[dict release];
keys = [OFArray arrayWithObjects: @"k1", @"k2", nil];
objs = [OFArray arrayWithObjects: @"o1", @"o2", nil];
dict = [OFDictionary dictionaryWithObjects: objs
forKeys: keys];
if (![[dict objectForKey: @"k1"] isEqual: @"o1"]) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
if (![[dict objectForKey: @"k2"] isEqual: @"o2"]) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
dict2 = [dict copy];
[dict release];
if (![[dict2 objectForKey: @"k1"] isEqual: @"o1"] ||
![[dict2 objectForKey: @"k2"] isEqual: @"o2"]) {
printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
dict = [dict2 mutableCopy];
[dict2 release];
if (![[dict objectForKey: @"k1"] isEqual: @"o1"] ||
![[dict objectForKey: @"k2"] isEqual: @"o2"]) {
printf("\033[k\033[1;31mtest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
i++;
[dict setObject: @"o0" forKey: @"k1"];
[dict setObject: @"o3" forKey: @"k3"];
if (![[dict objectForKey: @"k1"] isEqual: @"o0"] ||
![[dict objectForKey: @"k2"] isEqual: @"o2"] ||
![[dict objectForKey: @"k3"] isEqual: @"o3"]) {
printf("\033[k\033[1;31mtest %d/%d failed!\033[m\n", i, TESTS);
return 1;
}
printf("\033[1;32mTests successful: %d/%d\033[0m\n", i, TESTS);
return 0;
}
|