diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/extra/canon/bad-boolean.ttl | 5 | ||||
-rw-r--r-- | test/extra/canon/bad-decimal-leading.ttl | 4 | ||||
-rw-r--r-- | test/extra/canon/bad-decimal-trailing.ttl | 4 | ||||
-rw-r--r-- | test/extra/canon/bad-empty-boolean.ttl | 5 | ||||
-rw-r--r-- | test/extra/canon/bad-integer-leading.ttl | 4 | ||||
-rw-r--r-- | test/extra/canon/bad-integer-trailing.ttl | 4 | ||||
-rw-r--r-- | test/extra/canon/bad-lang-long.ttl | 3 | ||||
-rw-r--r-- | test/extra/canon/manifest.ttl | 65 | ||||
-rw-r--r-- | test/extra/canon/test-canon.nt | 70 | ||||
-rw-r--r-- | test/extra/canon/test-canon.ttl | 76 | ||||
-rw-r--r-- | test/meson.build | 9 | ||||
-rw-r--r-- | test/test_canon.c | 103 |
12 files changed, 352 insertions, 0 deletions
diff --git a/test/extra/canon/bad-boolean.ttl b/test/extra/canon/bad-boolean.ttl new file mode 100644 index 00000000..c4fc3eb5 --- /dev/null +++ b/test/extra/canon/bad-boolean.ttl @@ -0,0 +1,5 @@ +@base <http://example.org/> . +@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . + +[] <boolean> " ja "^^xsd:boolean . + diff --git a/test/extra/canon/bad-decimal-leading.ttl b/test/extra/canon/bad-decimal-leading.ttl new file mode 100644 index 00000000..0d18eac7 --- /dev/null +++ b/test/extra/canon/bad-decimal-leading.ttl @@ -0,0 +1,4 @@ +@base <http://example.org/> . +@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . + +[] <decimal> " junk 1234.5678 "^^xsd:decimal . diff --git a/test/extra/canon/bad-decimal-trailing.ttl b/test/extra/canon/bad-decimal-trailing.ttl new file mode 100644 index 00000000..10882ef5 --- /dev/null +++ b/test/extra/canon/bad-decimal-trailing.ttl @@ -0,0 +1,4 @@ +@base <http://example.org/> . +@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . + +[] <decimal> " 1234.5678 junk "^^xsd:decimal . diff --git a/test/extra/canon/bad-empty-boolean.ttl b/test/extra/canon/bad-empty-boolean.ttl new file mode 100644 index 00000000..9a390c46 --- /dev/null +++ b/test/extra/canon/bad-empty-boolean.ttl @@ -0,0 +1,5 @@ +@base <http://example.org/> . +@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . + +[] <boolean> ""^^xsd:boolean . + diff --git a/test/extra/canon/bad-integer-leading.ttl b/test/extra/canon/bad-integer-leading.ttl new file mode 100644 index 00000000..80c1a6af --- /dev/null +++ b/test/extra/canon/bad-integer-leading.ttl @@ -0,0 +1,4 @@ +@base <http://example.org/> . +@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . + +[] <integer> " junk 987654321 "^^xsd:integer . diff --git a/test/extra/canon/bad-integer-trailing.ttl b/test/extra/canon/bad-integer-trailing.ttl new file mode 100644 index 00000000..a94a9ec4 --- /dev/null +++ b/test/extra/canon/bad-integer-trailing.ttl @@ -0,0 +1,4 @@ +@base <http://example.org/> . +@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . + +[] <integer> " 987654321 junk "^^xsd:integer . diff --git a/test/extra/canon/bad-lang-long.ttl b/test/extra/canon/bad-lang-long.ttl new file mode 100644 index 00000000..f84df07f --- /dev/null +++ b/test/extra/canon/bad-lang-long.ttl @@ -0,0 +1,3 @@ +@base <http://example.org/> . + +[] <tagged> "hello"@ridiculously-long-lang-tag-beyond-even-RFC4646-recommendation . diff --git a/test/extra/canon/manifest.ttl b/test/extra/canon/manifest.ttl new file mode 100644 index 00000000..143928ee --- /dev/null +++ b/test/extra/canon/manifest.ttl @@ -0,0 +1,65 @@ +@prefix mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> . +@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . +@prefix rdft: <http://www.w3.org/ns/rdftest#> . + +<> + a mf:Manifest ; + rdfs:comment "Serd canonical literal writing test suite" ; + mf:entries ( + <#bad-boolean> + <#bad-decimal-leading> + <#bad-decimal-trailing> + <#bad-empty-boolean> + <#bad-integer-leading> + <#bad-integer-trailing> + <#bad-lang-long> + <#test-canon> + ) . + +<#bad-boolean> + a rdft:TestTurtleNegativeEval ; + rdfs:comment "Invalid xsd::boolean syntax" ; + mf:action <bad-boolean.ttl> ; + mf:name "bad-boolean" . + +<#bad-decimal-leading> + a rdft:TestTurtleNegativeEval ; + rdfs:comment "Invalid xsd::decimal syntax (leading garbage)" ; + mf:action <bad-decimal-leading.ttl> ; + mf:name "bad-decimal-leading" . + +<#bad-decimal-trailing> + a rdft:TestTurtleNegativeEval ; + rdfs:comment "Invalid xsd::decimal syntax (trailing garbage)" ; + mf:action <bad-decimal-trailing.ttl> ; + mf:name "bad-decimal-trailing" . + +<#bad-empty-boolean> + a rdft:TestTurtleNegativeEval ; + rdfs:comment "Invalid xsd::boolean syntax (no value)" ; + mf:action <bad-empty-boolean.ttl> ; + mf:name "bad-empty-boolean" . + +<#bad-integer-leading> + a rdft:TestTurtleNegativeEval ; + rdfs:comment "Invalid xsd::integer syntax (leading garbage)" ; + mf:action <bad-integer-leading.ttl> ; + mf:name "bad-integer-leading" . + +<#bad-integer-trailing> + a rdft:TestTurtleNegativeEval ; + rdfs:comment "Invalid xsd::integer syntax (trailing garbage)" ; + mf:action <bad-integer-trailing.ttl> ; + mf:name "bad-integer-trailing" . + +<#bad-lang-long> + a rdft:TestTurtleNegativeEval ; + rdfs:comment "Overly long language tag" ; + mf:action <bad-lang-long.ttl> ; + mf:name "bad-lang-long" . + +<#test-canon> + a rdft:TestTurtleEval ; + mf:action <test-canon.ttl> ; + mf:name "test-canon" ; + mf:result <test-canon.nt> . diff --git a/test/extra/canon/test-canon.nt b/test/extra/canon/test-canon.nt new file mode 100644 index 00000000..ff492890 --- /dev/null +++ b/test/extra/canon/test-canon.nt @@ -0,0 +1,70 @@ +_:b1 <http://example.org/boolean> "false"^^<http://www.w3.org/2001/XMLSchema#boolean> . +_:b1 <http://example.org/boolean> "false"^^<http://www.w3.org/2001/XMLSchema#boolean> . +_:b1 <http://example.org/boolean> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> . +_:b1 <http://example.org/boolean> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> . +_:b1 <http://example.org/ieee754> "1.0E2"^^<http://www.w3.org/2001/XMLSchema#float> . +_:b1 <http://example.org/ieee754> "-1.0E2"^^<http://www.w3.org/2001/XMLSchema#float> . +_:b1 <http://example.org/ieee754> "1.0E3"^^<http://www.w3.org/2001/XMLSchema#double> . +_:b1 <http://example.org/ieee754> "-1.0E3"^^<http://www.w3.org/2001/XMLSchema#double> . +_:b1 <http://example.org/machine> "9223372036854775807"^^<http://www.w3.org/2001/XMLSchema#long> . +_:b1 <http://example.org/machine> "-9223372036854775808"^^<http://www.w3.org/2001/XMLSchema#long> . +_:b1 <http://example.org/machine> "2147483647"^^<http://www.w3.org/2001/XMLSchema#int> . +_:b1 <http://example.org/machine> "-2147483648"^^<http://www.w3.org/2001/XMLSchema#int> . +_:b1 <http://example.org/machine> "32767"^^<http://www.w3.org/2001/XMLSchema#short> . +_:b1 <http://example.org/machine> "-32768"^^<http://www.w3.org/2001/XMLSchema#short> . +_:b1 <http://example.org/machine> "127"^^<http://www.w3.org/2001/XMLSchema#byte> . +_:b1 <http://example.org/machine> "-128"^^<http://www.w3.org/2001/XMLSchema#byte> . +_:b1 <http://example.org/machine> "1"^^<http://www.w3.org/2001/XMLSchema#unsignedLong> . +_:b1 <http://example.org/machine> "18446744073709551615"^^<http://www.w3.org/2001/XMLSchema#unsignedLong> . +_:b1 <http://example.org/machine> "1"^^<http://www.w3.org/2001/XMLSchema#unsignedInt> . +_:b1 <http://example.org/machine> "4294967295"^^<http://www.w3.org/2001/XMLSchema#unsignedInt> . +_:b1 <http://example.org/machine> "1"^^<http://www.w3.org/2001/XMLSchema#unsignedShort> . +_:b1 <http://example.org/machine> "65535"^^<http://www.w3.org/2001/XMLSchema#unsignedShort> . +_:b1 <http://example.org/machine> "1"^^<http://www.w3.org/2001/XMLSchema#unsignedByte> . +_:b1 <http://example.org/machine> "255"^^<http://www.w3.org/2001/XMLSchema#unsignedByte> . +_:b1 <http://example.org/decimal> "0.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "0.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "-0.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "36893488147419103232.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "36893488147419103232.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "36893488147419103232.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "36893488147419103232.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "36893488147419103232.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "36893488147419103232.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "36893488147419103232.123"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "-36893488147419103232.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "-36893488147419103232.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "-36893488147419103232.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "-36893488147419103232.0"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "-36893488147419103232.123"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "0.123"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "0.123"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "0.123"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "0.123"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "-0.123"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/decimal> "-0.123"^^<http://www.w3.org/2001/XMLSchema#decimal> . +_:b1 <http://example.org/integer> "36893488147419103232"^^<http://www.w3.org/2001/XMLSchema#integer> . +_:b1 <http://example.org/integer> "36893488147419103232"^^<http://www.w3.org/2001/XMLSchema#integer> . +_:b1 <http://example.org/integer> "36893488147419103232"^^<http://www.w3.org/2001/XMLSchema#integer> . +_:b1 <http://example.org/integer> "36893488147419103232"^^<http://www.w3.org/2001/XMLSchema#integer> . +_:b1 <http://example.org/integer> "-36893488147419103232"^^<http://www.w3.org/2001/XMLSchema#integer> . +_:b1 <http://example.org/integer> "-36893488147419103232"^^<http://www.w3.org/2001/XMLSchema#integer> . +_:b1 <http://example.org/integer> "0"^^<http://www.w3.org/2001/XMLSchema#nonPositiveInteger> . +_:b1 <http://example.org/integer> "-36893488147419103232"^^<http://www.w3.org/2001/XMLSchema#nonPositiveInteger> . +_:b1 <http://example.org/integer> "-1"^^<http://www.w3.org/2001/XMLSchema#negativeInteger> . +_:b1 <http://example.org/integer> "-36893488147419103232"^^<http://www.w3.org/2001/XMLSchema#negativeInteger> . +_:b1 <http://example.org/integer> "0"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> . +_:b1 <http://example.org/integer> "36893488147419103232"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> . +_:b1 <http://example.org/integer> "1"^^<http://www.w3.org/2001/XMLSchema#positiveInteger> . +_:b1 <http://example.org/integer> "36893488147419103232"^^<http://www.w3.org/2001/XMLSchema#positiveInteger> . +_:b1 <http://example.org/langString> "no language tag" . +_:b1 <http://example.org/taggedString> "english"@en-ca . +_:b1 <http://example.org/time> "P1Y6M"^^<http://www.w3.org/2001/XMLSchema#duration> . +_:b1 <http://example.org/time> "12:15:01Z"^^<http://www.w3.org/2001/XMLSchema#time> . +_:b1 <http://example.org/time> "2004-04-12Z"^^<http://www.w3.org/2001/XMLSchema#date> . +_:b1 <http://example.org/binary> "A1B7F080"^^<http://www.w3.org/2001/XMLSchema#hexBinary> . +_:b1 <http://example.org/binary> "Zm9vYmF="^^<http://www.w3.org/2001/XMLSchema#base64Binary> . +_:b1 <http://example.org/other> "untyped" . +_:b1 <http://example.org/other> <http://example.org/uri> . +_:b1 <http://example.org/other> "notxsd"^^<http://example.org/sometype> . +_:b1 <http://example.org/other> "unsupported"^^<http://www.w3.org/2001/XMLSchema#name> . diff --git a/test/extra/canon/test-canon.ttl b/test/extra/canon/test-canon.ttl new file mode 100644 index 00000000..0d0b4682 --- /dev/null +++ b/test/extra/canon/test-canon.ttl @@ -0,0 +1,76 @@ +@base <http://example.org/> . +@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . +@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . + +[ + <boolean> " false "^^xsd:boolean , + " 0 "^^xsd:boolean , + " true "^^xsd:boolean , + " 1 "^^xsd:boolean ; + <ieee754> " +0100.0 "^^xsd:float , + " -0100.0 "^^xsd:float , + " +01000.0 "^^xsd:double , + " -01000.0 "^^xsd:double ; + <machine> " +09223372036854775807 "^^xsd:long , + " -09223372036854775808 "^^xsd:long , + " +02147483647 "^^xsd:int , + " -02147483648 "^^xsd:int , + " +032767 "^^xsd:short , + " -032768 "^^xsd:short , + " +0127 "^^xsd:byte , + " -0128 "^^xsd:byte , + " 01 "^^xsd:unsignedLong , + " 018446744073709551615 "^^xsd:unsignedLong , + " 01 "^^xsd:unsignedInt , + " 04294967295 "^^xsd:unsignedInt , + " 01 "^^xsd:unsignedShort , + " 065535 "^^xsd:unsignedShort , + " 01 "^^xsd:unsignedByte , + " 0255 "^^xsd:unsignedByte ; + <decimal> " 00 "^^xsd:decimal , + " +0 "^^xsd:decimal , + " -0 "^^xsd:decimal , + " 36893488147419103232 "^^xsd:decimal , + " 0036893488147419103232 "^^xsd:decimal , + " +36893488147419103232 "^^xsd:decimal , + " +0036893488147419103232 "^^xsd:decimal , + " +0036893488147419103232. "^^xsd:decimal , + " +0036893488147419103232.00 "^^xsd:decimal , + " +0036893488147419103232.12300 "^^xsd:decimal , + " -36893488147419103232 "^^xsd:decimal , + " -0036893488147419103232 "^^xsd:decimal , + " -0036893488147419103232. "^^xsd:decimal , + " -0036893488147419103232.00 "^^xsd:decimal , + " -0036893488147419103232.12300 "^^xsd:decimal , + " 00.12300 "^^xsd:decimal , + " .12300 "^^xsd:decimal , + " +.12300 "^^xsd:decimal , + " +00.12300 "^^xsd:decimal , + " -.12300 "^^xsd:decimal , + " -00.12300 "^^xsd:decimal ; + <integer> " 36893488147419103232 "^^xsd:integer , + " 0036893488147419103232 "^^xsd:integer , + " +36893488147419103232 "^^xsd:integer , + " +0036893488147419103232 "^^xsd:integer , + " -36893488147419103232 "^^xsd:integer , + " -0036893488147419103232 "^^xsd:integer , + " 00 "^^xsd:nonPositiveInteger , + " -036893488147419103232 "^^xsd:nonPositiveInteger , + " -01 "^^xsd:negativeInteger , + " -036893488147419103232 "^^xsd:negativeInteger , + " 00 "^^xsd:nonNegativeInteger , + " 036893488147419103232 "^^xsd:nonNegativeInteger , + " +01 "^^xsd:positiveInteger , + " 036893488147419103232 "^^xsd:positiveInteger ; + <langString> "no language tag"^^rdf:langString ; + <taggedString> "english"@EN-CA ; + <time> " P1Y6M0D "^^xsd:duration , + " 12:15:01+00:00 "^^xsd:time , + " 02004-04-12+00:00 "^^xsd:date ; + <binary> "A 1 B7 F080"^^xsd:hexBinary , + " Zm 9v Y m F="^^xsd:base64Binary ; + <other> "untyped" , + <uri> , + "notxsd"^^<sometype> , + "unsupported"^^xsd:name +] . diff --git a/test/meson.build b/test/meson.build index 5c0ac7e8..24180efa 100644 --- a/test/meson.build +++ b/test/meson.build @@ -29,6 +29,7 @@ ttl_metadata_file_paths = [ 'extra/abbreviate/manifest.ttl', 'extra/bad/manifest.ttl', 'extra/big/manifest.ttl', + 'extra/canon/manifest.ttl', 'extra/full/manifest.ttl', 'extra/good/manifest.ttl', 'extra/lax/manifest.ttl', @@ -124,6 +125,7 @@ subdir('headers') ############## unit_tests = [ + 'canon', 'caret', 'env', 'free_null', @@ -444,6 +446,13 @@ test_suites = { '-a', ['-b', '1'], ], + 'canon': [ + files('extra/canon/manifest.ttl'), + ns_serdtest + 'canon/', + '--', + '-C', + '-a', + ], 'fast': [ files('extra/perfect/manifest.ttl'), ns_serdtest + 'perfect/', diff --git a/test/test_canon.c b/test/test_canon.c new file mode 100644 index 00000000..1a569664 --- /dev/null +++ b/test/test_canon.c @@ -0,0 +1,103 @@ +// Copyright 2021 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#undef NDEBUG + +#include "failing_allocator.h" + +#include "serd/canon.h" +#include "serd/event.h" +#include "serd/node.h" +#include "serd/sink.h" +#include "serd/status.h" +#include "serd/string_view.h" +#include "serd/world.h" + +#include <assert.h> +#include <stddef.h> + +static SerdStatus +ignore_event(void* handle, const SerdEvent* event) +{ + (void)handle; + (void)event; + return SERD_SUCCESS; +} + +static void +test_new_failed_alloc(void) +{ + SerdFailingAllocator allocator = serd_failing_allocator(); + + SerdWorld* const world = serd_world_new(&allocator.base); + + SerdSink* target = serd_sink_new(&allocator.base, NULL, ignore_event, NULL); + const size_t n_setup_allocs = allocator.n_allocations; + + // Successfully allocate a canon to count the number of allocations + SerdSink* canon = serd_canon_new(world, target, 0U); + assert(canon); + + // Test that each allocation failing is handled gracefully + const size_t n_new_allocs = allocator.n_allocations - n_setup_allocs; + for (size_t i = 0; i < n_new_allocs; ++i) { + allocator.n_remaining = i; + assert(!serd_canon_new(world, target, 0U)); + } + + serd_sink_free(canon); + serd_sink_free(target); + serd_world_free(world); +} + +static void +test_write_failed_alloc(void) +{ + const SerdStringView s_string = serd_string("http://example.org/s"); + const SerdStringView p_string = serd_string("http://example.org/p"); + const SerdStringView o_string = serd_string("012.340"); + const SerdStringView xsd_float = + serd_string("http://www.w3.org/2001/XMLSchema#float"); + + SerdFailingAllocator allocator = serd_failing_allocator(); + + SerdWorld* const world = serd_world_new(&allocator.base); + + const SerdNode* const s = + serd_node_new(&allocator.base, serd_a_uri(s_string)); + + const SerdNode* const p = + serd_node_new(&allocator.base, serd_a_uri(p_string)); + + const SerdNode* const o = + serd_node_new(&allocator.base, serd_a_typed_literal(o_string, xsd_float)); + + SerdSink* target = serd_sink_new(&allocator.base, NULL, ignore_event, NULL); + SerdSink* canon = serd_canon_new(world, target, 0U); + const size_t n_setup_allocs = allocator.n_allocations; + + // Successfully write statement to count the number of allocations + assert(canon); + assert(!serd_sink_write(canon, 0U, s, p, o, NULL)); + + // Test that each allocation failing is handled gracefully + const size_t n_new_allocs = allocator.n_allocations - n_setup_allocs; + for (size_t i = 0; i < n_new_allocs; ++i) { + allocator.n_remaining = i; + + const SerdStatus st = serd_sink_write(canon, 0U, s, p, o, NULL); + assert(st == SERD_BAD_ALLOC); + } + + serd_sink_free(canon); + serd_sink_free(target); + serd_world_free(world); +} + +int +main(void) +{ + test_new_failed_alloc(); + test_write_failed_alloc(); + return 0; +} |