From-SVN: r40397
This commit is contained in:
Ovidiu Predescu 2001-03-12 06:24:50 +00:00
parent d5ae21aace
commit 5b57aeabb6
41 changed files with 1961 additions and 0 deletions

View File

@ -0,0 +1,39 @@
/* Contributed by Nicola Pero - Fri Mar 9 19:39:15 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Test getting and calling the IMP of a method */
@interface TestClass
{
Class isa;
}
- (int) next: (int)a;
@end
@implementation TestClass
- (int) next: (int)a
{
return a + 1;
}
@end
int main (void)
{
Class class;
SEL selector;
int (* imp) (id, SEL, int);
class = objc_get_class ("TestClass");
selector = @selector (next:);
imp = (int (*)(id, SEL, int))method_get_imp
(class_get_class_method (class, selector));
if (imp (class, selector, 5) != 6)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,30 @@
/* Contributed by Nicola Pero - Fri Mar 9 19:39:15 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Test the hidden argument _cmd to method calls */
@interface TestClass
{
Class isa;
}
+ (const char*) method;
@end
@implementation TestClass
+ (const char*) method;
{
return sel_get_name (_cmd);
}
@end
int main (void)
{
if (strcmp ([TestClass method], "method"))
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,55 @@
/* Contributed by Nicola Pero - Thu Mar 8 16:27:46 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
#include <objc/Object.h>
/* Test that by using -> we can access ivars of other objects of the same
class */
@interface TestClass : Object
{
int value;
}
- (int) value;
- (int) setValue: (int)number;
- (void) takeValueFrom: (TestClass *)object;
@end
@implementation TestClass : Object
{
int value;
}
- (int) value
{
return value;
}
- (int) setValue: (int)number
{
value = number;
}
- (void) takeValueFrom: (TestClass *)object
{
value = object->value;
}
@end
int main (void)
{
TestClass *a;
TestClass *b;
a = [TestClass new];
[a setValue: 10];
b = [TestClass new];
[b setValue: -10];
[b takeValueFrom: a];
if ([b value] != [a value])
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,23 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a RootClass */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
#include "class-tests-1.h"
int main (void)
{
test_class_with_superclass ("RootClass", "");
return 0;
}

View File

@ -0,0 +1,77 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with an ivar and
accessor methods and a subclass overriding the superclass'
implementation, and using self to call another method of itself */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
@interface SubClass : RootClass
{
int state;
}
- (void) setState: (int)number;
- (int) state;
@end
@implementation SubClass
- (void) setState: (int)number
{
state = number;
}
- (int) state
{
return state;
}
@end
@interface SubSubClass : SubClass
- (int) shift;
@end
@implementation SubSubClass
- (int) state
{
return state + [self shift];
}
- (int) shift
{
return 1;
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD SubClass *
#include "class-tests-2.h"
int main (void)
{
SubClass *object;
SubSubClass *sub_object;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_instance_method ("SubClass", @selector (setState:));
test_that_class_has_instance_method ("SubClass", @selector (state));
test_class_with_superclass ("SubSubClass", "SubClass");
test_that_class_has_instance_method ("SubSubClass", @selector (setState:));
test_that_class_has_instance_method ("SubSubClass", @selector (state));
test_that_class_has_instance_method ("SubSubClass", @selector (shift));
object = class_create_instance (objc_lookup_class ("SubClass"));
test_accessor_method (object, 0, -1, -1, 1, 1);
sub_object = class_create_instance (objc_lookup_class ("SubSubClass"));
test_accessor_method (sub_object, 1, -1, 0, 1, 2);
return 0;
}

View File

@ -0,0 +1,81 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with an ivar and
accessor methods and a subclass overriding the superclass'
implementation and using self to call another method of itself - in
a category */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
@interface SubClass : RootClass
{
int state;
}
- (void) setState: (int)number;
- (int) state;
@end
@implementation SubClass
- (void) setState: (int)number
{
state = number;
}
- (int) state
{
return state;
}
@end
@interface SubSubClass : SubClass
- (int) shift;
@end
@implementation SubSubClass
- (int) shift
{
return 1;
}
@end
@implementation SubSubClass (Additions)
- (int) state
{
return state + [self shift];
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD SubClass *
#include "class-tests-2.h"
int main (void)
{
SubClass *object;
SubSubClass *sub_object;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_instance_method ("SubClass", @selector (setState:));
test_that_class_has_instance_method ("SubClass", @selector (state));
test_class_with_superclass ("SubSubClass", "SubClass");
test_that_class_has_instance_method ("SubSubClass", @selector (setState:));
test_that_class_has_instance_method ("SubSubClass", @selector (state));
test_that_class_has_instance_method ("SubSubClass", @selector (shift));
object = class_create_instance (objc_lookup_class ("SubClass"));
test_accessor_method (object, 0, -1, -1, 1, 1);
sub_object = class_create_instance (objc_lookup_class ("SubSubClass"));
test_accessor_method (sub_object, 1, -1, 0, 1, 2);
return 0;
}

View File

@ -0,0 +1,50 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with a class methods */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
static int class_variable = 0;
@interface SubClass : RootClass
+ (void) setState: (int)number;
+ (int) state;
@end
@implementation SubClass
+ (void) setState: (int)number
{
class_variable = number;
}
+ (int) state
{
return class_variable;
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD Class
#include "class-tests-2.h"
int main (void)
{
Class class;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_class_method ("SubClass", @selector (setState:));
test_that_class_has_class_method ("SubClass", @selector (state));
class = objc_lookup_class ("SubClass");
test_accessor_method (class, 0, -1, -1, 1, 1);
return 0;
}

View File

@ -0,0 +1,71 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with a class accessor
methods and a subclass overriding the superclass' implementation
but reusing it with super */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
static int class_variable = 0;
@interface SubClass : RootClass
+ (void) setState: (int)number;
+ (int) state;
@end
@implementation SubClass
+ (void) setState: (int)number
{
class_variable = number;
}
+ (int) state
{
return class_variable;
}
@end
@interface SubSubClass : SubClass
@end
@implementation SubSubClass
+ (int) state
{
return [super state] + 1;
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD Class
#include "class-tests-2.h"
int main (void)
{
Class class;
Class sub_class;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_class_method ("SubClass", @selector (setState:));
test_that_class_has_class_method ("SubClass", @selector (state));
test_class_with_superclass ("SubSubClass", "SubClass");
test_that_class_has_class_method ("SubSubClass", @selector (setState:));
test_that_class_has_class_method ("SubSubClass", @selector (state));
class = objc_lookup_class ("SubClass");
test_accessor_method (class, 0, -1, -1, 1, 1);
sub_class = objc_lookup_class ("SubSubClass");
class_variable = 0;
test_accessor_method (sub_class, 1, -1, 0, 1, 2);
return 0;
}

View File

@ -0,0 +1,76 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with a class accessor
methods and a subclass overriding the superclass' implementation,
and using self to call another method of itself */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
static int class_variable = 0;
@interface SubClass : RootClass
+ (void) setState: (int)number;
+ (int) state;
@end
@implementation SubClass
+ (void) setState: (int)number
{
class_variable = number;
}
+ (int) state
{
return class_variable;
}
@end
@interface SubSubClass : SubClass
+ (int) shift;
@end
@implementation SubSubClass
+ (int) state
{
return class_variable + [self shift];
}
+ (int) shift
{
return 1;
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD Class
#include "class-tests-2.h"
int main (void)
{
Class class, sub_class;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_class_method ("SubClass", @selector (setState:));
test_that_class_has_class_method ("SubClass", @selector (state));
test_class_with_superclass ("SubSubClass", "SubClass");
test_that_class_has_class_method ("SubSubClass", @selector (setState:));
test_that_class_has_class_method ("SubSubClass", @selector (state));
test_that_class_has_class_method ("SubSubClass", @selector (shift));
class = objc_lookup_class ("SubClass");
test_accessor_method (class, 0, -1, -1, 1, 1);
sub_class = objc_lookup_class ("SubSubClass");
class_variable = 0;
test_accessor_method (sub_class, 1, -1, 0, 1, 2);
return 0;
}

View File

@ -0,0 +1,29 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
@interface SubClass : RootClass
@end
@implementation SubClass
@end
#include "class-tests-1.h"
int main (void)
{
test_class_with_superclass ("SubClass", "RootClass");
return 0;
}

View File

@ -0,0 +1,43 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a minimal subclass tree */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
@interface SubClassA : RootClass
@end
@implementation SubClassA
@end
@interface SubClassB : RootClass
@end
@implementation SubClassB
@end
@interface SubSubClass : SubClassA
@end
@implementation SubSubClass
@end
#include "class-tests-1.h"
int main (void)
{
test_class_with_superclass ("SubClassA", "RootClass");
test_class_with_superclass ("SubClassB", "RootClass");
test_class_with_superclass ("SubSubClass", "SubClassA");
return 0;
}

View File

@ -0,0 +1,52 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with an ivar and
accessor methods */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
@interface SubClass : RootClass
{
int state;
}
- (void) setState: (int)number;
- (int) state;
@end
@implementation SubClass
- (void) setState: (int)number
{
state = number;
}
- (int) state
{
return state;
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD SubClass *
#include "class-tests-2.h"
int main (void)
{
SubClass *object;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_instance_method ("SubClass", @selector (setState:));
test_that_class_has_instance_method ("SubClass", @selector (state));
object = class_create_instance (objc_lookup_class ("SubClass"));
test_accessor_method (object, 0, 1, 1, -3, -3);
return 0;
}

View File

@ -0,0 +1,71 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with an ivar and
accessor methods and a subclass overriding the superclass'
implementation */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
@interface SubClass : RootClass
{
int state;
}
- (void) setState: (int)number;
- (int) state;
@end
@implementation SubClass
- (void) setState: (int)number
{
state = number;
}
- (int) state
{
return state;
}
@end
@interface SubSubClass : SubClass
@end
@implementation SubSubClass
- (int) state
{
return state + 1;
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD SubClass *
#include "class-tests-2.h"
int main (void)
{
SubClass *object;
SubSubClass *sub_object;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_instance_method ("SubClass", @selector (setState:));
test_that_class_has_instance_method ("SubClass", @selector (state));
test_class_with_superclass ("SubSubClass", "SubClass");
test_that_class_has_instance_method ("SubSubClass", @selector (setState:));
test_that_class_has_instance_method ("SubSubClass", @selector (state));
object = class_create_instance (objc_lookup_class ("SubClass"));
test_accessor_method (object, 0, -1, -1, 1, 1);
sub_object = class_create_instance (objc_lookup_class ("SubSubClass"));
test_accessor_method (sub_object, 1, -1, 0, 1, 2);
return 0;
}

View File

@ -0,0 +1,71 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with an ivar and
accessor methods and a subclass overriding the superclass'
implementation but reusing it with super */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
@interface SubClass : RootClass
{
int state;
}
- (void) setState: (int)number;
- (int) state;
@end
@implementation SubClass
- (void) setState: (int)number
{
state = number;
}
- (int) state
{
return state;
}
@end
@interface SubSubClass : SubClass
@end
@implementation SubSubClass
- (int) state
{
return [super state] + 1;
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD SubClass *
#include "class-tests-2.h"
int main (void)
{
SubClass *object;
SubSubClass *sub_object;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_instance_method ("SubClass", @selector (setState:));
test_that_class_has_instance_method ("SubClass", @selector (state));
test_class_with_superclass ("SubSubClass", "SubClass");
test_that_class_has_instance_method ("SubSubClass", @selector (setState:));
test_that_class_has_instance_method ("SubSubClass", @selector (state));
object = class_create_instance (objc_lookup_class ("SubClass"));
test_accessor_method (object, 0, -1, -1, 1, 1);
sub_object = class_create_instance (objc_lookup_class ("SubSubClass"));
test_accessor_method (sub_object, 1, -1, 0, 1, 2);
return 0;
}

View File

@ -0,0 +1,59 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with an ivar and
accessor methods; accessor methods implemented in a separate
category */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
@interface SubClass : RootClass
{
int state;
}
@end
@implementation SubClass
@end
@interface SubClass (Additions)
- (void) setState: (int)number;
- (int) state;
@end
@implementation SubClass (Additions)
- (void) setState: (int)number
{
state = number;
}
- (int) state
{
return state;
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD SubClass *
#include "class-tests-2.h"
int main (void)
{
SubClass *object;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_instance_method ("SubClass", @selector (setState:));
test_that_class_has_instance_method ("SubClass", @selector (state));
object = class_create_instance (objc_lookup_class ("SubClass"));
test_accessor_method (object, 0, 1, 1, -3, -3);
return 0;
}

View File

@ -0,0 +1,74 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with an ivar and
accessor methods and a subclass overriding the superclass'
implementation - in a category */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
@interface SubClass : RootClass
{
int state;
}
- (void) setState: (int)number;
- (int) state;
@end
@implementation SubClass
- (void) setState: (int)number
{
state = number;
}
- (int) state
{
return state;
}
@end
@interface SubSubClass : SubClass
@end
@implementation SubSubClass
@end
@implementation SubSubClass (Additions)
- (int) state
{
return state + 1;
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD SubClass *
#include "class-tests-2.h"
int main (void)
{
SubClass *object;
SubSubClass *sub_object;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_instance_method ("SubClass", @selector (setState:));
test_that_class_has_instance_method ("SubClass", @selector (state));
test_class_with_superclass ("SubSubClass", "SubClass");
test_that_class_has_instance_method ("SubSubClass", @selector (setState:));
test_that_class_has_instance_method ("SubSubClass", @selector (state));
object = class_create_instance (objc_lookup_class ("SubClass"));
test_accessor_method (object, 0, -1, -1, 1, 1);
sub_object = class_create_instance (objc_lookup_class ("SubSubClass"));
test_accessor_method (sub_object, 1, -1, 0, 1, 2);
return 0;
}

View File

@ -0,0 +1,74 @@
/* Contributed by Nicola Pero - Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Tests creating a root class and a subclass with an ivar and
accessor methods and a subclass overriding the superclass'
implementation but reusing it with super - in a category */
@interface RootClass
{
Class isa;
}
@end
@implementation RootClass
@end
@interface SubClass : RootClass
{
int state;
}
- (void) setState: (int)number;
- (int) state;
@end
@implementation SubClass
- (void) setState: (int)number
{
state = number;
}
- (int) state
{
return state;
}
@end
@interface SubSubClass : SubClass
@end
@implementation SubSubClass
@end
@implementation SubSubClass (Additions)
- (int) state
{
return [super state] + 1;
}
@end
#include "class-tests-1.h"
#define TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD SubClass *
#include "class-tests-2.h"
int main (void)
{
SubClass *object;
SubSubClass *sub_object;
test_class_with_superclass ("SubClass", "RootClass");
test_that_class_has_instance_method ("SubClass", @selector (setState:));
test_that_class_has_instance_method ("SubClass", @selector (state));
test_class_with_superclass ("SubSubClass", "SubClass");
test_that_class_has_instance_method ("SubSubClass", @selector (setState:));
test_that_class_has_instance_method ("SubSubClass", @selector (state));
object = class_create_instance (objc_lookup_class ("SubClass"));
test_accessor_method (object, 0, -1, -1, 1, 1);
sub_object = class_create_instance (objc_lookup_class ("SubSubClass"));
test_accessor_method (sub_object, 1, -1, 0, 1, 2);
return 0;
}

View File

@ -0,0 +1,137 @@
/* Contributed by Nicola Pero on Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
#include <objc/Object.h>
#include <stdlib.h>
/*
* Standard Tests For Classes and Objects - abort upon failing; return
* normally if all is well.
*/
/* Test that `class' is a Class */
static void test_is_class (Class class)
{
if (object_is_class (class) == NO)
{
printf ("test_is_class failed\n");
abort ();
}
if (class_is_class (class) == NO)
{
printf ("test_is_class failed\n");
abort ();
}
}
/* Test that the superclass of `class' is `superclass' */
static void test_superclass (Class class, Class superclass)
{
if (class_get_super_class (class) != superclass)
{
printf ("test_superclass failed\n");
abort ();
}
}
/* Test that the classname of `class' is `classname' */
static void test_class_name (Class class, const char *classname)
{
if (strcmp (class_get_class_name (class), classname))
{
printf ("test_class_name failed\n");
abort ();
}
}
/* Test that we can allocate instances of `class' */
static void test_allocate (Class class)
{
/* The object we create is leaked but who cares, this is only a test */
id object = class_create_instance (class);
if (object == nil)
{
printf ("test_allocate failed\n");
abort ();
}
}
/* Test that instances of `class' are instances and not classes */
static void test_instances (Class class)
{
id object = class_create_instance (class);
if (object_is_class (object) == YES)
{
printf ("test_instances failed\n");
abort ();
}
}
/* Test that we can deallocate instances of `class' */
static void test_deallocate (Class class)
{
id object = class_create_instance (class);
object_dispose (object);
}
/* Test that the object and the class agree on what the class is */
static void test_object_class (Class class)
{
id object = class_create_instance (class);
if (object_get_class (object) != class)
{
printf ("test_object_class failed\n");
abort ();
}
}
/* Test that the object and the class agree on what the superclass is */
static void test_object_super_class (Class class)
{
id object = class_create_instance (class);
if (object_get_super_class (object) != class_get_super_class (class))
{
printf ("test_object_super_class failed\n");
abort ();
}
}
/*
* Runs all the tests in this file for the specified class
*/
void test_class_with_superclass (const char *class_name,
const char *superclass_name)
{
Class class;
Class superclass;
/* We need at least a method call before playing with the internals,
so that the runtime will call __objc_resolve_class_links () */
[Object initialize];
/* class_name must be an existing class */
class = objc_lookup_class (class_name);
test_is_class (class);
/* But superclass_name can be "", which means `Nil' */
superclass = objc_lookup_class (superclass_name);
if (superclass != Nil)
{
test_is_class (superclass);
}
/* Now the tests */
test_superclass (class, superclass);
test_class_name (class, class_name);
test_allocate (class);
test_instances (class);
test_deallocate (class);
test_object_class (class);
test_object_super_class (class);
}

View File

@ -0,0 +1,67 @@
/* Contributed by Nicola Pero on Tue Mar 6 23:05:53 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
#include <stdlib.h>
/*
* Standard Tests For Methods of Classes and Objects - abort upon
* failing; return normally if all is well.
*/
/* Test that `class' has an instance method for the selector `selector' */
void test_that_class_has_instance_method (const char *class_name,
SEL selector)
{
Class class = objc_lookup_class (class_name);
if (class_get_instance_method (class, selector) == NULL)
{
printf ("test_class_has_instance_method failed\n");
abort ();
}
}
/* Test that `class' has a class method for the selector `selector' */
void test_that_class_has_class_method (const char *class_name,
SEL selector)
{
Class meta_class = objc_get_meta_class (class_name);
if (class_get_class_method (meta_class, selector) == NULL)
{
printf ("test_class_has_class_method failed\n");
abort ();
}
}
/* Test the accessor methods (called -state and -setState:) on the
object `object'. */
#ifdef TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD
void test_accessor_method (TYPE_OF_OBJECT_WITH_ACCESSOR_METHOD object,
int initial_state,
int new_state_0, int expected_result_0,
int new_state_1, int expected_result_1)
{
if ([object state] != initial_state)
{
printf ("test_accessor_method (initial state) failed\n");
abort ();
}
[object setState: new_state_0];
if ([object state] != expected_result_0)
{
printf ("test_accessor_method (new_state_0) failed\n");
abort ();
}
[object setState: new_state_1];
if ([object state] != expected_result_1)
{
printf ("test_accessor_method (new_state_1) failed\n");
abort ();
}
}
#endif CLASS_WITH_ACCESSOR_METHOD

View File

@ -0,0 +1,12 @@
/* Contributed by Nicola Pero - Thu Mar 8 17:23:59 CET 2001 */
#include <objc/objc.h>
#include <objc/Object.h>
@compatibility_alias MyObject Object;
int main (void)
{
MyObject *object = [MyObject alloc];
return 0;
}

View File

@ -0,0 +1,31 @@
/* Contributed by Nicola Pero - Thu Mar 8 16:27:46 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
#include <objc/Object.h>
/* Test very simple @encode */
int main (void)
{
if (strcmp ("i", @encode (int)))
{
abort ();
}
if (strcmp ("@", @encode (id)))
{
abort ();
}
if (strcmp ("@", @encode (Object *)))
{
abort ();
}
if (strcmp (":", @encode (SEL)))
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,44 @@
/* Contributed by Nicola Pero - Fri Mar 9 21:35:47 CET 2001 */
#include <objc/objc.h>
#include <objc/Object.h>
/* Tests defining a protocol and a class adopting it */
@protocol Enabling
- (BOOL) isEnabled;
- (void) setEnabled: (BOOL)flag;
@end
@interface Feature : Object <Enabling>
{
const char *name;
BOOL isEnabled;
}
@end
@implementation Feature
- (BOOL) isEnabled
{
return isEnabled;
}
- (void) setEnabled: (BOOL)flag
{
isEnabled = flag;
}
@end
int main (void)
{
Feature *object;
object = [Feature new];
[object setEnabled: YES];
if (![object isEnabled])
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,45 @@
/* Contributed by Nicola Pero - Fri Mar 9 21:35:47 CET 2001 */
#include <objc/objc.h>
#include <objc/Object.h>
/* Test defining a protocol, a class adopting it, and using an object
of type `id <protocol>'. */
@protocol Enabling
- (BOOL) isEnabled;
- (void) setEnabled: (BOOL)flag;
@end
@interface Feature : Object <Enabling>
{
const char *name;
BOOL isEnabled;
}
@end
@implementation Feature
- (BOOL) isEnabled
{
return isEnabled;
}
- (void) setEnabled: (BOOL)flag
{
isEnabled = flag;
}
@end
int main (void)
{
id <Enabling> object;
object = [Feature new];
[object setEnabled: YES];
if (![object isEnabled])
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,58 @@
/* Contributed by Nicola Pero - Fri Mar 9 21:35:47 CET 2001 */
#include <objc/objc.h>
#include <objc/Object.h>
/* Test defining two protocol, a class adopting both of them,
and using an object of type `id <Protocol1, Protocol2>' */
@protocol Enabling
- (BOOL) isEnabled;
- (void) setEnabled: (BOOL)flag;
@end
@protocol Evaluating
- (int) importance;
@end
@interface Feature : Object <Enabling, Evaluating>
{
const char *name;
BOOL isEnabled;
}
@end
@implementation Feature
- (BOOL) isEnabled
{
return isEnabled;
}
- (void) setEnabled: (BOOL)flag
{
isEnabled = flag;
}
- (int) importance
{
return 1000;
}
@end
int main (void)
{
id <Enabling, Evaluating> object;
object = [Feature new];
[object setEnabled: YES];
if (![object isEnabled])
{
abort ();
}
if ([object importance] != 1000)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,40 @@
/* Contributed by Nicola Pero - Fri Mar 9 21:35:47 CET 2001 */
#include <objc/objc.h>
#include <objc/Object.h>
/* Test defining a protocol, a class adopting it in a category */
@protocol Evaluating
- (int) importance;
@end
@interface Feature : Object
@end
@implementation Feature
@end
@interface Feature (EvaluatingProtocol) <Evaluating>
@end
@implementation Feature (EvaluatingProtocol)
- (int) importance
{
return 1000;
}
@end
int main (void)
{
id <Evaluating> object;
object = [Feature new];
if ([object importance] != 1000)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,33 @@
/* Contributed by Nicola Pero - Fri Mar 9 21:35:47 CET 2001 */
#include <objc/objc.h>
#include <objc/Protocol.h>
/* Test defining a protocol, and accessing it using @protocol */
@protocol Evaluating
- (int) importance;
@end
/* A class adopting the protocol */
@interface Test <Evaluating>
@end
@implementation Test
- (int) importance
{
return 1000;
}
@end
int main (void)
{
Protocol *protocol = @protocol (Evaluating);
if (strcmp ([protocol name], "Evaluating"))
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,25 @@
/* Contributed by Nicola Pero - Fri Mar 9 21:35:47 CET 2001 */
#include <objc/objc.h>
#include <objc/Protocol.h>
/* Test defining a protocol, and accessing it using @protocol */
@protocol Evaluating
- (int) importance;
@end
/* Without a class adopting the protocol - this doesn't work
with gcc-2.95.2 as well */
int main (void)
{
Protocol *protocol = @protocol (Evaluating);
if (strcmp ([protocol name], "Evaluating"))
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,44 @@
/* Contributed by Nicola Pero - Fri Mar 9 21:35:47 CET 2001 */
#include <objc/objc.h>
#include <objc/Object.h>
#include <objc/Protocol.h>
/* Test defining two protocols, one incorporating the other one. */
@protocol Configuring
- (void) configure;
@end
@protocol Processing <Configuring>
- (void) process;
@end
/* A class adopting the protocol */
@interface Test : Object <Processing>
{
BOOL didConfigure;
BOOL didProcess;
}
@end
@implementation Test
- (void) configure
{
didConfigure = YES;
}
- (void) process
{
didProcess = YES;
}
@end
int main (void)
{
id <Processing> object = [Test new];
[object configure];
[object process];
return 0;
}

View File

@ -0,0 +1,13 @@
/* Contributed by Nicola Pero - Fri Mar 9 21:35:47 CET 2001 */
#include <objc/objc.h>
#include <objc/Object.h>
@interface Object (StopProtocol)
- (void) stop;
@end
int main (void)
{
return 0;
}

View File

@ -0,0 +1,36 @@
/* Contributed by Nicola Pero - Wed Mar 7 17:55:04 CET 2001 */
#include <objc/objc.h>
/* Test that +initialize is automatically called before the class is
accessed */
static int class_variable = 0;
@interface TestClass
{
Class isa;
}
+ (void) initialize;
+ (int) classVariable;
@end
@implementation TestClass
+ (void) initialize
{
class_variable = 1;
}
+ (int) classVariable
{
return class_variable;
}
@end
int main (void)
{
if ([TestClass classVariable] != 1)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,30 @@
/* Contributed by Nicola Pero - Wed Mar 7 17:55:04 CET 2001 */
#include <objc/objc.h>
/* Test that +load is automatically called before main is run */
static int static_variable = 0;
@interface TestClass
{
Class isa;
}
+ (void) load;
@end
@implementation TestClass
+ (void) load
{
static_variable = 1;
}
@end
int main (void)
{
if (static_variable != 1)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,56 @@
/* Contributed by Nicola Pero - Fri Mar 9 19:39:15 CET 2001 */
#include <objc/objc.h>
/* Test the syntax of methods with many arguments */
@interface TestClass
{
Class isa;
}
+ (int) sumInteger: (int)a withInteger: (int)b;
+ (int) sum: (int)a : (int)b;
+ (int) sumInteger: (int)a withInteger: (int)b withInteger: (int)c;
+ (int) sum: (int)a : (int)b : (int)c;
@end
@implementation TestClass
+ (int) sumInteger: (int)a withInteger: (int)b
{
return a + b;
}
+ (int) sum: (int)a : (int)b
{
return [self sumInteger: a withInteger: b];
}
+ (int) sumInteger: (int)a withInteger: (int)b withInteger: (int)c
{
return a + b + c;
}
+ (int) sum: (int)a : (int)b : (int)c
{
return [self sumInteger: a withInteger: b withInteger: c];
}
@end
int main (void)
{
if ([TestClass sumInteger: 1 withInteger: 1] != 2)
{
abort ();
}
if ([TestClass sum: 1 : 1] != 2)
{
abort ();
}
if ([TestClass sumInteger: 1 withInteger: 1 withInteger: 1] != 3)
{
abort ();
}
if ([TestClass sum: 1 : 1 : 1] != 3)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,37 @@
/* Contributed by Nicola Pero - Fri Mar 9 19:39:15 CET 2001 */
#include <objc/objc.h>
/* Test defining a nested function inside a method */
@interface Test
{
Class isa;
}
+ (int) test;
@end
@implementation Test
+ (int) test
{
int test (void)
{
return 1;
}
return test ();
}
@end
int main (void)
{
if ([Test test] != 1)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,41 @@
/* Contributed by Nicola Pero - Fri Mar 9 19:39:15 CET 2001 */
#include <objc/objc.h>
#include <objc/Object.h>
/* Test that using the same name for different things makes no
problem */
@interface TestClass : Object
{
int test;
}
+ (int) test;
- (int) test;
@end
@implementation TestClass
+ (int) test
{
return 1;
}
- (int) test
{
/* 0 + 2 as `test' is implicitly initialized to zero */
return test + 2;
}
@end
int main (void)
{
if ([TestClass test] != 1)
{
abort ();
}
if ([[[TestClass alloc] init] test] != 2)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,32 @@
/* Contributed by Nicola Pero - Fri Mar 9 19:39:15 CET 2001 */
#include <objc/objc.h>
#include <objc/Object.h>
/* Test the @private, @protected, @public keyworks for ivars. We only
check syntax. */
@interface TestClass : Object
{
int a;
@private
int ivarOne, ivarTwo;
id ivarThree;
@protected
int ivarFour;
@public
id ivarFive;
}
@end
@implementation TestClass
@end
int main (void)
{
/* Only test compilation */
return 0;
}

View File

@ -0,0 +1,31 @@
/* Contributed by Nicola Pero - Fri Mar 9 19:39:15 CET 2001 */
#include <objc/objc.h>
/* Test redefining self */
@interface TestClass
{
Class isa;
}
+ (Class) class;
@end
@implementation TestClass
+ (Class) class
{
self = Nil;
return self;
}
@end
int main (void)
{
if ([TestClass class] != Nil)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,42 @@
/* Contributed by Nicola Pero - Thu Mar 8 16:27:46 CET 2001 */
#include <objc/objc.h>
/* Test that instance methods of root classes are available as class
methods to other classes as well */
@interface RootClass
{
Class isa;
}
- (id) self;
@end
@implementation RootClass
- (id) self
{
return self;
}
@end
@interface NormalClass : RootClass
@end
@implementation NormalClass : RootClass
@end
int main (void)
{
Class normal = objc_get_class ("NormalClass");
if (normal == Nil)
{
abort ();
}
if ([NormalClass self] != normal)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,17 @@
/* Contributed by Nicola Pero - Thu Mar 8 16:27:46 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
#include <objc/Object.h>
int main (void)
{
SEL selector;
selector = @selector (alloc);
if (strcmp (sel_get_name (selector), "alloc"))
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,34 @@
/* Contributed by Nicola Pero - Fri Mar 9 19:39:15 CET 2001 */
#include <objc/objc.h>
/* Test defining a static variable *inside* a class implementation */
@interface Test
{
Class isa;
}
+ (int) test;
@end
@implementation Test
static int test = 1;
+ (int) test
{
return test;
}
@end
int main (void)
{
if ([Test test] != 1)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,37 @@
/* Contributed by Nicola Pero - Fri Mar 9 19:39:15 CET 2001 */
#include <objc/objc.h>
/* Test defining a static function *inside* a class implementation */
@interface Test
{
Class isa;
}
+ (int) test;
@end
@implementation Test
static int test (void)
{
return 1;
}
+ (int) test
{
return test ();
}
@end
int main (void)
{
if ([Test test] != 1)
{
abort ();
}
return 0;
}

View File

@ -0,0 +1,44 @@
/* Contributed by Nicola Pero - Thu Mar 8 16:27:46 CET 2001 */
#include <objc/objc.h>
#include <objc/objc-api.h>
/* Test method with variable number of arguments */
@interface MathClass
{
Class isa;
}
/* sum positive numbers; -1 ends the list */
+ (int) sum: (int)firstNumber, ...;
@end
@implementation MathClass
+ (int) sum: (int)firstNumber, ...
{
va_list ap;
int sum = 0, number = 0;
va_start (ap, firstNumber);
number = firstNumber;
while (number >= 0)
{
sum += number;
number = va_arg (ap, int);
}
va_end (ap);
return sum;
}
@end
int main (void)
{
if ([MathClass sum: 1, 2, 3, 4, 5, -1] != 15)
{
abort ();
}
return 0;
}