grim/gplate

Changed the license to LGPL3+

2012-09-03, Gary Kramlich
c00565257208
Changed the license to LGPL3+
Got the okay from John Bailey via im.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <check.h>
#include <gplate/gplate.h>
#include "test.h"
#include "test-collection.h"
#include "test-object.h"
/******************************************************************************
* Structs
*****************************************************************************/
typedef struct {
const gchar *name;
const gchar *value;
} VariableData;
/******************************************************************************
* Globals
*****************************************************************************/
static GPlateVariable *dict = NULL;
static GPlateIterator *iter = NULL;
/******************************************************************************
* Helpers
*****************************************************************************/
static void
iterator_test(VariableData *vd) {
gint i = 0, count = 0;
/* add all the values and make sure they got entered correctly */
for(i = 0, count = 0; vd[i].name; i++, count++) {
gplate_collection_add_string(dict, vd[i].name, vd[i].value);
test_collection_lookup(GPLATE_COLLECTION(dict),
vd[i].name, vd[i].value);
}
i = 0;
iter = gplate_collection_get_iterator(GPLATE_COLLECTION(dict));
while(gplate_iterator_has_next(iter)) {
GPlateVariable *var = gplate_iterator_next(iter);
const gchar *value = gplate_variable_get_value(var);
test_string(vd[i++].value, value);
}
fail_unless(i == count, "Iterated over %d/%d elements.", i, count);
}
/******************************************************************************
* Fixtures
*****************************************************************************/
static void
collection_interface_setup(void) {
dict = gplate_dictionary_variable_new("dict");
}
static void
collection_interface_teardown(void) {
g_object_unref(G_OBJECT(dict));
if(iter) {
g_object_unref(G_OBJECT(iter));
iter = NULL;
}
}
/******************************************************************************
* Object Hierarchy Tests
*****************************************************************************/
START_TEST(test_dictionary_hierarchy_type)
test_object_is_a(G_OBJECT(dict), GPLATE_TYPE_DICTIONARY_VARIABLE);
END_TEST
START_TEST(test_dictionary_hierarchy_parent)
test_object_is_a(G_OBJECT(dict), GPLATE_TYPE_VARIABLE);
END_TEST
START_TEST(test_dictionary_hierarchy_collection)
test_object_is_a(G_OBJECT(dict), GPLATE_TYPE_COLLECTION);
END_TEST
/******************************************************************************
* Collection Interface Tests
*****************************************************************************/
START_TEST(test_dictionary_add_string_null)
gplate_collection_add_string(dict, "null", NULL);
test_collection_lookup(GPLATE_COLLECTION(dict), "null", NULL);
END_TEST
START_TEST(test_dictionary_add_string_empty)
gplate_collection_add_string(dict, "empty", "");
test_collection_lookup(GPLATE_COLLECTION(dict), "empty", "");
END_TEST
START_TEST(test_dictionary_add_string)
gplate_collection_add_string(dict, "string", "string");
test_collection_lookup(GPLATE_COLLECTION(dict), "string", "string");
END_TEST
START_TEST(test_dictionary_add_boolean_true)
gplate_collection_add_boolean(dict, "true", TRUE);
test_collection_lookup(GPLATE_COLLECTION(dict), "true", "TRUE");
END_TEST
START_TEST(test_dictionary_add_boolean_false)
gplate_collection_add_boolean(dict, "false", FALSE);
test_collection_lookup(GPLATE_COLLECTION(dict), "false", "FALSE");
END_TEST
START_TEST(test_dictionary_add_float_positive)
gplate_collection_add_float(dict, "pi", 3.14f);
test_collection_lookup(GPLATE_COLLECTION(dict), "pi", "3.140000");
END_TEST
START_TEST(test_dictionary_add_float_positive_whole)
gplate_collection_add_float(dict, "three", 3.0f);
test_collection_lookup(GPLATE_COLLECTION(dict), "three", "3.000000");
END_TEST
START_TEST(test_dictionary_add_float_negative)
gplate_collection_add_float(dict, "negativepi", -3.14f);
test_collection_lookup(GPLATE_COLLECTION(dict), "negativepi", "-3.140000");
END_TEST
START_TEST(test_dictionary_add_float_negative_whole)
gplate_collection_add_float(dict, "negativethree", -3.0f);
test_collection_lookup(GPLATE_COLLECTION(dict), "negativethree", "-3.000000");
END_TEST
START_TEST(test_dictionary_add_double_positive)
gplate_collection_add_double(dict, "e", 2.71);
test_collection_lookup(GPLATE_COLLECTION(dict), "e", "2.71");
END_TEST
START_TEST(test_dictionary_add_double_positive_whole)
gplate_collection_add_double(dict, "two", 2.0);
test_collection_lookup(GPLATE_COLLECTION(dict), "two", "2");
END_TEST
START_TEST(test_dictionary_add_double_negative)
gplate_collection_add_double(dict, "negativee", -2.71);
test_collection_lookup(GPLATE_COLLECTION(dict), "negativee", "-2.71");
END_TEST
START_TEST(test_dictionary_add_double_negative_whole)
gplate_collection_add_double(dict, "negativetwo", -2.0);
test_collection_lookup(GPLATE_COLLECTION(dict), "negativetwo", "-2");
END_TEST
START_TEST(test_dictionary_nested)
GPlateVariable *nested = gplate_dictionary_variable_new("nested");
gplate_collection_add_variable(GPLATE_COLLECTION(dict), nested);
gplate_collection_add_string(GPLATE_COLLECTION(nested), "nestedstring",
"nested");
test_collection_lookup(GPLATE_COLLECTION(dict), "nested.nestedstring",
"nested");
END_TEST
START_TEST(test_dictionary_iterator_empty)
VariableData vd[] = {
{ NULL, NULL },
};
iterator_test(vd);
END_TEST
START_TEST(test_dictionary_iterator_one_element)
VariableData vd[] = {
{ "one", "1" },
{ NULL, NULL },
};
iterator_test(vd);
END_TEST
START_TEST(test_dictionary_iterator_many_elements_sorted)
VariableData vd[] = {
{ "eight", "8" },
{ "five", "5" },
{ "four", "4" },
{ "nine", "9" },
{ "one", "1" },
{ "seven", "7" },
{ "six", "6" },
{ "ten", "10" },
{ "three", "3" },
{ "two", "2" },
{ NULL, NULL },
};
iterator_test(vd);
END_TEST
/******************************************************************************
* API
*****************************************************************************/
Suite *
dictionary_variable_suite(void) {
Suite *s = suite_create("Dictionary Variable Suite");
TCase *tc = NULL;
/* object heirarchy */
tc = tcase_create("Object hierarchy");
tcase_add_checked_fixture(tc, collection_interface_setup,
collection_interface_teardown);
tcase_add_test(tc, test_dictionary_hierarchy_type);
tcase_add_test(tc, test_dictionary_hierarchy_parent);
tcase_add_test(tc, test_dictionary_hierarchy_collection);
suite_add_tcase(s, tc);
/* collection interface */
tc = tcase_create("Collection Interface");
tcase_add_checked_fixture(tc, collection_interface_setup,
collection_interface_teardown);
tcase_add_test(tc, test_dictionary_add_string_null);
tcase_add_test(tc, test_dictionary_add_string_empty);
tcase_add_test(tc, test_dictionary_add_string);
tcase_add_test(tc, test_dictionary_add_boolean_true);
tcase_add_test(tc, test_dictionary_add_boolean_false);
tcase_add_test(tc, test_dictionary_add_float_positive);
tcase_add_test(tc, test_dictionary_add_float_positive_whole);
tcase_add_test(tc, test_dictionary_add_float_negative);
tcase_add_test(tc, test_dictionary_add_float_negative_whole);
tcase_add_test(tc, test_dictionary_add_double_positive);
tcase_add_test(tc, test_dictionary_add_double_positive_whole);
tcase_add_test(tc, test_dictionary_add_double_negative);
tcase_add_test(tc, test_dictionary_add_double_negative_whole);
tcase_add_test(tc, test_dictionary_nested);
tcase_add_test(tc, test_dictionary_iterator_empty);
tcase_add_test(tc, test_dictionary_iterator_one_element);
tcase_add_test(tc, test_dictionary_iterator_many_elements_sorted);
suite_add_tcase(s, tc);
return s;
}