1 /* Contributed by Nicola Pero on Tue Mar  6 23:05:53 CET 2001 */
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include "../../objc-obj-c++-shared/runtime.h"
6 
7 extern int strcmp(const char *, const char *);
8 
9 /*
10  * Standard Tests For Classes and Objects - abort upon failing; return
11  * normally if all is well.
12  */
13 
14 /* Test that `class' is a Class */
test_is_class(Class class)15 static void test_is_class (Class class)
16 {
17   if (class_isMetaClass (object_getClass (class)) == NO)
18     {
19       printf ("test_is_class failed\n");
20       abort ();
21     }
22 }
23 
24 /* Test that the superclass of `class' is `superclass' */
test_superclass(Class class,Class superclass)25 static void test_superclass (Class class, Class superclass)
26 {
27   if (class_getSuperclass (class) != superclass)
28     {
29       printf ("test_superclass failed\n");
30       abort ();
31     }
32 }
33 
34 /* Test that the classname of `class' is `classname' */
test_class_name(Class class,const char * classname)35 static void test_class_name (Class class, const char *classname)
36 {
37   if (strcmp (class_getName (class), classname))
38     {
39       printf ("test_class_name failed\n");
40       abort ();
41     }
42 }
43 
44 /* Test that we can allocate instances of `class' */
test_allocate(Class class)45 static void test_allocate (Class class)
46 {
47   /* The object we create is leaked but who cares, this is only a test */
48   id object = class_createInstance (class, 0);
49 
50   if (object == nil)
51     {
52       printf ("test_allocate failed\n");
53       abort ();
54     }
55 }
56 
57 /* Test that instances of `class' are instances and not classes */
test_instances(Class class)58 static void test_instances (Class class)
59 {
60   id object = class_createInstance (class, 0);
61 
62   if (class_isMetaClass (object_getClass (object)) == YES)
63     {
64       printf ("test_instances failed\n");
65       abort ();
66     }
67 }
68 
69 /* Test that we can deallocate instances of `class' */
test_deallocate(Class class)70 static void test_deallocate (Class class)
71 {
72   id object = class_createInstance (class, 0);
73 
74   object_dispose (object);
75 }
76 
77 /* Test that the object and the class agree on what the class is */
test_object_class(Class class)78 static void test_object_class (Class class)
79 {
80   id object = class_createInstance (class, 0);
81 
82   if (object_getClass (object) != class)
83     {
84       printf ("test_object_class failed\n");
85       abort ();
86     }
87 }
88 
89 /*
90  *  Runs all the tests in this file for the specified class
91  */
test_class_with_superclass(const char * class_name,const char * superclass_name)92 void test_class_with_superclass (const char *class_name,
93 				 const char *superclass_name)
94 {
95   Class class;
96   Class superclass;
97 
98   /* class_name must be an existing class */
99   class = objc_getClass (class_name);
100   test_is_class (class);
101 
102   /* But superclass_name can be "", which means `Nil' */
103   superclass = objc_getClass (superclass_name);
104   if (superclass != Nil)
105     {
106       test_is_class (superclass);
107     }
108 
109   /* Now the tests */
110   test_superclass (class, superclass);
111   test_class_name (class, class_name);
112   test_allocate (class);
113   test_instances (class);
114   test_deallocate (class);
115   test_object_class (class);
116 }
117