Advertisement
Guest User

APTECHKA_5

a guest
Nov 15th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.69 KB | None | 0 0
  1. //////////////////////// SHARED PTR
  2. class OtchemOx {
  3.  public:
  4.   OtchemOx(int F) {
  5.     ponchik = F;
  6.   }
  7.   bool operator==(OtchemOx f) const {
  8.     return f.ponchik == ponchik;
  9.   }
  10.   bool operator!=(OtchemOx f) const {
  11.     return f.ponchik != ponchik;
  12.   }
  13.   bool operator==(int f) const {
  14.     return f == ponchik;
  15.   }
  16.   bool operator!=(int f) const {
  17.     return f != ponchik;
  18.   }
  19.   int ponchik{};
  20. };
  21.  
  22. TEST(globalTests, All) {
  23.   OtchemOx d(4);
  24.   OtchemOx x(4);
  25.   std::cout << (d == x);
  26.   while (true) {
  27.     SharedPtr<OtchemOx> x(new OtchemOx(24));
  28.     OtchemOx* x2(new OtchemOx(23));
  29.     SharedPtr<OtchemOx> sss(new OtchemOx(3));
  30.     ASSERT_TRUE(*sss == 3);
  31.     // Construct + release
  32.     {
  33.       SharedPtr<OtchemOx> c;
  34.       SharedPtr<OtchemOx> g(new OtchemOx(5));
  35.       SharedPtr<OtchemOx> f(x);
  36.       {
  37.         SharedPtr<OtchemOx> f2(x2);
  38.         f2.Release();
  39.         //ASSERT_TRUE(f2.Get() == nullptr);
  40.       }
  41.       SharedPtr<OtchemOx> sss2(sss);
  42.       SharedPtr<OtchemOx> sss3(sss);
  43.       SharedPtr<OtchemOx> sss4(sss);
  44.       ASSERT_TRUE(sss2 == sss3);
  45.       ASSERT_FALSE(sss2 != sss3);
  46.     }
  47.     // !!!!!!!!!!!!! x need to be deleted
  48.     ASSERT_TRUE(*x2 == 23);
  49.     delete (x2);
  50.     ASSERT_TRUE(*(sss.Get()) ==3);
  51.     ASSERT_TRUE((*sss) == 3);
  52.     //  // = = = = Shared
  53.     SharedPtr<OtchemOx> hoba;
  54.     const SharedPtr<OtchemOx> zoba(hoba); // !?
  55.     SharedPtr<OtchemOx> boba(hoba); // !?
  56.     boba = zoba;
  57.     const SharedPtr<OtchemOx> cobra(new OtchemOx(41));
  58.     boba = cobra;
  59.     ASSERT_TRUE(*cobra.Get() == 41);
  60.     {
  61.       boba.Release();
  62.       SharedPtr<OtchemOx> novaya(new OtchemOx(43));
  63.       ASSERT_TRUE ((*novaya) == 43 && *(novaya.Get()) == 43);
  64.       const SharedPtr<OtchemOx> novaya2(new OtchemOx(44));
  65.       ASSERT_TRUE ((*novaya2) == 44 && *(novaya2.Get()) == 44);
  66.       const SharedPtr<OtchemOx>* novaya4 = &novaya2;
  67.     }
  68.     OtchemOx* ccc = new OtchemOx(44);
  69.  
  70.     const SharedPtr<OtchemOx> skolko_mojno(ccc);
  71.  
  72.     ASSERT_TRUE (ccc == skolko_mojno);
  73.  
  74.     SharedPtr<OtchemOx> tttt;
  75.     tttt = skolko_mojno;
  76.     tttt = boba;
  77.     OtchemOx* ppp = new OtchemOx(4);
  78.     SharedPtr<OtchemOx> xxxxx(ppp);
  79.     SharedPtr<OtchemOx> fuf(std::move(xxxxx));
  80.     SharedPtr<OtchemOx> xxe;
  81.     xxe = std::move(fuf);
  82.     ASSERT_TRUE(xxe.Get() == ppp);
  83.   }
  84. }
  85. ////////////////////////////////////////////////// CONTAINER
  86.  
  87. class OtchemOx {
  88.  public:
  89.   OtchemOx(int F) {
  90.     ponchik = F;
  91.   }
  92.   bool operator==(OtchemOx f) const {
  93.     return f.ponchik == ponchik;
  94.   }
  95.   bool operator!=(OtchemOx f) const {
  96.     return f.ponchik != ponchik;
  97.   }
  98.   bool operator==(int f) const {
  99.     return f == ponchik;
  100.   }
  101.   bool operator!=(int f) const {
  102.     return f != ponchik;
  103.   }
  104.   int ponchik{};
  105. };
  106.  
  107. TEST(MyTests, cont) {
  108.   std::vector<OtchemOx> few;
  109.   few.reserve(4);
  110.   while (true) {
  111.     BiDirectionalList<OtchemOx> z{1, 2, 3};
  112.     z = z;
  113.     BiDirectionalList<OtchemOx> pushHere{};
  114. // PushFront PopFront
  115.     pushHere.PushFront(1);
  116.     ASSERT_TRUE(pushHere.Front()->value == 1);
  117.     pushHere.PushFront(2);
  118.     ASSERT_TRUE(pushHere.Front()->value == 2);
  119.     ASSERT_TRUE(pushHere.Back()->value == 1);
  120.     ASSERT_TRUE(pushHere.Size() == 2);
  121.     pushHere.PopFront();
  122.     ASSERT_TRUE(pushHere.Size() == 1);
  123.     pushHere.PopFront();
  124.     ASSERT_TRUE(pushHere.Size() == 0);
  125. // constructor
  126.     const BiDirectionalList<OtchemOx> okey{};
  127.     const BiDirectionalList<OtchemOx> okey2;
  128.     BiDirectionalList<OtchemOx> ktoProchelTotNeSdoxnet{1, 2};
  129.     const std::initializer_list<OtchemOx> of{1, 2};
  130.     BiDirectionalList<OtchemOx> ktoProchelTotNeSdoxnet2{of};
  131.     ASSERT_TRUE(*of.begin() == ktoProchelTotNeSdoxnet2.Front()->value);
  132.     ASSERT_TRUE(*of.begin() == ktoProchelTotNeSdoxnet.Front()->value);
  133.  
  134.     for (int i = 0; i < 100000; i++) {
  135.       BiDirectionalList<OtchemOx> fe;
  136.       fe.PushBack(OtchemOx(1));
  137.     }
  138.     ASSERT_TRUE(ktoProchelTotNeSdoxnet.Size() == 2);
  139.     ktoProchelTotNeSdoxnet.PushBack(1);
  140.     ASSERT_TRUE(ktoProchelTotNeSdoxnet.Size() == 3);
  141.     ktoProchelTotNeSdoxnet.PopFront();
  142.     ASSERT_TRUE(ktoProchelTotNeSdoxnet.Size() == 2);
  143.     ktoProchelTotNeSdoxnet.PopBack();
  144.     ASSERT_TRUE(ktoProchelTotNeSdoxnet.Size() == 1);
  145.     ktoProchelTotNeSdoxnet.PushBack(123);
  146.     ASSERT_TRUE(ktoProchelTotNeSdoxnet.Size() == 2);
  147.     ktoProchelTotNeSdoxnet.PushFront(0);
  148.     ASSERT_TRUE(ktoProchelTotNeSdoxnet.Size() == 3);
  149.     // PushFront PushBack
  150.     {
  151.       BiDirectionalList<OtchemOx> f;
  152.       for (int i = 0; i < 100; i++) {
  153.         f.PushFront(i);
  154.         f.PushBack(i + 1);
  155.         ASSERT_TRUE(f.Back()->value == i + 1);
  156.         ASSERT_TRUE(f.Front()->value == i);
  157.         ASSERT_TRUE(f.Size() == i * 2 + 2);
  158.       }
  159.     }
  160.     // Front Back []
  161.     {
  162.       BiDirectionalList<OtchemOx> x1{12, 32};
  163.       const BiDirectionalList<OtchemOx> x2(x1);
  164.       BiDirectionalList<OtchemOx> x3(x2);
  165.       ASSERT_TRUE(x3.Back()->value == 32);
  166.       ASSERT_TRUE(x2.Back()->value == 32);
  167.       ASSERT_TRUE(x1.Back()->value == 32);
  168.  
  169.       ASSERT_TRUE(x3[1]->value == 32);
  170.       ASSERT_TRUE(x2[1]->value == 32);
  171.       ASSERT_TRUE(x1[1]->value == 32);
  172.  
  173.       ASSERT_TRUE(x3.Front()->value == 12);
  174.       ASSERT_TRUE(x2.Front()->value == 12);
  175.       ASSERT_TRUE(x1.Front()->value == 12);
  176.  
  177.       ASSERT_TRUE(x3[0]->value == 12);
  178.       ASSERT_TRUE(x2[0]->value == 12);
  179.       ASSERT_TRUE(x1[0]->value == 12);
  180.       ASSERT_TRUE(x1.Size() == 2);
  181.       ASSERT_TRUE(x2.Size() == 2);
  182.       ASSERT_TRUE(x3.Size() == 2);
  183.  
  184.       x1.PopBack();
  185.       x3.PopBack();
  186.  
  187.       ASSERT_TRUE(x1.Size() == 1);
  188.       ASSERT_TRUE(x3.Size() == 1);
  189.       ASSERT_TRUE(
  190.           x3[0]->value == x3.Back()->value
  191.               && x3.Front()->value == x3[0]->value);
  192.       ASSERT_TRUE(
  193.           x1[0]->value == x1.Back()->value
  194.               && x1.Front()->value == x1[0]->value);
  195.  
  196.       x1.PopBack();
  197.       x3.PopBack();
  198.  
  199.       // ASSERT_ANY_THROW(x1[0]);
  200.       //ASSERT_ANY_THROW(x1[-1]);
  201.       // ASSERT_ANY_THROW(x1[100000]);
  202.       // ASSERT_ANY_THROW(x1.Back());
  203.       // ASSERT_ANY_THROW(x1.Front());
  204.  
  205.       ASSERT_TRUE(x1.Size() == 0);
  206.       ASSERT_TRUE(x3.Size() == 0);
  207.  
  208.     }
  209.  
  210.     // Remove Random|Front|Back element in List
  211.     {
  212.       BiDirectionalList<OtchemOx> xerox{1, 2, 3};
  213.       // ASSERT_ANY_THROW(xerox[4]);
  214.       ASSERT_TRUE(xerox[1]->value == 2);
  215.       ASSERT_TRUE(xerox[0]->value == 1);
  216.       ASSERT_TRUE(xerox[2]->value == 3);
  217.       ASSERT_TRUE(xerox.Size() == 3);
  218.       xerox.PushBack(4);
  219.       xerox.PushFront(0); // 1  3
  220.       xerox.Erase(xerox[2]);
  221.       ASSERT_TRUE(xerox[2]->value == 3);
  222.       ASSERT_TRUE(xerox.Size() == 4);
  223.  
  224.       xerox.Erase(xerox.Front());
  225.       ASSERT_TRUE(xerox[0]->value == 1);
  226.       ASSERT_TRUE(xerox.Size() == 3);
  227.  
  228.       xerox.Erase(xerox.Back());
  229.       ASSERT_TRUE(xerox[1]->value == 3);
  230.       ASSERT_TRUE(xerox.Size() == 2);
  231.  
  232.       xerox.PopBack();
  233.       ASSERT_TRUE(xerox.Size() == 1);
  234.       ASSERT_TRUE(xerox.Back() == xerox.Front());
  235.  
  236.       xerox.PushBack(2);
  237.       ASSERT_TRUE(xerox.Size() == 2);
  238.       ASSERT_TRUE(xerox[0]->value == 1 && xerox[1]->value == 2);
  239.       xerox.PopBack();
  240.       ASSERT_TRUE(xerox.Back() == xerox.Front());
  241.       ASSERT_TRUE(xerox.Size() == 1);
  242.       xerox.Erase(xerox.Front());
  243.     }
  244.     //  FindAll
  245.     {
  246.       BiDirectionalList<OtchemOx> oso{0,1, 2, 3, 4, 5, 6};
  247.       for (int i = 0; i < 7; i++) {
  248.         OtchemOx xs(i);
  249.         std::vector<int> All = oso.FindAll(xs);
  250.         ASSERT_TRUE(All[0] == i);
  251.         ASSERT_TRUE(oso.Size() == 7);
  252.       }
  253.       oso[0]->value = oso[1]->value = oso[2]->value = oso[4]->value =
  254.       oso[5]->value = oso[6]->value;
  255.       std::vector<int> All = oso.FindAll(6);
  256.       ASSERT_THAT(All, testing::ElementsAre(0, 1, 2, 4, 5, 6));
  257.       ASSERT_TRUE(oso.Size() == 7);
  258.     }
  259.     // To vector + == + =
  260.     {
  261.       std::initializer_list<OtchemOx> d = {0, 1, 2, 3, 4};
  262.       std::vector<OtchemOx> f(d);
  263.       BiDirectionalList<OtchemOx> xer(d);
  264.       ASSERT_TRUE(xer.ToVector() == f);
  265.       ASSERT_TRUE(xer.Size() == d.size());
  266.       BiDirectionalList<OtchemOx> dq(std::move(xer));
  267.       ASSERT_TRUE(xer.Size() == 0);
  268.       ASSERT_TRUE(dq.Size() == 5);
  269.       ASSERT_TRUE(dq.Size() == d.size());
  270.     }
  271.  
  272. // Find +InsertBefore after
  273.     {
  274.       std::initializer_list<OtchemOx>
  275.           d = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
  276.       std::vector<OtchemOx> f(d);
  277.       BiDirectionalList<OtchemOx> xer(d);
  278.       for (int i = 0; i < 15; i++) {
  279.         ASSERT_TRUE(xer.Find(i) == i && d.size() == xer.Size());
  280.       }
  281.       BiDirectionalList<OtchemOx> opra{0};
  282.       ASSERT_THAT(opra.ToVector(), testing::ElementsAre(0));
  283.       opra.InsertBefore(opra[0], 1);
  284.       ASSERT_THAT(opra.ToVector(), testing::ElementsAre(1, 0));
  285.       opra.InsertBefore(opra[1], 2);
  286.       ASSERT_THAT(opra.ToVector(), testing::ElementsAre(1, 2, 0));
  287.  
  288.       opra.InsertAfter(opra[1], 3);
  289.       ASSERT_THAT(opra.ToVector(), testing::ElementsAre(1, 2, 3, 0));
  290.       opra.InsertAfter(opra[3], 5);
  291.       ASSERT_THAT(opra.ToVector(), testing::ElementsAre(1, 2, 3, 0, 5));
  292.       opra.InsertBefore(opra[4], 2);
  293.       ASSERT_THAT(opra.ToVector(), testing::ElementsAre(1, 2, 3, 0, 2, 5));
  294.     }
  295. // Stack
  296.  
  297.     {
  298.       Container<OtchemOx> ff1;
  299.       Container<OtchemOx> ff2{};
  300.       Container<OtchemOx> ff3{1, 2, 3, 4};
  301.       ASSERT_TRUE(ff1.Size() == ff2.Size());
  302.       ASSERT_TRUE(ff3.Size() == 4);
  303.       ff3.Push(5);
  304.       ff3.Push(6);
  305.       ASSERT_TRUE(ff3.Size() == 6);
  306.       for (int i = 0; i < 6; i++) {
  307.         ASSERT_TRUE(ff3.Get() == 6 - i);
  308.         ASSERT_TRUE(ff3.Size() == 6 - i);
  309.         ff3.Pop();
  310.       }
  311.       Container<OtchemOx> x1{1, 2, 3};
  312.       Container<OtchemOx> x2{1, 2};
  313.       Container<OtchemOx> x3{1, 2, 3, 4};
  314.       ASSERT_FALSE(x1 == x2);
  315.       ASSERT_FALSE(x2 == x3);
  316.       ASSERT_FALSE(x1 == x3);
  317.       ASSERT_FALSE(x1 != x1);
  318.       x2.Push(3);
  319.       x3.Pop();
  320.       ASSERT_TRUE(x1 == x2);
  321.       ASSERT_TRUE(x1 == x3);
  322.       ASSERT_TRUE(x3 == x1);
  323.  
  324.     }
  325. // Const
  326.     const BiDirectionalList<OtchemOx> f;
  327.     BiDirectionalList<OtchemOx> f1{};
  328.     BiDirectionalList<OtchemOx> f3{1, 2, 3, 4};
  329.     const std::initializer_list<OtchemOx> x = {1, 2, 3};
  330.     BiDirectionalList<OtchemOx> f4(x);
  331.     std::initializer_list<OtchemOx> x1 = {1, 2, 3};
  332.     BiDirectionalList<OtchemOx> f5(x1);
  333.     const BiDirectionalList<OtchemOx> opa(f5);
  334.     BiDirectionalList<OtchemOx> opa2(opa);
  335.     const BiDirectionalList<OtchemOx>& opa3(std::move(opa2));
  336.     ASSERT_TRUE(f5.Size() != f.Size() && f.IsEmpty());
  337.     const OtchemOx zzzz = 2;
  338.     f1.PushFront(zzzz);
  339.     f1.PushBack(zzzz);
  340.     ASSERT_TRUE(f1.Back()->value == f1.Front()->value);
  341.     ASSERT_TRUE (opa.Front() != opa.Back());
  342.     f1.PopBack();
  343.     f1.PopFront();
  344.     std::cout << (f1.Size() == 0);
  345.     const std::vector<OtchemOx> buka = opa.ToVector();
  346.     const std::vector<int> buka2 = opa.FindAll(zzzz);
  347.     opa2 = opa;
  348.     opa2[1];
  349.     opa[1];
  350.     opa2.InsertBefore(opa2.Back(), zzzz);
  351.     opa2.InsertBefore(opa2.Front(), zzzz);
  352.     opa2.InsertAfter(opa2.Front(), zzzz);
  353.     opa2.InsertAfter(opa2.Back(), zzzz);
  354.     opa2.InsertAfter(opa2[0], zzzz);
  355.     while (!opa2.IsEmpty()) { opa2.Erase(opa2[0]); }
  356.     ASSERT_TRUE (opa2 == f1 && f1 == opa2);
  357.     f1 = opa;
  358.     f1 = opa2;
  359.     const Container<OtchemOx> skoka;
  360.     const std::initializer_list<OtchemOx> f42{12, 433};
  361.     Container<OtchemOx> dota(f42);
  362.     Container<OtchemOx> dota2(f42);
  363.     const Container<OtchemOx> doka(f42);
  364.     const int dudochka = 4;
  365.     int sudochka = 4;
  366.     dota.Push(dudochka);
  367.     dota.Push(sudochka);
  368.     dota.Push(std::move(sudochka));
  369.     dota.Pop();
  370.     ASSERT_TRUE(doka.Get() == 433 || doka.Get() == 12);
  371.     doka.Size();
  372.     doka.IsEmpty();
  373.     ASSERT_TRUE(dota != dota2 && !(dota2 == dota));
  374.   }
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement