123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #include "test.h"
- #include "codec/codec_ttl.h"
- #define W3C_POS_TEST_CT 30
- #define W3C_NEG_TEST_CT 14
- #define W3C_TEST_BASE "http://www.w3.org/2001/sw/DataAccess/df1/tests/"
- #define TRP_CT 8
- static LSUP_Triple *trp[TRP_CT];
- /// Encode a graph as TTL.
- int
- test_encode_ttl_graph()
- {
- log_info ("Test encoding graph document.");
- LSUP_Graph *gr = LSUP_graph_new (NULL, NULL, NULL);
- if (!gr) return LSUP_MEM_ERR;
- size_t ins;
- LSUP_graph_add (gr, trp, &ins);
- char *out = calloc (1, 1);
- void *it = codec.encode_graph_init (gr);
- ASSERT (it != NULL, "Error creating codec iterator!");
- char *tmp = NULL;
- LSUP_rc rc;
- while ((rc = codec.encode_graph_iter (it, &tmp)) != LSUP_END) {
- log_info ("Serialized fragment: %s\n", tmp);
- ASSERT (rc >= 0, "Encoding step failed!");
- out = realloc (out, strlen(out) + strlen (tmp) + 1);
- out = strcat (out, tmp);
- }
- log_info ("Serialized graph: %s\n", out);
- codec.encode_graph_done (it);
- free (tmp);
- LSUP_graph_free (gr);
- /*
- for (int i = 0; i < 7; i++)
- ASSERT (strstr (out, end_nt_doc[i]) != NULL, end_nt_doc[i]);
- */
- free (out);
- return 0;
- }
- /// Positive test suite from W3C.
- int
- test_w3c_pos()
- {
- char test_fname[36], out_fname[36];
- LSUP_Graph *gr;
- size_t ct;
- char *err;
- char ch;
- for (int i = 0; i <= W3C_POS_TEST_CT; i++) {
- #if 1
- // Tests 14÷16 with 10K triples is quite long. Skip them temporarily.
- // TODO use a switch based on env var.
- if (i > 12 && i <17) continue;
- #endif
- size_t nt_ct = 0;
- sprintf (test_fname, "test/assets/ttl/test-%02d.ttl", i);
- sprintf (out_fname, "test/assets/ttl/test-%02d.out", i);
- FILE *test_stream = fopen (test_fname, "r");
- FILE *out_stream = fopen (out_fname, "r");
- log_info ("Testing %s", test_fname);
- while ((ch=fgetc (out_stream)) != EOF) {
- if (ch == '\n') nt_ct++;
- }
- EXPECT_PASS (codec.decode_graph (test_stream, &gr, &ct, &err));
- EXPECT_INT_EQ (LSUP_graph_size (gr), nt_ct); // Just count NT lines.
- LSUP_graph_free (gr);
- fclose (test_stream);
- fclose (out_stream);
- free (err);
- }
- return 0;
- }
- /// Negative test suite from W3C.
- int
- test_w3c_neg()
- {
- char test_fname[36];
- LSUP_Graph *gr;
- size_t ct;
- char *err;
- for (int i = 0; i <= W3C_NEG_TEST_CT; i++) {
- sprintf (test_fname, "test/assets/ttl/bad-%02d.ttl", i);
- FILE *test_stream = fopen (test_fname, "r");
- log_info ("Testing %s", test_fname);
- LSUP_rc rc = codec.decode_graph (test_stream, &gr, &ct, &err);
- log_info ("rc: %d", rc);
- ASSERT (rc == LSUP_PARSE_ERR, "Bad test did not raise a parse error!");
- fclose (test_stream);
- free (err);
- }
- return 0;
- }
- int codec_ttl_tests()
- {
- LSUP_Term **terms = init_terms();
- init_triples (terms);
- codec = ttl_codec;
- RUN (test_encode_ttl_graph);
- RUN (test_decode_nt_graph);
- RUN (test_decode_nt_bad_graph);
- RUN (test_w3c_pos);
- RUN (test_w3c_neg);
- free_terms(terms);
- for (int i = 0; i < TRP_CT; i++)
- free (trp[i]);
- return 0;
- }
|