ObjFW  Check-in [ffd26c8238]

Overview
Comment:Add - hash for OFArray and OFString, main implementation in OFMacros.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ffd26c8238c2adb8b6090ae71d379c5b93569795a0c8e9e64efd36addf43b4e6
User & Date: js on 2009-02-27 11:17:54
Other Links: manifest | tags
Context
2009-02-27
12:31
(void)initialize -> initialize. check-in: 3b62c10b6f user: js tags: trunk
11:17
Add - hash for OFArray and OFString, main implementation in OFMacros. check-in: ffd26c8238 user: js tags: trunk
2009-02-15
16:40
Get rid of OFComparable and make it part of OFObject. check-in: d9308adc2b user: js tags: trunk
Changes

Modified src/OFArray.m from [3ecf264b3e] to [b65574e4bc].

161
162
163
164
165
166
167













168
169
170
171
172
173
174
	} else {
		if ((ret = memcmp(data, [obj data], items * itemsize)))
			return ret;

		return *(char*)[obj item: [self items]] * -1;
	}
}













@end

@implementation OFBigArray
- initWithItemSize: (size_t)is
{
	if (lastpagebyte == 0)
		lastpagebyte = getpagesize() - 1;







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







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
	} else {
		if ((ret = memcmp(data, [obj data], items * itemsize)))
			return ret;

		return *(char*)[obj item: [self items]] * -1;
	}
}

- (uint32_t)hash
{
	uint32_t hash;
	size_t i;

	OF_HASH_INIT(hash);
	for (i = 0; i < items * itemsize; i++)
		OF_HASH_ADD(hash, ((char*)data)[i]);
	OF_HASH_FINALIZE(hash);

	return hash;
}
@end

@implementation OFBigArray
- initWithItemSize: (size_t)is
{
	if (lastpagebyte == 0)
		lastpagebyte = getpagesize() - 1;

Modified src/OFMacros.h from [fd3e18f82c] to [ec8a81722f].

38
39
40
41
42
43
44














}
#else
#define OF_BSWAP_V(buf, len)
#endif

#define OF_ROL(val, bits) \
	(((val) << (bits)) | ((val) >> (32 - (bits))))





















>
>
>
>
>
>
>
>
>
>
>
>
>
>
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
}
#else
#define OF_BSWAP_V(buf, len)
#endif

#define OF_ROL(val, bits) \
	(((val) << (bits)) | ((val) >> (32 - (bits))))

#define OF_HASH_INIT(hash) hash = 0
#define OF_HASH_ADD(hash, byte)		\
	{				\
		hash += byte;		\
		hash += (hash << 10);	\
		hash ^= (hash >> 6);	\
	}
#define OF_HASH_FINALIZE(hash)		\
	{				\
		hash += (hash << 3);	\
		hash ^= (hash >> 11);	\
		hash += (hash << 15);	\
	}

Modified src/OFObject.m from [091b00dab8] to [b132f8aa53].

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	/* Classes containing data should reimplement this! */
	return (self == obj ? YES : NO);
}

- (uint32_t)hash
{
	/* Classes containing data should reimplement this! */
	return (uint32_t)self & 0xFFFFFF;
}

- addToMemoryPool: (void*)ptr
{
	void **memchunks;
	size_t memchunks_size;








|







77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	/* Classes containing data should reimplement this! */
	return (self == obj ? YES : NO);
}

- (uint32_t)hash
{
	/* Classes containing data should reimplement this! */
	return (uint32_t)self;
}

- addToMemoryPool: (void*)ptr
{
	void **memchunks;
	size_t memchunks_size;

Modified src/OFString.m from [6c57bbac26] to [504b483064].

283
284
285
286
287
288
289













290
291
292
293
294
295
296
- (int)compare: (id)obj
{
	if (![obj isKindOf: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: [self class]];

	return strcmp(string, [obj cString]);
}














- append: (OFString*)str
{
	return [self appendCString: [str cString]];
}

- appendCString: (const char*)str







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







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
- (int)compare: (id)obj
{
	if (![obj isKindOf: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: [self class]];

	return strcmp(string, [obj cString]);
}

- (uint32_t)hash
{
	uint32_t hash;
	size_t i;

	OF_HASH_INIT(hash);
	for (i = 0; i < length; i++)
		OF_HASH_ADD(hash, string[i]);
	OF_HASH_FINALIZE(hash);

	return hash;
}

- append: (OFString*)str
{
	return [self appendCString: [str cString]];
}

- appendCString: (const char*)str

Modified tests/OFArray/OFArray.m from [95c48806f0] to [c4671d9ef0].

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
	x = [OFArray arrayWithItemSize: 1];
	y = [OFArray bigArrayWithItemSize: 1];

	if (![x isEqual: y]) {
		puts("FAIL 1!");
		return 1;
	}






	[x add: "x"];
	if ([x isEqual: y]) {
		puts("FAIL 2!");
		return 2;
	}
	[pool releaseObjects];

	x = [OFArray arrayWithItemSize: 2];
	y = [OFArray bigArrayWithItemSize: 4];

	if ([x isEqual: y]) {
		puts("FAIL 3!");
		return 1;
	}
	[pool releaseObjects];

	x = [OFArray arrayWithItemSize: 1];
	[x addNItems: 3
	  fromCArray: "abc"];
	y = [x copy];
	if ([x compare: y]) {
		puts("FAIL 4!");
		return 1;
	}

	[y add: "de"];
	if ([x compare: y] != -100) {
		puts("FAIL 5!");





		return 1;
	}
	[pool release];

	return 0;
}







>
>
>
>
>



|
|







|









|





|
>
>
>
>
>






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
173
174
175
176
177
178
179
180
181
182
	x = [OFArray arrayWithItemSize: 1];
	y = [OFArray bigArrayWithItemSize: 1];

	if (![x isEqual: y]) {
		puts("FAIL 1!");
		return 1;
	}

	if ([x hash] != [y hash]) {
		puts("FAIL 2!");
		return 1;
	}

	[x add: "x"];
	if ([x isEqual: y]) {
		puts("FAIL 3!");
		return 1;
	}
	[pool releaseObjects];

	x = [OFArray arrayWithItemSize: 2];
	y = [OFArray bigArrayWithItemSize: 4];

	if ([x isEqual: y]) {
		puts("FAIL 4!");
		return 1;
	}
	[pool releaseObjects];

	x = [OFArray arrayWithItemSize: 1];
	[x addNItems: 3
	  fromCArray: "abc"];
	y = [x copy];
	if ([x compare: y]) {
		puts("FAIL 5!");
		return 1;
	}

	[y add: "de"];
	if ([x compare: y] != -100) {
		puts("FAIL 6!");
		return 1;
	}

	if ([y hash] != 0xCD8B6206) {
		puts("FAIL 7!");
		return 1;
	}
	[pool release];

	return 0;
}

Modified tests/OFString/OFString.m from [0abd9793f1] to [06121ecfbf].

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

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

#define NUM_TESTS 13
#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								\
	printf("\r\033[K\033[1;31mTest " ZD "/%d failed!\033[m\n",	\
	    i + 1, NUM_TESTS);						\







|







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

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

#define NUM_TESTS 15
#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								\
	printf("\r\033[K\033[1;31mTest " ZD "/%d failed!\033[m\n",	\
	    i + 1, NUM_TESTS);						\
60
61
62
63
64
65
66
67

68
69
70
71
72
73

74
75
76
77
78
79
80
	OFString *s2 = [OFString stringWithCString: ""];
	OFString *s3;
	OFString *s4 = [OFString string];

	s3 = [s1 copy];

	CHECK([s1 isEqual: s3])
	CHECK(![s1 isEqual: [OFObject new]]);


	[s2 appendCString: "123"];
	[s4 setTo: [s2 cString]];

	CHECK(![s2 compare: s4])
	CHECK(!strcmp([[s1 append: s2] cString], "test123"))

	CHECK(strlen([s1 cString]) == [s1 length] && [s1 length] == 7)
	CHECK(!strcmp([[s1 reverse] cString], "321tset"))
	CHECK(!strcmp([[s1 upper] cString], "321TSET"))
	CHECK(!strcmp([[s1 lower] cString], "321tset"))

	/* Also clears all the memory of the returned C strings */
	[pool release];







|
>






>







60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	OFString *s2 = [OFString stringWithCString: ""];
	OFString *s3;
	OFString *s4 = [OFString string];

	s3 = [s1 copy];

	CHECK([s1 isEqual: s3])
	CHECK(![s1 isEqual: [OFObject new]])
	CHECK([s1 hash] == [s3 hash])

	[s2 appendCString: "123"];
	[s4 setTo: [s2 cString]];

	CHECK(![s2 compare: s4])
	CHECK(!strcmp([[s1 append: s2] cString], "test123"))
	CHECK([s1 hash] == 0xC44F49A4)
	CHECK(strlen([s1 cString]) == [s1 length] && [s1 length] == 7)
	CHECK(!strcmp([[s1 reverse] cString], "321tset"))
	CHECK(!strcmp([[s1 upper] cString], "321TSET"))
	CHECK(!strcmp([[s1 lower] cString], "321tset"))

	/* Also clears all the memory of the returned C strings */
	[pool release];