From e2731cf7008b2f1f9e1f44283b07c3fe0296bbee Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 17 Jul 2022 17:33:37 -0400 Subject: Switch to meson build system --- .gitignore | 2 - .gitmodules | 3 - INSTALL | 66 ---- INSTALL.md | 70 ++++ NEWS | 3 +- doc/meson.build | 36 ++ doc/reference.doxygen.in | 4 +- meson.build | 154 +++++++++ meson/library/meson.build | 31 ++ meson/suppressions/meson.build | 92 +++++ meson/warnings/meson.build | 175 ++++++++++ meson_options.txt | 14 + sord.pc.in | 11 - src/sord.c | 2 +- src/sord_test.c | 769 ----------------------------------------- test/meson.build | 22 ++ test/test_sord.c | 769 +++++++++++++++++++++++++++++++++++++++++ waf | 27 -- waflib | 1 - wscript | 413 ---------------------- 20 files changed, 1368 insertions(+), 1296 deletions(-) delete mode 100644 .gitmodules delete mode 100644 INSTALL create mode 100644 INSTALL.md create mode 100644 doc/meson.build create mode 100644 meson.build create mode 100644 meson/library/meson.build create mode 100644 meson/suppressions/meson.build create mode 100644 meson/warnings/meson.build create mode 100644 meson_options.txt delete mode 100644 sord.pc.in delete mode 100644 src/sord_test.c create mode 100644 test/meson.build create mode 100644 test/test_sord.c delete mode 100755 waf delete mode 160000 waflib delete mode 100644 wscript diff --git a/.gitignore b/.gitignore index 204c242..467d5b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ build/** -.waf-* -.lock-waf* __pycache__ diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index b2babe7..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "waflib"] - path = waflib - url = ../autowaf.git diff --git a/INSTALL b/INSTALL deleted file mode 100644 index 9b54f51..0000000 --- a/INSTALL +++ /dev/null @@ -1,66 +0,0 @@ -Installation Instructions -========================= - -Basic Installation ------------------- - -Building this software requires only Python. To install with default options: - - ./waf configure - ./waf - ./waf install # or sudo ./waf install - -Configuration Options ---------------------- - -All supported options can be viewed using the command: - - ./waf --help - -Most options only need to be passed during the configure stage, for example: - - ./waf configure --prefix=/usr - ./waf - ./waf install - -Compiler Configuration ----------------------- - -Several standard environment variables can be used to control how compilers are -invoked: - - * CC: Path to C compiler - * CFLAGS: C compiler options - * CXX: Path to C++ compiler - * CXXFLAGS: C++ compiler options - * CPPFLAGS: C preprocessor options - * LINKFLAGS: Linker options - -Library Versioning ------------------- - -This library uses semantic versioning . - -Several major versions can be installed in parallel. The shared library name, -include directory, and pkg-config file are suffixed with the major version -number. For example, a library named "foo" at version 1.x.y might install: - - /usr/include/foo-1/foo/foo.h - /usr/lib/foo-1.so.1.x.y - /usr/lib/pkgconfig/foo-1.pc - -Dependencies can check for the package "foo-1" with pkg-config. - -Packaging ---------- - -Everything can be installed to a specific root directory by passing a --destdir -option to the install stage (or setting the DESTDIR environment variable), -which adds a prefix to all install paths. For example: - - ./waf configure --prefix=/usr - ./waf - ./waf install --destdir=/tmp/package - -Packages should allow parallel installation of several major versions. For -example, the above would be packaged as "foo-1". \ No newline at end of file diff --git a/INSTALL.md b/INSTALL.md new file mode 100644 index 0000000..7109c35 --- /dev/null +++ b/INSTALL.md @@ -0,0 +1,70 @@ +Installation Instructions +========================= + +Prerequisites +------------- + +To build from source, you will need: + + * A relatively modern C compiler (GCC, Clang, and MSVC are known to work). + + * [Meson](http://mesonbuild.com/), which depends on + [Python](http://python.org/). + +This is a brief overview of building this project with meson. See the meson +documentation for more detailed information. + +Configuration +------------- + +The build is configured with the `setup` command, which creates a new build +directory with the given name: + + meson setup build + +Some environment variables are read during `setup` and stored with the +configuration: + + * `CC`: Path to C compiler. + * `CFLAGS`: C compiler options. + * `LDFLAGS`: Linker options. + +However, it is better to use meson options for configuration. All options can +be inspected with the `configure` command from within the build directory: + + cd build + meson configure + +Options can be set by passing C-style "define" options to `configure`: + + meson configure -Dc_args="-march=native" -Dprefix="/opt/mypackage/" + +Building +-------- + +From within a configured build directory, everything can be built with the +`compile` command: + + meson compile + +Similarly, tests can be run with the `test` command: + + meson test + +Meson can also generate a project for several popular IDEs, see the `backend` +option for details. + +Installation +------------ + +A compiled project can be installed with the `install` command: + + meson install + +You may need to acquire root permissions to install to a system-wide prefix. +For packaging, the installation may be staged to a directory using the +`DESTDIR` environment variable or the `--destdir` option: + + DESTDIR=/tmp/mypackage/ meson install + + meson install --destdir=/tmp/mypackage/ diff --git a/NEWS b/NEWS index bf55edc..19a82bc 100644 --- a/NEWS +++ b/NEWS @@ -1,8 +1,9 @@ sord (0.16.11) unstable; * Fix various warnings + * Switch to meson build system - -- David Robillard Sun, 17 Jul 2022 21:21:11 +0000 + -- David Robillard Sun, 17 Jul 2022 21:22:04 +0000 sord (0.16.10) stable; diff --git a/doc/meson.build b/doc/meson.build new file mode 100644 index 0000000..a354f30 --- /dev/null +++ b/doc/meson.build @@ -0,0 +1,36 @@ +docdir = get_option('datadir') / 'doc' + +doxygen = find_program('doxygen', required: get_option('docs')) + +build_docs = doxygen.found() + +if build_docs + config = configuration_data() + config.set('SORD_VERSION', meson.project_version()) + config.set('SORD_SRCDIR', sord_src_root) + config.set('DOX_OUTPUT', meson.current_build_dir()) + + c_doxyfile = configure_file( + configuration: config, + input: files('reference.doxygen.in'), + output: 'reference.doxygen', + ) + + custom_target( + 'html', + build_by_default: true, + command: [doxygen, '@INPUT0@'], + input: [c_doxyfile] + c_headers, + install: true, + install_dir: docdir / versioned_name, + output: 'html', + ) +endif + +if not get_option('docs').disabled() + install_man(files('sord_validate.1', 'sordi.1')) +endif + +if not meson.is_subproject() + summary('API Documentation', build_docs, bool_yn: true) +endif diff --git a/doc/reference.doxygen.in b/doc/reference.doxygen.in index 9a685c5..a9e62eb 100644 --- a/doc/reference.doxygen.in +++ b/doc/reference.doxygen.in @@ -58,7 +58,7 @@ PROJECT_LOGO = # entered, it will be relative to the location where doxygen was started. If # left blank the current directory will be used. -OUTPUT_DIRECTORY = . +OUTPUT_DIRECTORY = @DOX_OUTPUT@ # If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub- # directories (in 2 levels) under the output directory of each output format and @@ -1788,7 +1788,7 @@ RTF_EXTENSIONS_FILE = # classes and files. # The default value is: NO. -GENERATE_MAN = YES +GENERATE_MAN = NO # The MAN_OUTPUT tag is used to specify where the man pages will be put. If a # relative path is entered the value of OUTPUT_DIRECTORY will be put in front of diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..9a77653 --- /dev/null +++ b/meson.build @@ -0,0 +1,154 @@ +# Copyright 2021-2022 David Robillard +# SPDX-License-Identifier: CC0-1.0 OR ISC + +project('sord', ['c'], + version: '0.16.11', + license: 'ISC', + meson_version: '>= 0.56.0', + default_options: [ + 'b_ndebug=if-release', + 'buildtype=release', + 'c_std=c99', + ]) + +sord_src_root = meson.current_source_dir() +major_version = meson.project_version().split('.')[0] +version_suffix = '-@0@'.format(major_version) +versioned_name = 'sord' + version_suffix + +####################### +# Compilers and Flags # +####################### + +# Required tools +pkg = import('pkgconfig') +cc = meson.get_compiler('c') + +# Set global warning flags +if get_option('strict') and not meson.is_subproject() + subdir('meson/warnings') +endif +subdir('meson/suppressions') + +# Build as C++ with MSVC +if cc.get_id() == 'msvc' + add_project_arguments(['/TP'], language: ['c']) +endif + +################ +# Dependencies # +################ + +m_dep = cc.find_library('m', required: false) + +serd_dep = dependency('serd-0', + version: '>= 0.30.9', + fallback: ['serd', 'serd_dep']) + +########### +# Library # +########### + +c_headers = files('include/sord/sord.h') +cpp_headers = files('include/sord/sordmm.hpp') + +sources = files( + 'src/sord.c', + 'src/syntax.c', +) + +# Set appropriate arguments for building against the library type +extra_c_args = [] +subdir('meson/library') +if get_option('default_library') == 'static' + extra_c_args = ['-DSORD_STATIC'] +endif + +# Build shared and/or static library +libsord = library( + meson.project_name() + library_suffix, + sources, + c_args: c_suppressions + extra_c_args + [ + '-DSORD_INTERNAL', + '-DSORD_VERSION="@0@"'.format(meson.project_version()), + ], + dependencies: [m_dep, serd_dep], + gnu_symbol_visibility: 'hidden', + include_directories: include_directories('include', 'src'), + install: true, + version: meson.project_version(), +) + +# Declare dependency for internal meson dependants +sord_dep = declare_dependency( + compile_args: extra_c_args, + dependencies: [m_dep, serd_dep], + include_directories: include_directories('include'), + link_with: libsord, +) + +# Generage pkg-config file for external dependants +pkg.generate( + libsord, + description: 'Lightweight C library for storing RDF in memory', + extra_cflags: extra_c_args, + filebase: versioned_name, + name: 'Sord', + subdirs: [versioned_name], + version: meson.project_version(), +) + +# Install headers to a versioned include directory +install_headers(c_headers, subdir: versioned_name / 'sord') +install_headers(cpp_headers, subdir: versioned_name / 'sord') + +######### +# Tools # +######### + +# Build sordi command line utility +if not get_option('tools').disabled() + sordi = executable('sordi', + files('src/sordi.c'), + c_args: c_suppressions, + install: true, + dependencies: sord_dep) + + pcre_dep = dependency('libpcre', required: false) + + if pcre_dep.found() + sordi = executable('sord_validate', + files('src/sord_validate.c'), + c_args: c_suppressions, + install: true, + dependencies: [sord_dep, pcre_dep]) + endif + + if not get_option('docs').disabled() + install_man(files('doc/sordi.1')) + install_man(files('doc/sord_validate.1')) + endif +endif + +if not get_option('docs').disabled() + subdir('doc') +endif + +if not get_option('tests').disabled() + subdir('test') +endif + +if not meson.is_subproject() + summary('Tests', not get_option('tests').disabled(), bool_yn: true) + summary('Utilities', not get_option('tools').disabled(), bool_yn: true) + + summary('Install prefix', get_option('prefix')) + + summary('Headers', get_option('prefix') / get_option('includedir')) + summary('Libraries', get_option('prefix') / get_option('libdir')) + + if not get_option('tools').disabled() + summary('Executables', get_option('prefix') / get_option('bindir')) + summary('Man pages', get_option('prefix') / get_option('mandir')) + endif +endif diff --git a/meson/library/meson.build b/meson/library/meson.build new file mode 100644 index 0000000..fffc831 --- /dev/null +++ b/meson/library/meson.build @@ -0,0 +1,31 @@ +# Copyright 2020-2022 David Robillard +# SPDX-License-Identifier: CC0-1.0 OR ISC + +# General definitions for building libraries. +# +# These are essentially workarounds for Meson/Windows/MSVC. Unfortunately, +# Meson's default_library option doesn't support shared and static builds very +# well. In particular, it's often necessary to define different symbols for +# static and shared builds of libraries so that symbols can be exported. To +# work around this, default_library=both isn't supported on Windows. On other +# platforms with GCC-like compilers, we can support both because symbols can +# safely be exported in the same way (giving them default visibility) in both +# static and shared builds. + +default_library = get_option('default_library') +host_system = host_machine.system() + +# Abort on Windows with default_library=both +if host_system == 'windows' and default_library == 'both' + error('default_library=both is not supported on Windows') +endif + +# Set library_suffix to the suffix for libraries +if host_system == 'windows' and default_library == 'shared' + # Meson appends a version to the name only for DLLs, which leads to + # inconsistent library names, like `mylib-1-1`. So, provide no suffix to + # ultimately get the same name as on other platforms, like `mylib-1`. + library_suffix = '' +else + library_suffix = '-@0@'.format(meson.project_version().split('.')[0]) +endif diff --git a/meson/suppressions/meson.build b/meson/suppressions/meson.build new file mode 100644 index 0000000..77de770 --- /dev/null +++ b/meson/suppressions/meson.build @@ -0,0 +1,92 @@ +# Copyright 2020-2022 David Robillard +# SPDX-License-Identifier: CC0-1.0 OR ISC + +# Project-specific warning suppressions. +# +# This should be used in conjunction with the generic "warnings" sibling that +# enables all reasonable warnings for the compiler. It lives here just to keep +# the top-level meson.build more readable. + +##### +# C # +##### + +if is_variable('cc') + c_suppressions = [] + + if get_option('strict') + if cc.get_id() == 'clang' + c_suppressions += [ + '-Wno-cast-align', + '-Wno-cast-qual', + '-Wno-conversion', + '-Wno-declaration-after-statement', + '-Wno-double-promotion', + '-Wno-format-nonliteral', + '-Wno-nullability-extension', + '-Wno-nullable-to-nonnull-conversion', + '-Wno-padded', + '-Wno-reserved-id-macro', + '-Wno-sign-conversion', + '-Wno-switch-enum', + '-Wno-unused-macros', + ] + + if host_machine.system() == 'freebsd' + c_suppressions += [ + '-Wno-c11-extensions', + ] + endif + + elif cc.get_id() == 'gcc' + c_suppressions += [ + '-Wno-cast-align', + '-Wno-cast-qual', + '-Wno-format-nonliteral', + '-Wno-inline', + '-Wno-padded', + '-Wno-sign-conversion', + '-Wno-strict-overflow', + '-Wno-suggest-attribute=const', + '-Wno-suggest-attribute=pure', + '-Wno-switch-default', + '-Wno-switch-enum', + '-Wno-unsuffixed-float-constants', + '-Wno-unused-const-variable', + '-Wno-unused-macros', + ] + + if host_machine.system() == 'windows' + c_suppressions += [ + '-Wno-float-conversion', + '-Wno-suggest-attribute=format', + ] + endif + + elif cc.get_id() == 'msvc' + c_suppressions += [ + '/wd4061', # enumerator in switch is not explicitly handled + '/wd4200', # zero-sized array in struct/union + '/wd4365', # signed/unsigned mismatch + '/wd4514', # unreferenced inline function has been removed + '/wd4706', # assignment within conditional expression + '/wd4710', # function not inlined + '/wd4711', # function selected for automatic inline expansion + '/wd4800', # implicit conversion from int to bool + '/wd4820', # padding added after construct + '/wd4996', # POSIX name for this item is deprecated + '/wd4996', # function or variable may be unsafe + '/wd5045', # will insert Spectre mitigation for memory load + ] + endif + endif + + if cc.get_id() == 'gcc' and host_machine.system() == 'windows' + c_suppressions += [ + '-Wno-format', + ] + endif + + c_suppressions = cc.get_supported_arguments(c_suppressions) +endif + diff --git a/meson/warnings/meson.build b/meson/warnings/meson.build new file mode 100644 index 0000000..4d23ad3 --- /dev/null +++ b/meson/warnings/meson.build @@ -0,0 +1,175 @@ +# Copyright 2020-2022 David Robillard +# SPDX-License-Identifier: CC0-1.0 OR ISC + +# General code to enable approximately all warnings in GCC 12, clang, and MSVC. +# +# This is trivial for clang and MSVC, but GCC doesn't have an "everything" +# option, so we need to enable everything we want explicitly. Wall is assumed, +# but Wextra is not, for stability. +# +# These are collected from common.opt and c.opt in the GCC source, and manually +# curated with the help of the GCC documentation. Warnings that are +# application-specific, historical, or about compatibility between specific +# language revisions are omitted. The intent here is to have roughly the same +# meaning as clang's Weverything: extremely strict, but general. Specifically +# omitted are: +# +# General: +# +# Wabi= +# Waggregate-return +# Walloc-size-larger-than=BYTES +# Walloca-larger-than=BYTES +# Wframe-larger-than=BYTES +# Wlarger-than=BYTES +# Wstack-usage=BYTES +# Wsystem-headers +# Wtraditional +# Wtraditional-conversion +# Wtrampolines +# Wvla-larger-than=BYTES +# +# Build specific: +# +# Wpoison-system-directories +# +# C Specific: +# +# Wc11-c2x-compat +# Wc90-c99-compat +# Wc99-c11-compat +# Wdeclaration-after-statement +# Wtraditional +# Wtraditional-conversion +# +# C++ Specific: +# +# Wc++0x-compat +# Wc++1z-compat +# Wc++2a-compat +# Wctad-maybe-unsupported +# Wnamespaces +# Wtemplates + +# GCC warnings that apply to all C-family languages +gcc_common_warnings = [ + '-Walloc-zero', + '-Walloca', + '-Wanalyzer-too-complex', + '-Warith-conversion', + '-Warray-bounds=2', + '-Wattribute-alias=2', + '-Wbidi-chars=ucn', + '-Wcast-align=strict', + '-Wcast-function-type', + '-Wcast-qual', + '-Wclobbered', + '-Wconversion', + '-Wdate-time', + '-Wdisabled-optimization', + '-Wdouble-promotion', + '-Wduplicated-branches', + '-Wduplicated-cond', + '-Wempty-body', + '-Wendif-labels', + '-Wfloat-equal', + '-Wformat-overflow=2', + '-Wformat-signedness', + '-Wformat-truncation=2', + '-Wformat=2', + '-Wignored-qualifiers', + '-Wimplicit-fallthrough=3', + '-Winit-self', + '-Winline', + '-Winvalid-pch', + '-Wlogical-op', + '-Wmissing-declarations', + '-Wmissing-field-initializers', + '-Wmissing-include-dirs', + '-Wmultichar', + '-Wnormalized=nfc', + '-Wnull-dereference', + '-Wopenacc-parallelism', + '-Woverlength-strings', + '-Wpacked', + '-Wpacked-bitfield-compat', + '-Wpadded', + '-Wpointer-arith', + '-Wredundant-decls', + '-Wshadow', + '-Wshift-negative-value', + '-Wshift-overflow=2', + '-Wstack-protector', + '-Wstrict-aliasing=3', + '-Wstrict-overflow=5', + '-Wstring-compare', + '-Wstringop-overflow=3', + '-Wsuggest-attribute=cold', + '-Wsuggest-attribute=const', + '-Wsuggest-attribute=format', + '-Wsuggest-attribute=malloc', + '-Wsuggest-attribute=noreturn', + '-Wsuggest-attribute=pure', + '-Wswitch-default', + '-Wswitch-enum', + '-Wtrampolines', + '-Wtrivial-auto-var-init', + '-Wtype-limits', + '-Wundef', + '-Wuninitialized', + '-Wunsafe-loop-optimizations', + '-Wunused', + '-Wunused-const-variable=2', + '-Wunused-macros', + '-Wvector-operation-performance', + '-Wvla', + '-Wwrite-strings', +] + +##### +# C # +##### + +if is_variable('cc') + # Set all_c_warnings for the current C compiler + all_c_warnings = [] + + if cc.get_id() == 'clang' + all_c_warnings += ['-Weverything'] + + if not meson.is_cross_build() + all_c_warnings += [ + '-Wno-poison-system-directories', + ] + endif + + elif cc.get_id() == 'gcc' + all_c_warnings += gcc_common_warnings + [ + '-Wabsolute-value', + '-Wbad-function-cast', + '-Wc++-compat', + '-Wenum-conversion', + '-Wjump-misses-init', + '-Wmissing-parameter-type', + '-Wmissing-prototypes', + '-Wnested-externs', + '-Wold-style-declaration', + '-Wold-style-definition', + '-Woverride-init', + '-Wsign-compare', + '-Wstrict-prototypes', + '-Wunsuffixed-float-constants', + ] + + elif cc.get_id() == 'msvc' + all_c_warnings += [ + '/Wall', + '/experimental:external', + '/external:W0', + '/external:anglebrackets', + ] + endif + + all_c_warnings = cc.get_supported_arguments(all_c_warnings) + add_global_arguments(all_c_warnings, language: ['c']) +endif diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 0000000..4fb4168 --- /dev/null +++ b/meson_options.txt @@ -0,0 +1,14 @@ +option('docs', type: 'feature', value: 'auto', yield: true, + description: 'Build documentation') + +option('strict', type: 'boolean', value: false, yield: true, + description: 'Enable ultra-strict warnings') + +option('tests', type: 'feature', value: 'auto', yield: true, + description: 'Build tests') + +option('title', type: 'string', value: 'Sord', + description: 'Project title') + +option('tools', type: 'feature', value: 'auto', yield: true, + description: 'Build command line utilities') diff --git a/sord.pc.in b/sord.pc.in deleted file mode 100644 index 64395c5..0000000 --- a/sord.pc.in +++ /dev/null @@ -1,11 +0,0 @@ -prefix=@PREFIX@ -exec_prefix=@EXEC_PREFIX@ -libdir=@LIBDIR@ -includedir=@INCLUDEDIR@ - -Name: Sord -Version: @SORD_VERSION@ -Description: A lightweight C library for storing RDF statements in memory. -Requires: @SORD_PKG_DEPS@ -Libs: -L${libdir} -l@LIB_SORD@ -Cflags: -I${includedir}/sord-@SORD_MAJOR_VERSION@ diff --git a/src/sord.c b/src/sord.c index 4ca022f..a80ab40 100644 --- a/src/sord.c +++ b/src/sord.c @@ -20,7 +20,7 @@ #include "serd/serd.h" #include "sord/sord.h" -#define ZIX_INLINE +#define ZIX_STATIC #include "zix/btree.c" #include "zix/btree.h" #include "zix/common.h" diff --git a/src/sord_test.c b/src/sord_test.c deleted file mode 100644 index e451049..0000000 --- a/src/sord_test.c +++ /dev/null @@ -1,769 +0,0 @@ -/* - Copyright 2011-2016 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. -*/ - -#include "serd/serd.h" -#include "sord/sord.h" - -#include -#include -#include -#include -#include -#include - -#ifdef __GNUC__ -# define SORD_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1))) -#else -# define SORD_LOG_FUNC(fmt, arg1) -#endif - -#define DIGITS 3 -#define N_OBJECTS_PER 2U - -static int n_expected_errors = 0; - -typedef struct { - SordQuad query; - int expected_num_results; -} QueryTest; - -#define USTR(s) ((const uint8_t*)(s)) - -static SordNode* -uri(SordWorld* world, unsigned num) -{ - if (num == 0) { - return 0; - } - - char str[16]; - snprintf(str, sizeof(str), "eg:%0*u", DIGITS, num); - return sord_new_uri(world, (const uint8_t*)str); -} - -SORD_LOG_FUNC(1, 2) -static int -test_fail(const char* fmt, ...) -{ - va_list args; - va_start(args, fmt); - fprintf(stderr, "error: "); - vfprintf(stderr, fmt, args); - va_end(args); - return 1; -} - -static int -generate(SordWorld* world, SordModel* sord, unsigned n_quads, SordNode* graph) -{ - fprintf(stderr, - "Generating %u (S P *) quads with %u objects each\n", - n_quads, - N_OBJECTS_PER); - - for (unsigned i = 0; i < n_quads; ++i) { - unsigned num = (i * N_OBJECTS_PER) + 1; - - SordNode* ids[2 + N_OBJECTS_PER]; - for (unsigned j = 0; j < 2 + N_OBJECTS_PER; ++j) { - ids[j] = uri(world, num++); - } - - for (unsigned j = 0; j < N_OBJECTS_PER; ++j) { - SordQuad tup = {ids[0], ids[1], ids[2 + j], graph}; - if (!sord_add(sord, tup)) { - return test_fail("Fail: Failed to add quad\n"); - } - } - - for (unsigned j = 0; j < 2 + N_OBJECTS_PER; ++j) { - sord_node_free(world, ids[j]); - } - } - - // Add some literals - - // (98 4 "hello") and (98 4 "hello"^^<5>) - SordQuad tup = {0, 0, 0, 0}; - tup[0] = uri(world, 98); - tup[1] = uri(world, 4); - tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); - tup[3] = graph; - sord_add(sord, tup); - sord_node_free(world, (SordNode*)tup[2]); - tup[2] = sord_new_literal(world, uri(world, 5), USTR("hello"), NULL); - if (!sord_add(sord, tup)) { - return test_fail("Failed to add typed literal\n"); - } - - // (96 4 "hello"^^<4>) and (96 4 "hello"^^<5>) - tup[0] = uri(world, 96); - tup[1] = uri(world, 4); - tup[2] = sord_new_literal(world, uri(world, 4), USTR("hello"), NULL); - tup[3] = graph; - sord_add(sord, tup); - sord_node_free(world, (SordNode*)tup[2]); - tup[2] = sord_new_literal(world, uri(world, 5), USTR("hello"), NULL); - if (!sord_add(sord, tup)) { - return test_fail("Failed to add typed literal\n"); - } - - // (94 5 "hello") and (94 5 "hello"@en-gb) - tup[0] = uri(world, 94); - tup[1] = uri(world, 5); - tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); - tup[3] = graph; - sord_add(sord, tup); - sord_node_free(world, (SordNode*)tup[2]); - tup[2] = sord_new_literal(world, NULL, USTR("hello"), "en-gb"); - if (!sord_add(sord, tup)) { - return test_fail("Failed to add literal with language\n"); - } - - // (92 6 "hello"@en-us) and (92 5 "hello"@en-gb) - tup[0] = uri(world, 92); - tup[1] = uri(world, 6); - tup[2] = sord_new_literal(world, 0, USTR("hello"), "en-us"); - tup[3] = graph; - sord_add(sord, tup); - sord_node_free(world, (SordNode*)tup[2]); - tup[2] = sord_new_literal(world, NULL, USTR("hello"), "en-gb"); - if (!sord_add(sord, tup)) { - return test_fail("Failed to add literal with language\n"); - } - - sord_node_free(world, (SordNode*)tup[0]); - sord_node_free(world, (SordNode*)tup[2]); - tup[0] = uri(world, 14); - tup[2] = sord_new_literal(world, 0, USTR("bonjour"), "fr"); - sord_add(sord, tup); - sord_node_free(world, (SordNode*)tup[2]); - tup[2] = sord_new_literal(world, 0, USTR("salut"), "fr"); - sord_add(sord, tup); - - // Attempt to add some duplicates - if (sord_add(sord, tup)) { - return test_fail("Fail: Successfully added duplicate quad\n"); - } - if (sord_add(sord, tup)) { - return test_fail("Fail: Successfully added duplicate quad\n"); - } - - // Add a blank node subject - sord_node_free(world, (SordNode*)tup[0]); - tup[0] = sord_new_blank(world, USTR("ablank")); - sord_add(sord, tup); - - sord_node_free(world, (SordNode*)tup[1]); - sord_node_free(world, (SordNode*)tup[2]); - tup[1] = uri(world, 6); - tup[2] = uri(world, 7); - sord_add(sord, tup); - sord_node_free(world, (SordNode*)tup[0]); - sord_node_free(world, (SordNode*)tup[1]); - sord_node_free(world, (SordNode*)tup[2]); - - return EXIT_SUCCESS; -} - -#define TUP_FMT "(%6s %6s %6s)" -#define TUP_FMT_ARGS(t) \ - ((t)[0] ? sord_node_get_string((t)[0]) : USTR("*")), \ - ((t)[1] ? sord_node_get_string((t)[1]) : USTR("*")), \ - ((t)[2] ? sord_node_get_string((t)[2]) : USTR("*")) - -static int -test_read(SordWorld* world, SordModel* sord, SordNode* g, const size_t n_quads) -{ - int ret = EXIT_SUCCESS; - - SordQuad id; - - SordIter* iter = sord_begin(sord); - if (sord_iter_get_model(iter) != sord) { - return test_fail("Fail: Iterator has incorrect sord pointer\n"); - } - - for (; !sord_iter_end(iter); sord_iter_next(iter)) { - sord_iter_get(iter, id); - } - - // Attempt to increment past end - if (!sord_iter_next(iter)) { - return test_fail("Fail: Successfully incremented past end\n"); - } - - sord_iter_free(iter); - - const uint8_t* s = USTR("hello"); - SordNode* plain_hello = sord_new_literal(world, 0, s, NULL); - SordNode* type4_hello = sord_new_literal(world, uri(world, 4), s, NULL); - SordNode* type5_hello = sord_new_literal(world, uri(world, 5), s, NULL); - SordNode* gb_hello = sord_new_literal(world, NULL, s, "en-gb"); - SordNode* us_hello = sord_new_literal(world, NULL, s, "en-us"); - -#define NUM_PATTERNS 18 - - QueryTest patterns[NUM_PATTERNS] = { - {{0, 0, 0}, (int)(n_quads * N_OBJECTS_PER) + 12}, - {{uri(world, 1), 0, 0}, 2}, - {{uri(world, 9), uri(world, 9), uri(world, 9)}, 0}, - {{uri(world, 1), uri(world, 2), uri(world, 4)}, 1}, - {{uri(world, 3), uri(world, 4), uri(world, 0)}, 2}, - {{uri(world, 0), uri(world, 2), uri(world, 4)}, 1}, - {{uri(world, 0), uri(world, 0), uri(world, 4)}, 1}, - {{uri(world, 1), uri(world, 0), uri(world, 0)}, 2}, - {{uri(world, 1), uri(world, 0), uri(world, 4)}, 1}, - {{uri(world, 0), uri(world, 2), uri(world, 0)}, 2}, - {{uri(world, 98), uri(world, 4), plain_hello}, 1}, - {{uri(world, 98), uri(world, 4), type5_hello}, 1}, - {{uri(world, 96), uri(world, 4), type4_hello}, 1}, - {{uri(world, 96), uri(world, 4), type5_hello}, 1}, - {{uri(world, 94), uri(world, 5), plain_hello}, 1}, - {{uri(world, 94), uri(world, 5), gb_hello}, 1}, - {{uri(world, 92), uri(world, 6), gb_hello}, 1}, - {{uri(world, 92), uri(world, 6), us_hello}, 1}}; - - SordQuad match = {uri(world, 1), uri(world, 2), uri(world, 4), g}; - if (!sord_contains(sord, match)) { - return test_fail("Fail: No match for " TUP_FMT "\n", TUP_FMT_ARGS(match)); - } - - SordQuad nomatch = {uri(world, 1), uri(world, 2), uri(world, 9), g}; - if (sord_contains(sord, nomatch)) { - return test_fail("Fail: False match for " TUP_FMT "\n", - TUP_FMT_ARGS(nomatch)); - } - - if (sord_get(sord, NULL, NULL, uri(world, 3), g)) { - return test_fail("Fail: Get *,*,3 succeeded\n"); - } else if (!sord_node_equals( - sord_get(sord, uri(world, 1), uri(world, 2), NULL, g), - uri(world, 3))) { - return test_fail("Fail: Get 1,2,* != 3\n"); - } else if (!sord_node_equals( - sord_get(sord, uri(world, 1), NULL, uri(world, 3), g), - uri(world, 2))) { - return test_fail("Fail: Get 1,*,3 != 2\n"); - } else if (!sord_node_equals( - sord_get(sord, NULL, uri(world, 2), uri(world, 3), g), - uri(world, 1))) { - return test_fail("Fail: Get *,2,3 != 1\n"); - } - - for (unsigned i = 0; i < NUM_PATTERNS; ++i) { - QueryTest test = patterns[i]; - SordQuad pat = {test.query[0], test.query[1], test.query[2], g}; - fprintf(stderr, "Query " TUP_FMT "... ", TUP_FMT_ARGS(pat)); - - iter = sord_find(sord, pat); - int num_results = 0; - for (; !sord_iter_end(iter); sord_iter_next(iter)) { - sord_iter_get(iter, id); - ++num_results; - if (!sord_quad_match(pat, id)) { - sord_iter_free(iter); - return test_fail("Fail: Query result " TUP_FMT - " does not match pattern\n", - TUP_FMT_ARGS(id)); - } - } - sord_iter_free(iter); - if (num_results != test.expected_num_results) { - return test_fail("Fail: Expected %d results, got %d\n", - test.expected_num_results, - num_results); - } - fprintf(stderr, "OK (%i matches)\n", test.expected_num_results); - } - - // Query blank node subject - SordQuad pat = {sord_new_blank(world, USTR("ablank")), 0, 0}; - if (!pat[0]) { - return test_fail("Blank node subject lost\n"); - } - fprintf(stderr, "Query " TUP_FMT "... ", TUP_FMT_ARGS(pat)); - iter = sord_find(sord, pat); - int num_results = 0; - for (; !sord_iter_end(iter); sord_iter_next(iter)) { - sord_iter_get(iter, id); - ++num_results; - if (!sord_quad_match(pat, id)) { - sord_iter_free(iter); - return test_fail("Fail: Query result " TUP_FMT - " does not match pattern\n", - TUP_FMT_ARGS(id)); - } - } - fprintf(stderr, "OK\n"); - sord_node_free(world, (SordNode*)pat[0]); - sord_iter_free(iter); - if (num_results != 2) { - return test_fail("Blank node subject query failed\n"); - } - - // Test nested queries - fprintf(stderr, "Nested Queries... "); - const SordNode* last_subject = 0; - iter = sord_search(sord, NULL, NULL, NULL, NULL); - for (; !sord_iter_end(iter); sord_iter_next(iter)) { - sord_iter_get(iter, id); - if (id[0] == last_subject) { - continue; - } - - SordQuad subpat = {id[0], 0, 0}; - SordIter* subiter = sord_find(sord, subpat); - unsigned num_sub_results = 0; - if (sord_iter_get_node(subiter, SORD_SUBJECT) != id[0]) { - return test_fail("Fail: Incorrect initial submatch\n"); - } - for (; !sord_iter_end(subiter); sord_iter_next(subiter)) { - SordQuad subid; - sord_iter_get(subiter, subid); - if (!sord_quad_match(subpat, subid)) { - sord_iter_free(iter); - sord_iter_free(subiter); - return test_fail("Fail: Nested query result does not match pattern\n"); - } - ++num_sub_results; - } - sord_iter_free(subiter); - if (num_sub_results != N_OBJECTS_PER) { - return test_fail("Fail: Nested query " TUP_FMT " failed" - " (%u results, expected %u)\n", - TUP_FMT_ARGS(subpat), - num_sub_results, - N_OBJECTS_PER); - } - - uint64_t count = sord_count(sord, id[0], 0, 0, 0); - if (count != num_sub_results) { - return test_fail("Fail: Query " TUP_FMT " sord_count() %" PRIu64 - "does not match result count %u\n", - TUP_FMT_ARGS(subpat), - count, - num_sub_results); - } - - last_subject = id[0]; - } - fprintf(stderr, "OK\n\n"); - sord_iter_free(iter); - - return ret; -} - -static SerdStatus -unexpected_error(void* handle, const SerdError* error) -{ - (void)handle; - - fprintf(stderr, "unexpected error: "); - vfprintf(stderr, error->fmt, *error->args); - return SERD_SUCCESS; -} - -static SerdStatus -expected_error(void* handle, const SerdError* error) -{ - (void)handle; - - fprintf(stderr, "expected error: "); - vfprintf(stderr, error->fmt, *error->args); - ++n_expected_errors; - return SERD_SUCCESS; -} - -static int -finished(SordWorld* world, SordModel* sord, int status) -{ - sord_free(sord); - sord_world_free(world); - return status; -} - -int -main(void) -{ - static const unsigned n_quads = 300U; - - sord_free(NULL); // Shouldn't crash - - SordWorld* world = sord_world_new(); - - // Attempt to create invalid URI - fprintf(stderr, "expected "); - SordNode* bad_uri = sord_new_uri(world, USTR("noscheme")); - if (bad_uri) { - return test_fail("Successfully created invalid URI \"noscheme\"\n"); - } - sord_node_free(world, bad_uri); - - sord_world_set_error_sink(world, expected_error, NULL); - - // Attempt to create invalid CURIE - SerdNode base = serd_node_from_string(SERD_URI, USTR("http://example.org/")); - SerdEnv* env = serd_env_new(&base); - SerdNode sbad = serd_node_from_string(SERD_CURIE, USTR("bad:")); - SordNode* bad = sord_node_from_serd_node(world, env, &sbad, NULL, NULL); - if (bad) { - return test_fail("Successfully created CURIE with bad namespace\n"); - } - sord_node_free(world, bad); - serd_env_free(env); - - // Attempt to create node from garbage - SerdNode junk = SERD_NODE_NULL; - junk.type = (SerdType)1234; - if (sord_node_from_serd_node(world, env, &junk, NULL, NULL)) { - return test_fail("Successfully created node from garbage serd node\n"); - } - - // Attempt to create NULL node - SordNode* nil_node = - sord_node_from_serd_node(world, NULL, &SERD_NODE_NULL, NULL, NULL); - if (nil_node) { - return test_fail("Successfully created NULL node\n"); - } - sord_node_free(world, nil_node); - - // Check node flags are set properly - SordNode* with_newline = sord_new_literal(world, NULL, USTR("a\nb"), NULL); - if (!(sord_node_get_flags(with_newline) & SERD_HAS_NEWLINE)) { - return test_fail("Newline flag not set\n"); - } - SordNode* with_quote = sord_new_literal(world, NULL, USTR("a\"b"), NULL); - if (!(sord_node_get_flags(with_quote) & SERD_HAS_QUOTE)) { - return test_fail("Quote flag not set\n"); - } - - // Create with minimal indexing - SordModel* sord = sord_new(world, SORD_SPO, false); - generate(world, sord, n_quads, NULL); - - if (test_read(world, sord, NULL, n_quads)) { - sord_free(sord); - sord_world_free(world); - return EXIT_FAILURE; - } - - // Check adding tuples with NULL fields fails - sord_world_set_error_sink(world, expected_error, NULL); - const size_t initial_num_quads = sord_num_quads(sord); - SordQuad tup = {0, 0, 0, 0}; - if (sord_add(sord, tup)) { - return test_fail("Added NULL tuple\n"); - } - tup[0] = uri(world, 1); - if (sord_add(sord, tup)) { - return test_fail("Added tuple with NULL P and O\n"); - } - tup[1] = uri(world, 2); - if (sord_add(sord, tup)) { - return test_fail("Added tuple with NULL O\n"); - } - - if (sord_num_quads(sord) != initial_num_quads) { - return test_fail( - "Num quads %zu != %zu\n", sord_num_quads(sord), initial_num_quads); - } - - // Check adding tuples with an active iterator fails - SordIter* iter = sord_begin(sord); - tup[2] = uri(world, 3); - if (sord_add(sord, tup)) { - return test_fail("Added tuple with active iterator\n"); - } - - // Check removing tuples with several active iterator fails - SordIter* iter2 = sord_begin(sord); - if (!sord_erase(sord, iter)) { - return test_fail("Erased tuple with several active iterators\n"); - } - n_expected_errors = 0; - sord_remove(sord, tup); - if (n_expected_errors != 1) { - return test_fail("Removed tuple with several active iterators\n"); - } - sord_iter_free(iter); - sord_iter_free(iter2); - - sord_world_set_error_sink(world, unexpected_error, NULL); - - // Check interning merges equivalent values - SordNode* uri_id = sord_new_uri(world, USTR("http://example.org")); - SordNode* blank_id = sord_new_blank(world, USTR("testblank")); - SordNode* lit_id = sord_new_literal(world, uri_id, USTR("hello"), NULL); - if (sord_node_get_type(uri_id) != SORD_URI) { - return test_fail("URI node has incorrect type\n"); - } else if (sord_node_get_type(blank_id) != SORD_BLANK) { - return test_fail("Blank node has incorrect type\n"); - } else if (sord_node_get_type(lit_id) != SORD_LITERAL) { - return test_fail("Literal node has incorrect type\n"); - } - - const size_t initial_num_nodes = sord_num_nodes(world); - - SordNode* uri_id2 = sord_new_uri(world, USTR("http://example.org")); - SordNode* blank_id2 = sord_new_blank(world, USTR("testblank")); - SordNode* lit_id2 = sord_new_literal(world, uri_id, USTR("hello"), NULL); - if (uri_id2 != uri_id || !sord_node_equals(uri_id2, uri_id)) { - fprintf(stderr, "Fail: URI interning failed (duplicates)\n"); - return finished(world, sord, EXIT_FAILURE); - } else if (blank_id2 != blank_id || !sord_node_equals(blank_id2, blank_id)) { - fprintf(stderr, "Fail: Blank node interning failed (duplicates)\n"); - return finished(world, sord, EXIT_FAILURE); - } else if (lit_id2 != lit_id || !sord_node_equals(lit_id2, lit_id)) { - fprintf(stderr, "Fail: Literal interning failed (duplicates)\n"); - return finished(world, sord, EXIT_FAILURE); - } - - if (sord_num_nodes(world) != initial_num_nodes) { - return test_fail( - "Num nodes %zu != %zu\n", sord_num_nodes(world), initial_num_nodes); - } - - const uint8_t ni_hao[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD, 0}; - SordNode* chello = sord_new_literal(world, NULL, ni_hao, "cmn"); - - // Test literal length - size_t n_bytes; - size_t n_chars; - const uint8_t* str = sord_node_get_string_counted(lit_id2, &n_bytes); - if (strcmp((const char*)str, "hello")) { - return test_fail("Literal node corrupt\n"); - } else if (n_bytes != strlen("hello")) { - return test_fail("ASCII literal byte count incorrect\n"); - } - - str = sord_node_get_string_measured(lit_id2, &n_bytes, &n_chars); - if (n_bytes != strlen("hello") || n_chars != strlen("hello")) { - return test_fail("ASCII literal measured length incorrect\n"); - } else if (strcmp((const char*)str, "hello")) { - return test_fail("ASCII literal string incorrect\n"); - } - - str = sord_node_get_string_measured(chello, &n_bytes, &n_chars); - if (n_bytes != 6) { - return test_fail("Multi-byte literal byte count incorrect\n"); - } else if (n_chars != 2) { - return test_fail("Multi-byte literal character count incorrect\n"); - } else if (strcmp((const char*)str, (const char*)ni_hao)) { - return test_fail("Multi-byte literal string incorrect\n"); - } - - // Check interning doesn't clash non-equivalent values - SordNode* uri_id3 = sord_new_uri(world, USTR("http://example.orgX")); - SordNode* blank_id3 = sord_new_blank(world, USTR("testblankX")); - SordNode* lit_id3 = sord_new_literal(world, uri_id, USTR("helloX"), NULL); - if (uri_id3 == uri_id || sord_node_equals(uri_id3, uri_id)) { - fprintf(stderr, "Fail: URI interning failed (clash)\n"); - return finished(world, sord, EXIT_FAILURE); - } else if (blank_id3 == blank_id || sord_node_equals(blank_id3, blank_id)) { - fprintf(stderr, "Fail: Blank node interning failed (clash)\n"); - return finished(world, sord, EXIT_FAILURE); - } else if (lit_id3 == lit_id || sord_node_equals(lit_id3, lit_id)) { - fprintf(stderr, "Fail: Literal interning failed (clash)\n"); - return finished(world, sord, EXIT_FAILURE); - } - - // Check literal interning - SordNode* lit4 = sord_new_literal(world, NULL, USTR("hello"), NULL); - SordNode* lit5 = sord_new_literal(world, uri_id2, USTR("hello"), NULL); - SordNode* lit6 = sord_new_literal(world, NULL, USTR("hello"), "en-ca"); - if (lit4 == lit5 || sord_node_equals(lit4, lit5) || lit4 == lit6 || - sord_node_equals(lit4, lit6) || lit5 == lit6 || - sord_node_equals(lit5, lit6)) { - fprintf(stderr, "Fail: Literal interning failed (type/lang clash)\n"); - return finished(world, sord, EXIT_FAILURE); - } - - // Check relative URI construction - SordNode* reluri = - sord_new_relative_uri(world, USTR("a/b"), USTR("http://example.org/")); - if (strcmp((const char*)sord_node_get_string(reluri), - "http://example.org/a/b")) { - fprintf(stderr, - "Fail: Bad relative URI constructed: <%s>\n", - sord_node_get_string(reluri)); - return finished(world, sord, EXIT_FAILURE); - } - SordNode* reluri2 = sord_new_relative_uri( - world, USTR("http://drobilla.net/"), USTR("http://example.org/")); - if (strcmp((const char*)sord_node_get_string(reluri2), - "http://drobilla.net/")) { - fprintf(stderr, - "Fail: Bad relative URI constructed: <%s>\n", - sord_node_get_string(reluri)); - return finished(world, sord, EXIT_FAILURE); - } - - // Check comparison with NULL - sord_node_free(world, uri_id); - sord_node_free(world, blank_id); - sord_node_free(world, lit_id); - sord_node_free(world, uri_id2); - sord_node_free(world, blank_id2); - sord_node_free(world, lit_id2); - sord_node_free(world, uri_id3); - sord_node_free(world, blank_id3); - sord_node_free(world, lit_id3); - sord_free(sord); - - static const char* const index_names[6] = { - "spo", "sop", "ops", "osp", "pso", "pos"}; - - for (int i = 0; i < 6; ++i) { - sord = sord_new(world, (1 << i), false); - printf("Testing Index `%s'\n", index_names[i]); - generate(world, sord, n_quads, 0); - if (test_read(world, sord, 0, n_quads)) { - return finished(world, sord, EXIT_FAILURE); - } - sord_free(sord); - } - - static const char* const graph_index_names[6] = { - "gspo", "gsop", "gops", "gosp", "gpso", "gpos"}; - - for (int i = 0; i < 6; ++i) { - sord = sord_new(world, (1 << i), true); - printf("Testing Index `%s'\n", graph_index_names[i]); - SordNode* graph = uri(world, 42); - generate(world, sord, n_quads, graph); - if (test_read(world, sord, graph, n_quads)) { - return finished(world, sord, EXIT_FAILURE); - } - sord_free(sord); - } - - // Test removing - sord = sord_new(world, SORD_SPO, true); - tup[0] = uri(world, 1); - tup[1] = uri(world, 2); - tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); - tup[3] = 0; - sord_add(sord, tup); - if (!sord_ask(sord, tup[0], tup[1], tup[2], tup[3])) { - fprintf(stderr, "Failed to add tuple\n"); - return finished(world, sord, EXIT_FAILURE); - } - sord_node_free(world, (SordNode*)tup[2]); - tup[2] = sord_new_literal(world, 0, USTR("hi"), NULL); - sord_add(sord, tup); - sord_remove(sord, tup); - if (sord_num_quads(sord) != 1) { - fprintf( - stderr, "Remove failed (%zu quads, expected 1)\n", sord_num_quads(sord)); - return finished(world, sord, EXIT_FAILURE); - } - - iter = sord_find(sord, tup); - if (!sord_iter_end(iter)) { - fprintf(stderr, "Found removed tuple\n"); - return finished(world, sord, EXIT_FAILURE); - } - sord_iter_free(iter); - - // Test double remove (silent success) - sord_remove(sord, tup); - - // Load a couple graphs - SordNode* graph42 = uri(world, 42); - SordNode* graph43 = uri(world, 43); - generate(world, sord, 1, graph42); - generate(world, sord, 1, graph43); - - // Remove one graph via iterator - SerdStatus st; - iter = sord_search(sord, NULL, NULL, NULL, graph43); - while (!sord_iter_end(iter)) { - if ((st = sord_erase(sord, iter))) { - fprintf(stderr, "Remove by iterator failed (%s)\n", serd_strerror(st)); - return finished(world, sord, EXIT_FAILURE); - } - } - sord_iter_free(iter); - - // Erase the first tuple (an element in the default graph) - iter = sord_begin(sord); - if (sord_erase(sord, iter)) { - return test_fail("Failed to erase begin iterator on non-empty model\n"); - } - sord_iter_free(iter); - - // Ensure only the other graph is left - SordQuad quad; - SordQuad pat = {0, 0, 0, graph42}; - for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { - sord_iter_get(iter, quad); - if (!sord_quad_match(quad, pat)) { - fprintf(stderr, "Graph removal via iteration failed\n"); - return finished(world, sord, EXIT_FAILURE); - } - } - sord_iter_free(iter); - - // Load file into two separate graphs - sord_free(sord); - sord = sord_new(world, SORD_SPO, true); - env = serd_env_new(&base); - SordNode* graph1 = sord_new_uri(world, USTR("http://example.org/graph1")); - SordNode* graph2 = sord_new_uri(world, USTR("http://example.org/graph2")); - SerdReader* reader = sord_new_reader(sord, env, SERD_TURTLE, graph1); - if ((st = serd_reader_read_string(reader, USTR("

.")))) { - fprintf(stderr, "Failed to read string (%s)\n", serd_strerror(st)); - return finished(world, sord, EXIT_FAILURE); - } - serd_reader_free(reader); - reader = sord_new_reader(sord, env, SERD_TURTLE, graph2); - if ((st = serd_reader_read_string(reader, USTR("

.")))) { - fprintf(stderr, "Failed to re-read string (%s)\n", serd_strerror(st)); - return finished(world, sord, EXIT_FAILURE); - } - serd_reader_free(reader); - serd_env_free(env); - - // Ensure we only see triple once - size_t n_triples = 0; - for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { - fprintf(stderr, - "%s %s %s %s\n", - sord_node_get_string(sord_iter_get_node(iter, SORD_SUBJECT)), - sord_node_get_string(sord_iter_get_node(iter, SORD_PREDICATE)), - sord_node_get_string(sord_iter_get_node(iter, SORD_OBJECT)), - sord_node_get_string(sord_iter_get_node(iter, SORD_GRAPH))); - - ++n_triples; - } - sord_iter_free(iter); - if (n_triples != 1) { - fprintf(stderr, "Found duplicate triple\n"); - return finished(world, sord, EXIT_FAILURE); - } - - // Test SPO iteration on an SOP indexed store - sord_free(sord); - sord = sord_new(world, SORD_SOP, false); - generate(world, sord, 1, graph42); - for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { - ++n_triples; - } - sord_iter_free(iter); - - return finished(world, sord, EXIT_SUCCESS); -} diff --git a/test/meson.build b/test/meson.build new file mode 100644 index 0000000..72a4870 --- /dev/null +++ b/test/meson.build @@ -0,0 +1,22 @@ +autoship = find_program('autoship', required: false) + +unit_tests = [ + 'sord', +] + +foreach unit : unit_tests + test( + unit, + executable( + 'test_@0@'.format(unit), + files('test_@0@.c'.format(unit)), + c_args: c_suppressions, + dependencies: sord_dep, + ), + suite: 'unit', + ) +endforeach + +if autoship.found() + test('autoship', autoship, args: ['test', sord_src_root], suite: 'data') +endif diff --git a/test/test_sord.c b/test/test_sord.c new file mode 100644 index 0000000..e451049 --- /dev/null +++ b/test/test_sord.c @@ -0,0 +1,769 @@ +/* + Copyright 2011-2016 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. +*/ + +#include "serd/serd.h" +#include "sord/sord.h" + +#include +#include +#include +#include +#include +#include + +#ifdef __GNUC__ +# define SORD_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1))) +#else +# define SORD_LOG_FUNC(fmt, arg1) +#endif + +#define DIGITS 3 +#define N_OBJECTS_PER 2U + +static int n_expected_errors = 0; + +typedef struct { + SordQuad query; + int expected_num_results; +} QueryTest; + +#define USTR(s) ((const uint8_t*)(s)) + +static SordNode* +uri(SordWorld* world, unsigned num) +{ + if (num == 0) { + return 0; + } + + char str[16]; + snprintf(str, sizeof(str), "eg:%0*u", DIGITS, num); + return sord_new_uri(world, (const uint8_t*)str); +} + +SORD_LOG_FUNC(1, 2) +static int +test_fail(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, args); + va_end(args); + return 1; +} + +static int +generate(SordWorld* world, SordModel* sord, unsigned n_quads, SordNode* graph) +{ + fprintf(stderr, + "Generating %u (S P *) quads with %u objects each\n", + n_quads, + N_OBJECTS_PER); + + for (unsigned i = 0; i < n_quads; ++i) { + unsigned num = (i * N_OBJECTS_PER) + 1; + + SordNode* ids[2 + N_OBJECTS_PER]; + for (unsigned j = 0; j < 2 + N_OBJECTS_PER; ++j) { + ids[j] = uri(world, num++); + } + + for (unsigned j = 0; j < N_OBJECTS_PER; ++j) { + SordQuad tup = {ids[0], ids[1], ids[2 + j], graph}; + if (!sord_add(sord, tup)) { + return test_fail("Fail: Failed to add quad\n"); + } + } + + for (unsigned j = 0; j < 2 + N_OBJECTS_PER; ++j) { + sord_node_free(world, ids[j]); + } + } + + // Add some literals + + // (98 4 "hello") and (98 4 "hello"^^<5>) + SordQuad tup = {0, 0, 0, 0}; + tup[0] = uri(world, 98); + tup[1] = uri(world, 4); + tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); + tup[3] = graph; + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, uri(world, 5), USTR("hello"), NULL); + if (!sord_add(sord, tup)) { + return test_fail("Failed to add typed literal\n"); + } + + // (96 4 "hello"^^<4>) and (96 4 "hello"^^<5>) + tup[0] = uri(world, 96); + tup[1] = uri(world, 4); + tup[2] = sord_new_literal(world, uri(world, 4), USTR("hello"), NULL); + tup[3] = graph; + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, uri(world, 5), USTR("hello"), NULL); + if (!sord_add(sord, tup)) { + return test_fail("Failed to add typed literal\n"); + } + + // (94 5 "hello") and (94 5 "hello"@en-gb) + tup[0] = uri(world, 94); + tup[1] = uri(world, 5); + tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); + tup[3] = graph; + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, NULL, USTR("hello"), "en-gb"); + if (!sord_add(sord, tup)) { + return test_fail("Failed to add literal with language\n"); + } + + // (92 6 "hello"@en-us) and (92 5 "hello"@en-gb) + tup[0] = uri(world, 92); + tup[1] = uri(world, 6); + tup[2] = sord_new_literal(world, 0, USTR("hello"), "en-us"); + tup[3] = graph; + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, NULL, USTR("hello"), "en-gb"); + if (!sord_add(sord, tup)) { + return test_fail("Failed to add literal with language\n"); + } + + sord_node_free(world, (SordNode*)tup[0]); + sord_node_free(world, (SordNode*)tup[2]); + tup[0] = uri(world, 14); + tup[2] = sord_new_literal(world, 0, USTR("bonjour"), "fr"); + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, 0, USTR("salut"), "fr"); + sord_add(sord, tup); + + // Attempt to add some duplicates + if (sord_add(sord, tup)) { + return test_fail("Fail: Successfully added duplicate quad\n"); + } + if (sord_add(sord, tup)) { + return test_fail("Fail: Successfully added duplicate quad\n"); + } + + // Add a blank node subject + sord_node_free(world, (SordNode*)tup[0]); + tup[0] = sord_new_blank(world, USTR("ablank")); + sord_add(sord, tup); + + sord_node_free(world, (SordNode*)tup[1]); + sord_node_free(world, (SordNode*)tup[2]); + tup[1] = uri(world, 6); + tup[2] = uri(world, 7); + sord_add(sord, tup); + sord_node_free(world, (SordNode*)tup[0]); + sord_node_free(world, (SordNode*)tup[1]); + sord_node_free(world, (SordNode*)tup[2]); + + return EXIT_SUCCESS; +} + +#define TUP_FMT "(%6s %6s %6s)" +#define TUP_FMT_ARGS(t) \ + ((t)[0] ? sord_node_get_string((t)[0]) : USTR("*")), \ + ((t)[1] ? sord_node_get_string((t)[1]) : USTR("*")), \ + ((t)[2] ? sord_node_get_string((t)[2]) : USTR("*")) + +static int +test_read(SordWorld* world, SordModel* sord, SordNode* g, const size_t n_quads) +{ + int ret = EXIT_SUCCESS; + + SordQuad id; + + SordIter* iter = sord_begin(sord); + if (sord_iter_get_model(iter) != sord) { + return test_fail("Fail: Iterator has incorrect sord pointer\n"); + } + + for (; !sord_iter_end(iter); sord_iter_next(iter)) { + sord_iter_get(iter, id); + } + + // Attempt to increment past end + if (!sord_iter_next(iter)) { + return test_fail("Fail: Successfully incremented past end\n"); + } + + sord_iter_free(iter); + + const uint8_t* s = USTR("hello"); + SordNode* plain_hello = sord_new_literal(world, 0, s, NULL); + SordNode* type4_hello = sord_new_literal(world, uri(world, 4), s, NULL); + SordNode* type5_hello = sord_new_literal(world, uri(world, 5), s, NULL); + SordNode* gb_hello = sord_new_literal(world, NULL, s, "en-gb"); + SordNode* us_hello = sord_new_literal(world, NULL, s, "en-us"); + +#define NUM_PATTERNS 18 + + QueryTest patterns[NUM_PATTERNS] = { + {{0, 0, 0}, (int)(n_quads * N_OBJECTS_PER) + 12}, + {{uri(world, 1), 0, 0}, 2}, + {{uri(world, 9), uri(world, 9), uri(world, 9)}, 0}, + {{uri(world, 1), uri(world, 2), uri(world, 4)}, 1}, + {{uri(world, 3), uri(world, 4), uri(world, 0)}, 2}, + {{uri(world, 0), uri(world, 2), uri(world, 4)}, 1}, + {{uri(world, 0), uri(world, 0), uri(world, 4)}, 1}, + {{uri(world, 1), uri(world, 0), uri(world, 0)}, 2}, + {{uri(world, 1), uri(world, 0), uri(world, 4)}, 1}, + {{uri(world, 0), uri(world, 2), uri(world, 0)}, 2}, + {{uri(world, 98), uri(world, 4), plain_hello}, 1}, + {{uri(world, 98), uri(world, 4), type5_hello}, 1}, + {{uri(world, 96), uri(world, 4), type4_hello}, 1}, + {{uri(world, 96), uri(world, 4), type5_hello}, 1}, + {{uri(world, 94), uri(world, 5), plain_hello}, 1}, + {{uri(world, 94), uri(world, 5), gb_hello}, 1}, + {{uri(world, 92), uri(world, 6), gb_hello}, 1}, + {{uri(world, 92), uri(world, 6), us_hello}, 1}}; + + SordQuad match = {uri(world, 1), uri(world, 2), uri(world, 4), g}; + if (!sord_contains(sord, match)) { + return test_fail("Fail: No match for " TUP_FMT "\n", TUP_FMT_ARGS(match)); + } + + SordQuad nomatch = {uri(world, 1), uri(world, 2), uri(world, 9), g}; + if (sord_contains(sord, nomatch)) { + return test_fail("Fail: False match for " TUP_FMT "\n", + TUP_FMT_ARGS(nomatch)); + } + + if (sord_get(sord, NULL, NULL, uri(world, 3), g)) { + return test_fail("Fail: Get *,*,3 succeeded\n"); + } else if (!sord_node_equals( + sord_get(sord, uri(world, 1), uri(world, 2), NULL, g), + uri(world, 3))) { + return test_fail("Fail: Get 1,2,* != 3\n"); + } else if (!sord_node_equals( + sord_get(sord, uri(world, 1), NULL, uri(world, 3), g), + uri(world, 2))) { + return test_fail("Fail: Get 1,*,3 != 2\n"); + } else if (!sord_node_equals( + sord_get(sord, NULL, uri(world, 2), uri(world, 3), g), + uri(world, 1))) { + return test_fail("Fail: Get *,2,3 != 1\n"); + } + + for (unsigned i = 0; i < NUM_PATTERNS; ++i) { + QueryTest test = patterns[i]; + SordQuad pat = {test.query[0], test.query[1], test.query[2], g}; + fprintf(stderr, "Query " TUP_FMT "... ", TUP_FMT_ARGS(pat)); + + iter = sord_find(sord, pat); + int num_results = 0; + for (; !sord_iter_end(iter); sord_iter_next(iter)) { + sord_iter_get(iter, id); + ++num_results; + if (!sord_quad_match(pat, id)) { + sord_iter_free(iter); + return test_fail("Fail: Query result " TUP_FMT + " does not match pattern\n", + TUP_FMT_ARGS(id)); + } + } + sord_iter_free(iter); + if (num_results != test.expected_num_results) { + return test_fail("Fail: Expected %d results, got %d\n", + test.expected_num_results, + num_results); + } + fprintf(stderr, "OK (%i matches)\n", test.expected_num_results); + } + + // Query blank node subject + SordQuad pat = {sord_new_blank(world, USTR("ablank")), 0, 0}; + if (!pat[0]) { + return test_fail("Blank node subject lost\n"); + } + fprintf(stderr, "Query " TUP_FMT "... ", TUP_FMT_ARGS(pat)); + iter = sord_find(sord, pat); + int num_results = 0; + for (; !sord_iter_end(iter); sord_iter_next(iter)) { + sord_iter_get(iter, id); + ++num_results; + if (!sord_quad_match(pat, id)) { + sord_iter_free(iter); + return test_fail("Fail: Query result " TUP_FMT + " does not match pattern\n", + TUP_FMT_ARGS(id)); + } + } + fprintf(stderr, "OK\n"); + sord_node_free(world, (SordNode*)pat[0]); + sord_iter_free(iter); + if (num_results != 2) { + return test_fail("Blank node subject query failed\n"); + } + + // Test nested queries + fprintf(stderr, "Nested Queries... "); + const SordNode* last_subject = 0; + iter = sord_search(sord, NULL, NULL, NULL, NULL); + for (; !sord_iter_end(iter); sord_iter_next(iter)) { + sord_iter_get(iter, id); + if (id[0] == last_subject) { + continue; + } + + SordQuad subpat = {id[0], 0, 0}; + SordIter* subiter = sord_find(sord, subpat); + unsigned num_sub_results = 0; + if (sord_iter_get_node(subiter, SORD_SUBJECT) != id[0]) { + return test_fail("Fail: Incorrect initial submatch\n"); + } + for (; !sord_iter_end(subiter); sord_iter_next(subiter)) { + SordQuad subid; + sord_iter_get(subiter, subid); + if (!sord_quad_match(subpat, subid)) { + sord_iter_free(iter); + sord_iter_free(subiter); + return test_fail("Fail: Nested query result does not match pattern\n"); + } + ++num_sub_results; + } + sord_iter_free(subiter); + if (num_sub_results != N_OBJECTS_PER) { + return test_fail("Fail: Nested query " TUP_FMT " failed" + " (%u results, expected %u)\n", + TUP_FMT_ARGS(subpat), + num_sub_results, + N_OBJECTS_PER); + } + + uint64_t count = sord_count(sord, id[0], 0, 0, 0); + if (count != num_sub_results) { + return test_fail("Fail: Query " TUP_FMT " sord_count() %" PRIu64 + "does not match result count %u\n", + TUP_FMT_ARGS(subpat), + count, + num_sub_results); + } + + last_subject = id[0]; + } + fprintf(stderr, "OK\n\n"); + sord_iter_free(iter); + + return ret; +} + +static SerdStatus +unexpected_error(void* handle, const SerdError* error) +{ + (void)handle; + + fprintf(stderr, "unexpected error: "); + vfprintf(stderr, error->fmt, *error->args); + return SERD_SUCCESS; +} + +static SerdStatus +expected_error(void* handle, const SerdError* error) +{ + (void)handle; + + fprintf(stderr, "expected error: "); + vfprintf(stderr, error->fmt, *error->args); + ++n_expected_errors; + return SERD_SUCCESS; +} + +static int +finished(SordWorld* world, SordModel* sord, int status) +{ + sord_free(sord); + sord_world_free(world); + return status; +} + +int +main(void) +{ + static const unsigned n_quads = 300U; + + sord_free(NULL); // Shouldn't crash + + SordWorld* world = sord_world_new(); + + // Attempt to create invalid URI + fprintf(stderr, "expected "); + SordNode* bad_uri = sord_new_uri(world, USTR("noscheme")); + if (bad_uri) { + return test_fail("Successfully created invalid URI \"noscheme\"\n"); + } + sord_node_free(world, bad_uri); + + sord_world_set_error_sink(world, expected_error, NULL); + + // Attempt to create invalid CURIE + SerdNode base = serd_node_from_string(SERD_URI, USTR("http://example.org/")); + SerdEnv* env = serd_env_new(&base); + SerdNode sbad = serd_node_from_string(SERD_CURIE, USTR("bad:")); + SordNode* bad = sord_node_from_serd_node(world, env, &sbad, NULL, NULL); + if (bad) { + return test_fail("Successfully created CURIE with bad namespace\n"); + } + sord_node_free(world, bad); + serd_env_free(env); + + // Attempt to create node from garbage + SerdNode junk = SERD_NODE_NULL; + junk.type = (SerdType)1234; + if (sord_node_from_serd_node(world, env, &junk, NULL, NULL)) { + return test_fail("Successfully created node from garbage serd node\n"); + } + + // Attempt to create NULL node + SordNode* nil_node = + sord_node_from_serd_node(world, NULL, &SERD_NODE_NULL, NULL, NULL); + if (nil_node) { + return test_fail("Successfully created NULL node\n"); + } + sord_node_free(world, nil_node); + + // Check node flags are set properly + SordNode* with_newline = sord_new_literal(world, NULL, USTR("a\nb"), NULL); + if (!(sord_node_get_flags(with_newline) & SERD_HAS_NEWLINE)) { + return test_fail("Newline flag not set\n"); + } + SordNode* with_quote = sord_new_literal(world, NULL, USTR("a\"b"), NULL); + if (!(sord_node_get_flags(with_quote) & SERD_HAS_QUOTE)) { + return test_fail("Quote flag not set\n"); + } + + // Create with minimal indexing + SordModel* sord = sord_new(world, SORD_SPO, false); + generate(world, sord, n_quads, NULL); + + if (test_read(world, sord, NULL, n_quads)) { + sord_free(sord); + sord_world_free(world); + return EXIT_FAILURE; + } + + // Check adding tuples with NULL fields fails + sord_world_set_error_sink(world, expected_error, NULL); + const size_t initial_num_quads = sord_num_quads(sord); + SordQuad tup = {0, 0, 0, 0}; + if (sord_add(sord, tup)) { + return test_fail("Added NULL tuple\n"); + } + tup[0] = uri(world, 1); + if (sord_add(sord, tup)) { + return test_fail("Added tuple with NULL P and O\n"); + } + tup[1] = uri(world, 2); + if (sord_add(sord, tup)) { + return test_fail("Added tuple with NULL O\n"); + } + + if (sord_num_quads(sord) != initial_num_quads) { + return test_fail( + "Num quads %zu != %zu\n", sord_num_quads(sord), initial_num_quads); + } + + // Check adding tuples with an active iterator fails + SordIter* iter = sord_begin(sord); + tup[2] = uri(world, 3); + if (sord_add(sord, tup)) { + return test_fail("Added tuple with active iterator\n"); + } + + // Check removing tuples with several active iterator fails + SordIter* iter2 = sord_begin(sord); + if (!sord_erase(sord, iter)) { + return test_fail("Erased tuple with several active iterators\n"); + } + n_expected_errors = 0; + sord_remove(sord, tup); + if (n_expected_errors != 1) { + return test_fail("Removed tuple with several active iterators\n"); + } + sord_iter_free(iter); + sord_iter_free(iter2); + + sord_world_set_error_sink(world, unexpected_error, NULL); + + // Check interning merges equivalent values + SordNode* uri_id = sord_new_uri(world, USTR("http://example.org")); + SordNode* blank_id = sord_new_blank(world, USTR("testblank")); + SordNode* lit_id = sord_new_literal(world, uri_id, USTR("hello"), NULL); + if (sord_node_get_type(uri_id) != SORD_URI) { + return test_fail("URI node has incorrect type\n"); + } else if (sord_node_get_type(blank_id) != SORD_BLANK) { + return test_fail("Blank node has incorrect type\n"); + } else if (sord_node_get_type(lit_id) != SORD_LITERAL) { + return test_fail("Literal node has incorrect type\n"); + } + + const size_t initial_num_nodes = sord_num_nodes(world); + + SordNode* uri_id2 = sord_new_uri(world, USTR("http://example.org")); + SordNode* blank_id2 = sord_new_blank(world, USTR("testblank")); + SordNode* lit_id2 = sord_new_literal(world, uri_id, USTR("hello"), NULL); + if (uri_id2 != uri_id || !sord_node_equals(uri_id2, uri_id)) { + fprintf(stderr, "Fail: URI interning failed (duplicates)\n"); + return finished(world, sord, EXIT_FAILURE); + } else if (blank_id2 != blank_id || !sord_node_equals(blank_id2, blank_id)) { + fprintf(stderr, "Fail: Blank node interning failed (duplicates)\n"); + return finished(world, sord, EXIT_FAILURE); + } else if (lit_id2 != lit_id || !sord_node_equals(lit_id2, lit_id)) { + fprintf(stderr, "Fail: Literal interning failed (duplicates)\n"); + return finished(world, sord, EXIT_FAILURE); + } + + if (sord_num_nodes(world) != initial_num_nodes) { + return test_fail( + "Num nodes %zu != %zu\n", sord_num_nodes(world), initial_num_nodes); + } + + const uint8_t ni_hao[] = {0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD, 0}; + SordNode* chello = sord_new_literal(world, NULL, ni_hao, "cmn"); + + // Test literal length + size_t n_bytes; + size_t n_chars; + const uint8_t* str = sord_node_get_string_counted(lit_id2, &n_bytes); + if (strcmp((const char*)str, "hello")) { + return test_fail("Literal node corrupt\n"); + } else if (n_bytes != strlen("hello")) { + return test_fail("ASCII literal byte count incorrect\n"); + } + + str = sord_node_get_string_measured(lit_id2, &n_bytes, &n_chars); + if (n_bytes != strlen("hello") || n_chars != strlen("hello")) { + return test_fail("ASCII literal measured length incorrect\n"); + } else if (strcmp((const char*)str, "hello")) { + return test_fail("ASCII literal string incorrect\n"); + } + + str = sord_node_get_string_measured(chello, &n_bytes, &n_chars); + if (n_bytes != 6) { + return test_fail("Multi-byte literal byte count incorrect\n"); + } else if (n_chars != 2) { + return test_fail("Multi-byte literal character count incorrect\n"); + } else if (strcmp((const char*)str, (const char*)ni_hao)) { + return test_fail("Multi-byte literal string incorrect\n"); + } + + // Check interning doesn't clash non-equivalent values + SordNode* uri_id3 = sord_new_uri(world, USTR("http://example.orgX")); + SordNode* blank_id3 = sord_new_blank(world, USTR("testblankX")); + SordNode* lit_id3 = sord_new_literal(world, uri_id, USTR("helloX"), NULL); + if (uri_id3 == uri_id || sord_node_equals(uri_id3, uri_id)) { + fprintf(stderr, "Fail: URI interning failed (clash)\n"); + return finished(world, sord, EXIT_FAILURE); + } else if (blank_id3 == blank_id || sord_node_equals(blank_id3, blank_id)) { + fprintf(stderr, "Fail: Blank node interning failed (clash)\n"); + return finished(world, sord, EXIT_FAILURE); + } else if (lit_id3 == lit_id || sord_node_equals(lit_id3, lit_id)) { + fprintf(stderr, "Fail: Literal interning failed (clash)\n"); + return finished(world, sord, EXIT_FAILURE); + } + + // Check literal interning + SordNode* lit4 = sord_new_literal(world, NULL, USTR("hello"), NULL); + SordNode* lit5 = sord_new_literal(world, uri_id2, USTR("hello"), NULL); + SordNode* lit6 = sord_new_literal(world, NULL, USTR("hello"), "en-ca"); + if (lit4 == lit5 || sord_node_equals(lit4, lit5) || lit4 == lit6 || + sord_node_equals(lit4, lit6) || lit5 == lit6 || + sord_node_equals(lit5, lit6)) { + fprintf(stderr, "Fail: Literal interning failed (type/lang clash)\n"); + return finished(world, sord, EXIT_FAILURE); + } + + // Check relative URI construction + SordNode* reluri = + sord_new_relative_uri(world, USTR("a/b"), USTR("http://example.org/")); + if (strcmp((const char*)sord_node_get_string(reluri), + "http://example.org/a/b")) { + fprintf(stderr, + "Fail: Bad relative URI constructed: <%s>\n", + sord_node_get_string(reluri)); + return finished(world, sord, EXIT_FAILURE); + } + SordNode* reluri2 = sord_new_relative_uri( + world, USTR("http://drobilla.net/"), USTR("http://example.org/")); + if (strcmp((const char*)sord_node_get_string(reluri2), + "http://drobilla.net/")) { + fprintf(stderr, + "Fail: Bad relative URI constructed: <%s>\n", + sord_node_get_string(reluri)); + return finished(world, sord, EXIT_FAILURE); + } + + // Check comparison with NULL + sord_node_free(world, uri_id); + sord_node_free(world, blank_id); + sord_node_free(world, lit_id); + sord_node_free(world, uri_id2); + sord_node_free(world, blank_id2); + sord_node_free(world, lit_id2); + sord_node_free(world, uri_id3); + sord_node_free(world, blank_id3); + sord_node_free(world, lit_id3); + sord_free(sord); + + static const char* const index_names[6] = { + "spo", "sop", "ops", "osp", "pso", "pos"}; + + for (int i = 0; i < 6; ++i) { + sord = sord_new(world, (1 << i), false); + printf("Testing Index `%s'\n", index_names[i]); + generate(world, sord, n_quads, 0); + if (test_read(world, sord, 0, n_quads)) { + return finished(world, sord, EXIT_FAILURE); + } + sord_free(sord); + } + + static const char* const graph_index_names[6] = { + "gspo", "gsop", "gops", "gosp", "gpso", "gpos"}; + + for (int i = 0; i < 6; ++i) { + sord = sord_new(world, (1 << i), true); + printf("Testing Index `%s'\n", graph_index_names[i]); + SordNode* graph = uri(world, 42); + generate(world, sord, n_quads, graph); + if (test_read(world, sord, graph, n_quads)) { + return finished(world, sord, EXIT_FAILURE); + } + sord_free(sord); + } + + // Test removing + sord = sord_new(world, SORD_SPO, true); + tup[0] = uri(world, 1); + tup[1] = uri(world, 2); + tup[2] = sord_new_literal(world, 0, USTR("hello"), NULL); + tup[3] = 0; + sord_add(sord, tup); + if (!sord_ask(sord, tup[0], tup[1], tup[2], tup[3])) { + fprintf(stderr, "Failed to add tuple\n"); + return finished(world, sord, EXIT_FAILURE); + } + sord_node_free(world, (SordNode*)tup[2]); + tup[2] = sord_new_literal(world, 0, USTR("hi"), NULL); + sord_add(sord, tup); + sord_remove(sord, tup); + if (sord_num_quads(sord) != 1) { + fprintf( + stderr, "Remove failed (%zu quads, expected 1)\n", sord_num_quads(sord)); + return finished(world, sord, EXIT_FAILURE); + } + + iter = sord_find(sord, tup); + if (!sord_iter_end(iter)) { + fprintf(stderr, "Found removed tuple\n"); + return finished(world, sord, EXIT_FAILURE); + } + sord_iter_free(iter); + + // Test double remove (silent success) + sord_remove(sord, tup); + + // Load a couple graphs + SordNode* graph42 = uri(world, 42); + SordNode* graph43 = uri(world, 43); + generate(world, sord, 1, graph42); + generate(world, sord, 1, graph43); + + // Remove one graph via iterator + SerdStatus st; + iter = sord_search(sord, NULL, NULL, NULL, graph43); + while (!sord_iter_end(iter)) { + if ((st = sord_erase(sord, iter))) { + fprintf(stderr, "Remove by iterator failed (%s)\n", serd_strerror(st)); + return finished(world, sord, EXIT_FAILURE); + } + } + sord_iter_free(iter); + + // Erase the first tuple (an element in the default graph) + iter = sord_begin(sord); + if (sord_erase(sord, iter)) { + return test_fail("Failed to erase begin iterator on non-empty model\n"); + } + sord_iter_free(iter); + + // Ensure only the other graph is left + SordQuad quad; + SordQuad pat = {0, 0, 0, graph42}; + for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { + sord_iter_get(iter, quad); + if (!sord_quad_match(quad, pat)) { + fprintf(stderr, "Graph removal via iteration failed\n"); + return finished(world, sord, EXIT_FAILURE); + } + } + sord_iter_free(iter); + + // Load file into two separate graphs + sord_free(sord); + sord = sord_new(world, SORD_SPO, true); + env = serd_env_new(&base); + SordNode* graph1 = sord_new_uri(world, USTR("http://example.org/graph1")); + SordNode* graph2 = sord_new_uri(world, USTR("http://example.org/graph2")); + SerdReader* reader = sord_new_reader(sord, env, SERD_TURTLE, graph1); + if ((st = serd_reader_read_string(reader, USTR("

.")))) { + fprintf(stderr, "Failed to read string (%s)\n", serd_strerror(st)); + return finished(world, sord, EXIT_FAILURE); + } + serd_reader_free(reader); + reader = sord_new_reader(sord, env, SERD_TURTLE, graph2); + if ((st = serd_reader_read_string(reader, USTR("

.")))) { + fprintf(stderr, "Failed to re-read string (%s)\n", serd_strerror(st)); + return finished(world, sord, EXIT_FAILURE); + } + serd_reader_free(reader); + serd_env_free(env); + + // Ensure we only see triple once + size_t n_triples = 0; + for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { + fprintf(stderr, + "%s %s %s %s\n", + sord_node_get_string(sord_iter_get_node(iter, SORD_SUBJECT)), + sord_node_get_string(sord_iter_get_node(iter, SORD_PREDICATE)), + sord_node_get_string(sord_iter_get_node(iter, SORD_OBJECT)), + sord_node_get_string(sord_iter_get_node(iter, SORD_GRAPH))); + + ++n_triples; + } + sord_iter_free(iter); + if (n_triples != 1) { + fprintf(stderr, "Found duplicate triple\n"); + return finished(world, sord, EXIT_FAILURE); + } + + // Test SPO iteration on an SOP indexed store + sord_free(sord); + sord = sord_new(world, SORD_SOP, false); + generate(world, sord, 1, graph42); + for (iter = sord_begin(sord); !sord_iter_end(iter); sord_iter_next(iter)) { + ++n_triples; + } + sord_iter_free(iter); + + return finished(world, sord, EXIT_SUCCESS); +} diff --git a/waf b/waf deleted file mode 100755 index 887215c..0000000 --- a/waf +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env python - -# Minimal waf script for projects that include waflib directly - -import sys -import inspect -import os - -try: - from waflib import Context, Scripting -except Exception as e: - sys.stderr.write('error: Failed to import waf (%s)\n' % e) - if os.path.exists('.git'): - sys.stderr.write("Are submodules up to date? " - "Try 'git submodule update --init --recursive'\n") - - sys.exit(1) - - -def main(): - script_path = os.path.abspath(inspect.getfile(inspect.getmodule(main))) - project_path = os.path.dirname(script_path) - Scripting.waf_entry_point(os.getcwd(), Context.WAFVERSION, project_path) - - -if __name__ == '__main__': - main() diff --git a/waflib b/waflib deleted file mode 160000 index b600c92..0000000 --- a/waflib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b600c928b221a001faeab7bd92786d0b25714bc8 diff --git a/wscript b/wscript deleted file mode 100644 index e7d35dd..0000000 --- a/wscript +++ /dev/null @@ -1,413 +0,0 @@ -#!/usr/bin/env python - -import glob -import os -import sys - -from waflib import Logs, Options -from waflib.extras import autowaf - -# Library and package version (UNIX style major, minor, micro) -# major increment <=> incompatible changes -# minor increment <=> compatible changes (additions) -# micro increment <=> no interface changes -SORD_VERSION = '0.16.11' -SORD_MAJOR_VERSION = '0' - -# Mandatory waf variables -APPNAME = 'sord' # Package name for waf dist -VERSION = SORD_VERSION # Package version for waf dist -top = '.' # Source directory -out = 'build' # Build directory - -# Release variables -uri = 'http://drobilla.net/sw/sord' -dist_pattern = 'http://download.drobilla.net/sord-%d.%d.%d.tar.bz2' -post_tags = ['Hacking', 'RDF', 'Sord'] - -def options(ctx): - ctx.load('compiler_c') - ctx.load('compiler_cxx') - opt = ctx.configuration_options() - - ctx.add_flags( - opt, - {'no-utils': 'do not build command line utilities', - 'static': 'build static library', - 'no-shared': 'do not build shared library', - 'static-progs': 'build programs as static binaries'}) - - opt.add_option('--dump', type='string', default='', dest='dump', - help='dump debugging output (iter, search, write, all)') - -def configure(conf): - conf.load('compiler_c', cache=True) - if Options.options.build_tests: - try: - conf.load('compiler_cxx', cache=True) - except: - Logs.warn("No C++ compiler, sordmm.hpp compile test skipped") - pass - - conf.load('autowaf', cache=True) - autowaf.set_c_lang(conf, 'c99') - autowaf.set_cxx_lang(conf, 'c++14') - - conf.env.BUILD_UTILS = not Options.options.no_utils - conf.env.BUILD_SHARED = not Options.options.no_shared - conf.env.STATIC_PROGS = Options.options.static_progs - conf.env.BUILD_STATIC = (Options.options.static or - Options.options.static_progs) - - if Options.options.ultra_strict: - autowaf.add_compiler_flags(conf.env, '*', { - 'gcc': [ - '-Wno-cast-align', - '-Wno-cast-qual', - '-Wno-conversion', - '-Wno-inline', - '-Wno-padded', - '-Wno-sign-conversion', - '-Wno-stack-protector', - '-Wno-suggest-attribute=const', - '-Wno-suggest-attribute=pure', - '-Wno-switch-enum', - '-Wno-unused-macros', - '-Wno-unused-parameter', - '-Wno-vla', - ], - 'clang': [ - '-Wno-cast-align', - '-Wno-cast-qual', - '-Wno-format-nonliteral', - '-Wno-nullability-extension', - '-Wno-padded', - '-Wno-reserved-id-macro', - '-Wno-shorten-64-to-32', - '-Wno-sign-conversion', - '-Wno-switch-enum', - '-Wno-unused-macros', - '-Wno-unused-parameter', - '-Wno-vla', - ], - 'msvc': [ - '/wd4061', # enumerator in switch is not explicitly handled - '/wd4100', # unreferenced formal parameter - '/wd4200', # zero-sized array in struct/union - '/wd4244', # conversion with possible loss of data - '/wd4267', # conversion from size_t to a smaller type - '/wd4365', # signed/unsigned mismatch - '/wd4389', # '==': signed/unsigned mismatch - '/wd4514', # unreferenced inline function has been removed - '/wd4702', # unreachable code - '/wd4706', # assignment within conditional expression - '/wd4820', # padding added after construct - '/wd5045', # will insert Spectre mitigation for memory load - ], - }) - - autowaf.add_compiler_flags(conf.env, 'cxx', { - 'gcc': [ - '-Wno-effc++', - '-Wno-multiple-inheritance', - ], - 'clang': [ - '-Wno-implicit-float-conversion', - ], - 'msvc': [ - '/wd4571', # catch semantics changed - '/wd4625', # copy constructor implicitly deleted - '/wd4626', # assignment opreator implicitly deleted - ], - }) - - conf.check_pkg('serd-0 >= 0.30.0', uselib_store='SERD') - conf.check_pkg('libpcre', uselib_store='PCRE', mandatory=False) - - if conf.env.HAVE_PCRE: - if conf.check(cflags=['-pthread'], mandatory=False): - conf.env.PTHREAD_CFLAGS = ['-pthread'] - if conf.env.CC_NAME != 'clang': - conf.env.PTHREAD_LINKFLAGS = ['-pthread'] - elif conf.check(linkflags=['-lpthread'], mandatory=False): - conf.env.PTHREAD_CFLAGS = [] - conf.env.PTHREAD_LINKFLAGS = ['-lpthread'] - else: - conf.env.PTHREAD_CFLAGS = [] - conf.env.PTHREAD_LINKFLAGS = [] - - # Parse dump options and define things accordingly - dump = Options.options.dump.split(',') - all = 'all' in dump - if all or 'iter' in dump: - conf.define('SORD_DEBUG_ITER', 1) - if all or 'search' in dump: - conf.define('SORD_DEBUG_SEARCH', 1) - if all or 'write' in dump: - conf.define('SORD_DEBUG_WRITE', 1) - - # Set up environment for building/using as a subproject - autowaf.set_lib_env(conf, 'sord', SORD_VERSION, - include_path=str(conf.path.find_node('include'))) - - if conf.env.BUILD_UTILS and conf.env.HAVE_PCRE: - sord_validate_node = conf.path.get_bld().make_node('sord_validate') - conf.env.SORD_VALIDATE = [sord_validate_node.abspath()] - - conf.define('SORD_NO_DEFAULT_CONFIG', 1) - - autowaf.display_summary( - conf, - {'Static library': bool(conf.env.BUILD_STATIC), - 'Shared library': bool(conf.env.BUILD_SHARED), - 'Utilities': bool(conf.env.BUILD_UTILS), - 'Unit tests': bool(conf.env.BUILD_TESTS), - 'Debug dumping': dump}) - -def build(bld): - # C/C++ Headers - includedir = '${INCLUDEDIR}/sord-%s/sord' % SORD_MAJOR_VERSION - bld.install_files(includedir, bld.path.ant_glob('include/sord/*.h')) - bld.install_files(includedir, bld.path.ant_glob('include/sord/*.hpp')) - - # Pkgconfig file - autowaf.build_pc(bld, 'SORD', SORD_VERSION, SORD_MAJOR_VERSION, [], - {'SORD_MAJOR_VERSION' : SORD_MAJOR_VERSION, - 'SORD_PKG_DEPS' : 'serd-0'}) - - source = 'src/sord.c src/syntax.c' - - libflags = ['-fvisibility=hidden'] - libs = ['m'] - defines = [] - if bld.env.MSVC_COMPILER: - libflags = [] - libs = [] - defines = [] - - # Shared Library - if bld.env.BUILD_SHARED: - obj = bld(features = 'c cshlib', - source = source, - includes = ['.', 'include', './src'], - export_includes = ['.', 'include'], - name = 'libsord', - target = 'sord-%s' % SORD_MAJOR_VERSION, - vnum = SORD_VERSION, - install_path = '${LIBDIR}', - libs = libs, - uselib = 'SERD', - defines = defines + ['SORD_INTERNAL', 'ZIX_STATIC'], - cflags = libflags) - - # Static Library - if bld.env.BUILD_STATIC: - obj = bld(features = 'c cstlib', - source = source, - includes = ['.', 'include', './src'], - export_includes = ['.', 'include'], - name = 'libsord_static', - target = 'sord-%s' % SORD_MAJOR_VERSION, - vnum = SORD_VERSION, - install_path = '${LIBDIR}', - libs = libs, - uselib = 'SERD', - defines = ['SORD_STATIC', - 'SORD_INTERNAL', - 'ZIX_STATIC', - 'ZIX_INTERNAL']) - - if bld.env.BUILD_TESTS: - test_libs = libs - test_cflags = [''] - test_linkflags = [''] - if not bld.env.NO_COVERAGE: - test_cflags += ['--coverage'] - test_linkflags += ['--coverage'] - - # Profiled static library for test coverage - obj = bld(features = 'c cstlib', - source = source, - includes = ['.', 'include', './src'], - name = 'libsord_profiled', - target = 'sord_profiled', - install_path = '', - defines = defines + ['SORD_STATIC', - 'SORD_INTERNAL', - 'ZIX_STATIC', - 'ZIX_INTERNAL'], - cflags = test_cflags, - linkflags = test_linkflags, - lib = test_libs, - uselib = 'SERD') - - # Unit test program - obj = bld(features = 'c cprogram', - source = 'src/sord_test.c', - includes = ['.', 'include', './src'], - use = 'libsord_profiled', - lib = test_libs, - target = 'sord_test', - install_path = '', - defines = defines + ['SORD_STATIC', 'ZIX_STATIC'], - cflags = test_cflags, - linkflags = test_linkflags, - uselib = 'SERD') - - # Static profiled sordi for tests - obj = bld(features = 'c cprogram', - source = 'src/sordi.c', - includes = ['.', 'include', './src'], - use = 'libsord_profiled', - lib = test_libs, - target = 'sordi_static', - install_path = '', - defines = defines + ['SORD_STATIC', 'ZIX_STATIC'], - cflags = test_cflags, - linkflags = test_linkflags, - uselib = 'SERD') - - # C++ build test - if bld.env.COMPILER_CXX: - obj = bld(features = 'cxx cxxprogram', - source = 'src/sordmm_test.cpp', - includes = ['.', 'include', './src'], - use = 'libsord_profiled', - lib = test_libs, - target = 'sordmm_test', - install_path = '', - defines = defines + ['SORD_STATIC', 'ZIX_STATIC'], - cxxflags = test_cflags, - linkflags = test_linkflags, - uselib = 'SERD') - - # Utilities - if bld.env.BUILD_UTILS: - utils = ['sordi'] - if bld.env.HAVE_PCRE: - utils += ['sord_validate'] - for i in utils: - obj = bld(features = 'c cprogram', - source = 'src/%s.c' % i, - includes = ['.', 'include', './src'], - use = 'libsord', - lib = libs, - uselib = 'SERD', - target = i, - install_path = '${BINDIR}', - defines = defines) - if not bld.env.BUILD_SHARED or bld.env.STATIC_PROGS: - obj.use = 'libsord_static' - if bld.env.STATIC_PROGS: - obj.env.SHLIB_MARKER = obj.env.STLIB_MARKER - obj.linkflags = ['-static', '-Wl,--start-group'] - if i == 'sord_validate': - obj.uselib += ' PCRE' - obj.cflags = bld.env.PTHREAD_CFLAGS - obj.linkflags = bld.env.PTHREAD_LINKFLAGS - - # Documentation - autowaf.build_dox(bld, 'SORD', SORD_VERSION, top, out) - - # Man pages - bld.install_files('${MANDIR}/man1', bld.path.ant_glob('doc/*.1')) - - bld.add_post_fun(autowaf.run_ldconfig) - -def lint(ctx): - "checks code for style issues" - import subprocess - cmd = ("clang-tidy -p=. -header-filter=.* -checks=\"*," + - "-cert-dcl03-c," + - "-clang-analyzer-alpha.*," + - "-google-readability-todo," + - "-llvm-header-guard," + - "-llvm-include-order," + - "-misc-static-assert," + - "-misc-unused-parameters," + - "-readability-else-after-return\" " + - "../src/*.c") - subprocess.call(cmd, cwd='build', shell=True) - -def upload_docs(ctx): - os.system('rsync -ravz --delete -e ssh build/doc/html/ drobilla@drobilla.net:~/drobilla.net/docs/sord/') - for page in glob.glob('doc/*.[1-8]'): - os.system('soelim %s | pre-grohtml troff -man -wall -Thtml | post-grohtml > build/%s.html' % (page, page)) - os.system('rsync -avz --delete -e ssh build/%s.html drobilla@drobilla.net:~/drobilla.net/man/' % page) - -def test(tst): - import tempfile - try: - test_dir = os.path.join - os.mkdir('tests') - for i in glob.glob('tests/*.*'): - os.remove(i) - except: - pass - - srcdir = tst.path.abspath() - sordi = './sordi_static' - base = 'http://example.org/' - snippet = '<{0}s> <{0}p> <{0}o> .\n'.format(base) - manifest = 'file://%s/tests/manifest.ttl' % srcdir.replace('\\', '/') - - with tst.group('Unit') as check: - check(['./sord_test']) - - with tst.group('GoodCommands') as check: - check([sordi, manifest]) - check([sordi, '%s/tests/UTF-8.ttl' % srcdir]) - check([sordi, '-v']) - check([sordi, '-h']) - check([sordi, '-s', ' a <#Thingie> .', 'file:///test']) - check([sordi, os.devnull], stdout=os.devnull) - with tempfile.TemporaryFile(mode='r+') as stdin: - stdin.write(snippet + '\n') - check([sordi, '-', 'http://example.org/'], stdin=stdin) - check([sordi, '-o', 'turtle', '-', 'http://example.org/'], stdin=stdin) - - with tst.group('BadCommands', expected=1) as check: - check([sordi]) - check([sordi, 'ftp://example.org/unsupported.ttl']) - check([sordi, '-i']) - check([sordi, '-o']) - check([sordi, '-z']) - check([sordi, '-p']) - check([sordi, '-c']) - check([sordi, '-i illegal']) - check([sordi, '-o illegal']) - check([sordi, '-i turtle']) - check([sordi, '-i ntriples']) - check([sordi, '/no/such/file']) - - with tst.group('IoErrors', expected=1) as check: - check([sordi, 'file://%s/' % srcdir], name='Read directory') - if os.path.exists('/dev/full'): - check([sordi, manifest], stdout='/dev/full', name='Write error') - - with tst.group('good', verbosity=0) as check: - suite_base = 'http://www.w3.org/2001/sw/DataAccess/df1/' - good_tests = glob.glob(os.path.join(srcdir, 'tests', 'test-*.ttl')) - for test in good_tests: - path = os.path.relpath(test, srcdir) - base_uri = suite_base + path.replace('\\', '/') - - out_path = path + '.out' - check([sordi, test, base_uri], stdout=out_path) - - check_path = test.replace('.ttl', '.out') - out_lines = sorted(open(out_path).readlines()) - cmp_lines = sorted(open(check_path).readlines()) - check(lambda: out_lines == cmp_lines, - name='%s check' % path) - -def posts(ctx): - path = str(ctx.path.abspath()) - autowaf.news_to_posts( - os.path.join(path, 'NEWS'), - {'title' : 'Sord', - 'description' : autowaf.get_blurb(os.path.join(path, 'README')), - 'dist_pattern' : 'http://download.drobilla.net/sord-%s.tar.bz2'}, - { 'Author' : 'drobilla', - 'Tags' : 'Hacking, RDF, Sord' }, - os.path.join(out, 'posts')) -- cgit v1.2.1