SHARE
TWEET

Untitled

a guest Jan 21st, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "../../lab1/lab1/tritset.h"
  2. #include "gtest/gtest.h"
  3.  
  4. using tritset::True;
  5. using tritset::False;
  6. using tritset::Unknown;
  7. using tritset::Trit;
  8. using tritset::TritSet;
  9.  
  10. namespace
  11. {
  12.     class RandomTritSet
  13.     {
  14.     public:
  15.         const size_t TRIT_NUM;
  16.         Trit *arr;
  17.         TritSet set;
  18.  
  19.         RandomTritSet(size_t trit_num = 10000)
  20.             : TRIT_NUM(trit_num), set(trit_num)
  21.         {
  22.             Trit values[3] = { False, Unknown, True };
  23.             arr = new Trit[TRIT_NUM];
  24.             for (size_t i = 0; i < TRIT_NUM; i++)
  25.             {
  26.                 arr[i] = values[rand() % 3];
  27.                 set[i] = arr[i];
  28.             }
  29.         }
  30.  
  31.         ~RandomTritSet()
  32.         {
  33.             delete[] arr;
  34.         }
  35.     };
  36. }
  37.  
  38. namespace ConstructorsAndAssigningTest
  39. {
  40.     TEST(ConstructorsAndAssigningTest, UsualConstuctorTest_NoParams)
  41.     {
  42.         TritSet set;
  43.         ASSERT_EQ(set.size(), 16);
  44.         EXPECT_EQ(set.capacity(), 1);
  45.         for (int i = 0; i < 16; i++)
  46.         {
  47.             ASSERT_EQ(Trit(set[i]), Unknown);
  48.         }
  49.     }
  50.     TEST(ConstructorsAndAssigningTest, UsualConstructorTest_WithParam)
  51.     {
  52.         TritSet set(70, False);
  53.         ASSERT_EQ(set.size(), 70);
  54.         EXPECT_EQ(set.capacity(), 5);
  55.         for (int i = 0; i < 70; i++)
  56.         {
  57.             ASSERT_EQ(Trit(set[i]), False);
  58.         }
  59.     }
  60.     TEST(ConstructorsAndAssigningTest, AssigningOperatorTest)
  61.     {
  62.         RandomTritSet randSet;
  63.        
  64.         randSet.set[2] = True;
  65.         if (true)
  66.         {
  67.             TritSet set2;
  68.             set2 = randSet.set;
  69.             for (int i = 0; i < randSet.TRIT_NUM; i++)
  70.             {
  71.                 ASSERT_EQ(Trit(set2[i]), randSet.set[i]);
  72.             }
  73.         }
  74.         ASSERT_EQ(Trit(randSet.set[2]), True);
  75.     }
  76. }
  77.  
  78. namespace SizeChangingTest
  79. {
  80.     TEST(SizeChangingTest, GetLengthTest)
  81.     {
  82.         TritSet set(40, Unknown);
  83.         ASSERT_EQ(1, set.length());
  84.  
  85.         set[5] = True;
  86.         ASSERT_EQ(6, set.length());
  87.  
  88.         set[30] = False;
  89.         ASSERT_EQ(31, set.length());
  90.     }
  91.  
  92.     TEST(SizeChangingTest, ShrinkTest)
  93.     {
  94.         TritSet set(20);
  95.  
  96.         set[30] = True;
  97.         set[30] = Unknown;
  98.  
  99.         set[25] = True;
  100.  
  101.         set.shrink();
  102.         ASSERT_EQ(set.size(), 26);
  103.  
  104.         set[25] = Unknown;
  105.         set.shrink();
  106.         ASSERT_EQ(set.size(), 20);
  107.     }
  108.  
  109.     TEST(SizeChangingTest, TrimTest)
  110.     {
  111.         TritSet set(20);
  112.  
  113.         set.trim(30);
  114.         ASSERT_EQ(set.size(), 20);
  115.  
  116.         set.trim(10);
  117.         ASSERT_EQ(set.size(), 10);
  118.         ASSERT_EQ(set.capacity(), 1);
  119.     }
  120.  
  121.     TEST(SizeChangingTest, CardinalityTest)
  122.     {
  123.         RandomTritSet randomTritSet;
  124.         std::unordered_map<Trit, int> test_map;
  125.  
  126.         for (size_t i = randomTritSet.TRIT_NUM; i > randomTritSet.TRIT_NUM / 2; i--)
  127.         {
  128.             randomTritSet.set[i] = Unknown;
  129.         }
  130.         for (size_t i = 0; i < randomTritSet.set.length(); i++)
  131.         {
  132.             test_map[randomTritSet.arr[i]]++;
  133.         }
  134.  
  135.         std::unordered_map<Trit, int> res_map = randomTritSet.set.cardinality();
  136.         ASSERT_EQ(test_map[True], res_map[True]);
  137.         ASSERT_EQ(test_map[False], res_map[False]);
  138.         ASSERT_EQ(test_map[Unknown], res_map[Unknown]);
  139.     }
  140.  
  141. }
  142.  
  143. namespace AccessingToTritsTest
  144. {
  145.     TEST(AccessingToTritsTest, ReferenceGetTest)
  146.     {
  147.         RandomTritSet randomTritSet;
  148.  
  149.         for (size_t i = 0; i < randomTritSet.TRIT_NUM; i -= -1)
  150.         {
  151.             ASSERT_EQ(Trit(randomTritSet.set[i]), randomTritSet.arr[i]);
  152.         }
  153.     }
  154.  
  155.     TEST(AccessingToTritTest, ReferenceSetTest)
  156.     {
  157.         TritSet set(10, False);
  158.  
  159.         set[4] = True;
  160.         ASSERT_EQ(Trit(set[4]), True);
  161.  
  162.         set[5] = set[4];
  163.         ASSERT_EQ(Trit(set[5]), True);
  164.  
  165.         ASSERT_TRUE(set[5] == set[4]);
  166.     }
  167. }
  168.  
  169. namespace TritSetOperatorsTest
  170. {
  171.     void OperatorTesting(TritSet(*TritSetOperation)(const TritSet&, const TritSet&), Trit(*operation)(const Trit, const Trit))
  172.     {
  173.         RandomTritSet right(20000), left(10000);
  174.         TritSet res(TritSetOperation(right.set, left.set));
  175.         for (size_t i = 0; i < 10000; i++)
  176.         {
  177.             ASSERT_EQ(Trit(res[i]), operation(right.arr[i], left.arr[i]));
  178.         }
  179.         for (size_t i = 10000; i < 20000; i++)
  180.         {
  181.             ASSERT_EQ(Trit(res[i]), operation(right.arr[i], Unknown));
  182.         }
  183.     }
  184.  
  185.     TEST(TritSetOperatorsTest, AndOperatorTest) {
  186.         OperatorTesting(tritset::operator&, tritset::operator&);
  187.     }
  188.     TEST(TritSetOperatorsTest, OrOperatorTest)  {
  189.         OperatorTesting(tritset::operator|, tritset::operator|);
  190.     }
  191.     TEST(TritSetOperatorsTest, XorOperatorTest) {
  192.         OperatorTesting(tritset::operator^, tritset::operator^);
  193.     }
  194.     TEST(TritSetOperatorsTest, NotOperatorTest) {
  195.         OperatorTesting(
  196.             [](const TritSet& set, const TritSet& unused) { return ~set; },
  197.             [](const Trit trit, const Trit unused) { return ~trit; }
  198.         );
  199.     }
  200.  
  201.     TEST(TritSetOperatorsTest, EqualOperatorTest) {
  202.         TritSet a(20), b(30);
  203.         ASSERT_TRUE(a == b);
  204.  
  205.         for (int i = 0; i < 10; i++)
  206.         {
  207.             a[i] = b[i] = True;
  208.         }
  209.         ASSERT_TRUE(a == b);
  210.  
  211.         b[25] = False;
  212.         ASSERT_TRUE(a != b);
  213.  
  214.         a[25] = False;
  215.         ASSERT_TRUE(a == b);
  216.  
  217.         a[0] = False;
  218.         ASSERT_TRUE(a != b);
  219.     }
  220. }
  221.  
  222. namespace TritOperatorsTest
  223. {
  224.     TEST(TritOperatorsTest, AndOperatorTest)
  225.     {
  226.         EXPECT_EQ(True & True, True);
  227.         EXPECT_EQ(False & False, False);
  228.         EXPECT_EQ(Unknown & Unknown, Unknown);
  229.         EXPECT_EQ(True & False, False);
  230.         EXPECT_EQ(True & Unknown, Unknown);
  231.         EXPECT_EQ(False & Unknown, False);
  232.     }
  233.  
  234.     TEST(TritOperatorsTest, OrOperatorTest)
  235.     {
  236.         EXPECT_EQ(True | True, True);
  237.         EXPECT_EQ(False | False, False);
  238.         EXPECT_EQ(Unknown | Unknown, Unknown);
  239.         EXPECT_EQ(True | False, True);
  240.         EXPECT_EQ(True | Unknown, True);
  241.         EXPECT_EQ(False | Unknown, Unknown);
  242.     }
  243.  
  244.     TEST(TritOperatorsTest, XorOperatorTest)
  245.     {
  246.         EXPECT_EQ(True ^ True, False);
  247.         EXPECT_EQ(False ^ False, False);
  248.         EXPECT_EQ(Unknown ^ Unknown, Unknown);
  249.         EXPECT_EQ(True ^ False, True);
  250.         EXPECT_EQ(True ^ Unknown, Unknown);
  251.         EXPECT_EQ(False ^ Unknown, Unknown);
  252.     }
  253.  
  254.     TEST(TritOperatorsTest, NotOperatorTest)
  255.     {
  256.         EXPECT_EQ(~True, False);
  257.         EXPECT_EQ(~False, True);
  258.         EXPECT_EQ(~Unknown, Unknown);
  259.     }
  260. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top