In testsuite/:
authornicola <nicola@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 16 Oct 2010 21:27:22 +0000 (21:27 +0000)
committernicola <nicola@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 16 Oct 2010 21:27:22 +0000 (21:27 +0000)
2010-10-16  Nicola Pero  <nicola.pero@meta-innovation.com>

        * objc.dg/gnu-api-2-class.m: New.
        * objc.dg/gnu-api-2-objc.m: New.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@165564 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/testsuite/ChangeLog
gcc/testsuite/objc.dg/gnu-api-2-class.m [new file with mode: 0644]
gcc/testsuite/objc.dg/gnu-api-2-objc.m [new file with mode: 0644]

index 4a3aa42..06cc2bf 100644 (file)
@@ -1,5 +1,10 @@
 2010-10-16  Nicola Pero  <nicola.pero@meta-innovation.com>
 
+       * objc.dg/gnu-api-2-class.m: New.
+       * objc.dg/gnu-api-2-objc.m: New.
+
+2010-10-16  Nicola Pero  <nicola.pero@meta-innovation.com>
+
        * objc.dg/gnu-api-2-ivar.m: New.
 
 2010-10-15  Nicola Pero  <nicola.pero@meta-innovation.com>
diff --git a/gcc/testsuite/objc.dg/gnu-api-2-class.m b/gcc/testsuite/objc.dg/gnu-api-2-class.m
new file mode 100644 (file)
index 0000000..5e0df7e
--- /dev/null
@@ -0,0 +1,441 @@
+/* Test the Modern GNU Objective-C Runtime API.
+
+  This is test 'class', covering all functions starting with 'class'.  */
+
+/* { dg-do run } */
+/* { dg-skip-if "" { *-*-* } { "-fnext-runtime" } { "" } } */
+
+/* To get the modern GNU Objective-C Runtime API, you include
+   objc/runtime.h.  */
+#include <objc/runtime.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+@interface MyRootClass
+{ Class isa; }
++ alloc;
+- init;
+@end
+
+@implementation MyRootClass
++ alloc { return class_createInstance (self, 0); }
+- init  { return self; }
+@end
+
+@protocol MyProtocol
+- (id) variable;
+@end
+
+@protocol MySecondProtocol
+- (id) setVariable: (id)value;
+@end
+
+@interface MySubClass : MyRootClass <MyProtocol>
+{ id variable_ivar; }
+- (void) setVariable: (id)value;
+- (id) variable;
+@end
+
+@implementation MySubClass
+- (void) setVariable: (id)value { variable_ivar = value; }
+- (id) variable { return variable_ivar; }
+@end
+
+@interface DifferentClass : MyRootClass
+- (id) myClass;
+- (id) self;
+@end
+
+@implementation DifferentClass
+- (id) myClass { return object_getClass (self); }
+- (id) self { return self; }
+@end
+
+@interface MySubClass (MySelf)
+- (id) mySelf;
+@end
+
+int main(int argc, void **args)
+{
+  /* Functions are tested in alphabetical order.  */
+
+  printf ("Testing class_addIvar ()...\n");
+  {
+    Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0);
+
+    if (new_class == Nil)
+      abort ();
+    
+    if (! class_addIvar (new_class, "variable2_ivar", sizeof (id),
+                        __alignof__ (id), @encode (id)))
+      abort ();
+
+    if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char),
+                        __alignof__ (unsigned char), @encode (unsigned char)))
+      abort ();
+
+    if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),
+                        __alignof__ (unsigned long), @encode (unsigned long)))
+      abort ();
+
+    objc_registerClassPair (new_class);    
+
+    {
+      MySubClass *o = [[objc_getClass ("MySubSubClass") alloc] init];
+      Ivar variable2 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable2_ivar");
+      Ivar variable3 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable3_ivar");
+      Ivar variable4 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable4_ivar");
+
+      if (variable2 == NULL  || variable3 == NULL  ||  variable4 == NULL)
+       abort ();
+      
+      if (strcmp (ivar_getName (variable2), "variable2_ivar") != 0)
+       abort ();
+
+      if (strcmp (ivar_getName (variable3), "variable3_ivar") != 0)
+       abort ();
+
+      if (strcmp (ivar_getName (variable4), "variable4_ivar") != 0)
+       abort ();
+
+      {
+       unsigned char *var3 = (unsigned char *)((char *)o + ivar_getOffset (variable3));
+       unsigned long *var4 = (unsigned long *)((char *)o + ivar_getOffset (variable4));
+
+       object_setIvar (o, variable2, new_class);
+       *var3 = 230;
+       *var4 = 89000L;
+
+       if (object_getIvar (o, variable2) != new_class)
+         abort ();
+
+       if (*var3 != 230)
+         abort ();
+
+       if (*var4 != 89000L)
+         abort ();
+      }
+    }
+  }
+
+  printf ("Testing class_addMethod ()...\n");
+  {
+    Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass2", 0);
+    Method method1 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:));
+    Method method2 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (variable));
+
+    if (new_class == Nil)
+      abort ();
+    
+    if (! class_addIvar (new_class, "variable_ivar", sizeof (id),
+                        __alignof__ (id), @encode (id)))
+      abort ();
+
+    if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1),
+                          method_getTypeEncoding (method1)))
+      abort ();
+
+    if (! class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
+                          method_getTypeEncoding (method2)))
+      abort ();
+
+    objc_registerClassPair (new_class);    
+
+    /* Now, MySubClass2 is basically the same as MySubClass!  We'll
+       use the variable and setVariable: methods on it.  */
+    {
+      MySubClass *o = (MySubClass *)[[objc_getClass ("MySubClass2") alloc] init];
+
+      [o setVariable: o];
+
+      if ([o variable] != o)
+       abort ();
+    }
+  }
+
+  printf ("Testing class_addProtocol ()...\n");
+  {
+    if (!class_addProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
+      abort ();
+    
+    if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
+      abort ();
+
+    if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
+      abort ();
+  }
+
+  printf ("Testing class_conformsToProtocol ()...\n");
+  {
+    if (class_conformsToProtocol (objc_getClass ("MyRootClass"), @protocol (MyProtocol)))
+      abort ();
+
+    if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
+      abort ();
+  }
+
+  printf ("Testing class_copyIvarList ()...\n");
+  {
+    unsigned int count;
+    Ivar * list = class_copyIvarList (objc_getClass ("MySubClass"), &count);
+
+    if (count != 1)
+      abort ();
+
+    if (strcmp (ivar_getName (list[0]), "variable_ivar") != 0)
+      abort ();
+    
+    if (list[1] != NULL)
+      abort ();
+  }
+
+  printf ("Testing class_copyMethodList ()...\n");
+  {
+    unsigned int count;
+    Method * list = class_copyMethodList (objc_getClass ("MySubClass"), &count);
+
+    if (count != 2)
+      abort ();
+    
+    if (! ((strcmp (sel_getName (method_getName (list[0])), "variable") == 0
+           && strcmp (sel_getName (method_getName (list[1])), "setVariable:") == 0)
+          || (strcmp (sel_getName (method_getName (list[0])), "setVariable:") == 0
+              && strcmp (sel_getName (method_getName (list[1])), "variable") == 0)))
+      abort ();
+    
+    if (list[2] != NULL)
+      abort ();
+  }
+
+  /* TODO: Test new ABI (when available).  */
+  printf ("Testing class_copyPropertyList ()...\n");
+  {
+    unsigned int count;
+    Property * list = class_copyPropertyList (objc_getClass ("MySubClass"), &count);
+
+    if (count != 0  ||  list != NULL)
+      abort ();
+  }
+
+  printf ("Testing class_copyProtocolList ()...\n");
+  {
+    unsigned int count;
+    Protocol ** list = class_copyProtocolList (objc_getClass ("MySubClass"), &count);
+
+    /* Remember that we added MySecondProtocol in the test above.  */
+    if (count != 2)
+      abort ();
+
+    if (! ((strcmp (protocol_getName (list[0]), "MyProtocol") == 0
+           && strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0)
+          || (strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0
+              && strcmp (protocol_getName (list[1]), "MyProtocol") == 0)))
+      abort ();
+    
+    if (list[2] != NULL)
+      abort ();
+  }
+
+  printf ("Testing class_createInstance ()...\n");
+  {
+    MySubClass *object = [[MySubClass alloc] init];
+
+    [object setVariable: object];
+    if ([object variable] != object)
+      abort ();
+  }
+
+  printf ("Testing class_getClassMethod ()...\n");
+  {
+    Method method = class_getClassMethod (objc_getClass ("MySubClass"),
+                                         @selector(alloc));
+
+    if (method == NULL)
+      abort ();
+
+    if (strcmp (sel_getName (method_getName (method)), "alloc") != 0)
+      abort ();
+
+    if (class_getClassMethod (objc_getClass ("MySubClass"), 
+                             @selector(variable)))
+      abort ();
+  }
+
+  printf ("Testing class_getClassVariable ()...\n");
+  {
+    if (class_getClassVariable (objc_getClass ("MySubClass"), "variable_ivar"))
+      abort ();
+  }
+
+  printf ("Testing class_getInstanceMethod ()...\n");
+  {
+    Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), 
+                                            @selector(variable));
+
+    if (method == NULL)
+      abort ();
+
+    if (strcmp (sel_getName (method_getName (method)), "variable") != 0)
+      abort ();
+
+    if (class_getInstanceMethod (objc_getClass ("MySubClass"), 
+                                @selector(alloc)))
+      abort ();
+  }
+
+  printf ("Testing class_getInstanceSize ()...\n");
+  {
+    if (class_getInstanceSize (objc_getClass ("MyRootClass")) != sizeof (struct objc_object))
+      abort ();
+  }
+
+  printf ("Testing class_getInstanceVariable ()...\n");
+  {
+    Ivar variable = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
+
+    if (variable == NULL)
+      abort ();
+
+    if (strcmp (ivar_getName (variable), "variable_ivar") != 0)
+      abort ();
+
+    if (class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar_no"))
+      abort ();
+  }
+
+  printf ("Testing class_getIvarLayout ()...\n");
+  {
+    if (class_getIvarLayout (objc_getClass ("MyRootClass")) != NULL)
+      abort ();
+  }
+
+  printf ("Testing class_getMethodImplementation ()...\n");
+  {
+    MySubClass *object = [[MySubClass alloc] init];
+    IMP imp = class_getMethodImplementation (objc_getClass ("MySubClass"), 
+                                            @selector(variable));
+
+    if (imp == NULL)
+      abort ();
+
+    [object setVariable: object];
+
+    if ((*imp)(object, @selector(variable)) != object)
+      abort ();
+  }
+
+  /* This function does not exist with the GNU runtime.  */
+  /* printf ("Testing class_getMethodImplementation_stret ()...\n"); */
+
+  printf ("Testing class_getName ()...\n");
+  {
+    if (strcmp (class_getName (objc_getClass ("MyRootClass")),
+               "MyRootClass") != 0)
+      abort ();
+  }
+
+  /* TODO: Test new ABI (when available).  */
+  printf ("Testing class_getProperty ()...\n");
+  {
+    if (class_getProperty (objc_getClass ("MyRootClass"), "property") != NULL)
+      abort ();
+  }
+
+  printf ("Testing class_getSuperclass ()...\n");
+  {
+    MySubClass *object = [[MySubClass alloc] init];
+    if (class_getSuperclass (object_getClass (object)) != objc_getClass ("MyRootClass"))
+      abort ();
+  }
+
+  printf ("Testing class_getVersion ()...\n");
+  {
+    if (class_getVersion (objc_getClass ("MySubClass")) != 0)
+      abort ();
+  }
+
+   printf ("Testing class_getWeakIvarLayout ()...\n");
+  {
+    if (class_getWeakIvarLayout (objc_getClass ("MyRootClass")) != NULL)
+      abort ();
+  }
+
+  printf ("Testing class_isMetaClass ()...\n");
+  {
+    MySubClass *object = [[MySubClass alloc] init];
+    if (class_isMetaClass (object_getClass (object)) 
+       || ! class_isMetaClass (object_getClass (object_getClass (object))))
+      abort ();
+  }
+
+  printf ("Testing class_replaceMethod ()...\n");
+  {
+    Method new_method = class_getInstanceMethod (objc_getClass ("DifferentClass"),
+                                                @selector (myClass));
+    Method old_method = class_getInstanceMethod (objc_getClass ("MySubClass"),
+                                                @selector (variable));
+    const char *new_types = method_getTypeEncoding (new_method);
+    IMP new_imp = method_getImplementation (new_method);
+    const char *old_types = method_getTypeEncoding (old_method);
+    IMP old_imp = class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
+                                      method_getImplementation (new_method),
+                                      method_getTypeEncoding (new_method));
+    MySubClass *o = [[MySubClass alloc] init];
+
+    [o setVariable: o];
+
+    /* Try the new method implementation.  */
+    if ([o variable] != objc_getClass ("MySubClass"))
+      abort ();
+
+    /* Put the original method back.  */
+    class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
+                        old_imp, old_types);
+
+    /* Test it's back to what it was.  */
+    if ([o variable] != o)
+      abort ();    
+
+    {
+      DifferentClass *o = [[DifferentClass alloc] init];
+
+      /* Finally, try adding a new method.  */
+      class_replaceMethod (objc_getClass ("DifferentClass"), @selector (mySelf),
+                          new_imp, new_types);
+      
+      if ([(MySubClass*)o mySelf] != objc_getClass ("DifferentClass"))
+       abort ();
+    }
+  }
+
+  printf ("Testing class_respondsToSelector ()...\n");
+  {
+    if (! class_respondsToSelector (objc_getClass ("MySubClass"), @selector(setVariable:)))
+      abort ();
+
+    if (class_respondsToSelector (objc_getClass ("MyRootClass"), @selector(setVariable:)))
+      abort ();
+  }
+
+  /* This is not really implemented with the GNU runtime.  */
+  /* printf ("Testing class_setIvarLayout ()...\n"); */
+
+  printf ("Testing class_setVersion ()...\n");
+  {
+    class_setVersion (objc_getClass ("MySubClass"), 45);
+    
+    if (class_getVersion (objc_getClass ("MySubClass")) != 45)
+      abort ();
+
+    class_setVersion (objc_getClass ("MySubClass"), 46);
+
+    if (class_getVersion (objc_getClass ("MySubClass")) != 46)
+      abort ();
+  }
+
+  /* This is not really implemented with the GNU runtime.  */
+  /* printf ("Testing class_setWeakIvarLayout ()...\n"); */
+
+  return 0;
+}
diff --git a/gcc/testsuite/objc.dg/gnu-api-2-objc.m b/gcc/testsuite/objc.dg/gnu-api-2-objc.m
new file mode 100644 (file)
index 0000000..802e5c8
--- /dev/null
@@ -0,0 +1,242 @@
+/* Test the Modern GNU Objective-C Runtime API.
+
+  This is test 'objc', covering all functions starting with 'objc'.  */
+
+/* { dg-do run } */
+/* { dg-skip-if "" { *-*-* } { "-fnext-runtime" } { "" } } */
+
+/* To get the modern GNU Objective-C Runtime API, you include
+   objc/runtime.h.  */
+#include <objc/runtime.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+@interface MyRootClass
+{ Class isa; }
++ alloc;
+- init;
+@end
+
+@implementation MyRootClass
++ alloc { return class_createInstance (self, 0); }
+- init  { return self; }
+@end
+
+@protocol MyProtocol
+- (id) variable;
+@end
+
+@protocol MySecondProtocol
+- (id) setVariable: (id)value;
+@end
+
+@interface MySubClass : MyRootClass <MyProtocol>
+{ id variable_ivar; }
+- (void) setVariable: (id)value;
+- (id) variable;
+@end
+
+@implementation MySubClass
+- (void) setVariable: (id)value { variable_ivar = value; }
+- (id) variable { return variable_ivar; }
+@end
+
+
+int main(int argc, void **args)
+{
+  /* Functions are tested in alphabetical order.  */
+
+  printf ("Testing objc_allocateClassPair ()...\n");
+  {
+    Class new_root_class = objc_allocateClassPair (Nil, "MyNewRootClass", 0);
+    Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MyNewSubClass", 0);
+
+    /* A new root class would obviously need at least an 'isa'
+       instance variable.  We don't add it so we never actually
+       instantiate an instance of the class, which wouldn't work.  */
+
+    objc_registerClassPair (new_root_class);
+    objc_registerClassPair (new_class);
+
+    if (strcmp (class_getName (new_class), "MyNewSubClass") != 0)
+      abort ();
+
+    if (class_getSuperclass (new_class) != objc_getClass ("MyRootClass"))
+      abort ();
+
+    if (strcmp (class_getName (new_root_class), "MyNewRootClass") != 0)
+      abort ();
+
+    if (class_getSuperclass (new_root_class) != Nil)
+      abort ();
+
+    {
+      MySubClass *o = [[objc_getClass ("MyNewSubClass") alloc] init];
+      
+      if (object_getClass (o) != objc_getClass ("MyNewSubClass"))
+       abort ();
+    }
+  }
+
+  printf ("Testing objc_copyProtocolList ()...\n");
+  {
+    /* Make sure both our two protocols are known to the runtime.  */
+    id my_protocol = @protocol (MyProtocol);
+    id my_second_protocol = @protocol (MySecondProtocol);
+    unsigned int count;
+    Protocol ** list = objc_copyProtocolList (&count);
+
+    if (count != 2)
+      abort ();
+
+    if (! ((strcmp (protocol_getName (list[0]), "MyProtocol") == 0
+           && strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0)
+          || (strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0
+              && strcmp (protocol_getName (list[1]), "MyProtocol") == 0)))
+      abort ();
+    
+    if (list[2] != NULL)
+      abort ();
+  }
+
+  printf ("Testing objc_disposeClassPair ()...\n");
+  {
+    Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:));
+    Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MyNewSubClass", 0);
+
+    /* Add a bit of everything to the class to exercise undoing all these changes.  */
+
+    /* Instance variable.  */
+    class_addIvar (new_class, "my_variable", sizeof (float), __alignof__ (float), @encode (float));
+
+    /* Instance method.  */
+    class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method),
+                    method_getTypeEncoding (method));
+
+    /* Class method.  */
+    class_addMethod (object_getClass (new_class), @selector (setVariable:), method_getImplementation (method),
+                    method_getTypeEncoding (method));
+
+    /* Protocol.  */
+    class_addProtocol (new_class, @protocol (MyProtocol));
+
+    objc_disposeClassPair (new_class);
+  }
+
+  /* This function currently does not exist with the GNU runtime.  */
+  /* printf ("Testing objc_duplicateClass ()...\n"); */
+
+  /* TODO - Test it when implemented in the GNU Runtime */
+  /*  printf ("Testing objc_getAssociatedObject ()...\n");  */
+
+  printf ("Testing objc_getClass ()...\n");
+  {
+    if (strcmp (class_getName (objc_getClass ("MySubClass")),
+               "MySubClass") != 0)
+      abort ();
+  }
+
+  printf ("Testing objc_getClassList ()...\n");
+  {
+    Class *list;
+    int i, count, other_count;
+    count = objc_getClassList (NULL, 0);
+
+    /* count most likely will be 5, (MyRootClass, MySubClass,
+       Protocol, Object, NXConstantString).  */
+    if (count < 3)
+      abort ();
+    
+    list = malloc (sizeof (Class) * count);
+    other_count = objc_getClassList (list, count);
+
+    if (other_count != count)
+      abort ();
+
+    /* Spot-check: search for class 'MyRootClass' in the list.  */
+    for (i = 0; i < count; i++)
+      {
+       if (strcmp (class_getName (list[i]), "MyRootClass") == 0)
+         break;
+      }
+    if (i == count)
+      abort ();
+
+    /* Spot-check: search for class 'MySubClass' in the list.  */
+    for (i = 0; i < count; i++)
+      {
+       if (strcmp (class_getName (list[i]), "MySubClass") == 0)
+         break;
+      }
+    if (i == count)
+      abort ();
+
+    /* Spot-check: search for class 'Protocol' in the list.  */
+    for (i = 0; i < count; i++)
+      {
+       if (strcmp (class_getName (list[i]), "Protocol") == 0)
+         break;
+      }
+    if (i == count)
+      abort ();
+  }
+
+  /* This function does not exist with the GNU runtime.  */
+  /* printf ("Testing objc_getFutureClass ()...\n"); */
+
+  printf ("Testing objc_getMetaClass ()...\n");
+  {
+    if (! class_isMetaClass (objc_getMetaClass ("MyRootClass")))
+      abort ();
+  }
+
+  printf ("Testing objc_getProtocol ()...\n");
+  {
+    if (! protocol_isEqual (objc_getProtocol ("MyProtocol"), @protocol (MyProtocol)))
+      abort ();
+  }
+
+  printf ("Testing objc_getRequiredClass ()...\n");
+  {
+    if (strcmp (class_getName (objc_getRequiredClass ("MyRootClass")),
+               "MyRootClass") != 0)
+      abort ();
+  }
+
+  printf ("Testing objc_lookupClass ()...\n");
+  {
+    if (strcmp (class_getName (objc_lookupClass ("MyRootClass")),
+               "MyRootClass") != 0)
+      abort ();
+  }
+
+  /* This function does not exist with the GNU runtime.  */
+  /* printf ("Testing objc_setFutureClass ()...\n"); */
+
+  printf ("Testing objc_registerClassPair ()...\n");
+  {
+    Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0);
+
+    class_addProtocol (new_class, @protocol (MySecondProtocol));
+    
+    objc_registerClassPair (new_class);
+    
+    if (strcmp (class_getName (new_class), "MySubSubClass") != 0)
+      abort ();
+
+    if (class_getSuperclass (new_class) != objc_getClass ("MySubClass"))
+      abort ();
+
+    if (! class_conformsToProtocol (new_class, @protocol (MySecondProtocol)))
+      abort ();
+  }
+
+  /* TODO - Test it when implemented in the GNU Runtime */
+  /*  printf ("Testing objc_removeAssociatedObjects ()...\n");  */
+
+  /* TODO - Test it when implemented in the GNU Runtime */
+  /*  printf ("Testing objc_setAssociatedObject ()...\n");  */
+
+  return 0;
+}