test_codec_ttl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 fragment: %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. printf ("Serialized graph: %s\n", out);
  30. codec.encode_graph_done (it);
  31. free (tmp);
  32. LSUP_graph_free (gr);
  33. /*
  34. for (int i = 0; i < 7; i++)
  35. ASSERT (strstr (out, end_nt_doc[i]) != NULL, end_nt_doc[i]);
  36. */
  37. free (out);
  38. return 0;
  39. }
  40. /// Positive test suite from W3C.
  41. int
  42. test_w3c_pos()
  43. {
  44. char test_fname[36], out_fname[36];
  45. LSUP_Graph *gr;
  46. size_t ct;
  47. char *err;
  48. char ch;
  49. for (int i = 0; i <= W3C_POS_TEST_CT; i++) {
  50. #if 1
  51. // Tests 14÷16 with 10K triples is quite long. Skip them temporarily.
  52. // TODO use a switch based on env var.
  53. if (i > 12 && i <17) continue;
  54. #endif
  55. size_t nt_ct = 0;
  56. sprintf (test_fname, "test/assets/ttl/test-%02d.ttl", i);
  57. sprintf (out_fname, "test/assets/ttl/test-%02d.out", i);
  58. FILE *test_stream = fopen (test_fname, "r");
  59. FILE *out_stream = fopen (out_fname, "r");
  60. log_info ("Testing %s", test_fname);
  61. while ((ch=fgetc (out_stream)) != EOF) {
  62. if (ch == '\n') nt_ct++;
  63. }
  64. EXPECT_PASS (codec.decode_graph (test_stream, &gr, &ct, &err));
  65. EXPECT_INT_EQ (LSUP_graph_size (gr), nt_ct); // Just count NT lines.
  66. LSUP_graph_free (gr);
  67. fclose (test_stream);
  68. fclose (out_stream);
  69. free (err);
  70. }
  71. return 0;
  72. }
  73. /// Negative test suite from W3C.
  74. int
  75. test_w3c_neg()
  76. {
  77. char test_fname[36];
  78. LSUP_Graph *gr;
  79. size_t ct;
  80. char *err;
  81. for (int i = 0; i <= W3C_NEG_TEST_CT; i++) {
  82. sprintf (test_fname, "test/assets/ttl/bad-%02d.ttl", i);
  83. FILE *test_stream = fopen (test_fname, "r");
  84. log_info ("Testing %s", test_fname);
  85. LSUP_rc rc = codec.decode_graph (test_stream, &gr, &ct, &err);
  86. log_info ("rc: %d", rc);
  87. ASSERT (rc == LSUP_PARSE_ERR, "Bad test did not raise a parse error!");
  88. fclose (test_stream);
  89. free (err);
  90. }
  91. return 0;
  92. }
  93. int codec_ttl_tests()
  94. {
  95. LSUP_Term **terms = init_terms();
  96. init_triples (terms);
  97. codec = ttl_codec;
  98. RUN (test_encode_ttl_graph);
  99. //RUN (test_decode_nt_graph);
  100. //RUN (test_decode_nt_bad_graph);
  101. //RUN (test_w3c_pos);
  102. //RUN (test_w3c_neg);
  103. free_terms(terms);
  104. return 0;
  105. }