test_graph.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "test.h"
  2. #include "graph.h"
  3. #define NUM_TRP 10
  4. static int _create_triples(LSUP_Triple *trp)
  5. {
  6. // These constitute overall 10 individual triples, 8 unique.
  7. trp[0].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:0", NULL, NULL);
  8. trp[0].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:0", NULL, NULL);
  9. trp[0].o = LSUP_term_new(LSUP_TERM_URI, "urn:o:0", NULL, NULL);
  10. trp[1].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:1", NULL, NULL);
  11. trp[1].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:1", NULL, NULL);
  12. trp[1].o = LSUP_term_new(LSUP_TERM_URI, "urn:o:1", NULL, NULL);
  13. trp[2].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:2", NULL, NULL);
  14. trp[2].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:2", NULL, NULL);
  15. trp[2].o = LSUP_term_new(LSUP_TERM_URI, "urn:o:2", NULL, NULL);
  16. trp[3].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:0", NULL, NULL);
  17. trp[3].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:1", NULL, NULL);
  18. trp[3].o = LSUP_term_new(LSUP_TERM_URI, "urn:o:2", NULL, NULL);
  19. trp[4].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:0", NULL, NULL);
  20. trp[4].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:2", NULL, NULL);
  21. trp[4].o = LSUP_term_new(
  22. LSUP_TERM_LITERAL, "String 1", NULL, NULL);
  23. trp[5].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:0", NULL, NULL);
  24. trp[5].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:5", NULL, NULL);
  25. trp[5].o = LSUP_term_new(
  26. LSUP_TERM_LITERAL, "String 2", "xsd:string", NULL);
  27. trp[6].s = LSUP_term_new(LSUP_TERM_URI, "urn:s:1", NULL, NULL);
  28. trp[6].p = LSUP_term_new(LSUP_TERM_URI, "urn:p:6", NULL, NULL);
  29. trp[6].o = LSUP_term_new(
  30. LSUP_TERM_LITERAL, "String 3", "xsd:string", "es-ES");
  31. // Let's reuse pointers. Do not double-free.
  32. trp[7].s = trp[0].s; // <urn:s:0>
  33. trp[7].p = trp[2].p; // <urn:p:2>
  34. trp[7].o = trp[5].o; // "String 2"^^xsd:string
  35. // Duplicate of trp[7]. Do not double-free.
  36. trp[8].s = trp[0].s;
  37. trp[8].p = trp[2].p;
  38. trp[8].o = trp[5].o;
  39. // Duplicate of trp[7] from different pointers with same value.
  40. // Do not double-free.
  41. trp[9].s = trp[5].s;
  42. trp[9].p = trp[4].p;
  43. trp[9].o = trp[5].o;
  44. return 0;
  45. }
  46. static void _free_triples(LSUP_Triple *trp)
  47. {
  48. // Last three triples are second pointers.
  49. for(int i=0; i < NUM_TRP - 3; i++) {
  50. LSUP_term_free(trp[i].s);
  51. LSUP_term_free(trp[i].p);
  52. LSUP_term_free(trp[i].o);
  53. }
  54. free(trp);
  55. }
  56. static int test_graph_heap()
  57. {
  58. LSUP_Graph *gr = LSUP_graph_new(10, "urn:gr:1", LSUP_STORE_MEM);
  59. ASSERT(strcmp(LSUP_graph_uri(gr)->data, "urn:gr:1") == 0, "Graph URI mismatch!");
  60. EXPECT_INT_EQ(LSUP_graph_size(gr), 0);
  61. LSUP_graph_free(gr);
  62. return 0;
  63. }
  64. static int test_graph_add()
  65. {
  66. LSUP_Triple *trp = malloc(NUM_TRP * sizeof(LSUP_Triple));
  67. _create_triples(trp);
  68. LSUP_Graph *gr = LSUP_graph_new(NUM_TRP + 2, "urn:gr:2", LSUP_STORE_MEM);
  69. LSUP_graph_add(gr, trp, NUM_TRP);
  70. for (int i = 0; i < sizeof(trp); i++) {
  71. printf("checking triple #%d... ", i);
  72. ASSERT(LSUP_graph_contains(gr, trp + i), "Triple not in graph!");
  73. printf("OK.\n");
  74. }
  75. _free_triples(trp); // gr copied data.
  76. EXPECT_INT_EQ(LSUP_graph_size(gr), 8);
  77. LSUP_graph_free(gr);
  78. return 0;
  79. }
  80. int graph_tests()
  81. {
  82. RUN(test_graph_heap);
  83. RUN(test_graph_add);
  84. return 0;
  85. }