test_codec_ttl.c 3.1 KB

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