check-qjson: convert to gtest

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Anthony Liguori 2012-01-10 13:10:49 -06:00
parent 91479dd0b5
commit ef76dc59fa
2 changed files with 130 additions and 192 deletions

View File

@ -8,7 +8,7 @@
* See the COPYING.LIB file in the top-level directory.
*
*/
#include <check.h>
#include <glib.h>
#include "qstring.h"
#include "qint.h"
@ -20,7 +20,7 @@
#include "qemu-common.h"
START_TEST(escaped_string)
static void escaped_string(void)
{
int i;
struct {
@ -52,28 +52,23 @@ START_TEST(escaped_string)
obj = qobject_from_json(test_cases[i].encoded);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QSTRING);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
str = qobject_to_qstring(obj);
fail_unless(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0,
"%s != %s\n", qstring_get_str(str), test_cases[i].decoded);
g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].decoded);
if (test_cases[i].skip == 0) {
str = qobject_to_json(obj);
fail_unless(strcmp(qstring_get_str(str),test_cases[i].encoded) == 0,
"%s != %s\n", qstring_get_str(str),
test_cases[i].encoded);
g_assert_cmpstr(qstring_get_str(str), ==, test_cases[i].encoded);
qobject_decref(obj);
}
QDECREF(str);
}
}
END_TEST
START_TEST(simple_string)
static void simple_string(void)
{
int i;
struct {
@ -92,23 +87,22 @@ START_TEST(simple_string)
obj = qobject_from_json(test_cases[i].encoded);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QSTRING);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
str = qobject_to_qstring(obj);
fail_unless(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
str = qobject_to_json(obj);
fail_unless(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
qobject_decref(obj);
QDECREF(str);
}
}
END_TEST
START_TEST(single_quote_string)
static void single_quote_string(void)
{
int i;
struct {
@ -127,18 +121,17 @@ START_TEST(single_quote_string)
obj = qobject_from_json(test_cases[i].encoded);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QSTRING);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
str = qobject_to_qstring(obj);
fail_unless(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
QDECREF(str);
}
}
END_TEST
START_TEST(vararg_string)
static void vararg_string(void)
{
int i;
struct {
@ -155,18 +148,17 @@ START_TEST(vararg_string)
obj = qobject_from_jsonf("%s", test_cases[i].decoded);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QSTRING);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QSTRING);
str = qobject_to_qstring(obj);
fail_unless(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
QDECREF(str);
}
}
END_TEST
START_TEST(simple_number)
static void simple_number(void)
{
int i;
struct {
@ -187,25 +179,24 @@ START_TEST(simple_number)
QInt *qint;
obj = qobject_from_json(test_cases[i].encoded);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QINT);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QINT);
qint = qobject_to_qint(obj);
fail_unless(qint_get_int(qint) == test_cases[i].decoded);
g_assert(qint_get_int(qint) == test_cases[i].decoded);
if (test_cases[i].skip == 0) {
QString *str;
str = qobject_to_json(obj);
fail_unless(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
QDECREF(str);
}
QDECREF(qint);
}
}
END_TEST
START_TEST(float_number)
static void float_number(void)
{
int i;
struct {
@ -225,26 +216,25 @@ START_TEST(float_number)
QFloat *qfloat;
obj = qobject_from_json(test_cases[i].encoded);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QFLOAT);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QFLOAT);
qfloat = qobject_to_qfloat(obj);
fail_unless(qfloat_get_double(qfloat) == test_cases[i].decoded);
g_assert(qfloat_get_double(qfloat) == test_cases[i].decoded);
if (test_cases[i].skip == 0) {
QString *str;
str = qobject_to_json(obj);
fail_unless(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
g_assert(strcmp(qstring_get_str(str), test_cases[i].encoded) == 0);
QDECREF(str);
}
QDECREF(qfloat);
}
}
END_TEST
START_TEST(vararg_number)
static void vararg_number(void)
{
QObject *obj;
QInt *qint;
@ -254,85 +244,83 @@ START_TEST(vararg_number)
double valuef = 2.323423423;
obj = qobject_from_jsonf("%d", value);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QINT);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QINT);
qint = qobject_to_qint(obj);
fail_unless(qint_get_int(qint) == value);
g_assert(qint_get_int(qint) == value);
QDECREF(qint);
obj = qobject_from_jsonf("%" PRId64, value64);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QINT);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QINT);
qint = qobject_to_qint(obj);
fail_unless(qint_get_int(qint) == value64);
g_assert(qint_get_int(qint) == value64);
QDECREF(qint);
obj = qobject_from_jsonf("%f", valuef);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QFLOAT);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QFLOAT);
qfloat = qobject_to_qfloat(obj);
fail_unless(qfloat_get_double(qfloat) == valuef);
g_assert(qfloat_get_double(qfloat) == valuef);
QDECREF(qfloat);
}
END_TEST
START_TEST(keyword_literal)
static void keyword_literal(void)
{
QObject *obj;
QBool *qbool;
QString *str;
obj = qobject_from_json("true");
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QBOOL);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QBOOL);
qbool = qobject_to_qbool(obj);
fail_unless(qbool_get_int(qbool) != 0);
g_assert(qbool_get_int(qbool) != 0);
str = qobject_to_json(obj);
fail_unless(strcmp(qstring_get_str(str), "true") == 0);
g_assert(strcmp(qstring_get_str(str), "true") == 0);
QDECREF(str);
QDECREF(qbool);
obj = qobject_from_json("false");
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QBOOL);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QBOOL);
qbool = qobject_to_qbool(obj);
fail_unless(qbool_get_int(qbool) == 0);
g_assert(qbool_get_int(qbool) == 0);
str = qobject_to_json(obj);
fail_unless(strcmp(qstring_get_str(str), "false") == 0);
g_assert(strcmp(qstring_get_str(str), "false") == 0);
QDECREF(str);
QDECREF(qbool);
obj = qobject_from_jsonf("%i", false);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QBOOL);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QBOOL);
qbool = qobject_to_qbool(obj);
fail_unless(qbool_get_int(qbool) == 0);
g_assert(qbool_get_int(qbool) == 0);
QDECREF(qbool);
obj = qobject_from_jsonf("%i", true);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QBOOL);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QBOOL);
qbool = qobject_to_qbool(obj);
fail_unless(qbool_get_int(qbool) != 0);
g_assert(qbool_get_int(qbool) != 0);
QDECREF(qbool);
}
END_TEST
typedef struct LiteralQDictEntry LiteralQDictEntry;
typedef struct LiteralQObject LiteralQObject;
@ -426,7 +414,7 @@ static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs)
return 0;
}
START_TEST(simple_dict)
static void simple_dict(void)
{
int i;
struct {
@ -460,26 +448,25 @@ START_TEST(simple_dict)
QString *str;
obj = qobject_from_json(test_cases[i].encoded);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QDICT);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QDICT);
fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
str = qobject_to_json(obj);
qobject_decref(obj);
obj = qobject_from_json(qstring_get_str(str));
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QDICT);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QDICT);
fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
qobject_decref(obj);
QDECREF(str);
}
}
END_TEST
START_TEST(simple_list)
static void simple_list(void)
{
int i;
struct {
@ -524,26 +511,25 @@ START_TEST(simple_list)
QString *str;
obj = qobject_from_json(test_cases[i].encoded);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QLIST);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QLIST);
fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
str = qobject_to_json(obj);
qobject_decref(obj);
obj = qobject_from_json(qstring_get_str(str));
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QLIST);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QLIST);
fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
qobject_decref(obj);
QDECREF(str);
}
}
END_TEST
START_TEST(simple_whitespace)
static void simple_whitespace(void)
{
int i;
struct {
@ -593,27 +579,26 @@ START_TEST(simple_whitespace)
QString *str;
obj = qobject_from_json(test_cases[i].encoded);
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QLIST);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QLIST);
fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
str = qobject_to_json(obj);
qobject_decref(obj);
obj = qobject_from_json(qstring_get_str(str));
fail_unless(obj != NULL);
fail_unless(qobject_type(obj) == QTYPE_QLIST);
g_assert(obj != NULL);
g_assert(qobject_type(obj) == QTYPE_QLIST);
fail_unless(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
g_assert(compare_litqobj_to_qobj(&test_cases[i].decoded, obj) == 1);
qobject_decref(obj);
QDECREF(str);
}
}
END_TEST
START_TEST(simple_varargs)
static void simple_varargs(void)
{
QObject *embedded_obj;
QObject *obj;
@ -627,169 +612,122 @@ START_TEST(simple_varargs)
{}}));
embedded_obj = qobject_from_json("[32, 42]");
fail_unless(embedded_obj != NULL);
g_assert(embedded_obj != NULL);
obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
fail_unless(obj != NULL);
g_assert(obj != NULL);
fail_unless(compare_litqobj_to_qobj(&decoded, obj) == 1);
g_assert(compare_litqobj_to_qobj(&decoded, obj) == 1);
qobject_decref(obj);
}
END_TEST
START_TEST(empty_input)
static void empty_input(void)
{
const char *empty = "";
QObject *obj = qobject_from_json(empty);
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
START_TEST(unterminated_string)
static void unterminated_string(void)
{
QObject *obj = qobject_from_json("\"abc");
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
START_TEST(unterminated_sq_string)
static void unterminated_sq_string(void)
{
QObject *obj = qobject_from_json("'abc");
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
START_TEST(unterminated_escape)
static void unterminated_escape(void)
{
QObject *obj = qobject_from_json("\"abc\\\"");
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
START_TEST(unterminated_array)
static void unterminated_array(void)
{
QObject *obj = qobject_from_json("[32");
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
START_TEST(unterminated_array_comma)
static void unterminated_array_comma(void)
{
QObject *obj = qobject_from_json("[32,");
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
START_TEST(invalid_array_comma)
static void invalid_array_comma(void)
{
QObject *obj = qobject_from_json("[32,}");
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
START_TEST(unterminated_dict)
static void unterminated_dict(void)
{
QObject *obj = qobject_from_json("{'abc':32");
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
START_TEST(unterminated_dict_comma)
static void unterminated_dict_comma(void)
{
QObject *obj = qobject_from_json("{'abc':32,");
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
#if 0
START_TEST(invalid_dict_comma)
static void invalid_dict_comma(void)
{
QObject *obj = qobject_from_json("{'abc':32,}");
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
START_TEST(unterminated_literal)
static void unterminated_literal(void)
{
QObject *obj = qobject_from_json("nul");
fail_unless(obj == NULL);
g_assert(obj == NULL);
}
END_TEST
#endif
static Suite *qjson_suite(void)
int main(int argc, char **argv)
{
Suite *suite;
TCase *string_literals, *number_literals, *keyword_literals;
TCase *dicts, *lists, *whitespace, *varargs, *errors;
g_test_init(&argc, &argv, NULL);
string_literals = tcase_create("String Literals");
tcase_add_test(string_literals, simple_string);
tcase_add_test(string_literals, escaped_string);
tcase_add_test(string_literals, single_quote_string);
tcase_add_test(string_literals, vararg_string);
g_test_add_func("/literals/string/simple", simple_string);
g_test_add_func("/literals/string/escaped", escaped_string);
g_test_add_func("/literals/string/single_quote", single_quote_string);
g_test_add_func("/literals/string/vararg", vararg_string);
number_literals = tcase_create("Number Literals");
tcase_add_test(number_literals, simple_number);
tcase_add_test(number_literals, float_number);
tcase_add_test(number_literals, vararg_number);
g_test_add_func("/literals/number/simple", simple_number);
g_test_add_func("/literals/number/float", float_number);
g_test_add_func("/literals/number/vararg", vararg_number);
keyword_literals = tcase_create("Keywords");
tcase_add_test(keyword_literals, keyword_literal);
dicts = tcase_create("Objects");
tcase_add_test(dicts, simple_dict);
lists = tcase_create("Lists");
tcase_add_test(lists, simple_list);
g_test_add_func("/literals/keyword", keyword_literal);
whitespace = tcase_create("Whitespace");
tcase_add_test(whitespace, simple_whitespace);
g_test_add_func("/dicts/simple_dict", simple_dict);
g_test_add_func("/lists/simple_list", simple_list);
varargs = tcase_create("Varargs");
tcase_add_test(varargs, simple_varargs);
g_test_add_func("/whitespace/simple_whitespace", simple_whitespace);
errors = tcase_create("Invalid JSON");
tcase_add_test(errors, empty_input);
tcase_add_test(errors, unterminated_string);
tcase_add_test(errors, unterminated_escape);
tcase_add_test(errors, unterminated_sq_string);
tcase_add_test(errors, unterminated_array);
tcase_add_test(errors, unterminated_array_comma);
tcase_add_test(errors, invalid_array_comma);
tcase_add_test(errors, unterminated_dict);
tcase_add_test(errors, unterminated_dict_comma);
g_test_add_func("/varargs/simple_varargs", simple_varargs);
g_test_add_func("/errors/empty_input", empty_input);
g_test_add_func("/errors/unterminated/string", unterminated_string);
g_test_add_func("/errors/unterminated/escape", unterminated_escape);
g_test_add_func("/errors/unterminated/sq_string", unterminated_sq_string);
g_test_add_func("/errors/unterminated/array", unterminated_array);
g_test_add_func("/errors/unterminated/array_comma", unterminated_array_comma);
g_test_add_func("/errors/unterminated/dict", unterminated_dict);
g_test_add_func("/errors/unterminated/dict_comma", unterminated_dict_comma);
g_test_add_func("/errors/invalid_array_comma", invalid_array_comma);
#if 0
/* FIXME: this print parse error messages on stderr. */
tcase_add_test(errors, invalid_dict_comma);
tcase_add_test(errors, unterminated_literal);
g_test_add_func("/errors/invalid_dict_comma", invalid_dict_comma);
g_test_add_func("/errors/unterminated/literal", unterminated_literal);
#endif
suite = suite_create("QJSON test-suite");
suite_add_tcase(suite, string_literals);
suite_add_tcase(suite, number_literals);
suite_add_tcase(suite, keyword_literals);
suite_add_tcase(suite, dicts);
suite_add_tcase(suite, lists);
suite_add_tcase(suite, whitespace);
suite_add_tcase(suite, varargs);
suite_add_tcase(suite, errors);
return suite;
}
int main(void)
{
int nf;
Suite *s;
SRunner *sr;
s = qjson_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
nf = srunner_ntests_failed(sr);
srunner_free(sr);
return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
return g_test_run();
}

4
configure vendored
View File

@ -2800,9 +2800,9 @@ if test "$softmmu" = yes ; then
tools="qemu-ga\$(EXESUF) $tools"
fi
if [ "$check_utests" = "yes" ]; then
checks="check-qjson $checks"
checks="$checks"
fi
test_progs="$checks check-qdict check-qfloat check-qint check-qstring check-qlist test-coroutine test-qmp-output-visitor test-qmp-input-visitor"
test_progs="$checks check-qdict check-qfloat check-qint check-qstring check-qlist check-qjson test-coroutine test-qmp-output-visitor test-qmp-input-visitor"
fi
fi