/* Copyright 2011-2020 David Robillard Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #undef NDEBUG #include "serd/serd.h" #include #include #include #include #include #include #include #include #ifndef INFINITY # define INFINITY (DBL_MAX + DBL_MAX) #endif #ifndef NAN # define NAN (INFINITY - INFINITY) #endif #define NS_XSD "http://www.w3.org/2001/XMLSchema#" static void test_strtod(double dbl, double max_delta) { char buf[1024]; snprintf(buf, sizeof(buf), "%f", dbl); char* endptr = NULL; const double out = serd_strtod(buf, &endptr); const double diff = fabs(out - dbl); assert(diff <= max_delta); } static void test_string_to_double(void) { const double expt_test_nums[] = { 2.0E18, -5e19, +8e20, 2e+24, -5e-5, 8e0, 9e-0, 2e+0}; const char* expt_test_strs[] = { "02e18", "-5e019", "+8e20", "2E+24", "-5E-5", "8E0", "9e-0", " 2e+0"}; for (size_t i = 0; i < sizeof(expt_test_nums) / sizeof(double); ++i) { const double num = serd_strtod(expt_test_strs[i], NULL); const double delta = fabs(num - expt_test_nums[i]); assert(delta <= DBL_EPSILON); test_strtod(expt_test_nums[i], DBL_EPSILON); } } static void test_double_to_node(void) { const double dbl_test_nums[] = {0.0, 9.0, 10.0, .01, 2.05, -16.00001, 5.000000005, 0.0000000001, NAN, INFINITY}; const char* dbl_test_strs[] = {"0.0", "9.0", "10.0", "0.01", "2.05", "-16.00001", "5.00000001", "0.0", NULL, NULL}; for (size_t i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) { SerdNode* node = serd_new_decimal(dbl_test_nums[i], 8, NULL); const char* node_str = node ? serd_node_string(node) : NULL; const bool pass = (node_str && dbl_test_strs[i]) ? !strcmp(node_str, dbl_test_strs[i]) : (node_str == dbl_test_strs[i]); assert(pass); const size_t len = node_str ? strlen(node_str) : 0; assert((!node && len == 0) || serd_node_length(node) == len); if (node) { const SerdNode* const datatype = serd_node_datatype(node); assert(datatype); assert(!dbl_test_strs[i] || !strcmp(serd_node_string(datatype), NS_XSD "decimal")); serd_node_free(node); } } } static void test_integer_to_node(void) { const long int_test_nums[] = {0, -0, -23, 23, -12340, 1000, -1000}; const char* int_test_strs[] = { "0", "0", "-23", "23", "-12340", "1000", "-1000"}; for (size_t i = 0; i < sizeof(int_test_nums) / sizeof(double); ++i) { SerdNode* node = serd_new_integer(int_test_nums[i], NULL); const char* node_str = serd_node_string(node); assert(!strcmp(node_str, int_test_strs[i])); const size_t len = strlen(node_str); assert(serd_node_length(node) == len); const SerdNode* const datatype = serd_node_datatype(node); assert(datatype); assert(!strcmp(serd_node_string(datatype), NS_XSD "integer")); serd_node_free(node); } } static void test_blob_to_node(void) { assert(!serd_new_blob(&SERD_URI_NULL, 0, false, NULL)); for (size_t size = 1; size < 256; ++size) { uint8_t* const data = (uint8_t*)malloc(size); for (size_t i = 0; i < size; ++i) { data[i] = (uint8_t)((size + i) % 256); } size_t out_size = 0; SerdNode* blob = serd_new_blob(data, size, size % 5, NULL); const char* blob_str = serd_node_string(blob); uint8_t* out = (uint8_t*)serd_base64_decode(blob_str, serd_node_length(blob), &out_size); assert(serd_node_length(blob) == strlen(blob_str)); assert(out_size == size); for (size_t i = 0; i < size; ++i) { assert(out[i] == data[i]); } const SerdNode* const datatype = serd_node_datatype(blob); assert(datatype); assert(!strcmp(serd_node_string(datatype), NS_XSD "base64Binary")); serd_node_free(blob); serd_free(out); free(data); } } static void test_node_equals(void) { static const uint8_t replacement_char_str[] = {0xEF, 0xBF, 0xBD, 0}; static const SerdStringView replacement_char = { (const char*)replacement_char_str, 3}; SerdNode* lhs = serd_new_string(replacement_char); SerdNode* rhs = serd_new_string(SERD_STATIC_STRING("123")); assert(!serd_node_equals(lhs, rhs)); SerdNode* const qnode = serd_new_curie(SERD_STATIC_STRING("foo:bar")); assert(!serd_node_equals(lhs, qnode)); serd_node_free(qnode); assert(!serd_node_copy(NULL)); serd_node_free(lhs); serd_node_free(rhs); } static void test_node_from_string(void) { SerdNode* const hello = serd_new_string(SERD_STATIC_STRING("hello\"")); assert(serd_node_length(hello) == 6); assert(serd_node_flags(hello) == SERD_HAS_QUOTE); assert(!strncmp(serd_node_string(hello), "hello\"", 6)); serd_node_free(hello); } static void test_node_from_substring(void) { SerdNode* const a_b = serd_new_string(SERD_STRING_VIEW("a\"bc", 3)); assert(serd_node_length(a_b) == 3); assert(serd_node_flags(a_b) == SERD_HAS_QUOTE); assert(strlen(serd_node_string(a_b)) == 3); assert(!strncmp(serd_node_string(a_b), "a\"b", 3)); serd_node_free(a_b); } static void test_simple_node(void) { assert(!serd_new_simple_node(SERD_LITERAL, SERD_STATIC_STRING("Literal"))); } static void test_literal(void) { SerdNode* hello2 = serd_new_string(SERD_STATIC_STRING("hello\"")); assert(serd_node_length(hello2) == 6 && serd_node_flags(hello2) == SERD_HAS_QUOTE && !strcmp(serd_node_string(hello2), "hello\"")); SerdNode* hello3 = serd_new_plain_literal(SERD_STATIC_STRING("hello\""), SERD_EMPTY_STRING()); assert(serd_node_equals(hello2, hello3)); SerdNode* hello4 = serd_new_typed_literal(SERD_STATIC_STRING("hello\""), SERD_EMPTY_STRING()); assert(serd_node_equals(hello4, hello2)); serd_node_free(hello4); serd_node_free(hello3); serd_node_free(hello2); const char* lang_lit_str = "\"Hello\"@en"; SerdNode* sliced_lang_lit = serd_new_plain_literal(SERD_STRING_VIEW(lang_lit_str + 1, 5), SERD_STRING_VIEW(lang_lit_str + 8, 2)); assert(!strcmp(serd_node_string(sliced_lang_lit), "Hello")); const SerdNode* const lang = serd_node_language(sliced_lang_lit); assert(lang); assert(!strcmp(serd_node_string(lang), "en")); serd_node_free(sliced_lang_lit); const char* type_lit_str = "\"Hallo\"^^"; SerdNode* sliced_type_lit = serd_new_typed_literal(SERD_STRING_VIEW(type_lit_str + 1, 5), SERD_STRING_VIEW(type_lit_str + 10, 27)); assert(!strcmp(serd_node_string(sliced_type_lit), "Hallo")); const SerdNode* const datatype = serd_node_datatype(sliced_type_lit); assert(datatype); assert(!strcmp(serd_node_string(datatype), "http://example.org/Greeting")); serd_node_free(sliced_type_lit); SerdNode* const plain_lit = serd_new_plain_literal(SERD_STATIC_STRING("Plain"), SERD_EMPTY_STRING()); assert(!strcmp(serd_node_string(plain_lit), "Plain")); serd_node_free(plain_lit); } static void test_blank(void) { SerdNode* blank = serd_new_blank(SERD_STATIC_STRING("b0")); assert(serd_node_length(blank) == 2); assert(serd_node_flags(blank) == 0); assert(!strcmp(serd_node_string(blank), "b0")); serd_node_free(blank); } int main(void) { test_string_to_double(); test_double_to_node(); test_integer_to_node(); test_blob_to_node(); test_node_equals(); test_node_from_string(); test_node_from_substring(); test_simple_node(); test_literal(); test_blank(); printf("Success\n"); return 0; }