summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2022-05-22 08:34:38 -0400
committerMatthias Clasen <mclasen@redhat.com>2022-05-23 15:51:33 -0400
commitc8e14df6f71d051e4893585df59341bcdcc6d0fa (patch)
tree9d2fa17f7e2c7968c82019528d29fc3aaf14c0a8
parent24925658f54f187b660fb7c69bf6bb34c6b7882a (diff)
downloadglib-c8e14df6f71d051e4893585df59341bcdcc6d0fa.tar.gz
Add another test for g_param_value_is_valid
Add a test that exercises the fallback in g_param_value_is_valid.
-rw-r--r--gobject/tests/param.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/gobject/tests/param.c b/gobject/tests/param.c
index 7081daf7e..08d9f9767 100644
--- a/gobject/tests/param.c
+++ b/gobject/tests/param.c
@@ -1241,6 +1241,110 @@ test_param_is_valid_name (void)
g_assert_false (g_param_spec_is_valid_name (invalid_names[i]));
}
+static void
+param_int_init (GParamSpec *pspec)
+{
+ GParamSpecInt *ispec = (GParamSpecInt *)pspec;
+
+ ispec->minimum = 0x7fffffff;
+ ispec->maximum = 0x80000000;
+ ispec->default_value = 0;
+}
+
+static void
+param_int_set_default (GParamSpec *pspec,
+ GValue *value)
+{
+ value->data[0].v_int = ((GParamSpecInt *)pspec)->default_value;
+}
+
+static gboolean
+param_int_validate (GParamSpec *pspec,
+ GValue *value)
+{
+ GParamSpecInt *ispec = (GParamSpecInt *)pspec;
+ int oval = value->data[0].v_int;
+
+ value->data[0].v_int = CLAMP (value->data[0].v_int, ispec->minimum, ispec->maximum);
+
+ return value->data[0].v_int != oval;
+}
+
+static int
+param_int_values_cmp (GParamSpec *pspec,
+ const GValue *value1,
+ const GValue *value2)
+{
+ if (value1->data[0].v_int < value2->data[0].v_int)
+ return -1;
+ else
+ return value1->data[0].v_int > value2->data[0].v_int;
+}
+
+static GType custom_type;
+
+/* Register a pspec that has a validate vfunc, but not
+ * value_is_valid, to test the fallback in g_param_value_is_valid
+ */
+static void
+register_custom_pspec (void)
+{
+ const GParamSpecTypeInfo pspec_info = {
+ sizeof (GParamSpecInt), /* instance_size */
+ 16, /* n_preallocs */
+ param_int_init, /* instance_init */
+ G_TYPE_INT, /* value_type */
+ NULL, /* finalize */
+ param_int_set_default, /* value_set_default */
+ param_int_validate, /* value_validate */
+ param_int_values_cmp, /* values_cmp */
+ };
+
+ custom_type = g_param_type_register_static ("GParamInt2", &pspec_info);
+}
+
+static GParamSpec *
+g_param_spec_custom (const char *name,
+ int minimum,
+ int maximum,
+ int default_value,
+ GParamFlags flags)
+{
+ GParamSpecInt *ispec;
+
+ g_return_val_if_fail (default_value >= minimum && default_value <= maximum, NULL);
+
+ ispec = g_param_spec_internal (custom_type, name, NULL, NULL, flags);
+ if (ispec == NULL)
+ return NULL;
+
+ ispec->minimum = minimum;
+ ispec->maximum = maximum;
+ ispec->default_value = default_value;
+
+ return G_PARAM_SPEC (ispec);
+}
+
+static void
+test_param_spec_custom (void)
+{
+ GParamSpec *pspec;
+ GValue value = G_VALUE_INIT;
+
+ register_custom_pspec ();
+
+ pspec = g_param_spec_custom ("myint", 10, 30, 20, G_PARAM_READWRITE);
+
+ g_value_init (&value, G_TYPE_INT);
+
+ g_value_set_int (&value, 40);
+
+ g_assert_false (g_param_value_is_valid (pspec, &value));
+ g_assert_cmpint (g_value_get_int (&value), ==, 40);
+
+ g_param_spec_unref (pspec);
+}
+
int
main (int argc, char *argv[])
{
@@ -1284,6 +1388,7 @@ main (int argc, char *argv[])
g_test_add_func ("/paramspec/gtype", test_param_spec_gtype);
g_test_add_func ("/paramspec/variant", test_param_spec_variant);
g_test_add_func ("/paramspec/variant/cmp", test_param_spec_variant_cmp);
+ g_test_add_func ("/paramspec/custom", test_param_spec_custom);
return g_test_run ();
}