[glib/param-speedups2: 4/5] Add another test for g_param_value_is_valid




commit bb3052b1d283131f28f7e94d25a7529e3d512fb4
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun May 22 08:34:38 2022 -0400

    Add another test for g_param_value_is_valid
    
    Add a test that exercises the fallback
    in g_param_value_is_valid.

 gobject/tests/param.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)
---
diff --git a/gobject/tests/param.c b/gobject/tests/param.c
index fffaaab386..76bc4040ad 100644
--- a/gobject/tests/param.c
+++ b/gobject/tests/param.c
@@ -1235,6 +1235,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[])
 {
@@ -1278,6 +1382,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 ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]