~ubuntu-branches/ubuntu/trusty/gnustep-base/trusty

« back to all changes in this revision

Viewing changes to Testing/nsdictionary.m

Tags: upstream-1.20.0
ImportĀ upstreamĀ versionĀ 1.20.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Test/example program for the base library
2
 
 
3
 
   Copyright (C) 2005 Free Software Foundation, Inc.
4
 
   
5
 
  Copying and distribution of this file, with or without modification,
6
 
  are permitted in any medium without royalty provided the copyright
7
 
  notice and this notice are preserved.
8
 
 
9
 
   This file is part of the GNUstep Base Library.
10
 
*/
11
 
#include <Foundation/NSDictionary.h>
12
 
#include <Foundation/NSEnumerator.h>
13
 
#include <Foundation/NSString.h>
14
 
#include <Foundation/NSArray.h>
15
 
#include <Foundation/NSValue.h>
16
 
#include <Foundation/NSDate.h>
17
 
#include <Foundation/NSAutoreleasePool.h>
18
 
#include <assert.h>
19
 
 
20
 
int
21
 
main(int argc, char** argv, char** envp)
22
 
{
23
 
  NSString      *strs[100000];
24
 
  NSMutableDictionary   *dict;
25
 
  NSDate        *when;
26
 
  int           i, j;
27
 
  NSAutoreleasePool     *arp;
28
 
  id a, b;                      /* dictionaries */
29
 
  id enumerator;
30
 
  id objects, keys;
31
 
  id key;
32
 
  BOOL ok;
33
 
  id o1, o2, o3, o4, o5, o6;
34
 
  NSMutableDictionary   *d1, *d2;
35
 
 
36
 
  arp = [NSAutoreleasePool new];
37
 
 
38
 
  o1 = [[NSNumber numberWithInt:1] stringValue];
39
 
  o2 = [[NSNumber numberWithInt:2] stringValue];
40
 
  o3 = [[NSNumber numberWithInt:3] stringValue];
41
 
  o4 = [[NSNumber numberWithInt:4] stringValue];
42
 
  o5 = [[NSNumber numberWithInt:5] stringValue];
43
 
  o6 = [[NSNumber numberWithInt:6] stringValue];
44
 
 
45
 
  d1 = [[NSMutableDictionary new] autorelease];
46
 
  [d1 setObject:o1 forKey:o1];
47
 
  [d1 setObject:o2 forKey:o2];
48
 
  [d1 setObject:o3 forKey:o3];
49
 
 
50
 
  d2 = [[NSMutableDictionary new] autorelease];
51
 
  [d2 setObject:o4 forKey:o4];
52
 
  [d2 setObject:o5 forKey:o5];
53
 
  [d2 setObject:o6 forKey:o6];
54
 
 
55
 
  [d1 addEntriesFromDictionary: d2];
56
 
 
57
 
  enumerator = [d1 objectEnumerator];
58
 
  while ((b = [enumerator nextObject]))
59
 
    printf("%s ", [b cString]);
60
 
  printf("\n");
61
 
 
62
 
  //behavior_set_debug(0);
63
 
 
64
 
  objects = [NSArray arrayWithObjects:
65
 
                     @"vache", @"poisson", @"cheval", @"poulet", nil];
66
 
  keys = [NSArray arrayWithObjects:
67
 
                  @"cow", @"fish", @"horse", @"chicken", nil];
68
 
  a = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
69
 
  b = [NSDictionary dictionaryWithObjectsAndKeys:
70
 
    @"vache",
71
 
    @"cow",
72
 
    @"poisson",
73
 
    @"fish",
74
 
    @"cheval",
75
 
    @"horse",
76
 
    @"poulet",
77
 
    @"chicken", nil];
78
 
 
79
 
  printf("Match is %d\n", [a isEqual: b]);
80
 
 
81
 
  printf("NSDictionary has count %d\n", [a count]);
82
 
  key = @"fish";
83
 
  printf("Object at key %s is %s\n",
84
 
         [key cString],
85
 
         [[a objectForKey:key] cString]);
86
 
 
87
 
  assert([a count] == [[a allValues] count]);
88
 
 
89
 
  enumerator = [a objectEnumerator];
90
 
  while ((b = [enumerator nextObject]))
91
 
    printf("%s ", [b cString]);
92
 
  printf("\n");
93
 
 
94
 
  enumerator = [a keyEnumerator];
95
 
  while ((b = [enumerator nextObject]))
96
 
    printf("%s ", [b cString]);
97
 
  printf("\n");
98
 
 
99
 
  b = [a mutableCopy];
100
 
  assert([b count]);
101
 
 
102
 
  ok = [b isEqual: a];
103
 
  assert(ok);
104
 
 
105
 
  [b setObject:@"formi" forKey:@"ant"];
106
 
  [b removeObjectForKey:@"horse"];
107
 
 
108
 
 
109
 
  when = [NSDate date];
110
 
  dict = [NSMutableDictionary dictionaryWithCapacity: 100];
111
 
  for (i = 0; i < 10; i++)
112
 
    {
113
 
      strs[i] = [NSString stringWithFormat: @"Dictkey-%d", i];
114
 
      [dict setObject: strs[i] forKey: strs[i]];
115
 
    }
116
 
  printf("    10 creation: %f\n", [[NSDate date] timeIntervalSinceDate: when]);
117
 
printf("%s\n", [[[dict allKeys] description] cString]);
118
 
 
119
 
  when = [NSDate date];
120
 
  for (i = 0; i < 100000; i++) {
121
 
    for (j = 0; j < 10; j++) {
122
 
      NSString  *val = [dict objectForKey: strs[j]];
123
 
    }
124
 
  }
125
 
  printf("    10 For: %f\n", [[NSDate date] timeIntervalSinceDate: when]);
126
 
  [arp release];
127
 
 
128
 
  arp = [NSAutoreleasePool new];
129
 
 
130
 
  when = [NSDate date];
131
 
  dict = [NSMutableDictionary dictionaryWithCapacity: 100];
132
 
  for (i = 0; i < 100; i++)
133
 
    {
134
 
      strs[i] = [NSString stringWithFormat: @"Dictkey-%d", i];
135
 
      [dict setObject: strs[i] forKey: strs[i]];
136
 
    }
137
 
  printf("   100 creation: %f\n", [[NSDate date] timeIntervalSinceDate: when]);
138
 
 
139
 
  when = [NSDate date];
140
 
  for (i = 0; i < 10000; i++) {
141
 
    for (j = 0; j < 100; j++) {
142
 
      NSString  *val = [dict objectForKey: strs[j]];
143
 
    }
144
 
  }
145
 
  printf("   100 For: %f\n", [[NSDate date] timeIntervalSinceDate: when]);
146
 
  [arp release];
147
 
 
148
 
  arp = [NSAutoreleasePool new];
149
 
 
150
 
  when = [NSDate date];
151
 
  dict = [NSMutableDictionary dictionaryWithCapacity: 1000];
152
 
  for (i = 0; i < 1000; i++)
153
 
    {
154
 
      strs[i] = [NSString stringWithFormat: @"Dictkey-%d", i];
155
 
      [dict setObject: strs[i] forKey: strs[i]];
156
 
    }
157
 
  printf("  1000 creation: %f\n", [[NSDate date] timeIntervalSinceDate: when]);
158
 
 
159
 
  when = [NSDate date];
160
 
  for (i = 0; i < 1000; i++) {
161
 
    for (j = 0; j < 1000; j++) {
162
 
      NSString  *val = [dict objectForKey: strs[j]];
163
 
    }
164
 
  }
165
 
  printf("  1000 For: %f\n", [[NSDate date] timeIntervalSinceDate: when]);
166
 
  [arp release];
167
 
 
168
 
  arp = [NSAutoreleasePool new];
169
 
 
170
 
  when = [NSDate date];
171
 
  dict = [NSMutableDictionary dictionaryWithCapacity: 10000];
172
 
  for (i = 0; i < 10000; i++)
173
 
    {
174
 
      strs[i] = [NSString stringWithFormat: @"Dictkey-%d", i];
175
 
      [dict setObject: strs[i] forKey: strs[i]];
176
 
    }
177
 
  printf(" 10000 creation: %f\n", [[NSDate date] timeIntervalSinceDate: when]);
178
 
 
179
 
  when = [NSDate date];
180
 
  for (i = 0; i < 100; i++) {
181
 
    for (j = 0; j < 10000; j++) {
182
 
      NSString  *val = [dict objectForKey: strs[j]];
183
 
    }
184
 
  }
185
 
  printf(" 10000 For: %f\n", [[NSDate date] timeIntervalSinceDate: when]);
186
 
  [arp release];
187
 
 
188
 
  arp = [NSAutoreleasePool new];
189
 
 
190
 
  when = [NSDate date];
191
 
  dict = [NSMutableDictionary dictionaryWithCapacity: 100000];
192
 
  for (i = 0; i < 100000; i++)
193
 
    {
194
 
      strs[i] = [NSString stringWithFormat: @"Dictkey-%d", i];
195
 
      [dict setObject: strs[i] forKey: strs[i]];
196
 
    }
197
 
  printf("100000 creation: %f\n", [[NSDate date] timeIntervalSinceDate: when]);
198
 
 
199
 
  when = [NSDate date];
200
 
  for (i = 0; i < 10; i++) {
201
 
    for (j = 0; j < 100000; j++) {
202
 
      NSString  *val = [dict objectForKey: strs[j]];
203
 
    }
204
 
  }
205
 
  printf("100000 For: %f\n", [[NSDate date] timeIntervalSinceDate: when]);
206
 
 
207
 
  exit(0);
208
 
}