1 /* Contributed by Nicola Pero on Tue Mar 6 23:05:53 CET 2001 */
2 #include <objc/objc.h>
3 #include <objc/objc-api.h>
4 #include <objc/Object.h>
5 #include <stdlib.h>
6
7 /*
8 * Standard Tests For Classes and Objects - abort upon failing; return
9 * normally if all is well.
10 */
11
12 /* Test that `class' is a Class */
test_is_class(Class class)13 static void test_is_class (Class class)
14 {
15 if (object_is_class (class) == NO)
16 {
17 printf ("test_is_class failed\n");
18 abort ();
19 }
20
21 if (class_is_class (class) == NO)
22 {
23 printf ("test_is_class failed\n");
24 abort ();
25 }
26 }
27
28 /* Test that the superclass of `class' is `superclass' */
test_superclass(Class class,Class superclass)29 static void test_superclass (Class class, Class superclass)
30 {
31 if (class_get_super_class (class) != superclass)
32 {
33 printf ("test_superclass failed\n");
34 abort ();
35 }
36 }
37
38 /* Test that the classname of `class' is `classname' */
test_class_name(Class class,const char * classname)39 static void test_class_name (Class class, const char *classname)
40 {
41 if (strcmp (class_get_class_name (class), classname))
42 {
43 printf ("test_class_name failed\n");
44 abort ();
45 }
46 }
47
48 /* Test that we can allocate instances of `class' */
test_allocate(Class class)49 static void test_allocate (Class class)
50 {
51 /* The object we create is leaked but who cares, this is only a test */
52 id object = class_create_instance (class);
53
54 if (object == nil)
55 {
56 printf ("test_allocate failed\n");
57 abort ();
58 }
59 }
60
61 /* Test that instances of `class' are instances and not classes */
test_instances(Class class)62 static void test_instances (Class class)
63 {
64 id object = class_create_instance (class);
65
66 if (object_is_class (object) == YES)
67 {
68 printf ("test_instances failed\n");
69 abort ();
70 }
71 }
72
73 /* Test that we can deallocate instances of `class' */
test_deallocate(Class class)74 static void test_deallocate (Class class)
75 {
76 id object = class_create_instance (class);
77
78 object_dispose (object);
79 }
80
81 /* Test that the object and the class agree on what the class is */
test_object_class(Class class)82 static void test_object_class (Class class)
83 {
84 id object = class_create_instance (class);
85
86 if (object_get_class (object) != class)
87 {
88 printf ("test_object_class failed\n");
89 abort ();
90 }
91 }
92
93 /* Test that the object and the class agree on what the superclass is */
test_object_super_class(Class class)94 static void test_object_super_class (Class class)
95 {
96 id object = class_create_instance (class);
97
98 if (object_get_super_class (object) != class_get_super_class (class))
99 {
100 printf ("test_object_super_class failed\n");
101 abort ();
102 }
103 }
104
105 /*
106 * Runs all the tests in this file for the specified class
107 */
test_class_with_superclass(const char * class_name,const char * superclass_name)108 void test_class_with_superclass (const char *class_name,
109 const char *superclass_name)
110 {
111 Class class;
112 Class superclass;
113
114 /* We need at least a method call before playing with the internals,
115 so that the runtime will call __objc_resolve_class_links () */
116 [Object class];
117
118 /* class_name must be an existing class */
119 class = objc_lookup_class (class_name);
120 test_is_class (class);
121
122 /* But superclass_name can be "", which means `Nil' */
123 superclass = objc_lookup_class (superclass_name);
124 if (superclass != Nil)
125 {
126 test_is_class (superclass);
127 }
128
129 /* Now the tests */
130 test_superclass (class, superclass);
131 test_class_name (class, class_name);
132 test_allocate (class);
133 test_instances (class);
134 test_deallocate (class);
135 test_object_class (class);
136 test_object_super_class (class);
137 }
138