LCOV - code coverage report
Current view: top level - src/bindings/cpp/tests - tests.hpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 27 40 67.5 %
Date: 2019-09-12 12:28:41 Functions: 4 6 66.7 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Some common functions in use for testing framework
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #ifndef KDB_TESTS_HPP
      10             : #define KDB_TESTS_HPP
      11             : 
      12             : #include <kdb.hpp>
      13             : #include <kdbmacros.h>
      14             : #include <key.hpp>
      15             : #include <keyset.hpp>
      16             : 
      17             : #include <cstdlib>
      18             : #include <cstring>
      19             : #include <exception>
      20             : #include <iostream>
      21             : #include <string>
      22             : 
      23             : #include <gtest/gtest.h>
      24             : 
      25             : using namespace std;
      26             : using namespace kdb;
      27             : 
      28             : // -- Macros -------------------------------------------------------------------------------------------------------------------------------
      29             : 
      30             : #define succeed_if(x, y) ASSERT_TRUE (x) << y
      31             : 
      32             : #define exit_if_fail(expression, message)                                                                                                  \
      33             :         if (!(expression))                                                                                                                 \
      34             :         {                                                                                                                                  \
      35             :                 cerr << __FILE__ << ":" << __LINE__ << ": Failure" << endl;                                                                \
      36             :                 cerr << "Value of: " << ELEKTRA_STRINGIFY (expression) << endl;                                                            \
      37             :                 cerr << "  Actual: false" << endl;                                                                                         \
      38             :                 cerr << "Expected: true" << endl;                                                                                          \
      39             :                 cerr << message << endl;                                                                                                   \
      40             :                 exit (1);                                                                                                                  \
      41             :         }                                                                                                                                  \
      42             :         SUCCEED () << message
      43             : 
      44             : #define succeed_if_same(x, y, message) ASSERT_EQ (x, y) << message
      45             : 
      46             : #define compare_keyset(keySet1, keySet2) ASSERT_TRUE (compareKeySet (keySet1, keySet2))
      47             : 
      48             : // -- Functions ----------------------------------------------------------------------------------------------------------------------------
      49             : 
      50             : /**
      51             :  * @brief This function adds the string representation of a key to an assertion result.
      52             :  *
      53             :  * @param stream This parameter specifies the assertion result to which this function adds a string representation of `key`.
      54             :  * @param key This parameter stores the key this function converts to an assertion result.
      55             :  *
      56             :  * @returns An assertion result containing the string representation of `key`
      57             :  */
      58           0 : testing::AssertionResult & operator<< (testing::AssertionResult & stream, kdb::Key & key)
      59             : {
      60           0 :         stream << key.getName () << ": “" << (key.isString () ? key.getString () : (key.getBinarySize () == 0 ? "NULL" : "BINARY")) << "”";
      61             : 
      62             :         key.rewindMeta ();
      63           0 :         while (key.nextMeta ())
      64             :         {
      65           0 :                 stream << ", " << key.currentMeta ().getName () << ": “" << key.currentMeta ().getString () << "”";
      66             :         }
      67           0 :         stream << endl;
      68           0 :         return stream;
      69             : }
      70             : 
      71             : /**
      72             :  * @brief This function adds the string representation of a key set to an assertion result.
      73             :  *
      74             :  * @param stream This parameter specifies the assertion result to which this function adds a string representation of `keys`.
      75             :  * @param key This parameter stores the key set this function converts to an assertion result.
      76             :  *
      77             :  * @returns An assertion result containing the string representation of `keys`
      78             :  */
      79           0 : testing::AssertionResult & operator<< (testing::AssertionResult & stream, kdb::KeySet & keys)
      80             : {
      81           0 :         for (auto key : keys)
      82             :         {
      83           0 :                 stream << key;
      84             :         }
      85           0 :         return stream;
      86             : }
      87             : 
      88             : /**
      89             :  * @brief This function checks if the meta key set of two keys are equal.
      90             :  *
      91             :  * @param key1 This parameter stores the first meta key set this function uses for comparison.
      92             :  * @param key2 This parameter stores the second meta key set this function uses for comparison.
      93             :  *
      94             :  * @retval true if all meta keys of `key1` and `key2` are equal
      95             :  * @retval false otherwise
      96             :  */
      97         380 : bool isMetaDataEqual (kdb::Key & key1, kdb::Key & key2)
      98             : {
      99         380 :         key1.rewindMeta ();
     100             :         key2.rewindMeta ();
     101             : 
     102        1368 :         while (key1.nextMeta ())
     103             :         {
     104         152 :                 key2.nextMeta ();
     105         228 :                 if (!key2.currentMeta ()) return false;
     106         532 :                 if (key1.currentMeta ().getName () != key2.currentMeta ().getName ()) return false;
     107         532 :                 if (key1.currentMeta ().getString () != key2.currentMeta ().getString ()) return false;
     108             :         }
     109             : 
     110        1900 :         return key1.nextMeta () == key2.nextMeta ();
     111             : }
     112             : 
     113             : /**
     114             :  * @brief This function checks if two keys are equal.
     115             :  *
     116             :  * @param key1 This parameter stores the first key this function compares.
     117             :  * @param key2 This parameter stores the second key this function compares.
     118             :  *
     119             :  * @retval true if all attributes of `key1` and `key2` are equal
     120             :  * @retval false otherwise
     121             :  */
     122         380 : bool isKeyEqual (kdb::Key & key1, kdb::Key & key2)
     123             : {
     124        1520 :         if (key1.getName () != key2.getName ()) return false;
     125        1140 :         if ((key1.isString () && key2.isBinary ()) || (key1.isBinary () && key2.isString ())) return false;
     126         760 :         if (key1.getBinarySize () != key2.getBinarySize ()) return false;
     127        1140 :         if (memcmp (key1.getValue (), key2.getValue (), key1.getBinarySize ())) return false;
     128         380 :         return isMetaDataEqual (key1, key2);
     129             : }
     130             : 
     131             : /**
     132             :  * @brief This function checks if two key sets are equal.
     133             :  *
     134             :  * @param keys1 This parameter stores the first key set this function compares.
     135             :  * @param keys2 This parameter stores the second key set this function compares.
     136             :  *
     137             :  * @retval true if all keys of `keys1` and `keys2` are equal
     138             :  * @retval false otherwise
     139             :  */
     140          86 : bool isKeySetEqual (kdb::KeySet & keys1, kdb::KeySet & keys2)
     141             : {
     142         172 :         if (keys1.size () != keys2.size ()) return false;
     143             : 
     144          86 :         keys1.rewind ();
     145             :         keys2.rewind ();
     146        1398 :         while (keys1.next ())
     147             :         {
     148         760 :                 keys2.next ();
     149        1140 :                 if (!keys2.current ()) return false;
     150         760 :                 kdb::Key key1 = keys1.current ();
     151         760 :                 kdb::Key key2 = keys2.current ();
     152         380 :                 if (!isKeyEqual (key1, key2)) return false;
     153             :         }
     154             : 
     155         430 :         return keys1.next () == keys2.next ();
     156             : }
     157             : 
     158             : /**
     159             :  * @brief This function compares the given key sets.
     160             :  *
     161             :  * @param keys1 This parameter stores the first key set this function compares.
     162             :  * @param keys2 This parameter stores the second key set this function compares.
     163             :  *
     164             :  * @retval AssertionSuccess if all keys of `keys1` and `keys2` are equal
     165             :  * @retval AssertionFailure otherwise
     166             :  */
     167          86 : testing::AssertionResult compareKeySet (kdb::KeySet & keys1, kdb::KeySet & keys2)
     168             : {
     169          86 :         if (isKeySetEqual (keys1, keys2))
     170          86 :                 return testing::AssertionSuccess ();
     171             :         else
     172           0 :                 return testing::AssertionFailure () << "\n\nFirst key set:\n\n"
     173           0 :                                                     << keys1 << "\n——————————"
     174             :                                                     << "\n\nSecond key set:\n\n"
     175           0 :                                                     << keys2 << endl;
     176             : }
     177             : 
     178             : #endif

Generated by: LCOV version 1.13