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

« back to all changes in this revision

Viewing changes to Testing/fref.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
 
/* Test NSArchiver on encoding of self-referential forward references. */
12
 
 
13
 
#include <Foundation/NSArchiver.h>
14
 
#include <Foundation/NSArray.h>
15
 
#include <Foundation/NSAutoreleasePool.h>
16
 
 
17
 
/* This object encodes an -encodeConditionalObject: reference to a Foo. */
18
 
@interface SubFoo : NSObject
19
 
{
20
 
  id super_foo;
21
 
  int label;
22
 
}
23
 
@end
24
 
 
25
 
/* This object encodes an -encodeObject: reference to a SubFoo. */
26
 
@interface Foo : NSObject
27
 
{
28
 
  id sub_foo;
29
 
  int label;
30
 
}
31
 
- (int) label;
32
 
@end
33
 
 
34
 
@implementation SubFoo
35
 
 
36
 
- (void) dealloc
37
 
{
38
 
  RELEASE(super_foo);
39
 
  [super dealloc];
40
 
}
41
 
 
42
 
- (id) initWithSuperFoo: (id)o label: (int)l
43
 
{
44
 
  self = [super init];
45
 
  super_foo = RETAIN(o);
46
 
  label = l;
47
 
  return self;
48
 
}
49
 
 
50
 
- (id) superFoo
51
 
{
52
 
  return super_foo;
53
 
}
54
 
 
55
 
- (void) encodeWithCoder: (NSCoder*)coder
56
 
{
57
 
  printf ("In [SubFoo encodeWithCoder:]\n");
58
 
  [coder encodeConditionalObject: super_foo];
59
 
  [coder encodeValueOfObjCType: @encode(int)
60
 
         at: &label];
61
 
}
62
 
 
63
 
- (id) initWithCoder: (NSCoder*)coder
64
 
{
65
 
  [coder decodeValueOfObjCType: @encode(id)
66
 
                            at: &super_foo];
67
 
  [coder decodeValueOfObjCType: @encode(int)
68
 
                            at: &label];
69
 
  return self;
70
 
}
71
 
 
72
 
- (void) print
73
 
{
74
 
  printf ("label = %d, super label = %d\n",
75
 
          label, [super_foo label]);
76
 
}
77
 
 
78
 
@end
79
 
 
80
 
@implementation Foo
81
 
 
82
 
- (void) dealloc
83
 
{
84
 
  RELEASE(sub_foo);
85
 
  [super dealloc];
86
 
}
87
 
 
88
 
- (id) init
89
 
{
90
 
  self = [super init];
91
 
  sub_foo = nil;
92
 
  label = 0;
93
 
  return self;
94
 
}
95
 
 
96
 
- (void) setSubFoo: o
97
 
{
98
 
  ASSIGN(sub_foo, o);
99
 
}
100
 
 
101
 
- (id) subFoo
102
 
{
103
 
  return sub_foo;
104
 
}
105
 
 
106
 
- (void) encodeWithCoder: (NSCoder*)coder
107
 
{
108
 
  printf ("In [Foo encodeWithCoder:]\n");
109
 
  [coder encodeObject: sub_foo];
110
 
  [coder encodeValueOfObjCType: @encode(int)
111
 
         at: &label];
112
 
}
113
 
 
114
 
- (id) initWithCoder: (NSCoder*)coder
115
 
{
116
 
  [coder decodeValueOfObjCType: @encode(id)
117
 
                            at: &sub_foo];
118
 
  [coder decodeValueOfObjCType: @encode(int)
119
 
                            at: &label];
120
 
  return self;
121
 
}
122
 
 
123
 
- (int) label
124
 
{
125
 
  return label;
126
 
}
127
 
 
128
 
- (void) setLabel: (int)l
129
 
{
130
 
  label = l;
131
 
}
132
 
 
133
 
@end
134
 
 
135
 
/* Test the use of -encodeConditional to encode a forward reference
136
 
   to an object. */
137
 
void
138
 
test_fref ()
139
 
{
140
 
  id array;
141
 
  id foo, sub_foo;
142
 
  printf ("\nTest encoding of forward references\n");
143
 
 
144
 
  array = [[NSMutableArray alloc] init];
145
 
  foo = [[Foo alloc] init];
146
 
  [foo setLabel: 4];
147
 
  sub_foo = [[SubFoo alloc] initWithSuperFoo: foo label: 3];
148
 
  [foo setSubFoo: sub_foo];
149
 
  [array addObject: foo];
150
 
  [array insertObject: sub_foo atIndex: 0];
151
 
 
152
 
  [NSArchiver archiveRootObject: array toFile: @"fref.dat"];
153
 
  printf ("Encoded:  ");
154
 
  [sub_foo print];
155
 
  RELEASE(foo);
156
 
  RELEASE(sub_foo);
157
 
  RELEASE(array);
158
 
 
159
 
  array = [NSUnarchiver unarchiveObjectWithFile: @"fref.dat"];
160
 
  foo = [array objectAtIndex: 1];
161
 
  sub_foo = [foo subFoo];
162
 
  printf ("Decoded:  ");
163
 
  [sub_foo print];
164
 
}
165
 
 
166
 
/* Test the encode of a self-referential forward reference. */
167
 
void
168
 
test_self_fref ()
169
 
{
170
 
  id foo, sub_foo;
171
 
  printf ("\nTest encoding of self-referential forward references\n");
172
 
 
173
 
  foo = [[Foo alloc] init];
174
 
  [foo setLabel: 4];
175
 
  sub_foo = [[SubFoo alloc] initWithSuperFoo: foo label: 3];
176
 
  [foo setSubFoo: sub_foo];
177
 
 
178
 
  [NSArchiver archiveRootObject: foo toFile: @"fref.dat"];
179
 
  printf ("Encoded:  ");
180
 
  [sub_foo print];
181
 
  RELEASE(foo);
182
 
  RELEASE(sub_foo);
183
 
 
184
 
  foo = [NSUnarchiver unarchiveObjectWithFile: @"fref.dat"];
185
 
  sub_foo = [foo subFoo];
186
 
  printf ("Decoded:  ");
187
 
  [sub_foo print];
188
 
}
189
 
 
190
 
int
191
 
main ()
192
 
{
193
 
  CREATE_AUTORELEASE_POOL(arp);
194
 
 
195
 
  test_fref ();
196
 
  test_self_fref ();
197
 
 
198
 
  RELEASE(arp);
199
 
 
200
 
  exit (0);
201
 
}