Added more thorough structure handling tests and removed some old ones.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@16418 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2003-04-10 16:28:07 +00:00
parent 794ed2711b
commit 524250e8c1
7 changed files with 74 additions and 572 deletions

View file

@ -37,6 +37,25 @@ int main ()
id x;
NSString *s;
NSArray *a = [NSArray arrayWithObjects: @"a", @"b", nil];
struct aa {char a; double b; char c;} bb[2];
struct objc_struct_layout layout;
unsigned i;
printf("size = %d\n", objc_sizeof_type(@encode(struct aa)));
printf("pos = %d\n", (void*)&bb[1] - (void*)&bb[0]);
objc_layout_structure (@encode(struct aa), &layout);
while (objc_layout_structure_next_member (&layout))
{
int position, align;
const char *type;
objc_layout_structure_get_info (&layout, &position, &align, &type);
printf ("element %d has offset %d, alignment %d\n",
i++, position, align);
}
o = [GCMutableArray new];
x = [GCMutableArray new];

View file

@ -1,242 +0,0 @@
#include <stdio.h>
#include <Foundation/NSObject.h>
#include <Foundation/NSConnection.h>
#include <Foundation/NSDistantObject.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSString.h>
#include <Foundation/NSRunLoop.h>
#include <Foundation/NSData.h>
#include <Foundation/NSDate.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSDebug.h>
#include <assert.h>
#include "server.h"
@interface Auth : NSObject
@end
@implementation Auth
- (BOOL) authenticateComponents: (NSMutableArray*)components
withData: (NSData*)authData
{
unsigned count = [components count];
while (count-- > 0)
{
id obj = [components objectAtIndex: count];
if ([obj isKindOfClass: [NSData class]] == YES)
{
NSMutableData *d = [obj mutableCopy];
unsigned l = [d length];
char *p = (char*)[d mutableBytes];
while (l-- > 0)
p[l] ^= 42;
[components replaceObjectAtIndex: count withObject: d];
RELEASE(d);
}
}
return YES;
}
@end
int main (int argc, char *argv[])
{
id a;
id p;
id callback_receiver = [NSObject new];
id o;
id localObj;
unsigned long i = 4;
id c;
int j,k;
foo f = {99, "cow", 9876543};
foo f2;
small_struct ss;
foo *fp;
const char *n;
int a3[3] = {66,77,88};
struct myarray ma = {{55,66,77}};
double dbl = 3.14159265358979323846264338327;
double *dbl_ptr;
char *string = "Hello from the client";
small_struct small = {12};
BOOL b;
const char *type;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
Auth *auth = [Auth new];
GSDebugAllocationActive(YES);
[NSConnection setDebug: 10];
[NSDistantObject setDebug: 10];
[NSPort setDebug: 10];
#if NeXT_runtime
[NSDistantObject setProtocolForProxies:@protocol(AllProxies)];
#endif
printf("oneway %d\n", _F_ONEWAY);
if (argc > 1)
{
if (argc > 2)
p = [NSConnection rootProxyForConnectionWithRegisteredName: [NSString stringWithCString: argv[2]]
host: [NSString stringWithCString:argv[1]]];
else
p = [NSConnection rootProxyForConnectionWithRegisteredName:@"test2server"
host:[NSString stringWithCString:argv[1]]];
}
else
p = [NSConnection rootProxyForConnectionWithRegisteredName:@"test2server"
host: @"*"];
c = [p connectionForProxy];
[c setDelegate:auth];
[c setRequestTimeout:180.0];
[c setReplyTimeout:180.0];
localObj = [[NSObject alloc] init];
[p outputStats:localObj];
[p getLong:&i];
[p getLong:&i];
[p outputStats:localObj];
printf(">>list proxy's hash is 0x%x\n",
(unsigned)[p hash]);
printf(">>list proxy's self is 0x%x = 0x%x\n",
(unsigned)[p self], (unsigned)p);
n = [p name];
printf(">>proxy's name is (%s)\n", n);
[p print:">>This is a message from the client."];
printf(">>getLong:(out) to server i = %lu\n", i);
[p getLong:&i];
printf(">>getLong:(out) from server i = %lu\n", i);
assert(i == 3);
o = [p objectAt:0];
printf(">>object proxy's hash is 0x%x\n", (unsigned)[o hash]);
[p shout];
[p callbackNameOn:callback_receiver];
/* this next line doesn't actually test callbacks, it tests
sending the same object twice in the same message. */
[p callbackNameOn:p];
b = [p doBoolean:YES];
printf(">>BOOL value is '%c' (0x%x)\n", b, (int)b);
#if 0
/* Both these cause problems because GCC encodes them as "*",
indistinguishable from strings. */
b = NO;
[p getBoolean:&b];
printf(">>BOOL reference is '%c' (0x%x)\n", b, (int)b);
b = NO;
[p getUCharPtr:&b];
printf(">>UCHAR reference is '%c' (0x%x)\n", b, (int)b);
#endif
fp = [p sendStructPtr:&f];
fp->i = 11;
[p sendStruct:f];
[p sendSmallStruct:small];
[p sendStructArray:ma];
#if 1
f2 = [p returnStruct];
printf(">>returned foo: i=%d s=%s l=%lu\n",
f2.i, f2.s, f2.l);
ss = [p returnSmallStruct];
printf(">>returned ss: %d\n", ss.z);
f2 = [p returnSetStruct: 99];
printf(">>returned foo: i=%d s=%s l=%lu\n",
f2.i, f2.s, f2.l);
ss = [p returnSetSmallStruct: 99];
printf(">>returned ss: %d\n", ss.z);
#endif
{
float f = 98.6f;
printf(">>sending double %f, float %f\n", dbl, f);
[p sendDouble:dbl andFloat:f];
}
dbl_ptr = [p doDoublePointer:&dbl];
printf(">>got double %f from server\n", *dbl_ptr);
[p sendCharPtrPtr:&string];
/* testing "-performSelector:" */
if (p != [p performSelector:sel_get_any_uid("self")])
[NSObject error:"trying performSelector:"];
/* testing "bycopy" */
/* reverse the order on these next two and it doesn't crash,
however, having manyArgs called always seems to crash.
Was this problem here before object forward references?
Check a snapshot.
Hmm. It seems like a runtime selector-handling bug. */
if (![p isProxy])
[p manyArgs:1 :2 :3 :4 :5 :6 :7 :8 :9 :10 :11 :12];
[p sendBycopy:callback_receiver];
printf(">>returned float %f\n", [p returnFloat]);
printf(">>returned double %f\n", [p returnDouble]);
#ifdef _F_BYREF
[p sendByref:callback_receiver];
[p sendByref:@"hello"];
[p sendByref:[NSDate date]];
#endif
[p addObject:localObj];
k = [p count];
for (j = 0; j < k; j++)
{
id remote_peer_obj = [p objectAt:j];
printf("triangle %d object proxy's hash is 0x%x\n",
j, (unsigned)[remote_peer_obj hash]);
#if 0
/* xxx look at this again after we use release/retain everywhere */
if ([remote_peer_obj isProxy])
[remote_peer_obj release];
#endif
remote_peer_obj = [p objectAt:j];
printf("repeated triangle %d object proxy's hash is 0x%x\n",
j, (unsigned)[remote_peer_obj hash]);
}
[p outputStats:localObj];
o = [c statistics];
a = [o allKeys];
for (j = 0; j < [a count]; j++)
{
id k = [a objectAtIndex:j];
id v = [o objectForKey:k];
printf("%s - %s\n", [k cString], [[v description] cString]);
}
{
NSDate *d = [NSDate date];
NSData *sen = [NSMutableData data];
id rep;
[sen setLength: 100000];
rep = [p echoObject: sen];
NSLog(@"Send: 0x%x, Reply: 0x%x, Length: %d", sen, rep, [rep length]);
[NSConnection setDebug: 0];
[NSDistantObject setDebug: 0];
[NSPort setDebug: 0];
for (i = 0; i < 10000; i++)
{
k = [p count];
for (j = 0; j < k; j++)
{
id remote_peer_obj = [p objectAt: j];
}
[p echoObject: localObj];
}
NSLog(@"Delay is %f", [d timeIntervalSinceNow]);
exit(0);
}
[arp release];
arp = [NSAutoreleasePool new];
printf("%d\n", [c retainCount]);
printf("%s\n", [[[c statistics] description] cString]);
// printf("%s\n", GSDebugAllocationList(YES));
[[NSRunLoop currentRunLoop] runUntilDate:
[NSDate dateWithTimeIntervalSinceNow: 20 * 60]];
[c invalidate];
[arp release];
exit(0);
}

View file

@ -10,6 +10,7 @@
#include <Foundation/NSValue.h>
#include <Foundation/NSDate.h>
typedef struct { char a; double b; char c;} tstruct;
int main()
{
id set;
@ -18,6 +19,8 @@ int main()
id una;
id xxx;
id apl;
tstruct ss;
tstruct tt;
[NSAutoreleasePool enableDoubleReleaseCheck:YES];
@ -40,6 +43,19 @@ int main()
printf("%s\n", [[o description] cString]);
}
apl = [[NSAutoreleasePool alloc] init];
ss.a = 'A';
ss.b = 1.234;
ss.c = 'Z';
arc = [[NSArchiver new] autorelease];
[arc encodeValueOfObjCType: @encode(tstruct) at: &ss];
una = [[[NSUnarchiver alloc] initForReadingWithData: [arc archiverData]] autorelease];
[una decodeValueOfObjCType: @encode(tstruct) at: &tt];
[apl release];
if (ss.a != tt.a) printf("Encoded '%c' in 'a' but got '%c'\n", ss.a, tt.a);
if (ss.b != tt.b) printf("Encoded '%f' in 'a' but got '%f'\n", ss.b, tt.b);
if (ss.c != tt.c) printf("Encoded '%c' in 'a' but got '%c'\n", ss.c, tt.c);
apl = [[NSAutoreleasePool alloc] init];
arc = [[NSArchiver new] autorelease];
printf("%u\n", [arc retainCount]);

View file

@ -48,6 +48,7 @@
int con_data (id prx)
{
id pool;
BOOL b, br;
unsigned char uc, ucr;
char c, cr;
@ -59,7 +60,7 @@ int con_data (id prx)
char *str;
id obj;
small_struct small = {12};
foo ffoo = {99, "cow", 9876543};
foo ffoo = {'Z', 1234.5678, 99, "cow", 9876543};
int a3[3] = {66,77,88};
struct myarray ma = {{55,66,77}};
@ -85,6 +86,7 @@ int con_data (id prx)
printf(" error is ok (due to incorrect encoding by gcc)\n");
#define TEST_CALL(test, send, got, sendp, var, varr, val, msg1, msg2) \
pool = [NSAutoreleasePool new]; \
printf(test); \
var = val; \
printf(send, var); \
@ -101,9 +103,11 @@ int con_data (id prx)
if (varr != (var+ADD_CONST)) \
printf(" *** ERROR ***\n"); \
else \
printf(" ...ok\n");
printf(" ...ok\n"); \
[pool release];
#define TEST_FCALL(test, send, got, sendp, var, varr, val, msg1, msg2) \
pool = [NSAutoreleasePool new]; \
printf(test); \
var = val; \
printf(send, var); \
@ -120,7 +124,8 @@ int con_data (id prx)
if (varr - (var+ADD_CONST) > 1e-3) \
printf(" *** ERROR ***\n"); \
else \
printf(" ...ok\n");
printf(" ...ok\n"); \
[pool release];
TEST_CALL("UChar:\n", " sending %d", " got %d", " sending ptr to %d",
uc, ucr, 23, sendUChar:, getUChar:)
@ -151,16 +156,22 @@ int con_data (id prx)
[prx sendDouble:dbl andFloat:flt];
pool = [NSAutoreleasePool new];
printf("String:\n");
str = "My String 1";
printf(" sending (%s)", str);
str = [prx sendString: str];
printf(" got (%s)\n", str);
[pool release];
pool = [NSAutoreleasePool new];
str = "My String 3";
printf(" sending ptr to (%s)", str);
[prx getString: &str];
printf(" got (%s)\n", str);
[pool release];
pool = [NSAutoreleasePool new];
printf("Small Struct:\n");
//printf(" sending %x", small.z);
//small = [prx sendSmallStruct: small];
@ -168,15 +179,23 @@ int con_data (id prx)
printf(" sending ptr to %x", small.z);
[prx getSmallStruct: &small];
printf(" got %x\n", small.z);
[pool release];
pool = [NSAutoreleasePool new];
printf("Struct:\n");
printf(" sending i=%d,s=%s,l=%ld", ffoo.i, ffoo.s, ffoo.l);
printf(" sending c='%c',d=%g,i=%d,s=%s,l=%ld",
ffoo.c, ffoo.d, ffoo.i, ffoo.s, ffoo.l);
ffoo = [prx sendStruct: ffoo];
printf(" got %d %s %ld\n", ffoo.i, ffoo.s, ffoo.l);
printf(" sending ptr to i=%d,s=%s,l=%ld", ffoo.i, ffoo.s, ffoo.l);
printf(" got c='%c',d=%g,i=%d,s=%s,l=%ld\n",
ffoo.c, ffoo.d, ffoo.i, ffoo.s, ffoo.l);
printf(" sending ptr to c='%c',d=%g,i=%d,s=%s,l=%ld",
ffoo.c, ffoo.d, ffoo.i, ffoo.s, ffoo.l);
[prx getStruct: &ffoo];
printf(" got i=%d,s=%s,l=%ld\n", ffoo.i, ffoo.s, ffoo.l);
printf(" got c='%c',d=%g,i=%d,s=%s,l=%ld\n",
ffoo.c, ffoo.d, ffoo.i, ffoo.s, ffoo.l);
[pool release];
pool = [NSAutoreleasePool new];
printf("Object:\n");
obj = [NSObject new];
[prx addObject: obj]; // FIXME: Why is this needed?
@ -186,6 +205,7 @@ int con_data (id prx)
printf(" sending ptr to %s", [[obj description] cString]);
[prx getObject: &obj];
printf(" got %s\n", [[obj description] cString]);
[pool release];
printf("Many Arguments:\n");
[prx manyArgs:1 :2 :3 :4 :5 :6 :7 :8 :9 :10 :11 :12];

View file

@ -244,20 +244,24 @@
- (foo) sendStruct: (foo)f
{
foo f2 = {1, "horse", 987654};
printf("(%s) got i=%d s=%s l=%lu", sel_get_name(_cmd), f.i, f.s, f.l);
foo f2 = {'A', 123.456, 1, "horse", 987654};
printf("(%s) got c='%c', d=%g, i=%d, s=%s, l=%lu",
sel_get_name(_cmd), f.c, f.d, f.i, f.s, f.l);
fflush(stdout);
printf(" returning i=%d s=%s l=%lu\n", f2.i, f2.s, f2.l);
printf(" returning c='%c', d=%g, i=%d, s=%s, l=%lu\n",
f2.c, f2.d, f2.i, f2.s, f2.l);
fflush(stdout);
return f2;
}
- (void) getStruct: (foo *)f
{
foo f2 = {1, "horse", 987654};
printf("(%s) got i=%d s=%s l=%lu,", sel_get_name(_cmd), f->i, f->s, f->l);
foo f2 = {'A', 123.456, 1, "horse", 987654};
printf("(%s) got c='%c', d=%g, i=%d, s=%s, l=%lu",
sel_get_name(_cmd), f->c, f->d, f->i, f->s, f->l);
fflush(stdout);
printf(" returning i=%d s=%s l=%lu\n", f2.i, f2.s, f2.l);
printf(" returning c='%c', d=%g, i=%d, s=%s, l=%lu\n",
f2.c, f2.d, f2.i, f2.s, f2.l);
fflush(stdout);
*f = f2;
}

View file

@ -9,6 +9,8 @@ typedef struct _small_struct {
} small_struct;
typedef struct _foo {
char c;
double d;
int i;
char *s;
unsigned long l;

View file

@ -1,317 +0,0 @@
#include <base/preface.h>
#include <stdio.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSConnection.h>
#include <Foundation/NSDistantObject.h>
#include <Foundation/NSString.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSData.h>
#include <Foundation/NSRunLoop.h>
#include <base/BinaryCStream.h>
#include <Foundation/NSAutoreleasePool.h>
#include "server.h"
@implementation Server
- (NSData*) authenticationDataForComponents: (NSMutableArray*)components
{
unsigned count = [components count];
while (count-- > 0)
{
id obj = [components objectAtIndex: count];
if ([obj isKindOfClass: [NSData class]] == YES)
{
NSMutableData *d = [obj mutableCopy];
unsigned l = [d length];
char *p = (char*)[d mutableBytes];
while (l-- > 0)
p[l] ^= 42;
[components replaceObjectAtIndex: count withObject: d];
RELEASE(d);
}
}
return [NSData data];
}
- init
{
the_array = [[NSMutableArray alloc] init];
return self;
}
- (unsigned) count
{
return [the_array count];
}
- (void) addObject: o
{
[the_array addObject:o];
}
- objectAt: (unsigned)i
{
if (i < [the_array count])
return [the_array objectAtIndex: i];
else
return nil;
}
- print: (const char *)msg
{
printf(">>%s\n", msg);
return self;
}
- getLong: (out unsigned long*)i
{
printf(">>getLong:(out) from client %lu\n", *i);
*i = 3;
printf(">>getLong:(out) to client %lu\n", *i);
return self;
}
- (oneway void) shout
{
printf(">>Ahhhhh\n");
return;
}
- callbackNameOn: obj
{
printf (">>callback name is (%s)\n", object_get_class_name (obj));
return self;
}
/* sender must also respond to 'bounce:count:' */
- bounce: sender count: (int)c
{
if (--c)
[sender bounce:self count:c];
return self;
}
- (BOOL) doBoolean: (BOOL)b
{
printf(">> got boolean '%c' (0x%x) from client\n", b, (unsigned int)b);
return YES;
}
/* This causes problems, because the runtime encodes this as "*",
a string! */
- getBoolean: (BOOL*)bp
{
printf(">> got boolean pointer '%c' (0x%x) from client\n",
*bp, (unsigned int)*bp);
return self;
}
/* This also causes problems, because the runtime also encodes this as "*",
a string! */
- getUCharPtr: (unsigned char *)ucp
{
printf(">> got unsignec char pointer '%c' (0x%x) from client\n",
*ucp, (unsigned int)*ucp);
return self;
}
- (id) echoObject: (id)obj
{
static BOOL debugging = YES;
if (debugging)
{
[BinaryCStream setDebugging:NO];
}
return obj;
}
- (void) outputStats:obj
{
id c = [obj connectionForProxy];
id o = [c statistics];
id a = [o allKeys];
int j;
printf("Number of connections - %d\n", [[NSConnection allConnections] count]);
printf("This connection -\n");
for (j = 0; j < [a count]; j++)
{
id k = [a objectAtIndex:j];
id v = [o objectForKey:k];
printf("%s - %s\n", [k cString], [[v description] cString]);
}
}
/* This isn't working yet */
- (foo*) sendStructPtr: (foo*)f
{
printf(">>reference: i=%d s=%s l=%lu\n",
f->i, f->s, f->l);
f->i = 88;
return f;
}
- sendStruct: (foo)f
{
printf(">>value: i=%d s=%s l=%lu\n",
f.i, f.s, f.l);
f.i = 88;
return self;
}
- sendSmallStruct: (small_struct)small
{
printf(">>small value struct: z=%d\n", small.z);
return self;
}
- (foo) returnStruct
{
foo f = {1, "horse", 987654};
return f;
}
- (small_struct) returnSmallStruct
{
small_struct f = {22};
return f;
}
- (foo) returnSetStruct: (int)x
{
foo f = {1, "horse", 987654};
f.l = x;
return f;
}
- (small_struct) returnSetSmallStruct: (int)x
{
small_struct f = {22};
f.z = x;
return f;
}
/* Doesn't work because GCC generates the wrong encoding: "@0@+8:+12^i+16" */
- sendArray: (int[3])a
{
printf(">> array %d %d %d\n", a[0], a[1], a[2]);
return self;
}
- sendStructArray: (struct myarray)ma
{
printf(">>struct array %d %d %d\n", ma.a[0], ma.a[1], ma.a[2]);
return self;
}
- sendDouble: (double)d andFloat: (float)f
{
printf(">> double %f, float %f\n", d, f);
return self;
}
- (double*) doDoublePointer: (double*)d
{
printf(">> got double %f from client\n", *d);
*d = 1.234567;
printf(">> returning double %f to client\n", *d);
return d;
}
- sendCharPtrPtr: (char**)sp
{
printf(">> got char**, string %s\n", *sp);
return self;
}
- sendBycopy: (bycopy id)o
{
printf(">> bycopy class is %s\n", object_get_class_name (o));
return self;
}
#ifdef _F_BYREF
- sendByref: (byref id)o
{
printf(">> byref class is %s\n", object_get_class_name (o));
return self;
}
#endif
- manyArgs: (int)i1 : (int)i2 : (int)i3 : (int)i4 : (int)i5 : (int)i6
: (int)i7 : (int)i8 : (int)i9 : (int)i10 : (int)i11 : (int)i12
{
printf(">> manyArgs: %d %d %d %d %d %d %d %d %d %d %d %d\n",
i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12);
return self;
}
- (float) returnFloat
{
static float f = 2.3456789f;
return f;
}
- (double) returnDouble
{
/* static <This is crashing gcc ss-940902 config'ed for irix5.1,
but running on irix5.2> */
double d = 4.567891234;
return d;
}
- connectionBecameInvalid: notification
{
id anObj = [notification object];
if ([anObj isKindOf:[NSConnection class]])
{
int i, count = [the_array count];
for (i = count-1; i >= 0; i--)
{
id o = [the_array objectAtIndex: i];
if ([o isProxy] && [o connectionForProxy] == anObj)
[the_array removeObjectAtIndex: i];
}
if (count != [the_array count])
printf("$$$$$ connectionBecameInvalid: removed from the_array\n");
}
else
{
[self error:"non Connection is invalid"];
}
return self;
}
- (NSConnection*) connection: ancestor didConnect: newConn
{
printf("%s\n", sel_get_name(_cmd));
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(connectionBecameInvalid:)
name: NSConnectionDidDieNotification
object: newConn];
[newConn setDelegate: self];
return newConn;
}
@end
int main(int argc, char *argv[])
{
id l = [[Server alloc] init];
id o = [[NSObject alloc] init];
double d;
NSConnection *c;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
[BinaryCStream setDebugging:YES];
#if NeXT_runtime
[NSDistantObject setProtocolForProxies:@protocol(AllProxies)];
#endif
c = [NSConnection defaultConnection];
[c setRootObject: l];
if (argc > 1)
[c registerName: [NSString stringWithCString: argv[1]]];
else
[c registerName: @"test2server"];
[[NSNotificationCenter defaultCenter]
addObserver: l
selector: @selector(connectionBecameInvalid:)
name: NSConnectionDidDieNotification
object: c];
[c setDelegate:l];
[l addObject: o];
d = [l returnDouble];
printf("got double %f\n", d);
printf("list's hash is 0x%x\n", (unsigned)[l hash]);
printf("object's hash is 0x%x\n", (unsigned)[o hash]);
[[NSRunLoop currentRunLoop] run];
[arp release];
exit(0);
}