1#import "Testing.h"
2#import <Foundation/NSAutoreleasePool.h>
3#import <Foundation/NSString.h>
4#import <Foundation/NSArray.h>
5#import <Foundation/NSDictionary.h>
6#import <Foundation/NSData.h>
7#import <Foundation/NSPropertyList.h>
8#import <Foundation/NSProcessInfo.h>
9#import <Foundation/NSValue.h>
10
11static BOOL
12test_parse_unparse(id object)
13{
14  return [[[object description] propertyList] isEqual: object];
15}
16
17static BOOL
18test_parse_unparse_xml(id object)
19{
20  NSPropertyListFormat	format;
21  NSData		*d;
22  id			u;
23
24  d = [NSPropertyListSerialization dataFromPropertyList: object
25    format: NSPropertyListXMLFormat_v1_0 errorDescription: 0];
26  u = [NSPropertyListSerialization propertyListFromData: d
27    mutabilityOption: NSPropertyListImmutable
28    format: &format
29    errorDescription: 0];
30  return [u isEqual: object];
31}
32
33static BOOL
34test_parse_unparse_openstep(id object)
35{
36  NSPropertyListFormat	format;
37  NSData		*d;
38  id			u;
39
40  d = [NSPropertyListSerialization dataFromPropertyList: object
41    format: NSPropertyListOpenStepFormat errorDescription: 0];
42  u = [NSPropertyListSerialization propertyListFromData: d
43    mutabilityOption: NSPropertyListImmutable
44    format: &format
45    errorDescription: 0];
46  return [u isEqual: object];
47}
48
49static BOOL
50test_parse_unparse_binary(id object)
51{
52  NSPropertyListFormat	format;
53  NSData		*d;
54  id			u;
55
56  d = [NSPropertyListSerialization dataFromPropertyList: object
57    format: NSPropertyListBinaryFormat_v1_0 errorDescription: 0];
58  u = [NSPropertyListSerialization propertyListFromData: d
59    mutabilityOption: NSPropertyListImmutable
60    format: &format
61    errorDescription: 0];
62  return [u isEqual: object];
63}
64
65#if     defined(GNUSTEP_BASE_LIBRARY)
66static BOOL
67test_parse_unparse_binary_old(id object)
68{
69  NSPropertyListFormat	format;
70  NSData		*d;
71  id			u;
72
73  d = [NSPropertyListSerialization dataFromPropertyList: object
74    format: NSPropertyListGNUstepBinaryFormat errorDescription: 0];
75  u = [NSPropertyListSerialization propertyListFromData: d
76    mutabilityOption: NSPropertyListImmutable
77    format: &format
78    errorDescription: 0];
79  return [u isEqual: object];
80}
81static BOOL
82test_parse_unparse_gnustep(id object)
83{
84  NSPropertyListFormat	format;
85  NSData		*d;
86  id			u;
87
88  d = [NSPropertyListSerialization dataFromPropertyList: object
89    format: NSPropertyListGNUstepFormat errorDescription: 0];
90  u = [NSPropertyListSerialization propertyListFromData: d
91    mutabilityOption: NSPropertyListImmutable
92    format: &format
93    errorDescription: 0];
94  return [u isEqual: object];
95}
96#endif
97
98int main()
99{
100  BOOL	(*func)(id);
101  int	i;
102  NSAutoreleasePool   *arp = [NSAutoreleasePool new];
103
104  for (i = 0; i < 6; i++)
105    {
106      switch (i)
107        {
108	  case 0:
109	    func = test_parse_unparse;
110	    NSLog(@"test descriptions");
111	    break;
112	  case 1:
113	    func = test_parse_unparse_xml;
114	    NSLog(@"test XML");
115	    break;
116	  case 2:
117	    func = test_parse_unparse_binary;
118	    NSLog(@"test binary");
119	    break;
120	  case 3:
121	    func = test_parse_unparse_openstep;
122	    NSLog(@"test OpenStep");
123	    break;
124	  case 4:
125#if     defined(GNUSTEP_BASE_LIBRARY)
126	    func = test_parse_unparse_gnustep;
127	    NSLog(@"test GNUStep text");
128#else
129	    func = 0;
130#endif
131	  case 5:
132#if     defined(GNUSTEP_BASE_LIBRARY)
133	    func = test_parse_unparse_binary_old;
134	    NSLog(@"test GNUStep old binary");
135#else
136	    func = 0;
137#endif
138	    break;
139	}
140
141      if (func == 0) continue;
142
143      PASS(func(@"ariosto"),
144	   "We can generate a property list from a string");
145
146      PASS(func([@"ariosto" dataUsingEncoding: NSASCIIStringEncoding]),
147	   "We can generate a property list from data");
148
149      PASS(func([NSArray array]),
150	   "We can generate a property list from an empty array");
151
152      PASS(func([NSArray arrayWithObject: @"Palinuro"]),
153	   "We can generate a property list from an array with a single object");
154
155      PASS(func([NSArray arrayWithObjects:
156					 @"Palinuro", @"Enea", nil]),
157	   "We can generate a property list from an array with two objects");
158
159      PASS(func([NSArray arrayWithObjects:
160					 @"Palinuro", @"Enea",
161				       @"Eurialo e Niso", nil]),
162       "We can generate a property list from "
163       "an array with three objects and \"s");
164
165      PASS(func([NSDictionary dictionary]),
166	   "We can generate a property list from an empty dictionary");
167
168      PASS(func([NSDictionary dictionaryWithObject: @"Virgilio"
169					    forKey: @"Autore"]),
170	"We can generate a property list from a "
171	"dictionary with a single key/value pair");
172
173      PASS(func([NSDictionary dictionaryWithObjectsAndKeys:
174					      @"Virgilio", @"Autore",
175					    @"Eneide", @"Titolo", nil]),
176	"We can generate a property list from a "
177	"dictionary with two key/value pairs");
178
179      PASS(func([NSDictionary dictionaryWithObjectsAndKeys:
180					      @"Virgilio", @"Autore",
181					    [NSArray arrayWithObject: @"Molte"],
182					    @"Opere", nil]),
183	"We can generate a property list from a "
184	"dictionary with an array inside");
185
186      {
187	id object  = [NSMutableDictionary dictionary];
188	id objectA = [NSArray arrayWithObject: @"Ciao,"];
189	id objectB = [NSArray arrayWithObject: objectA];
190	id objectC = [NSDictionary dictionary];
191	id objectD = [NSArray arrayWithObject:
192	  [NSArray arrayWithObject:
193	   [NSDictionary dictionaryWithObject:
194	     [NSArray arrayWithObject: @"Ciao,"]
195	     forKey: @"Ciao,"]]];
196	[object setObject: objectA forKey: @"Utinam"];
197	[object setObject: objectB forKey: @"bbb"];
198	[object setObject: objectC forKey: @"ccc"];
199	[object setObject: objectD forKey: @"Auri;"];
200	PASS(func(object),
201	     "We can generate a medium-size property list (1)");
202      }
203      {
204	id object;
205	id objectA;
206	id objectA_A;
207	id objectA_B;
208	id objectB;
209	id objectB_A;
210	id objectB_A_A;
211	id objectB_A_B;
212	id objectB_B;
213
214	/* objectA */
215	objectA_A = [NSMutableDictionary dictionary];
216	[objectA_A setObject: @"1 2 3 4 5 6 7 8 9 0" forKey: @"numeri"];
217	[objectA_A setObject: @"A B C D E F G H I J" forKey: @"lettere"];
218
219	objectA_B = [NSMutableDictionary dictionary];
220	[objectA_B setObject: @"3.1415296" forKey: @"PI greco"];
221	[objectA_B setObject: @"0" forKey: @"zero"];
222	[objectA_B setObject: @"1" forKey: @"uno"];
223
224	objectA = [NSMutableDictionary dictionary];
225	[objectA setObject: objectA_A forKey: @"Informazioni Utili"];
226	[objectA setObject: objectA_B forKey: @"Costanti Numeriche"];
227
228	/* objectB */
229	objectB_A = [NSMutableDictionary dictionary];
230
231	objectB_A_A = [NSMutableArray array];
232	[objectB_A_A addObject: @"1"];
233	[objectB_A_A addObject: @"2"];
234	[objectB_A_A addObject: @"3"];
235	[objectB_A_A addObject: @"4"];
236	[objectB_A_A addObject: @"5"];
237	[objectB_A_A addObject: @"6"];
238	[objectB_A_A addObject: @"7"];
239	[objectB_A_A addObject: @"8"];
240	[objectB_A_A addObject: @"9"];
241	[objectB_A_A addObject: @"0"];
242        if (func == test_parse_unparse_binary
243          || func == test_parse_unparse_xml)
244	  {
245	    [objectB_A_A addObject: [NSNumber numberWithInteger: 1]];
246	    [objectB_A_A addObject: [NSNumber numberWithInteger: 2]];
247	    [objectB_A_A addObject: [NSNumber numberWithInteger: 3]];
248	    [objectB_A_A addObject: [NSNumber numberWithInteger: 4]];
249	    [objectB_A_A addObject: [NSNumber numberWithInteger: 5]];
250	    [objectB_A_A addObject: [NSNumber numberWithInteger: 6]];
251	    [objectB_A_A addObject: [NSNumber numberWithInteger: 7]];
252	    [objectB_A_A addObject: [NSNumber numberWithInteger: 8]];
253	    [objectB_A_A addObject: [NSNumber numberWithInteger: 9]];
254	    [objectB_A_A addObject: [NSNumber numberWithInteger: 0]];
255	    [objectB_A_A addObject: [NSNumber numberWithShort: 1]];
256	    [objectB_A_A addObject: [NSNumber numberWithShort: 2]];
257	    [objectB_A_A addObject: [NSNumber numberWithShort: 3]];
258	    [objectB_A_A addObject: [NSNumber numberWithShort: 4]];
259	    [objectB_A_A addObject: [NSNumber numberWithShort: 5]];
260	    [objectB_A_A addObject: [NSNumber numberWithShort: 6]];
261	    [objectB_A_A addObject: [NSNumber numberWithShort: 7]];
262	    [objectB_A_A addObject: [NSNumber numberWithShort: 8]];
263	    [objectB_A_A addObject: [NSNumber numberWithShort: 9]];
264	    [objectB_A_A addObject: [NSNumber numberWithShort: 0]];
265	    [objectB_A_A addObject: [NSNumber numberWithFloat: 1]];
266	    [objectB_A_A addObject: [NSNumber numberWithFloat: 2]];
267	    [objectB_A_A addObject: [NSNumber numberWithFloat: 3]];
268	    [objectB_A_A addObject: [NSNumber numberWithFloat: 4]];
269	    [objectB_A_A addObject: [NSNumber numberWithFloat: 5]];
270	    [objectB_A_A addObject: [NSNumber numberWithFloat: 6]];
271	    [objectB_A_A addObject: [NSNumber numberWithFloat: 7]];
272	    [objectB_A_A addObject: [NSNumber numberWithFloat: 8]];
273	    [objectB_A_A addObject: [NSNumber numberWithFloat: 9]];
274	    [objectB_A_A addObject: [NSNumber numberWithFloat: 0]];
275	  }
276	[objectB_A setObject: objectB_A_A forKey: @"numeri"];
277
278	objectB_A_B = [NSMutableArray array];
279	[objectB_A_B addObject: @"A"];
280	[objectB_A_B addObject: @"B"];
281	[objectB_A_B addObject: @"C"];
282	[objectB_A_B addObject: @"D"];
283	[objectB_A_B addObject: @"E"];
284	[objectB_A_B addObject: @"F"];
285	[objectB_A_B addObject: @"G"];
286	[objectB_A_B addObject: @"H"];
287	[objectB_A_B addObject: @"I"];
288	[objectB_A_B addObject: @"J"];
289	[objectB_A setObject: objectB_A_B forKey: @"letterine"];
290
291	objectB_B = [NSMutableDictionary dictionary];
292	[objectB_B setObject: @"3.1415296" forKey: @"PI greca"];
293	[objectB_B setObject: @"0" forKey: @"el zero"];
294	[objectB_B setObject: @"1" forKey: @"el uno"];
295
296	objectB = [NSMutableDictionary dictionary];
297	[objectB setObject: objectB_A forKey: @"Informazioni Utili"];
298	[objectB setObject: objectB_B forKey: @"Costanti Numeriche"];
299
300	/* object */
301	object = [NSMutableDictionary dictionary];
302	[object setObject: objectA forKey: @"Un dizionario"];
303	[object setObject: objectB forKey: @"Un altro dizionario"];
304
305	PASS(func(object),
306	     "We can generate a medium-size property list (2)");
307      }
308
309      PASS(func([NSData data]),
310	   "We can generate a property list from an empty data");
311
312      PASS(func([@"Questo e` un test" dataUsingEncoding: 1]),
313	   "We can generate a property list from very simple data");
314
315      PASS(func([[[NSProcessInfo processInfo]
316				 globallyUniqueString] dataUsingEncoding: 7]),
317	   "We can generate a property list from very simple data (2)");
318
319      PASS(func([NSMutableArray arrayWithObject:
320				[@"*()3\"#@Q``''" dataUsingEncoding: 1]]),
321	"We can generate a property list from an "
322	"array containing very simple data");
323
324      {
325	id object = [NSMutableArray array];
326
327	[object addObject: [@"*()3\"#@Q``''" dataUsingEncoding: 1]];
328	[object addObject: @"nicola \" , ; <"];
329	[object addObject: @"<nicola"];
330	[object addObject: @"nicola;"];
331	[object addObject: @"nicola,"];
332	[object addObject: @"nicola>"];
333	[object addObject: @"nicola@"];
334	[object addObject: @"nicola "];
335	[object addObject: @"nicola="];
336	[object addObject: [NSArray arrayWithObject: @"="]];
337	[object addObject: [NSDictionary dictionary]];
338
339	PASS(func(object),
340	  "We can generate a property list from an array containing various things");
341      }
342    }
343#if     defined(GNUSTEP_BASE_LIBRARY)
344{
345  NSData        	*d = [NSData dataWithContentsOfFile: @"props"];
346  NSPropertyListFormat	format;
347  id			u;
348
349  u = [NSPropertyListSerialization propertyListFromData: d
350    mutabilityOption: NSPropertyListImmutable
351    format: &format
352    errorDescription: 0];
353  PASS(nil != u, "parses complex plist");
354}
355#endif
356
357#if     defined(GNUSTEP_BASE_LIBRARY)
358{
359  NSData        	*d = [NSData dataWithContentsOfFile: @"cyclic.plist"];
360  NSPropertyListFormat	format;
361  id			u = nil;
362  PASS_EXCEPTION(
363  u = [NSPropertyListSerialization propertyListFromData: d
364    mutabilityOption: NSPropertyListImmutable
365    format: &format
366    errorDescription: 0];, NSGenericException, "Does not crash on binary plist with cyclic references." );
367  PASS(nil == u, "Rejects cyclic plist");
368}
369#endif
370
371
372  [arp release]; arp = nil;
373  return 0;
374}
375
376