LCOV - code coverage report
Current view: top level - src/libs/tools/tests - mergetestutils.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 65 65 100.0 %
Date: 2019-09-12 12:28:41 Functions: 9 10 90.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Implements a helper class for merge related tests
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <gtest/gtest.h>
      11             : #include <iostream>
      12             : #include <kdbprivate.h>
      13             : #include <keysetio.hpp>
      14             : #include <merging/threewaymerge.hpp>
      15             : 
      16             : using namespace kdb;
      17             : using namespace kdb::tools::merging;
      18             : 
      19             : class MergeTest : public ::testing::Test
      20             : {
      21             : protected:
      22             :         KeySet base;
      23             :         KeySet ours;
      24             :         KeySet theirs;
      25             :         KeySet mergeKeys;
      26             :         Key baseParent;
      27             :         Key ourParent;
      28             :         Key theirParent;
      29             :         Key mergeParent;
      30             :         Key mk1, mk2, mk3, mk4, mk5;
      31             : 
      32          60 :         MergeTest ()
      33          60 :         : baseParent (Key ("user/parentb", KEY_END)), ourParent (Key ("user/parento", KEY_END)),
      34         600 :           theirParent (Key ("user/parentt", KEY_END)), mergeParent (Key ("user/parentm", KEY_END))
      35             :         {
      36         120 :                 base.append (baseParent);
      37         180 :                 base.append (Key ("user/parentb/config/key1", KEY_VALUE, "value1", KEY_END));
      38         180 :                 base.append (Key ("user/parentb/config/key2", KEY_VALUE, "value2", KEY_END));
      39         180 :                 base.append (Key ("user/parentb/config/key3", KEY_VALUE, "value3", KEY_END));
      40         180 :                 base.append (Key ("user/parentb/config/key4", KEY_VALUE, "value4", KEY_END));
      41             : 
      42         120 :                 ours.append (ourParent);
      43         180 :                 ours.append (Key ("user/parento/config/key1", KEY_VALUE, "value1", KEY_END));
      44         180 :                 ours.append (Key ("user/parento/config/key2", KEY_VALUE, "value2", KEY_END));
      45         180 :                 ours.append (Key ("user/parento/config/key3", KEY_VALUE, "value3", KEY_END));
      46         180 :                 ours.append (Key ("user/parento/config/key4", KEY_VALUE, "value4", KEY_END));
      47             : 
      48         120 :                 theirs.append (theirParent);
      49         180 :                 theirs.append (Key ("user/parentt/config/key1", KEY_VALUE, "value1", KEY_END));
      50         180 :                 theirs.append (Key ("user/parentt/config/key2", KEY_VALUE, "value2", KEY_END));
      51         180 :                 theirs.append (Key ("user/parentt/config/key3", KEY_VALUE, "value3", KEY_END));
      52         180 :                 theirs.append (Key ("user/parentt/config/key4", KEY_VALUE, "value4", KEY_END));
      53             : 
      54             :                 // used as references for comparing
      55         180 :                 mk1 = Key ("user/parentm/config/key1", KEY_VALUE, "value1", KEY_END);
      56         180 :                 mk2 = Key ("user/parentm/config/key2", KEY_VALUE, "value2", KEY_END);
      57         180 :                 mk3 = Key ("user/parentm/config/key3", KEY_VALUE, "value3", KEY_END);
      58         180 :                 mk4 = Key ("user/parentm/config/key4", KEY_VALUE, "value4", KEY_END);
      59             : 
      60             :                 // used only by some tests
      61         180 :                 mk5 = Key ("user/parentm/config/key5", KEY_VALUE, "value5", KEY_END);
      62             : 
      63         120 :                 mergeKeys.append (mk1);
      64         120 :                 mergeKeys.append (mk2);
      65         120 :                 mergeKeys.append (mk3);
      66         120 :                 mergeKeys.append (mk4);
      67          60 :         }
      68             : 
      69          60 :         virtual ~MergeTest ()
      70         840 :         {
      71          60 :         }
      72             : 
      73          60 :         virtual void SetUp () override
      74             :         {
      75          60 :         }
      76             : 
      77          60 :         virtual void TearDown () override
      78             :         {
      79          60 :         }
      80             : 
      81           6 :         virtual void unsyncKeys (KeySet & ks)
      82             :         {
      83          12 :                 Key current;
      84             :                 ks.rewind ();
      85         174 :                 while ((current = ks.next ()))
      86             :                 {
      87          30 :                         current.getKey ()->flags = static_cast<ckdb::keyflag_t> (current.getKey ()->flags & ~(ckdb::KEY_FLAG_SYNC));
      88             : 
      89             :                         // This does not work because C++ complains about an invalid conversion from int to keyflags_t
      90             :                         // clear_bit(current.getKey()->flags, static_cast<int>(ckdb::KEY_FLAG_SYNC));
      91             :                 }
      92           6 :         }
      93             : 
      94         118 :         virtual void compareKeys (const Key & k1, const Key & k2)
      95             :         {
      96         236 :                 EXPECT_EQ (k1, k2) << "keys have different names";
      97         590 :                 EXPECT_EQ (k1.getString (), k2.getString ()) << "keys have different values";
      98         118 :         }
      99             : 
     100          14 :         virtual void compareAllKeys (KeySet & merged)
     101             :         {
     102          42 :                 compareKeys (mk1, merged.lookup (mk1));
     103          42 :                 compareKeys (mk2, merged.lookup (mk2));
     104          42 :                 compareKeys (mk3, merged.lookup (mk3));
     105          42 :                 compareKeys (mk4, merged.lookup (mk4));
     106          14 :         }
     107             : 
     108          18 :         virtual void compareAllExceptKey1 (KeySet & merged)
     109             :         {
     110          54 :                 compareKeys (mk2, merged.lookup (mk2));
     111          54 :                 compareKeys (mk3, merged.lookup (mk3));
     112          54 :                 compareKeys (mk4, merged.lookup (mk4));
     113          18 :         }
     114             : 
     115          10 :         virtual void testConflictMeta (const Key & key, ConflictOperation our, ConflictOperation their)
     116             :         {
     117          60 :                 Key const ourConflict = key.getMeta<Key const> ("conflict/operation/our");
     118          20 :                 EXPECT_TRUE (ourConflict) << "No conflict metakey for our operation present";
     119          20 :                 ConflictOperation operation = MergeConflictOperation::getFromName (ourConflict.getString ());
     120          20 :                 EXPECT_EQ (our, operation);
     121             : 
     122          60 :                 Key const theirConflict = key.getMeta<Key const> ("conflict/operation/their");
     123          20 :                 EXPECT_TRUE (theirConflict) << "No conflict metakey for their operation present";
     124          20 :                 operation = MergeConflictOperation::getFromName (theirConflict.getString ());
     125          20 :                 EXPECT_EQ (their, operation);
     126          10 :         }
     127             : };

Generated by: LCOV version 1.13