test_codec_ttl.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /// Positive test suite from W3C.
  8. int
  9. test_w3c_pos()
  10. {
  11. char test_fname[36], out_fname[36];
  12. LSUP_Graph *gr;
  13. size_t ct;
  14. char *err;
  15. char ch;
  16. for (int i = 0; i <= W3C_POS_TEST_CT; i++) {
  17. #if 1
  18. // Tests 14÷16 with 10K triples is quite long. Skip them temporarily.
  19. // TODO use a switch based on env var.
  20. if (i > 12 && i <17) continue;
  21. #endif
  22. size_t nt_ct = 0;
  23. sprintf (test_fname, "test/assets/ttl/test-%02d.ttl", i);
  24. sprintf (out_fname, "test/assets/ttl/test-%02d.out", i);
  25. FILE *test_stream = fopen (test_fname, "r");
  26. FILE *out_stream = fopen (out_fname, "r");
  27. log_info ("Testing %s", test_fname);
  28. while ((ch=fgetc (out_stream)) != EOF) {
  29. if (ch == '\n') nt_ct++;
  30. }
  31. EXPECT_PASS (codec.decode_graph (test_stream, &gr, &ct, &err));
  32. EXPECT_INT_EQ (LSUP_graph_size (gr), nt_ct); // Just count NT lines.
  33. }
  34. return 0;
  35. }
  36. /// Negative test suite from W3C.
  37. int
  38. test_w3c_neg()
  39. {
  40. char test_fname[36];
  41. LSUP_Graph *gr;
  42. size_t ct;
  43. char *err;
  44. for (int i = 0; i <= W3C_NEG_TEST_CT; i++) {
  45. sprintf (test_fname, "test/assets/ttl/bad-%02d.ttl", i);
  46. FILE *test_stream = fopen (test_fname, "r");
  47. log_info ("Testing %s", test_fname);
  48. LSUP_rc rc = codec.decode_graph (test_stream, &gr, &ct, &err);
  49. log_info ("rc: %d", rc);
  50. ASSERT (rc == LSUP_PARSE_ERR, "Bad test did not raise a parse error!");
  51. }
  52. for (int i = 0; i <= W3C_NEG_TEST_CT; i++) {
  53. sprintf (test_fname, "test/assets/ttl/bad-%02d.ttl", i);
  54. FILE *test_stream = fopen (test_fname, "r");
  55. log_info ("Testing %s", test_fname);
  56. LSUP_rc rc = codec.decode_graph (test_stream, &gr, &ct, &err);
  57. log_info ("rc: %d", rc);
  58. ASSERT (rc == LSUP_PARSE_ERR, "Bad test did not raise a parse error!");
  59. }
  60. return 0;
  61. }
  62. int codec_ttl_tests()
  63. {
  64. LSUP_Term **terms = init_terms();
  65. init_triples (terms);
  66. codec = ttl_codec;
  67. RUN (test_w3c_pos);
  68. RUN (test_w3c_neg);
  69. //RUN (test_encode_nt_graph);
  70. RUN (test_decode_nt_graph);
  71. RUN (test_decode_nt_bad_graph);
  72. free_terms(terms);
  73. return 0;
  74. }