summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Trevisan (TreviƱo) <mail@3v1n0.net>2022-07-15 02:49:37 +0200
committerPhilip Chimento <philip.chimento@gmail.com>2022-11-21 02:48:44 +0000
commitc2d6e0bc2478b63d0e07c004e38da6162a5ca988 (patch)
tree52a0d497cbc5a000882e4bd975d57c6446122788
parent5112dff728bbc60ab3a807e647e12a88070a8f4b (diff)
downloadgobject-introspection-c2d6e0bc2478b63d0e07c004e38da6162a5ca988.tar.gz
gimarshallingtests: Add Structured Union types, unions with struct fields
This allows to perform more introspection tests in gjs and other implementations when it comes to set/get fields or "cast" an union member to the main union type
-rw-r--r--tests/gimarshallingtests.c150
-rw-r--r--tests/gimarshallingtests.h79
2 files changed, 229 insertions, 0 deletions
diff --git a/tests/gimarshallingtests.c b/tests/gimarshallingtests.c
index 2072b9f2..f7ebe709 100644
--- a/tests/gimarshallingtests.c
+++ b/tests/gimarshallingtests.c
@@ -4243,6 +4243,156 @@ gi_marshalling_tests_union_method (GIMarshallingTestsUnion *union_)
g_assert_cmpint (union_->long_, ==, 42);
}
+/**
+ * gi_marshalling_tests_structured_union_new:
+ * @type: Type of #GIMarshallingTestsStructuredUnion to create
+ */
+GIMarshallingTestsStructuredUnion *
+gi_marshalling_tests_structured_union_new (GIMarshallingTestsStructuredUnionType type)
+{
+ GIMarshallingTestsStructuredUnion *new_union;
+
+ new_union = g_new0 (GIMarshallingTestsStructuredUnion, 1);
+ new_union->type = type;
+
+ switch (type)
+ {
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_NONE:
+ break;
+
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_SIMPLE_STRUCT:
+ new_union->simple_struct.parent.long_ = 6;
+ new_union->simple_struct.parent.int8 = 7;
+ break;
+
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_NESTED_STRUCT:
+ new_union->nested_struct.parent.simple_struct.long_ = 6;
+ new_union->nested_struct.parent.simple_struct.int8 = 7;
+ break;
+
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_BOXED_STRUCT:
+ new_union->boxed_struct.parent.long_ = 42;
+ new_union->boxed_struct.parent.string_ = g_strdup ("hello");
+ new_union->boxed_struct.parent.g_strv = g_new0 (gchar *, 4);
+ new_union->boxed_struct.parent.g_strv[0] = g_strdup ("0");
+ new_union->boxed_struct.parent.g_strv[1] = g_strdup ("1");
+ new_union->boxed_struct.parent.g_strv[2] = g_strdup ("2");
+ new_union->boxed_struct.parent.g_strv[3] = NULL;
+ break;
+
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_BOXED_STRUCT_PTR:
+ new_union->boxed_struct_ptr.parent = g_boxed_copy (
+ gi_marshalling_tests_boxed_struct_get_type(),
+ gi_marshalling_tests_boxed_struct_returnv ());
+ break;
+
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_POINTER_STRUCT:
+ new_union->pointer_struct.parent.long_ = 42;
+ break;
+
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_SINGLE_UNION:
+ new_union->single_union.parent.union_.long_ = 42;
+ break;
+
+ default:
+ g_free (new_union);
+ g_return_val_if_reached (NULL);
+ }
+
+ return new_union;
+}
+
+static GIMarshallingTestsStructuredUnion *
+gi_marshalling_tests_structured_union_copy (GIMarshallingTestsStructuredUnion *union_)
+{
+ GIMarshallingTestsStructuredUnion *new_union;
+
+ new_union = g_new (GIMarshallingTestsStructuredUnion, 1);
+ *new_union = *union_;
+
+ switch (union_->type)
+ {
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_BOXED_STRUCT:
+ new_union->boxed_struct.parent.string_ = g_strdup (union_->boxed_struct.parent.string_);
+ if (union_->boxed_struct.parent.g_strv)
+ {
+ guint length = g_strv_length (union_->boxed_struct.parent.g_strv);
+ guint i;
+
+ new_union->boxed_struct.parent.g_strv = g_new0 (gchar *, length + 1);
+ for (i = 0; i < length; i++)
+ new_union->boxed_struct.parent.g_strv[i] = g_strdup (union_->boxed_struct.parent.g_strv[i]);
+ new_union->boxed_struct.parent.g_strv[i] = NULL;
+ }
+ break;
+
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_BOXED_STRUCT_PTR:
+ new_union->boxed_struct_ptr.parent = g_boxed_copy (
+ gi_marshalling_tests_boxed_struct_get_type(), union_->boxed_struct_ptr.parent);
+ break;
+
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_NONE:
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_SINGLE_UNION:
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_POINTER_STRUCT:
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_SIMPLE_STRUCT:
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_NESTED_STRUCT:
+ break;
+
+ default:
+ g_return_val_if_reached (new_union);
+ }
+
+ return new_union;
+}
+
+static void
+gi_marshalling_tests_structured_union_free (GIMarshallingTestsStructuredUnion *union_)
+{
+ switch (union_->type)
+ {
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_BOXED_STRUCT:
+ g_free (union_->boxed_struct.parent.string_);
+ g_strfreev (union_->boxed_struct.parent.g_strv);
+ break;
+
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_BOXED_STRUCT_PTR:
+ g_boxed_free (gi_marshalling_tests_boxed_struct_get_type(), union_->boxed_struct_ptr.parent);
+ break;
+
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_NONE:
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_SINGLE_UNION:
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_POINTER_STRUCT:
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_SIMPLE_STRUCT:
+ case GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_NESTED_STRUCT:
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+
+ g_free (union_);
+}
+
+GType gi_marshalling_tests_structured_union_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ type = g_boxed_type_register_static ("GIMarshallingTestsStructuredUnion",
+ (GBoxedCopyFunc)
+ gi_marshalling_tests_structured_union_copy,
+ (GBoxedFreeFunc) gi_marshalling_tests_structured_union_free);
+ }
+
+ return type;
+}
+
+GIMarshallingTestsStructuredUnionType
+gi_marshalling_tests_structured_union_type (GIMarshallingTestsStructuredUnion *structured_union)
+{
+ return structured_union->type;
+}
enum
diff --git a/tests/gimarshallingtests.h b/tests/gimarshallingtests.h
index d4901987..5d0656d7 100644
--- a/tests/gimarshallingtests.h
+++ b/tests/gimarshallingtests.h
@@ -1327,6 +1327,12 @@ typedef union {
glong long_;
} GIMarshallingTestsUnion;
+typedef union {
+ glong long_;
+ gsize size;
+ const gchar *str;
+} GIMarshallingTestsUnregisteredUnion;
+
_GI_TEST_EXTERN
GType gi_marshalling_tests_union_get_type (void) G_GNUC_CONST;
@@ -1343,6 +1349,79 @@ void gi_marshalling_tests_union_inv (GIMarshallingTestsUnion *union_);
_GI_TEST_EXTERN
void gi_marshalling_tests_union_method (GIMarshallingTestsUnion *union_);
+typedef enum
+{
+ GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_NONE,
+ GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_SIMPLE_STRUCT,
+ GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_NESTED_STRUCT,
+ GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_POINTER_STRUCT,
+ GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_BOXED_STRUCT,
+ GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_BOXED_STRUCT_PTR,
+ GI_MARSHALLING_TESTS_STRUCTURED_UNION_TYPE_SINGLE_UNION,
+} GIMarshallingTestsStructuredUnionType;
+
+typedef struct {
+ GIMarshallingTestsStructuredUnionType type;
+ GIMarshallingTestsSimpleStruct parent;
+} GIMarshallingTestsStructuredUnionSimpleStruct;
+
+typedef struct {
+ GIMarshallingTestsStructuredUnionType type;
+ GIMarshallingTestsNestedStruct parent;
+} GIMarshallingTestsStructuredUnionNestedStruct;
+
+typedef struct {
+ GIMarshallingTestsStructuredUnionType type;
+ GIMarshallingTestsPointerStruct parent;
+} GIMarshallingTestsStructuredUnionPointerStruct;
+
+typedef struct {
+ GIMarshallingTestsStructuredUnionType type;
+ GIMarshallingTestsBoxedStruct parent;
+} GIMarshallingTestsStructuredUnionBoxedStruct;
+
+typedef struct {
+ GIMarshallingTestsStructuredUnionType type;
+ GIMarshallingTestsBoxedStruct *parent;
+} GIMarshallingTestsStructuredUnionBoxedStructPtr;
+
+typedef struct {
+ GIMarshallingTestsStructuredUnionType type;
+ GIMarshallingTestsUnion union_;
+} GIMarshallingTestsStructuredUnionUnionStruct;
+
+typedef union {
+ GIMarshallingTestsStructuredUnionUnionStruct parent;
+} GIMarshallingTestsStructuredUnionSingleUnion;
+
+typedef union {
+ /*< private >*/
+ GIMarshallingTestsStructuredUnionType type;
+
+ GIMarshallingTestsStructuredUnionSimpleStruct simple_struct;
+ GIMarshallingTestsStructuredUnionNestedStruct nested_struct;
+ GIMarshallingTestsStructuredUnionPointerStruct pointer_struct;
+ GIMarshallingTestsStructuredUnionBoxedStruct boxed_struct;
+ GIMarshallingTestsStructuredUnionBoxedStructPtr boxed_struct_ptr;
+ GIMarshallingTestsStructuredUnionSingleUnion single_union;
+} GIMarshallingTestsStructuredUnion;
+
+_GI_TEST_EXTERN
+GType gi_marshalling_tests_structured_union_get_type (void) G_GNUC_CONST;
+
+_GI_TEST_EXTERN
+GIMarshallingTestsStructuredUnion *gi_marshalling_tests_structured_union_new (GIMarshallingTestsStructuredUnionType type);
+
+/* we want this to clash with a field value name */
+_GI_TEST_EXTERN
+GIMarshallingTestsStructuredUnionType gi_marshalling_tests_structured_union_type (GIMarshallingTestsStructuredUnion *structured_union);
+
+typedef union {
+ GIMarshallingTestsStructuredUnionType type;
+ GIMarshallingTestsStructuredUnionSimpleStruct simple_struct1;
+ GIMarshallingTestsStructuredUnionSimpleStruct simple_struct2;
+} GIMarshallingTestsStructuredUnionSingleType;
+
/* Object */
#define GI_MARSHALLING_TESTS_TYPE_OBJECT (gi_marshalling_tests_object_get_type ())