test_graph.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "test.h"
  2. #include "graph.h"
  3. #include "assets/triples.h"
  4. static int
  5. _graph_new (LSUP_store_type type)
  6. {
  7. LSUP_Graph *gr;
  8. gr = LSUP_graph_new (type);
  9. ASSERT (gr != NULL, "Error creating graph!");
  10. EXPECT_PASS (LSUP_graph_set_uri (gr, "urn:gr:1"));
  11. EXPECT_STR_EQ (LSUP_graph_uri (gr)->data, "urn:gr:1");
  12. ASSERT (
  13. strcmp (LSUP_graph_uri (gr)->data, "urn:gr:1") == 0,
  14. "Graph URI mismatch!");
  15. EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
  16. LSUP_graph_free (gr);
  17. return 0;
  18. }
  19. static int
  20. _graph_add (LSUP_store_type type)
  21. {
  22. LSUP_Triple *trp = create_triples();
  23. LSUP_Graph *gr = LSUP_graph_new (type);
  24. ASSERT (gr != NULL, "Error creating graph!");
  25. size_t ct;
  26. LSUP_graph_add_trp (gr, trp, &ct);
  27. EXPECT_INT_EQ (ct, 8);
  28. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  29. for (int i = 0; i < sizeof (trp); i++) {
  30. printf ("checking triple #%d... ", i);
  31. ASSERT (LSUP_graph_contains (gr, trp + i), "Triple not in graph!");
  32. printf ("OK.\n");
  33. }
  34. LSUP_Triple *missing_trp = LSUP_triple_new (trp[1].s, trp[6].p, trp[4].o);
  35. ASSERT (! LSUP_graph_contains (gr, missing_trp), "Triple in graph!");
  36. free (missing_trp);
  37. free_triples (trp); // gr copied data.
  38. LSUP_graph_free (gr);
  39. return 0;
  40. }
  41. static int
  42. _graph_remove (LSUP_store_type type)
  43. {
  44. LSUP_Triple *trp = create_triples();
  45. LSUP_Graph *gr = LSUP_graph_new (type);
  46. size_t ct;
  47. LSUP_graph_add_trp (gr, trp, &ct);
  48. EXPECT_INT_EQ (ct, 8);
  49. EXPECT_INT_EQ (LSUP_graph_size (gr), 8);
  50. LSUP_Triple *spo = LSUP_triple_new (trp[0].s, NULL, NULL);
  51. LSUP_graph_remove (gr, spo, &ct);
  52. ASSERT (!LSUP_graph_contains (gr, trp + 0), "Unexpected triple found!");
  53. ASSERT (LSUP_graph_contains (gr, trp + 1), "Triple not in graph!");
  54. ASSERT (LSUP_graph_contains (gr, trp + 2), "Triple not in graph!");
  55. ASSERT (!LSUP_graph_contains (gr, trp + 3), "Unexpected triple found!");
  56. ASSERT (!LSUP_graph_contains (gr, trp + 4), "Unexpected triple found!");
  57. ASSERT (!LSUP_graph_contains (gr, trp + 5), "Unexpected triple found!");
  58. ASSERT (LSUP_graph_contains (gr, trp + 6), "Triple not in graph!");
  59. ASSERT (!LSUP_graph_contains (gr, trp + 7), "Unexpected triple found!");
  60. EXPECT_INT_EQ (LSUP_graph_size (gr), 3);
  61. LSUP_triple_free (spo);
  62. free_triples (trp); // gr copied data.
  63. LSUP_graph_free (gr);
  64. return 0;
  65. }
  66. static int test_graph_new() {
  67. if (_graph_new (LSUP_STORE_MEM) != 0) return -1;
  68. if (_graph_new (LSUP_STORE_MDB_TMP) != 0) return -1;
  69. return 0;
  70. }
  71. static int test_graph_add() {
  72. if (_graph_add (LSUP_STORE_MEM) != 0) return -1;
  73. if (_graph_add (LSUP_STORE_MDB_TMP) != 0) return -1;
  74. return 0;
  75. }
  76. static int test_graph_remove() {
  77. if (_graph_remove (LSUP_STORE_MEM) != 0) return -1;
  78. if (_graph_remove (LSUP_STORE_MDB_TMP) != 0) return -1;
  79. return 0;
  80. }
  81. int graph_tests()
  82. {
  83. RUN (test_graph_new);
  84. RUN (test_graph_add);
  85. RUN (test_graph_remove);
  86. return 0;
  87. }