test_graph.c 3.1 KB

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