mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
New file.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1395 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
c9a18f5baf
commit
30ee076154
2 changed files with 269 additions and 0 deletions
234
Testing/fref.m
Normal file
234
Testing/fref.m
Normal file
|
@ -0,0 +1,234 @@
|
|||
/* Test NSArchiver on encoding of self-referential forward references. */
|
||||
|
||||
#include <Foundation/NSArchiver.h>
|
||||
#include <Foundation/NSArray.h>
|
||||
|
||||
/* Use GNU Archiving features, if they are available. */
|
||||
#define TRY_GNU_ARCHIVING 1
|
||||
|
||||
/* The -initWithCoder methods substitutes another object for self. */
|
||||
static int decode_substitutes;
|
||||
|
||||
#define GNU_ARCHIVING (TRY_GNU_ARCHIVING && defined(OBJECTS_MAJOR_VERSION))
|
||||
|
||||
#if GNU_ARCHIVING
|
||||
#include <objects/Archiver.h>
|
||||
/* Use text coding instead of binary coding */
|
||||
#define TEXTCSTREAM 0
|
||||
#if TEXTCSTREAM
|
||||
#include <objects/Archiver.h>
|
||||
#include <objects/TextCStream.h>
|
||||
#endif
|
||||
#endif /* GNU_ARCHIVING */
|
||||
|
||||
/* This object encodes an -encodeConditionalObject: reference to a Foo. */
|
||||
@interface SubFoo : NSObject
|
||||
{
|
||||
id super_foo;
|
||||
int label;
|
||||
}
|
||||
@end
|
||||
|
||||
/* This object encodes an -encodeObject: reference to a SubFoo. */
|
||||
@interface Foo : NSObject
|
||||
{
|
||||
id sub_foo;
|
||||
int label;
|
||||
}
|
||||
- (int) label;
|
||||
@end
|
||||
|
||||
@implementation SubFoo
|
||||
|
||||
- initWithSuperFoo: o label: (int)l
|
||||
{
|
||||
[super init];
|
||||
super_foo = o;
|
||||
label = l;
|
||||
return self;
|
||||
}
|
||||
|
||||
- superFoo
|
||||
{
|
||||
return super_foo;
|
||||
}
|
||||
|
||||
- (void) encodeWithCoder: coder
|
||||
{
|
||||
[super encodeWithCoder: coder];
|
||||
#if GNU_ARCHIVING
|
||||
[coder encodeObjectReference: super_foo
|
||||
withName: @"super foo"];
|
||||
#else
|
||||
[coder encodeConditionalObject: super_foo];
|
||||
#endif
|
||||
[coder encodeValueOfObjCType: @encode(int)
|
||||
at: &label];
|
||||
}
|
||||
|
||||
- initWithCoder: coder
|
||||
{
|
||||
if (decode_substitutes)
|
||||
{
|
||||
id o = self;
|
||||
self = [[[self class] alloc] init];
|
||||
[o release];
|
||||
}
|
||||
else
|
||||
{
|
||||
self = [super initWithCoder: coder];
|
||||
}
|
||||
#if GNU_ARCHIVING
|
||||
[coder decodeObjectAt: &super_foo
|
||||
withName: NULL];
|
||||
#else
|
||||
super_foo = [coder decodeObject];
|
||||
#endif
|
||||
[coder decodeValueOfObjCType: @encode(int)
|
||||
at: &label];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) print
|
||||
{
|
||||
printf ("label = %d, super label = %d\n",
|
||||
label, [super_foo label]);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation Foo
|
||||
|
||||
- init
|
||||
{
|
||||
[super init];
|
||||
sub_foo = nil;
|
||||
label = 0;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) setSubFoo: o
|
||||
{
|
||||
sub_foo = o;
|
||||
}
|
||||
|
||||
- subFoo
|
||||
{
|
||||
return sub_foo;
|
||||
}
|
||||
|
||||
- (void) encodeWithCoder: coder
|
||||
{
|
||||
[super encodeWithCoder: coder];
|
||||
[coder encodeObject: sub_foo];
|
||||
[coder encodeValueOfObjCType: @encode(int)
|
||||
at: &label];
|
||||
}
|
||||
|
||||
- initWithCoder: coder
|
||||
{
|
||||
if (decode_substitutes)
|
||||
{
|
||||
id o = self;
|
||||
self = [[[self class] alloc] init];
|
||||
[o release];
|
||||
}
|
||||
else
|
||||
{
|
||||
self = [super initWithCoder: coder];
|
||||
}
|
||||
sub_foo = [coder decodeObject];
|
||||
[coder decodeValueOfObjCType: @encode(int)
|
||||
at: &label];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (int) label
|
||||
{
|
||||
return label;
|
||||
}
|
||||
|
||||
- (void) setLabel: (int)l
|
||||
{
|
||||
label = l;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
/* Test the use of -encodeConditional to encode a forward reference
|
||||
to an object. */
|
||||
void
|
||||
test_fref ()
|
||||
{
|
||||
id array;
|
||||
id foo, sub_foo;
|
||||
printf ("Test encoding of forward references\n");
|
||||
decode_substitutes = 0;
|
||||
|
||||
array = [[NSArray alloc] init];
|
||||
foo = [[Foo alloc] init];
|
||||
[foo setLabel: 4];
|
||||
sub_foo = [[SubFoo alloc] initWithSuperFoo: foo label: 3];
|
||||
[foo setSubFoo: sub_foo];
|
||||
[array addObject: foo];
|
||||
[array insertObject: sub_foo atIndex: 0];
|
||||
|
||||
[NSArchiver archiveRootObject: array toFile: @"fref.dat"];
|
||||
printf ("Encoded: ");
|
||||
[sub_foo print];
|
||||
[foo release];
|
||||
[sub_foo release];
|
||||
[array release];
|
||||
|
||||
array = [NSUnarchiver unarchiveObjectWithFile: @"fref.dat"];
|
||||
foo = [array objectAtIndex: 1];
|
||||
sub_foo = [foo subFoo];
|
||||
printf ("Decoded: ");
|
||||
[sub_foo print];
|
||||
}
|
||||
|
||||
/* Test the encode of a self-referential forward reference. */
|
||||
void
|
||||
test_self_fref ()
|
||||
{
|
||||
id foo, sub_foo;
|
||||
printf ("Test encoding of self-referential forward references\n");
|
||||
decode_substitutes = 1;
|
||||
|
||||
foo = [[Foo alloc] init];
|
||||
[foo setLabel: 4];
|
||||
sub_foo = [[SubFoo alloc] initWithSuperFoo: foo label: 3];
|
||||
[foo setSubFoo: sub_foo];
|
||||
|
||||
[NSArchiver archiveRootObject: foo toFile: @"fref.dat"];
|
||||
printf ("Encoded: ");
|
||||
[sub_foo print];
|
||||
[foo release];
|
||||
[sub_foo release];
|
||||
|
||||
foo = [NSUnarchiver unarchiveObjectWithFile: @"fref.dat"];
|
||||
sub_foo = [foo subFoo];
|
||||
printf ("Decoded: ");
|
||||
[sub_foo print];
|
||||
}
|
||||
|
||||
int
|
||||
main ()
|
||||
{
|
||||
|
||||
#if TEXTCSTREAM
|
||||
[Archiver setDefaultCStreamClass: [TextCStream class]];
|
||||
#endif
|
||||
|
||||
test_fref ();
|
||||
test_self_fref ();
|
||||
|
||||
#if 0
|
||||
printf ("foo 0x%x sub_foo 0x%x\n",
|
||||
(unsigned)foo, (unsigned)sub_foo);
|
||||
printf ("sub_foo 0x%x super_foo 0x%x\n",
|
||||
(unsigned)sub_foo, (unsigned)[sub_foo superFoo]);
|
||||
#endif
|
||||
|
||||
exit (0);
|
||||
}
|
35
Testing/randoms.m
Normal file
35
Testing/randoms.m
Normal file
|
@ -0,0 +1,35 @@
|
|||
|
||||
#include <objects/Random.h>
|
||||
#include <objects/RNGBerkeley.h>
|
||||
#include <objects/RNGAdditiveCongruential.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
id r;
|
||||
id rng;
|
||||
int i;
|
||||
|
||||
r = [[Random alloc] init];
|
||||
printf("float\n");
|
||||
for (i = 0; i < 20; i++)
|
||||
printf("%f\n", [r randomFloat]);
|
||||
printf("doubles\n");
|
||||
for (i = 0; i < 20; i++)
|
||||
printf("%f\n", [r randomDouble]);
|
||||
|
||||
rng = [[RNGBerkeley alloc] init];
|
||||
printf("%s chi^2 = %f\n",
|
||||
[rng name], [Random chiSquareOfRandomGenerator:rng]);
|
||||
[r release];
|
||||
|
||||
rng = [[RNGAdditiveCongruential alloc] init];
|
||||
/*
|
||||
for (i = 0; i < 50; i++)
|
||||
printf("%ld\n", [r nextRandom]);
|
||||
*/
|
||||
printf("%s chi^2 = %f\n",
|
||||
[rng name], [Random chiSquareOfRandomGenerator:rng]);
|
||||
[rng release];
|
||||
|
||||
exit(0);
|
||||
}
|
Loading…
Reference in a new issue