test_graph.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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), "urn:gr:1") == 0, "Graph URI mismatch!");
  60. EXPECT_INT_EQ(LSUP_graph_capacity(gr), 10);
  61. EXPECT_INT_EQ(LSUP_graph_size(gr), 0);
  62. LSUP_graph_free(gr);
  63. return 0;
  64. }
  65. static int test_graph_add()
  66. {
  67. LSUP_Triple *trp = malloc(NUM_TRP * sizeof(LSUP_Triple));
  68. _create_triples(trp);
  69. LSUP_Graph *gr = LSUP_graph_new(NUM_TRP + 2, "urn:gr:2", LSUP_STORE_MEM);
  70. LSUP_graph_add(gr, trp, NUM_TRP);
  71. _free_triples(trp); // gr takes ownership of data.
  72. EXPECT_INT_EQ(LSUP_graph_capacity(gr), NUM_TRP + 2);
  73. EXPECT_INT_EQ(LSUP_graph_size(gr), 8);
  74. LSUP_graph_free(gr);
  75. return 0;
  76. }
  77. static int test_graph_add_100k()
  78. {
  79. size_t nt = 10000;
  80. LSUP_Triple *trp = malloc(nt * sizeof(LSUP_Triple));
  81. for (size_t i; i < nt; i++) {
  82. //printf("i: %lu\n", i);
  83. trp[i].s = LSUP_term_new(
  84. LSUP_TERM_URI, LSUP_term_gen_random_str(), NULL, NULL);
  85. trp[i].p = LSUP_term_new(
  86. LSUP_TERM_URI, LSUP_term_gen_random_str(), NULL, NULL);
  87. trp[i].o = LSUP_term_new(
  88. LSUP_TERM_URI, LSUP_term_gen_random_str(), NULL, NULL);
  89. }
  90. TRACE(STR, "Triples generated.");
  91. LSUP_Graph *gr = LSUP_graph_new(nt, NULL, LSUP_STORE_MEM);
  92. LSUP_graph_add(gr, trp, nt);
  93. TRACE(STR, "Graph populated.");
  94. _free_triples(trp); // gr takes ownership of data.
  95. LSUP_graph_free(gr);
  96. return 0;
  97. }
  98. int graph_tests()
  99. {
  100. RUN(test_graph_heap);
  101. RUN(test_graph_add);
  102. RUN(test_graph_add_100k);
  103. return 0;
  104. }