ObjFW  Diff

Differences From Artifact [3fa2e6f5e3]:

To Artifact [cdca107008]:

  • File tests/OFDictionary/OFDictionary.m — part of check-in [bbf1f79b8f] at 2009-09-08 16:06:10 on branch trunk — New OFDictionary implementation and removal of a hack in OFList.

    The new implementation is easier to use as it does automatic resizing,
    but therefore it's not realtime-capable anymore. The new implementation
    should also be a little bit faster.

    I decided to change the implementation as only very few need a
    realtime-capable dictionary and those few will most likely write their
    own implementation for their specific case anyway.

    As the new implementation no longer uses OFList, this also made it
    possible to remove a hack from OFList. (user: js, size: 4105) [annotate] [blame] [check-ins using]


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
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







-
+






-
+


-
+



-
-
-
-
+
+
+
+




















-
-
-
-
-
-
-
-
-
-
-
-












-
+



















-
-
+
+







#include <string.h>

#import "OFAutoreleasePool.h"
#import "OFDictionary.h"
#import "OFString.h"
#import "OFExceptions.h"

#define TESTS 15
#define TESTS 14

int
main()
{
	int i = 0;

	OFDictionary *dict = [OFMutableDictionary dictionaryWithHashSize: 16];
	OFDictionary *dict = [OFMutableDictionary dictionary];
	OFDictionary *dict2;
	OFArray *keys, *objs;
	OFIterator *iter = [dict iterator];
	OFIterator *iter;
	of_iterator_pair_t pair[2];

	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *key1 = [OFString stringWithCString: "key1"];
	OFString *key2 = [OFString stringWithCString: "key2"];
	OFString *value1 = [OFString stringWithCString: "value1"];
	OFString *value2 = [OFString stringWithCString: "value2"];
	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++;
	pair[0] = [iter nextKeyObjectPair];
	pair[1] = [iter nextKeyObjectPair];
	if (![pair[0].key isEqual: @"key2"] ||
	    ![pair[0].object isEqual: @"value2"] ||
	    ![pair[1].key isEqual: @"key1"] ||
	    ![pair[1].object isEqual: @"value1"]) {
		printf("\033[K\033[1;31mTest %d/%d failed!\033[m\n", i, TESTS);
		return 1;
	}

	i++;
	[dict changeHashSize: 8];
	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 averageItemsPerBucket] != 1.0) {
	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];
							   @"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++;