LCOV - code coverage report
Current view: top level - tests/ctest - test_ks.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 107 107 100.0 %
Date: 2019-09-12 12:28:41 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include <tests_internal.h>
      10             : 
      11             : ssize_t ksCopyInternal (KeySet * ks, size_t to, size_t from);
      12             : 
      13           2 : static void test_elektraRenameKeys (void)
      14             : {
      15           2 :         printf ("test rename keys\n");
      16           2 :         KeySet * ks = ksNew (20, keyNew ("system/some/common/prefix", KEY_END), keyNew ("system/some/common/prefix/dir", KEY_END),
      17             :                              keyNew ("system/some/common/prefix/dir/keya", KEY_END),
      18             :                              keyNew ("system/some/common/prefix/some", KEY_VALUE, "huhu", KEY_END),
      19             :                              keyNew ("system/some/common/prefix/other", KEY_END), KS_END);
      20           2 :         KeySet * cmp = ksNew (20, keyNew ("user/x/dir", KEY_END), keyNew ("user/x/dir/keya", KEY_END),
      21             :                               keyNew ("user/x/some", KEY_VALUE, "huhu", KEY_END), keyNew ("user/x/other", KEY_END), KS_END);
      22             : 
      23           2 :         KeySet * result = elektraRenameKeys (ks, "user/x");
      24           2 :         compare_keyset (result, cmp);
      25             :         // output_keyset(result);
      26           2 :         ksDel (cmp);
      27           2 :         ksDel (result);
      28           2 :         ksDel (ks);
      29             : 
      30           2 :         ks = ksNew (0, KS_END);
      31           2 :         result = elektraRenameKeys (ks, "user");
      32           2 :         output_keyset (result);
      33             : 
      34           2 :         ksDel (result);
      35           2 :         ksDel (ks);
      36           2 : }
      37             : 
      38           2 : static void test_elektraEmptyKeys (void)
      39             : {
      40           2 :         printf ("test empty keys\n");
      41           2 :         Key * key = keyNew ("", KEY_END);
      42           2 :         KeySet * ks = ksNew (0, KS_END);
      43             : 
      44           2 :         elektraKeySetName (key, "", KEY_META_NAME | KEY_CASCADING_NAME);
      45           2 :         succeed_if_same_string (keyName (key), "");
      46           2 :         succeed_if (key->key != 0, "null pointer?");
      47           2 :         ksAppendKey (ks, key);
      48             : 
      49           2 :         succeed_if (ksLookup (ks, key, 0) == key, "could not find empty key");
      50             : 
      51           2 :         ksDel (ks);
      52           2 : }
      53             : 
      54           2 : static void test_cascadingLookup (void)
      55             : {
      56           2 :         printf ("test cascading lookup\n");
      57             :         Key * k0;
      58             :         Key * k1;
      59             :         Key * k2;
      60             :         Key * k3;
      61           2 :         KeySet * ks = ksNew (10, k0 = keyNew ("system/benchmark/override/#0", 0), k1 = keyNew ("system/benchmark/override/#1", 0),
      62             :                              k2 = keyNew ("user/benchmark/override/#2", 0), k3 = keyNew ("user/benchmark/override/#3", 0), KS_END);
      63           2 :         Key * search = keyNew ("/benchmark/override/#0", KEY_CASCADING_NAME, KEY_END);
      64           2 :         Key * found = ksLookup (ks, search, 0);
      65           2 :         succeed_if (found == k0, "found wrong key");
      66             : 
      67           2 :         elektraKeySetName (search, "/benchmark/override/#1", KEY_CASCADING_NAME);
      68           2 :         found = ksLookup (ks, search, 0);
      69           2 :         succeed_if (found == k1, "found wrong key");
      70           2 :         keyDel (search);
      71             : 
      72           2 :         search = keyNew ("/benchmark/override/#2", KEY_CASCADING_NAME, KEY_END);
      73           2 :         found = ksLookup (ks, search, 0);
      74           2 :         succeed_if (found == k2, "found wrong key");
      75             : 
      76           2 :         elektraKeySetName (search, "/benchmark/override/#3", KEY_CASCADING_NAME);
      77           2 :         found = ksLookup (ks, search, 0);
      78           2 :         succeed_if (found == k3, "found wrong key");
      79           2 :         keyDel (search);
      80           2 :         ksDel (ks);
      81           2 : }
      82             : 
      83           2 : static void test_creatingLookup (void)
      84             : {
      85           2 :         printf ("Test creating lookup\n");
      86             : 
      87           2 :         KeySet * ks = ksNew (10, KS_END);
      88             : 
      89           2 :         Key * searchKey = keyNew ("user/something", KEY_VALUE, "a value", KEY_END);
      90           2 :         Key * k0 = ksLookup (ks, searchKey, KDB_O_CREATE);
      91           2 :         exit_if_fail (k0, "no key was created");
      92           2 :         succeed_if_same_string (keyName (k0), keyName (searchKey));
      93           2 :         succeed_if_same_string (keyString (k0), keyString (searchKey));
      94             : 
      95           2 :         Key * k1 = ksLookup (ks, searchKey, KDB_O_CREATE);
      96           2 :         exit_if_fail (k1, "no key was returned");
      97           2 :         succeed_if (k0 == k1, "not the same key");
      98             : 
      99           2 :         keyDel (searchKey);
     100           2 :         ksDel (ks);
     101             : 
     102             : 
     103           2 :         ks = ksNew (10, KS_END);
     104             : 
     105           2 :         searchKey = keyNew ("dir/something", KEY_VALUE, "a value", KEY_END);
     106           2 :         k0 = ksLookup (ks, searchKey, KDB_O_CREATE);
     107           2 :         exit_if_fail (k0, "no key was created");
     108           2 :         succeed_if_same_string (keyName (k0), keyName (searchKey));
     109           2 :         succeed_if_same_string (keyString (k0), keyString (searchKey));
     110             : 
     111           2 :         k1 = ksLookup (ks, searchKey, KDB_O_CREATE);
     112           2 :         exit_if_fail (k1, "no key was returned");
     113           2 :         succeed_if (k0 == k1, "not the same key");
     114             : 
     115           2 :         keyDel (searchKey);
     116           2 :         ksDel (ks);
     117             : 
     118             : 
     119           2 :         ks = ksNew (10, KS_END);
     120             : 
     121           2 :         searchKey = keyNew ("/something", KEY_CASCADING_NAME, KEY_VALUE, "a value", KEY_END);
     122             : 
     123             :         // check if duplication works:
     124           2 :         Key * dupKey = keyDup (searchKey);
     125           2 :         succeed_if_same_string (keyName (dupKey), keyName (searchKey));
     126           2 :         succeed_if_same_string (keyString (dupKey), keyString (searchKey));
     127           2 :         ksAppendKey (ks, dupKey);
     128           2 :         keyDel (dupKey);
     129             : 
     130           2 :         k0 = ksLookup (ks, searchKey, KDB_O_CREATE);
     131           2 :         exit_if_fail (k0, "no key was created");
     132           2 :         succeed_if_same_string (keyName (k0), keyName (searchKey));
     133           2 :         succeed_if_same_string (keyString (k0), keyString (searchKey));
     134             : 
     135           2 :         k1 = ksLookup (ks, searchKey, KDB_O_CREATE);
     136           2 :         exit_if_fail (k1, "no key was returned");
     137           2 :         succeed_if (k0 == k1, "not the same key");
     138             : 
     139           2 :         keyDel (searchKey);
     140           2 :         ksDel (ks);
     141             : 
     142             : 
     143           2 :         ks = ksNew (10, KS_END);
     144             : 
     145           2 :         searchKey = keyNew ("proc/something", KEY_VALUE, "a value", KEY_END);
     146           2 :         k0 = ksLookup (ks, searchKey, KDB_O_CREATE);
     147           2 :         exit_if_fail (k0, "no key was created");
     148           2 :         succeed_if_same_string (keyName (k0), keyName (searchKey));
     149           2 :         succeed_if_same_string (keyString (k0), keyString (searchKey));
     150             : 
     151           2 :         k1 = ksLookup (ks, searchKey, KDB_O_CREATE);
     152           2 :         exit_if_fail (k1, "no key was returned");
     153           2 :         succeed_if (k0 == k1, "not the same key");
     154             : 
     155           2 :         keyDel (searchKey);
     156           2 :         ksDel (ks);
     157           2 : }
     158             : 
     159           2 : int main (int argc, char ** argv)
     160             : {
     161           2 :         printf ("KS         TESTS\n");
     162           2 :         printf ("==================\n\n");
     163             : 
     164           2 :         init (argc, argv);
     165             : 
     166           2 :         test_elektraRenameKeys ();
     167           2 :         test_elektraEmptyKeys ();
     168           2 :         test_cascadingLookup ();
     169           2 :         test_creatingLookup ();
     170             : 
     171           2 :         printf ("\ntest_ks RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
     172             : 
     173           2 :         return nbError;
     174             : }

Generated by: LCOV version 1.13