test_codec_ttl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "test.h"
  2. #include "codec/codec_ttl.h"
  3. #define W3C_POS_TEST_CT 30
  4. #define W3C_NEG_TEST_CT 14
  5. #define W3C_TEST_BASE "http://www.w3.org/2001/sw/DataAccess/df1/tests/"
  6. #define TRP_CT 8
  7. static LSUP_Triple *trp[TRP_CT];
  8. /// Encode a graph as TTL.
  9. int
  10. test_encode_ttl_graph()
  11. {
  12. log_info ("Test encoding graph document.");
  13. LSUP_Graph *gr = LSUP_graph_new (NULL, NULL, NULL);
  14. if (!gr) return LSUP_MEM_ERR;
  15. size_t ins;
  16. LSUP_graph_add (gr, trp, &ins);
  17. char *out = calloc (1, 1);
  18. void *it = codec.encode_graph_init (gr);
  19. ASSERT (it != NULL, "Error creating codec iterator!");
  20. char *tmp = NULL;
  21. LSUP_rc rc;
  22. while ((rc = codec.encode_graph_iter (it, &tmp)) != LSUP_END) {
  23. log_info ("Serialized fragment: %s\n", tmp);
  24. ASSERT (rc >= 0, "Encoding step failed!");
  25. out = realloc (out, strlen(out) + strlen (tmp) + 1);
  26. out = strcat (out, tmp);
  27. }
  28. log_info ("Serialized graph: %s\n", out);
  29. codec.encode_graph_done (it);
  30. free (tmp);
  31. LSUP_graph_free (gr);
  32. /*
  33. for (int i = 0; i < 7; i++)
  34. ASSERT (strstr (out, end_nt_doc[i]) != NULL, end_nt_doc[i]);
  35. */
  36. free (out);
  37. return 0;
  38. }
  39. /// Positive test suite from W3C.
  40. int
  41. test_w3c_pos()
  42. {
  43. char test_fname[36], out_fname[36];
  44. LSUP_Graph *gr;
  45. size_t ct;
  46. char *err;
  47. char ch;
  48. for (int i = 0; i <= W3C_POS_TEST_CT; i++) {
  49. #if 1
  50. // Tests 14÷16 with 10K triples is quite long. Skip them temporarily.
  51. // TODO use a switch based on env var.
  52. if (i > 12 && i <17) continue;
  53. #endif
  54. size_t nt_ct = 0;
  55. sprintf (test_fname, "test/assets/ttl/test-%02d.ttl", i);
  56. sprintf (out_fname, "test/assets/ttl/test-%02d.out", i);
  57. FILE *test_stream = fopen (test_fname, "r");
  58. ASSERT (test_stream != NULL, "Error reading TTL evaluation file!");
  59. FILE *out_stream = fopen (out_fname, "r");
  60. ASSERT (out_stream != NULL, "Error reading TTL output file!");
  61. log_info ("Testing %s", test_fname);
  62. while ((ch=fgetc (out_stream)) != EOF) {
  63. if (ch == '\n') nt_ct++;
  64. }
  65. EXPECT_PASS (codec.decode_graph (test_stream, &gr, &ct, &err));
  66. EXPECT_INT_EQ (LSUP_graph_size (gr), nt_ct); // Just count NT lines.
  67. LSUP_graph_free (gr);
  68. fclose (test_stream);
  69. fclose (out_stream);
  70. free (err);
  71. }
  72. return 0;
  73. }
  74. /// Negative test suite from W3C.
  75. int
  76. test_w3c_neg()
  77. {
  78. char test_fname[36];
  79. LSUP_Graph *gr;
  80. size_t ct;
  81. char *err;
  82. for (int i = 0; i <= W3C_NEG_TEST_CT; i++) {
  83. sprintf (test_fname, "test/assets/ttl/bad-%02d.ttl", i);
  84. FILE *test_stream = fopen (test_fname, "r");
  85. log_info ("Testing %s", test_fname);
  86. LSUP_rc rc = codec.decode_graph (test_stream, &gr, &ct, &err);
  87. log_info ("rc: %d", rc);
  88. ASSERT (rc == LSUP_PARSE_ERR, "Bad test did not raise a parse error!");
  89. fclose (test_stream);
  90. free (err);
  91. }
  92. return 0;
  93. }
  94. int codec_ttl_tests()
  95. {
  96. LSUP_Term **terms = init_terms();
  97. init_triples (terms);
  98. codec = ttl_codec;
  99. RUN (test_encode_ttl_graph);
  100. RUN (test_decode_nt_graph);
  101. RUN (test_decode_nt_bad_graph);
  102. // TODO temporarily disabled; full W3C test suite at
  103. // https://w3c.github.io/rdf-tests/rdf/ shall replace these.
  104. //RUN (test_w3c_pos);
  105. //RUN (test_w3c_neg);
  106. free_terms(terms);
  107. for (int i = 0; i < TRP_CT; i++)
  108. free (trp[i]);
  109. return 0;
  110. }