ObjFW  Check-in [7f7e9715e1]

Overview
Comment:Add - isEqual: for OFList.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7f7e9715e1529ed3b1a32448279ba8952db8d7ef3d8b617f60a4d8a5950288a4
User & Date: js on 2009-05-19 16:51:56
Other Links: manifest | tags
Context
2009-05-19
17:13
Rename + tcpSocket to + socket in OFTCPSocket. check-in: 0b8c5616cb user: js tags: trunk
16:51
Add - isEqual: for OFList. check-in: 7f7e9715e1 user: js tags: trunk
16:15
One more convenience method for OFDictionary. check-in: 5413ba3c49 user: js tags: trunk
Changes

Modified src/OFDictionary.m from [7df4d449e0] to [921468f6aa].

303
304
305
306
307
308
309
310

311
312
313
314
315
316
317
303
304
305
306
307
308
309

310
311
312
313
314
315
316
317







-
+







	size_t items, buckets, i;

	items = 0;
	buckets = 0;

	for (i = 0; i < size; i++) {
		if (data[i] != nil) {
			items += [data[i] items] / 2;
			items += [data[i] count] / 2;
			buckets++;
		}
	}

	return (float)items / buckets;
}

Modified src/OFList.h from [01b97195c0] to [ea241d3479].

103
104
105
106
107
108
109
110

111
103
104
105
106
107
108
109

110
111







-
+


/**
 * Get the number of items in the list. Use with caution, as this means one
 * iteration through the whole list!
 *
 * \return The number of items in the list.
 */
- (size_t)items;
- (size_t)count;
@end

Modified src/OFList.m from [c5e0bd60b4] to [c06f8c5d7d].

164
165
166
167
168
169
170
171

172
173
174
175
176
177
178
179
180



















181
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







-
+









+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

		[listobj->object release];

	[self freeMem: listobj];

	return self;
}

- (size_t)items
- (size_t)count
{
	size_t i;
	of_list_object_t *iter;

	for (i = 0, iter = first; iter != NULL; iter = iter->next)
		i++;

	return i;
}

- (BOOL)isEqual: (id)obj
{
	of_list_object_t *iter, *iter2;

	if (![obj isKindOf: [OFList class]])
		return NO;

	for (iter = first, iter2 = [obj first]; iter != NULL && iter2 != NULL;
	    iter = iter->next, iter2 = iter2->next)
		if (![iter->object isEqual: iter2->object])
			return NO;

	/* One has still items */
	if (iter != NULL || iter2 != NULL)
		return NO;

	return YES;
}
@end

Modified tests/OFList/OFList.m from [dda59ebbf8] to [95b616ccee].

19
20
21
22
23
24
25
26

27
28
29
30
31
32
33
19
20
21
22
23
24
25

26
27
28
29
30
31
32
33







-
+








#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 11
#define NUM_TESTS 14
#define SUCCESS								\
{									\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);							\
}
#define FAIL								\
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
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







-
-
-
-
+
+
+
+






-
+




-
-
-
+
+
+


-
+




-
-
+
+


-
+


-
+
-

-
+

-
+



-
+

+
-
+
+
+
+
+
+
+
+
+
+
+
+
+





	if (cond)							\
		SUCCESS							\
	else								\
		FAIL							\
	i++;								\
}

const char *strings[] = {
	"First String Object",
	"Second String Object",
	"Third String Object"
const OFString *strings[] = {
	@"First String Object",
	@"Second String Object",
	@"Third String Object"
};

int
main()
{
	size_t i, j;
	OFList *list;
	OFList *list, *list2;
	of_list_object_t *iter;

	list = [OFList list];

	[list append: [OFString stringWithCString: strings[0]]];
	[list append: [OFString stringWithCString: strings[1]]];
	[list append: [OFString stringWithCString: strings[2]]];
	[list append: strings[0]];
	[list append: strings[1]];
	[list append: strings[2]];

	for (iter = [list first], i = 0; iter != NULL; iter = iter->next, i++)
		if (!strcmp([iter->object cString], strings[i]))
		if ([iter->object isEqual: strings[i]])
			SUCCESS
		else
			FAIL

	CHECK(!strcmp([[list first]->object cString], strings[0]))
	CHECK(!strcmp([[list last]->object cString], strings[2]))
	CHECK([[list first]->object isEqual: strings[0]])
	CHECK([[list last]->object isEqual: strings[2]])

	[list remove: [list last]];
	CHECK(!strcmp([[list last]->object cString], strings[1]))
	CHECK([[list last]->object isEqual: strings[1]])

	[list remove: [list first]];
	CHECK(!strcmp([[list first]->object cString],
	CHECK([[list first]->object isEqual: [list last]->object])
	    [[list last]->object cString]))

	[list insert: [OFString stringWithCString: strings[0]]
	[list insert: strings[0]
	      before: [list last]];
	[list insert: [OFString stringWithCString: strings[2]]
	[list insert: strings[2]
	       after: [list first]->next];

	for (iter = [list first], j = 0; iter != NULL; iter = iter->next, j++)
		CHECK(!strcmp([iter->object cString], strings[j]))
		CHECK([iter->object isEqual: strings[j]])

	CHECK([list count] == 3)
	CHECK ([list items] == 3)

	list2 = [OFList list];

	[list2 append: strings[0]];
	[list2 append: strings[1]];
	[list2 append: strings[2]];
	CHECK([list2 isEqual: list]);

	[list2 remove: [list2 last]];
	CHECK(![list2 isEqual: list]);

	[list2 append: @"foo"];
	CHECK(![list2 isEqual: list]);

	puts("");

	return 0;
}