Advertisement
anechka_ne_plach

Untitled

Oct 28th, 2021
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <catch.hpp>
  2. #include <any.h>
  3. #include <string>
  4. #include <typeinfo>
  5.  
  6. struct SomeStruct {
  7.     int x;
  8. };
  9.  
  10. TEST_CASE("Simple") {
  11.     Any a(5);
  12.     REQUIRE(5 == a.GetValue<int>());
  13.  
  14.     Any b(std::string("abacaba"));
  15.     REQUIRE("abacaba" == b.GetValue<std::string>());
  16.  
  17.     Any c;
  18.     c = 7.0;
  19.     REQUIRE(false == c.Empty());
  20.     REQUIRE(7.0 == c.GetValue<double>());
  21.  
  22.     Any d;
  23.     int *ptr = nullptr;
  24.     d = ptr;
  25.     REQUIRE(false == d.Empty());
  26. }
  27.  
  28. TEST_CASE("Empty") {
  29.     Any a;
  30.     REQUIRE(true == a.Empty());
  31.  
  32.     std::vector<int> t{1, 2};
  33.     Any b(t);
  34.     REQUIRE(false == b.Empty());
  35.     a.Swap(b);
  36.  
  37.     REQUIRE(t == a.GetValue<std::vector<int>>());
  38.     REQUIRE(false == a.Empty());
  39.     REQUIRE(true == b.Empty());
  40.  
  41.     a.Clear();
  42.     REQUIRE(true == a.Empty());
  43. }
  44.  
  45. TEST_CASE("Copy") {
  46.     Any a(5);
  47.     Any b = a;
  48.  
  49.     REQUIRE(a.GetValue<int>() == b.GetValue<int>());
  50.  
  51.     Any c;
  52.     c = b;
  53.  
  54.     REQUIRE(b.GetValue<int>() == c.GetValue<int>());
  55.     b.Clear();
  56.     REQUIRE(5 == c.GetValue<int>());
  57.  
  58.     Any d(SomeStruct{3});
  59.     REQUIRE(3 == d.GetValue<SomeStruct>().x);
  60.  
  61.     d = std::string("check");
  62.     REQUIRE("check" == d.GetValue<std::string>());
  63.  
  64.     Any e = Any(std::string("dorou"));
  65.     e = e;
  66.  
  67.     REQUIRE("dorou" == e.GetValue<std::string>());
  68.  
  69.     a.Swap(e);
  70.     REQUIRE(5 == e.GetValue<int>());
  71. }
  72.  
  73. TEST_CASE("Any throws") {
  74.     Any a(std::string("just a test"));
  75.     REQUIRE_THROWS_AS(a.GetValue<int>(), std::bad_cast);
  76.     Any b(std::vector<int>{1, 2, 3});
  77.     REQUIRE(b.GetValue<std::vector<int>>().size() == 3);
  78.     REQUIRE_THROWS_AS(b.GetValue<std::string>(), std::bad_cast);
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement