test_literal_gvalue.c (7080B) - raw
1 2 /* 3 * test_literal_gvalue.c, part of glibrdf 4 * Copyright 2012 Dan Callaghan <djc@djc.id.au> 5 * Licensed under GPLv3 6 */ 7 8 #include "glibrdf.h" 9 10 #define FIXTURE_TYPE LiteralGValueFixture 11 #define SETUP literal_gvalue_setup 12 #define TEARDOWN literal_gvalue_teardown 13 #define DATA const void *user_data G_GNUC_UNUSED 14 #define PACKAGE "/literal_gvalue/" 15 #define TEST_ADD(func) g_test_add(PACKAGE #func, FIXTURE_TYPE, NULL, SETUP, func, TEARDOWN) 16 17 typedef struct { 18 librdf_world *world; 19 librdf_storage *storage; 20 librdf_model *model; 21 } FIXTURE_TYPE; 22 23 static void SETUP(FIXTURE_TYPE *fixture, DATA) { 24 fixture->world = librdf_new_world(); 25 g_assert(fixture->world != NULL); 26 librdf_world_open(fixture->world); 27 fixture->storage = librdf_new_storage(fixture->world, 28 "hashes", NULL, "hash-type='memory'"); 29 g_assert(fixture->storage != NULL); 30 fixture->model = librdf_new_model(fixture->world, fixture->storage, ""); 31 g_assert(fixture->model != NULL); 32 33 librdf_parser *parser = librdf_new_parser(fixture->world, 34 NULL, "application/rdf+xml", NULL); 35 g_assert(parser != NULL); 36 librdf_uri *source_uri = librdf_new_uri_from_filename( 37 fixture->world, "test-data.xml"); 38 g_assert(source_uri != NULL); 39 int parse_error = librdf_parser_parse_into_model(parser, 40 source_uri, NULL, fixture->model); 41 g_assert(!parse_error); 42 librdf_free_uri(source_uri); 43 librdf_free_parser(parser); 44 } 45 46 static void TEARDOWN(FIXTURE_TYPE *fixture, DATA) { 47 librdf_free_model(fixture->model); 48 librdf_free_storage(fixture->storage); 49 librdf_free_world(fixture->world); 50 } 51 52 static void test_untyped(FIXTURE_TYPE *fixture, DATA) { 53 librdf_node *subj = librdf_new_node_from_uri_string(fixture->world, 54 (const unsigned char *)"http://example.com/Resource"); 55 g_assert(subj != NULL); 56 librdf_node *prop = librdf_new_node_from_uri_string(fixture->world, 57 (const unsigned char *)"http://example.com/untyped"); 58 g_assert(prop != NULL); 59 librdf_node *obj = librdf_model_get_target(fixture->model, subj, prop); 60 g_assert(obj != NULL); 61 GValue value = {0}; 62 librdf_node_get_literal_gvalue(obj, NULL, &value); 63 g_assert(G_VALUE_HOLDS(&value, G_TYPE_STRING)); 64 g_assert_cmpstr(g_value_get_string(&value), ==, "blah"); 65 g_value_unset(&value); 66 librdf_free_node(obj); 67 librdf_free_node(prop); 68 librdf_free_node(subj); 69 } 70 71 static void test_integer(FIXTURE_TYPE *fixture, DATA) { 72 librdf_node *subj = librdf_new_node_from_uri_string(fixture->world, 73 (const unsigned char *)"http://example.com/Resource"); 74 g_assert(subj != NULL); 75 librdf_node *prop = librdf_new_node_from_uri_string(fixture->world, 76 (const unsigned char *)"http://example.com/integer"); 77 g_assert(prop != NULL); 78 librdf_node *obj = librdf_model_get_target(fixture->model, subj, prop); 79 g_assert(obj != NULL); 80 GValue value = {0}; 81 librdf_node_get_literal_gvalue(obj, NULL, &value); 82 g_assert(G_VALUE_HOLDS(&value, G_TYPE_INT64)); 83 g_assert_cmpint(g_value_get_int64(&value), ==, 123); 84 g_value_unset(&value); 85 librdf_free_node(obj); 86 librdf_free_node(prop); 87 librdf_free_node(subj); 88 } 89 90 static void test_date(FIXTURE_TYPE *fixture, DATA) { 91 librdf_node *subj = librdf_new_node_from_uri_string(fixture->world, 92 (const unsigned char *)"http://example.com/Resource"); 93 g_assert(subj != NULL); 94 librdf_node *prop = librdf_new_node_from_uri_string(fixture->world, 95 (const unsigned char *)"http://example.com/date"); 96 g_assert(prop != NULL); 97 librdf_node *obj = librdf_model_get_target(fixture->model, subj, prop); 98 g_assert(obj != NULL); 99 GValue value = {0}; 100 librdf_node_get_literal_gvalue(obj, NULL, &value); 101 g_assert(G_VALUE_HOLDS(&value, G_TYPE_DATE)); 102 GDate *date = (GDate *)g_value_get_boxed(&value); 103 g_assert_cmpuint(g_date_get_year(date), ==, 1986); 104 g_assert_cmpuint(g_date_get_month(date), ==, 8); 105 g_assert_cmpuint(g_date_get_day(date), ==, 16); 106 g_value_unset(&value); 107 librdf_free_node(obj); 108 librdf_free_node(prop); 109 librdf_free_node(subj); 110 } 111 112 static void test_datetime(FIXTURE_TYPE *fixture, DATA) { 113 librdf_node *subj = librdf_new_node_from_uri_string(fixture->world, 114 (const unsigned char *)"http://example.com/Resource"); 115 g_assert(subj != NULL); 116 librdf_node *prop = librdf_new_node_from_uri_string(fixture->world, 117 (const unsigned char *)"http://example.com/datetime"); 118 g_assert(prop != NULL); 119 librdf_node *obj = librdf_model_get_target(fixture->model, subj, prop); 120 g_assert(obj != NULL); 121 GValue value = {0}; 122 librdf_node_get_literal_gvalue(obj, NULL, &value); 123 g_assert(G_VALUE_HOLDS(&value, G_TYPE_DATE_TIME)); 124 GDateTime *datetime = (GDateTime *)g_value_get_boxed(&value); 125 g_assert_cmpint(g_date_time_get_year(datetime), ==, 2012); 126 g_assert_cmpint(g_date_time_get_month(datetime), ==, 7); 127 g_assert_cmpint(g_date_time_get_day_of_month(datetime), ==, 22); 128 g_assert_cmpint(g_date_time_get_hour(datetime), ==, 15); 129 g_assert_cmpint(g_date_time_get_minute(datetime), ==, 57); 130 g_assert_cmpfloat(g_date_time_get_seconds(datetime), ==, 41.0); 131 g_assert_cmpint(g_date_time_get_utc_offset(datetime), ==, 132 /* 10 hours in us */ 10 * 60 * 60 * 1000000LL); 133 g_value_unset(&value); 134 librdf_free_node(obj); 135 librdf_free_node(prop); 136 librdf_free_node(subj); 137 } 138 139 static void custom_adaptor(const gchar *lv, GValue *value_out) { 140 g_value_init(value_out, G_TYPE_STRING); 141 g_value_set_static_string(value_out, "custom"); 142 return; 143 } 144 static librdf_gvalue_adaptor_func custom_adaptor_map(librdf_uri *datatype_uri) { 145 const gchar *datatype_uri_string = 146 (const gchar *)librdf_uri_as_string(datatype_uri); 147 if (g_strcmp0(datatype_uri_string, "http://example.com/customType") == 0) 148 return custom_adaptor; 149 return librdf_default_gvalue_adaptor_map(datatype_uri); 150 } 151 static void test_custom_adaptor(FIXTURE_TYPE *fixture, DATA) { 152 librdf_node *subj = librdf_new_node_from_uri_string(fixture->world, 153 (const unsigned char *)"http://example.com/Resource"); 154 g_assert(subj != NULL); 155 librdf_node *prop = librdf_new_node_from_uri_string(fixture->world, 156 (const unsigned char *)"http://example.com/custom"); 157 g_assert(prop != NULL); 158 librdf_node *obj = librdf_model_get_target(fixture->model, subj, prop); 159 g_assert(obj != NULL); 160 GValue value = {0}; 161 librdf_node_get_literal_gvalue(obj, custom_adaptor_map, &value); 162 g_assert(G_VALUE_HOLDS(&value, G_TYPE_STRING)); 163 g_assert_cmpstr(g_value_get_string(&value), ==, "custom"); 164 g_value_unset(&value); 165 librdf_free_node(obj); 166 librdf_free_node(prop); 167 librdf_free_node(subj); 168 } 169 170 void add_literal_gvalue_tests(void) { 171 TEST_ADD(test_untyped); 172 TEST_ADD(test_integer); 173 TEST_ADD(test_date); 174 TEST_ADD(test_datetime); 175 TEST_ADD(test_custom_adaptor); 176 }