diff --git a/lib/wp/spa-pod.c b/lib/wp/spa-pod.c index 057e204373aa611e3aef0784ad3e9d921bced5ab..2f54c1876e19703f743ea565fd26769988bc5845 100644 --- a/lib/wp/spa-pod.c +++ b/lib/wp/spa-pod.c @@ -1517,6 +1517,98 @@ wp_spa_pod_set_pod (WpSpaPod *self, const WpSpaPod *pod) return TRUE; } +/** + * wp_spa_pod_equal: + * @self: the spa pod object + * @pod: the pod with the value to be compared with + * + * Checks whether two spa pod objects have the same value or not + * + * Returns: TRUE if both spa pod objects have the same values, FALSE othewrise. + */ +gboolean +wp_spa_pod_equal (const WpSpaPod *self, const WpSpaPod *pod) +{ + if (self->type != pod->type) + return FALSE; + if (SPA_POD_TYPE (self->pod) != SPA_POD_TYPE (pod->pod)) + return FALSE; + + switch (SPA_POD_TYPE (self->pod)) { + case SPA_TYPE_None: + break; + case SPA_TYPE_Bool: + if (((struct spa_pod_bool *)self->pod)->value != ((struct spa_pod_bool *)pod->pod)->value) + return FALSE; + break; + case SPA_TYPE_Id: + if (((struct spa_pod_id *)self->pod)->value != ((struct spa_pod_id *)pod->pod)->value) + return FALSE; + break; + case SPA_TYPE_Int: + if (((struct spa_pod_int *)self->pod)->value != ((struct spa_pod_int *)pod->pod)->value) + return FALSE; + break; + case SPA_TYPE_Long: + if (((struct spa_pod_long *)self->pod)->value != ((struct spa_pod_long *)pod->pod)->value) + return FALSE; + break; + case SPA_TYPE_Float: + if (((struct spa_pod_float *)self->pod)->value != ((struct spa_pod_float *)pod->pod)->value) + return FALSE; + break; + case SPA_TYPE_Double: + if (((struct spa_pod_double *)self->pod)->value != ((struct spa_pod_double *)pod->pod)->value) + return FALSE; + break; + case SPA_TYPE_Pointer: + if (((struct spa_pod_pointer *)self->pod)->body.type != ((struct spa_pod_pointer *)pod->pod)->body.type || + ((struct spa_pod_pointer *)self->pod)->body.value != ((struct spa_pod_pointer *)pod->pod)->body.value) + return FALSE; + break; + case SPA_TYPE_Fd: + if (((struct spa_pod_fd *)self->pod)->value != ((struct spa_pod_fd *)pod->pod)->value) + return FALSE; + break; + case SPA_TYPE_Rectangle: + if (((struct spa_pod_rectangle *)self->pod)->value.width != ((struct spa_pod_rectangle *)pod->pod)->value.width || + ((struct spa_pod_rectangle *)self->pod)->value.height != ((struct spa_pod_rectangle *)pod->pod)->value.height) + return FALSE; + break; + case SPA_TYPE_Fraction: + if (((struct spa_pod_fraction *)self->pod)->value.num != ((struct spa_pod_fraction *)pod->pod)->value.num || + ((struct spa_pod_fraction *)self->pod)->value.denom != ((struct spa_pod_fraction *)pod->pod)->value.denom) + return FALSE; + break; + default: + if (self->pod->size != pod->pod->size || + memcmp (SPA_MEMBER (self->pod, sizeof (struct spa_pod), void), + SPA_MEMBER (pod->pod, sizeof (struct spa_pod), void), + self->pod->size) != 0) + return FALSE; + break; + } + + switch (self->type) { + case WP_SPA_POD_PROPERTY: + if (self->static_pod.data_property.table != pod->static_pod.data_property.table || + self->static_pod.data_property.table != pod->static_pod.data_property.table || + self->static_pod.data_property.flags != pod->static_pod.data_property.flags) + return FALSE; + break; + case WP_SPA_POD_CONTROL: + if (self->static_pod.data_control.offset != pod->static_pod.data_control.offset || + self->static_pod.data_control.type != pod->static_pod.data_control.type) + return FALSE; + break; + case WP_SPA_POD_REGULAR: + default: + break; + } + + return TRUE; +} + /** * wp_spa_pod_get_object: * @self: the spa pod object diff --git a/lib/wp/spa-pod.h b/lib/wp/spa-pod.h index 6b6f0f430bf0a111b0f711b5351420eecbf502b1..66936755e358377830118d1ad38d6149e896e326 100644 --- a/lib/wp/spa-pod.h +++ b/lib/wp/spa-pod.h @@ -240,6 +240,9 @@ gboolean wp_spa_pod_set_fraction (WpSpaPod *self, guint32 num, guint32 denom); WP_API gboolean wp_spa_pod_set_pod (WpSpaPod *self, const WpSpaPod *pod); +WP_API +gboolean wp_spa_pod_equal (const WpSpaPod *self, const WpSpaPod *pod); + WP_API gboolean wp_spa_pod_get_object (WpSpaPod *self, const char *type_name, const char **id_name, ...) G_GNUC_NULL_TERMINATED; diff --git a/tests/wp/spa-pod.c b/tests/wp/spa-pod.c index 53cb49e307fdeec59427827b35223f0acfccb084..7ba96a7c13a4a18206b0df2697249ca7c4081263 100644 --- a/tests/wp/spa-pod.c +++ b/tests/wp/spa-pod.c @@ -20,6 +20,8 @@ test_spa_pod_basic (void) g_assert_true (wp_spa_pod_is_none (pod)); g_assert_false (wp_spa_pod_is_id (pod)); g_assert_cmpstr ("None", ==, wp_spa_pod_get_type_name (pod)); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_none (); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* Boolean */ @@ -48,6 +50,11 @@ test_spa_pod_basic (void) g_assert_true (wp_spa_pod_get_boolean (copy, &value)); g_assert_false (value); g_assert_cmpstr ("Bool", ==, wp_spa_pod_get_type_name (copy)); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_boolean (TRUE); + g_assert_true (wp_spa_pod_set_pod (copy, other)); + g_assert_true (wp_spa_pod_get_boolean (copy, &value)); + g_assert_true (value); + g_assert_true (wp_spa_pod_equal (copy, other)); } /* Id */ @@ -62,6 +69,11 @@ test_spa_pod_basic (void) g_assert_true (wp_spa_pod_set_id (pod, 10)); g_assert_true (wp_spa_pod_get_id (pod, &value)); g_assert_cmpuint (value, ==, 10); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_id (20); + g_assert_true (wp_spa_pod_set_pod (pod, other)); + g_assert_true (wp_spa_pod_get_id (pod, &value)); + g_assert_cmpuint (value, ==, 20); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* Int */ @@ -76,6 +88,11 @@ test_spa_pod_basic (void) g_assert_true (wp_spa_pod_set_int (pod, 9999)); g_assert_true (wp_spa_pod_get_int (pod, &value)); g_assert_cmpint (value, ==, 9999); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_int (1000); + g_assert_true (wp_spa_pod_set_pod (pod, other)); + g_assert_true (wp_spa_pod_get_int (pod, &value)); + g_assert_cmpuint (value, ==, 1000); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* Long */ @@ -90,6 +107,11 @@ test_spa_pod_basic (void) g_assert_true (wp_spa_pod_set_long (pod, LONG_MIN)); g_assert_true (wp_spa_pod_get_long (pod, &value)); g_assert_cmpuint (value, ==, LONG_MIN); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_long (0); + g_assert_true (wp_spa_pod_set_pod (pod, other)); + g_assert_true (wp_spa_pod_get_long (pod, &value)); + g_assert_cmpuint (value, ==, 0); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* Float */ @@ -104,6 +126,11 @@ test_spa_pod_basic (void) g_assert_true (wp_spa_pod_set_float (pod, 1.0)); g_assert_true (wp_spa_pod_get_float (pod, &value)); g_assert_cmpfloat_with_epsilon (value, 1.0, 0.001); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_float (-3.14); + g_assert_true (wp_spa_pod_set_pod (pod, other)); + g_assert_true (wp_spa_pod_get_float (pod, &value)); + g_assert_cmpfloat_with_epsilon (value, -3.14, 0.001); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* Double */ @@ -118,6 +145,11 @@ test_spa_pod_basic (void) g_assert_true (wp_spa_pod_set_double (pod, 2.0)); g_assert_true (wp_spa_pod_get_double (pod, &value)); g_assert_cmpfloat_with_epsilon (value, 2.0, 0.0000000001); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_double (3.0); + g_assert_true (wp_spa_pod_set_pod (pod, other)); + g_assert_true (wp_spa_pod_get_double (pod, &value)); + g_assert_cmpfloat_with_epsilon (value, 3.0, 0.0000000001); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* String */ @@ -130,13 +162,13 @@ test_spa_pod_basic (void) g_assert_nonnull (value); g_assert_cmpstr (value, ==, "WirePlumber"); g_assert_cmpstr ("String", ==, wp_spa_pod_get_type_name (pod)); - g_autoptr (WpSpaPod) other = wp_spa_pod_new_string ("Other"); g_assert_nonnull (other); g_assert_true (wp_spa_pod_set_pod (pod, other)); g_assert_true (wp_spa_pod_get_string (pod, &value)); g_assert_nonnull (value); g_assert_cmpstr (value, ==, "Other"); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* Bytes */ @@ -151,6 +183,13 @@ test_spa_pod_basic (void) g_assert_cmpmem (value, len, "bytes", 5); g_assert_cmpuint (len, ==, 5); g_assert_cmpstr ("Bytes", ==, wp_spa_pod_get_type_name (pod)); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_bytes ("pod", 3); + g_assert_true (wp_spa_pod_set_pod (pod, other)); + g_assert_true (wp_spa_pod_get_bytes (pod, &value, &len)); + g_assert_nonnull (value); + g_assert_cmpmem (value, len, "pod", 3); + g_assert_cmpuint (len, ==, 3); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* Pointer */ @@ -176,6 +215,16 @@ test_spa_pod_basic (void) g_assert_cmpstr (type_name, ==, "Bool"); g_assert_true (p == &b); g_assert_cmpint (*(gboolean *)p, ==, TRUE); + float f = 1.1; + g_autoptr (WpSpaPod) other = wp_spa_pod_new_pointer ("Float", &f); + g_assert_true (wp_spa_pod_set_pod (pod, other)); + g_assert_true (wp_spa_pod_get_pointer (pod, &type_name, &p)); + g_assert_nonnull (type_name); + g_assert_nonnull (p); + g_assert_cmpstr (type_name, ==, "Float"); + g_assert_true (p == &f); + g_assert_cmpfloat_with_epsilon (*(float *)p, 1.1, 0.01); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* Fd */ @@ -190,6 +239,11 @@ test_spa_pod_basic (void) g_assert_true (wp_spa_pod_set_fd (pod, 1)); g_assert_true (wp_spa_pod_get_fd (pod, &value)); g_assert_cmpuint (value, ==, 1); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_fd (10); + g_assert_true (wp_spa_pod_set_pod (pod, other)); + g_assert_true (wp_spa_pod_get_fd (pod, &value)); + g_assert_cmpuint (value, ==, 10); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* Rectangle */ @@ -207,6 +261,12 @@ test_spa_pod_basic (void) g_assert_true (wp_spa_pod_get_rectangle (pod, &width, &height)); g_assert_cmpint (width, ==, 640); g_assert_cmpint (height, ==, 480); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_rectangle (200, 100); + g_assert_true (wp_spa_pod_set_pod (pod, other)); + g_assert_true (wp_spa_pod_get_rectangle (pod, &width, &height)); + g_assert_cmpint (width, ==, 200); + g_assert_cmpint (height, ==, 100); + g_assert_true (wp_spa_pod_equal (pod, other)); } /* Fraction */ @@ -224,6 +284,12 @@ test_spa_pod_basic (void) g_assert_true (wp_spa_pod_get_fraction (pod, &num, &denom)); g_assert_cmpint (num, ==, 4); g_assert_cmpint (denom, ==, 3); + g_autoptr (WpSpaPod) other = wp_spa_pod_new_fraction (2, 1); + g_assert_true (wp_spa_pod_set_pod (pod, other)); + g_assert_true (wp_spa_pod_get_fraction (pod, &num, &denom)); + g_assert_cmpint (num, ==, 2); + g_assert_cmpint (denom, ==, 1); + g_assert_true (wp_spa_pod_equal (pod, other)); } wp_spa_type_deinit ();