deushiro

tests

Mar 26th, 2021
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <iostream>
  2. #include "graph.h"
  3. #include "gtest/gtest.h"
  4.  
  5. #include "src/listGraph.h"
  6. #include "src/matrixGraph.h"
  7. #include "src/arcGraph.h"
  8. #include "src/ptrsGraph.h"
  9.  
  10. TEST(IGraph, Creating) {
  11.     IGraph<int> *listGr = new ListGraph<int>;
  12.     IGraph<int> *arcGr = new ArcGraph<int>;
  13.     IGraph<int> *matGr = new MatrixGraph<int>;
  14.     IPtrsGraph<int> *pGraph = new PtrsGraph<int>;
  15.  
  16.     delete listGr;
  17.     delete arcGr;
  18.     delete matGr;
  19.     delete pGraph;
  20. }
  21.  
  22. // Simple linear graph
  23. // 1 --> 2 --> 3 --> 4
  24. TEST(ListGraph, Simple) {
  25.     std::shared_ptr<IGraph<int>> listGr = std::dynamic_pointer_cast<IGraph<int>>(std::make_shared<ListGraph<int>>());
  26.     listGr->AddEdge(1, 2, 10);
  27.     listGr->AddEdge(2, 3, 20);
  28.     listGr->AddEdge(3, 4, 30);
  29.  
  30.     std::vector<int> res;
  31.     listGr->DeepFirstSearch(1, res);
  32.     EXPECT_EQ(res.back(), 4);
  33.     EXPECT_EQ(res.size(), 4);
  34.  
  35.     std::vector<int> res2;
  36.     listGr->GetPrevVertices(3, res2);
  37.     EXPECT_EQ(res2.front(), 2);
  38.     EXPECT_EQ(res2.size(), 1);
  39.  
  40. }
  41.  
  42. // Cycled graph
  43. // 1 --> 2 --> 3000 --> 1
  44. TEST(MatrixGraph, Cycled) {
  45.     std::shared_ptr<IGraph<int>> matGr = std::dynamic_pointer_cast<IGraph<int>>(std::make_shared<MatrixGraph<int>>());
  46.     matGr->AddEdge(1, 2, 10);
  47.     matGr->AddEdge(2, 3000, 20);
  48.     matGr->AddEdge(3000, 1, 30);
  49.  
  50.     std::vector<int> res;
  51.     matGr->DeepFirstSearch(1, res);
  52.     EXPECT_EQ(res.back(), 3000);
  53.     EXPECT_EQ(res.size(), 3);
  54.  
  55.     std::vector<int> res2;
  56.     matGr->GetPrevVertices(3000, res2);
  57.     EXPECT_EQ(res2[0], 2);
  58.     EXPECT_EQ(res2.size(), 1);
  59. }
  60.  
  61. // Tree graph
  62. // 1 --> 2 --> 3
  63. //   \
  64. //     -> 4 --> 5
  65. //        \
  66. //          ->6
  67. TEST(ArcGraph, Cycled) {
  68.     std::shared_ptr<IGraph<int>> arcGr = std::dynamic_pointer_cast<IGraph<int>>(std::make_shared<ArcGraph<int>>());
  69.     arcGr->AddEdge(1, 2, 10);
  70.     arcGr->AddEdge(2, 3, 20);
  71.     arcGr->AddEdge(1, 4, 30);
  72.     arcGr->AddEdge(4, 5, 40);
  73.     arcGr->AddEdge(4, 6, 50);
  74.  
  75.     std::vector<int> res;
  76.     arcGr->BreadthFirstSearch(4, res);
  77.     EXPECT_EQ(res.size(), 3);
  78.  
  79.     std::vector<int> res2;
  80.     arcGr->GetNextVertices(1, res2);
  81.     EXPECT_EQ(res2.size(), 2);
  82. }
  83.  
  84. // Cycled graph
  85. // 1 --> 2 --> 3 --> 1
  86. TEST(PtrsGraph, Cycled) {
  87.     std::shared_ptr<IPtrsGraph<int>> ptrGr = std::dynamic_pointer_cast<IPtrsGraph<int>>(
  88.             std::make_shared<PtrsGraph<int>>());
  89.     Node<int> *first = new Node<int>;
  90.     Node<int> *second = new Node<int>;
  91.     Node<int> *third = new Node<int>;
  92.     ptrGr->AddEdge(first, second, 10);
  93.     ptrGr->AddEdge(second, third, 20);
  94.     ptrGr->AddEdge(third, first, 30);
  95.  
  96.     std::vector<Node<int> *> res;
  97.     ptrGr->DeepFirstSearch(first, res);
  98.     EXPECT_EQ(res.back(), third);
  99.     EXPECT_EQ(res.size(), 3);
  100.  
  101.     std::vector<Node<int> *> res2;
  102.     ptrGr->GetPrevVertices(third, res2);
  103.     EXPECT_EQ(res2.size(), 1);
  104.  
  105.     delete first;
  106.     delete second;
  107.     delete third;
  108. }
  109.  
  110. // Cycled graph
  111. // 1 --> 2 --> 3000 --> 1
  112. TEST(IGraph, Copying) {
  113.     IGraph<int> *matGr = new MatrixGraph<int>;
  114.     matGr->AddEdge(1, 2, 10);
  115.     matGr->AddEdge(2, 3000, 20);
  116.     matGr->AddEdge(3000, 1, 30);
  117.  
  118.     IGraph<int> *listGr = new ListGraph<int>(matGr);
  119.     IGraph<int> *arcGr = new ArcGraph<int>(listGr);
  120.     IGraph<int> *mat2Gr = new MatrixGraph<int>(arcGr);
  121.  
  122.     delete matGr;
  123.     delete listGr;
  124.     delete arcGr;
  125.     delete mat2Gr;
  126. }
  127.  
  128. int main(int argc, char **argv) {
  129.     ::testing::InitGoogleTest(&argc, argv);
  130.     return RUN_ALL_TESTS();
  131. }
Advertisement
Add Comment
Please, Sign In to add comment