diff mbox

[08/13] tests: Add qdict_stringify_for_keyval() test

Message ID 20180509165530.29561-9-mreitz@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Max Reitz May 9, 2018, 4:55 p.m. UTC
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 tests/check-qdict.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

Comments

Eric Blake May 10, 2018, 4:02 p.m. UTC | #1
On 05/09/2018 11:55 AM, Max Reitz wrote:
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>   tests/check-qdict.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 54 insertions(+)
> 

> +static void qdict_stringify_for_keyval_test(void)
> +{
> +    QDict *dict = qdict_new();
> +
> +    /*
> +     * Test stringification of:
> +     *
> +     * {
> +     *     "a": "null",
> +     *     "b": 42,
> +     *     "c": -23,
> +     *     "d": false,
> +     *     "e": null,
> +     *     "f": "",
> +     *     "g": 0.5,
> +     *     "h": 0xffffffffffffffff,
> +     *     "i": true,
> +     *     "j": 0

Is it worth testing fun things like '-0.0'?


> +    g_assert(!strcmp(qdict_get_str(dict, "a"), "null"));
> +    g_assert(!strcmp(qdict_get_str(dict, "b"), "42"));
> +    g_assert(!strcmp(qdict_get_str(dict, "c"), "-23"));
> +    g_assert(!strcmp(qdict_get_str(dict, "d"), "off"));
> +    g_assert(qobject_type(qdict_get(dict, "e")) == QTYPE_QNULL);

Is it worth shortening this line to:
g_assert(qobject_to(QNull, qdict_get(dict, "e")));

Reviewed-by: Eric Blake <eblake@redhat.com>
Max Reitz May 11, 2018, 6:13 p.m. UTC | #2
On 2018-05-10 18:02, Eric Blake wrote:
> On 05/09/2018 11:55 AM, Max Reitz wrote:
>> Signed-off-by: Max Reitz <mreitz@redhat.com>
>> ---
>>   tests/check-qdict.c | 54
>> +++++++++++++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 54 insertions(+)
>>
> 
>> +static void qdict_stringify_for_keyval_test(void)
>> +{
>> +    QDict *dict = qdict_new();
>> +
>> +    /*
>> +     * Test stringification of:
>> +     *
>> +     * {
>> +     *     "a": "null",
>> +     *     "b": 42,
>> +     *     "c": -23,
>> +     *     "d": false,
>> +     *     "e": null,
>> +     *     "f": "",
>> +     *     "g": 0.5,
>> +     *     "h": 0xffffffffffffffff,
>> +     *     "i": true,
>> +     *     "j": 0
> 
> Is it worth testing fun things like '-0.0'?

Sure, why not.  Maybe even infinity, although I'm not quite sure the
input visitor can handle it...
>> +    g_assert(!strcmp(qdict_get_str(dict, "a"), "null"));
>> +    g_assert(!strcmp(qdict_get_str(dict, "b"), "42"));
>> +    g_assert(!strcmp(qdict_get_str(dict, "c"), "-23"));
>> +    g_assert(!strcmp(qdict_get_str(dict, "d"), "off"));
>> +    g_assert(qobject_type(qdict_get(dict, "e")) == QTYPE_QNULL);
> 
> Is it worth shortening this line to:
> g_assert(qobject_to(QNull, qdict_get(dict, "e")));

I think explicitly checking the type is a bit more expressive.

Max

> Reviewed-by: Eric Blake <eblake@redhat.com>
Eric Blake May 11, 2018, 6:33 p.m. UTC | #3
On 05/11/2018 01:13 PM, Max Reitz wrote:

>>> +     *     "h": 0xffffffffffffffff,
>>> +     *     "i": true,
>>> +     *     "j": 0
>>
>> Is it worth testing fun things like '-0.0'?
> 
> Sure, why not.  Maybe even infinity, although I'm not quite sure the
> input visitor can handle it...

JSON can't handle Inf or NaN, even if the input visitor can.  So 
probably best to not worry about those.

>>> +    g_assert(!strcmp(qdict_get_str(dict, "a"), "null"));
>>> +    g_assert(!strcmp(qdict_get_str(dict, "b"), "42"));
>>> +    g_assert(!strcmp(qdict_get_str(dict, "c"), "-23"));
>>> +    g_assert(!strcmp(qdict_get_str(dict, "d"), "off"));
>>> +    g_assert(qobject_type(qdict_get(dict, "e")) == QTYPE_QNULL);
>>
>> Is it worth shortening this line to:
>> g_assert(qobject_to(QNull, qdict_get(dict, "e")));
> 
> I think explicitly checking the type is a bit more expressive.

Okay (qobject_to() checks the type under the hood, and returns non-NULL 
only when it was the right type - but I see your point about that being 
a bit more magic)
diff mbox

Patch

diff --git a/tests/check-qdict.c b/tests/check-qdict.c
index 93e2112b6d..ef5d17f815 100644
--- a/tests/check-qdict.c
+++ b/tests/check-qdict.c
@@ -973,6 +973,57 @@  static void qdict_stress_test(void)
     qobject_unref(qdict);
 }
 
+static void qdict_stringify_for_keyval_test(void)
+{
+    QDict *dict = qdict_new();
+
+    /*
+     * Test stringification of:
+     *
+     * {
+     *     "a": "null",
+     *     "b": 42,
+     *     "c": -23,
+     *     "d": false,
+     *     "e": null,
+     *     "f": "",
+     *     "g": 0.5,
+     *     "h": 0xffffffffffffffff,
+     *     "i": true,
+     *     "j": 0
+     *  }
+     *
+     *  Note that null is not transformed into a string and remains a
+     *  QNull.
+     */
+
+    qdict_put_str(dict, "a", "null");
+    qdict_put_int(dict, "b", 42);
+    qdict_put_int(dict, "c", -23);
+    qdict_put_bool(dict, "d", false);
+    qdict_put_null(dict, "e");
+    qdict_put_str(dict, "f", "");
+    qdict_put(dict, "g", qnum_from_double(0.5));
+    qdict_put(dict, "h", qnum_from_uint(0xffffffffffffffffull));
+    qdict_put_bool(dict, "i", true);
+    qdict_put_int(dict, "j", 0);
+
+    qdict_stringify_for_keyval(dict);
+
+    g_assert(!strcmp(qdict_get_str(dict, "a"), "null"));
+    g_assert(!strcmp(qdict_get_str(dict, "b"), "42"));
+    g_assert(!strcmp(qdict_get_str(dict, "c"), "-23"));
+    g_assert(!strcmp(qdict_get_str(dict, "d"), "off"));
+    g_assert(qobject_type(qdict_get(dict, "e")) == QTYPE_QNULL);
+    g_assert(!strcmp(qdict_get_str(dict, "f"), ""));
+    g_assert(!strcmp(qdict_get_str(dict, "g"), "0.5"));
+    g_assert(!strcmp(qdict_get_str(dict, "h"), "18446744073709551615"));
+    g_assert(!strcmp(qdict_get_str(dict, "i"), "on"));
+    g_assert(!strcmp(qdict_get_str(dict, "j"), "0"));
+
+    qobject_unref(dict);
+}
+
 int main(int argc, char **argv)
 {
     g_test_init(&argc, &argv, NULL);
@@ -1010,6 +1061,9 @@  int main(int argc, char **argv)
 
     g_test_add_func("/public/rename_keys", qdict_rename_keys_test);
 
+    g_test_add_func("/public/stringify_for_keyval",
+                    qdict_stringify_for_keyval_test);
+
     /* The Big one */
     if (g_test_slow()) {
         g_test_add_func("/stress/test", qdict_stress_test);