LCOV - code coverage report
Current view: top level - src/bindings/cpp/tests - testcpp_meta.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 109 110 99.1 %
Date: 2019-09-12 12:28:41 Functions: 12 17 70.6 %

          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.hpp>
      10             : 
      11             : #include <stdexcept>
      12             : #include <string>
      13             : #include <vector>
      14             : 
      15          20 : TEST (meta, basic)
      16             : {
      17             :         // cout << "testing metainfo" << endl;
      18           4 :         Key test;
      19             : 
      20          12 :         succeed_if (test.hasMeta ("mode") == false, "has meta?");
      21          12 :         succeed_if (test.getMeta<mode_t> ("mode") == 0, "not properly default constructed");
      22             : 
      23           8 :         test.setMeta<mode_t> ("mode", 0775);
      24          12 :         succeed_if (test.hasMeta ("mode") == true, "has not meta even though set?");
      25             : 
      26          12 :         succeed_if (test.getMeta<mode_t> ("mode") == 0775, "not correct default mode for dir");
      27             : 
      28           8 :         test.setMeta<int> ("myint", 333);
      29          12 :         succeed_if (test.getMeta<int> ("myint") == 333, "could not set other meta");
      30             : 
      31           8 :         test.setMeta<double> ("mydouble", 333.3);
      32          12 :         succeed_if (test.hasMeta ("mydouble"), "no metadata even though it was just set");
      33          12 :         succeed_if (test.getMeta<double> ("mydouble") >= 333.2, "could not set other meta");
      34          12 :         succeed_if (test.getMeta<double> ("mydouble") <= 333.4, "could not set other meta");
      35             : 
      36          10 :         test.delMeta ("mydouble");
      37          12 :         succeed_if (!test.hasMeta ("mydouble"), "metadata there even though it was just deleted");
      38             : 
      39          14 :         test.setMeta<std::string> ("mystr", "str");
      40          16 :         succeed_if (test.getMeta<std::string> ("mystr") == "str", "could not set other meta");
      41             : 
      42          10 :         const ckdb::Key * cmeta = test.getMeta<const ckdb::Key *> ("mystr");
      43           6 :         succeed_if (!strcmp (static_cast<const char *> (ckdb::keyValue (cmeta)), "str"), "could not set other meta");
      44             : 
      45          10 :         const ckdb::Key * nmeta = test.getMeta<const ckdb::Key *> ("not available");
      46           6 :         succeed_if (nmeta == nullptr, "not available metadata did not give a null pointer");
      47             : 
      48          12 :         const Key meta = test.getMeta<const Key> ("mystr");
      49           4 :         succeed_if (meta, "null key");
      50          10 :         succeed_if (meta.getString () == "str", "could not get other meta");
      51             : 
      52          12 :         const Key xmeta = test.getMeta<const Key> ("not available");
      53           6 :         succeed_if (!xmeta, "not a null key");
      54             : 
      55          10 :         const char * str = test.getMeta<const char *> ("mystr");
      56           6 :         succeed_if (!strcmp (str, "str"), "could not get meta as c-string");
      57             : 
      58          10 :         const char * nstr = test.getMeta<const char *> ("not available");
      59           6 :         succeed_if (nstr == nullptr, "did not get null pointer on not available metadata");
      60             : 
      61          12 :         succeed_if (test.getMeta<int> ("not available") == 0, "not default constructed");
      62          16 :         succeed_if (test.getMeta<std::string> ("not available") == "", "not default constructed");
      63             : 
      64          14 :         test.setMeta<std::string> ("wrong", "not an int");
      65             : 
      66             :         try
      67             :         {
      68          12 :                 succeed_if (test.hasMeta ("wrong") == true, "meta is here");
      69           6 :                 test.getMeta<int> ("wrong");
      70           0 :                 succeed_if (0, "exception did not raise");
      71             :         }
      72           4 :         catch (KeyTypeConversion const & e)
      73             :         {
      74           6 :                 succeed_if (1, "no such metadata");
      75             :         }
      76             : }
      77             : 
      78          20 : TEST (meta, iter)
      79             : {
      80             :         // clang-format off
      81             :         Key k ("user/metakey",
      82             :                 KEY_META, "a", "meta",
      83             :                 KEY_META, "b", "my",
      84             :                 KEY_META, "c", "other",
      85           4 :                 KEY_END);
      86             :         // clang-format on
      87             : 
      88           4 :         Key meta; // key = keyNew(0)
      89             : 
      90           4 :         succeed_if (meta, "key is a not null key");
      91             : 
      92           4 :         Key end = static_cast<ckdb::Key *> (nullptr); // key = 0
      93           6 :         succeed_if (!end, "key is a null key");
      94             : 
      95           2 :         int count = 0;
      96             :         k.rewindMeta ();
      97          38 :         while ((meta = k.nextMeta ()))
      98           6 :                 count++;
      99           6 :         succeed_if (count == 3, "Not the correct number of metadata");
     100             : 
     101           8 :         k.setMeta ("d", "more");
     102           8 :         k.setMeta ("e", "even more");
     103             : 
     104           2 :         count = 0;
     105             :         k.rewindMeta ();
     106          58 :         while ((meta = k.nextMeta ()))
     107          10 :                 count++;
     108           6 :         succeed_if (count == 5, "Not the correct number of metadata");
     109             : }
     110             : 
     111          20 : TEST (test, copy)
     112             : {
     113             :         // cout << "testing copy meta" << std::endl;
     114             : 
     115             :         Key k ("user/metakey", KEY_META, "", "metavalue", KEY_META, "a", "a metavalue", KEY_META, "b", "b metavalue", KEY_META, "c",
     116           4 :                "c metavalue", KEY_END);
     117           4 :         Key c;
     118             : 
     119          10 :         c.copyMeta (k, "a");
     120             : 
     121          22 :         succeed_if (k.getMeta<const ckdb::Key *> ("a") == c.getMeta<const ckdb::Key *> ("a"), "copy meta did not work");
     122             : 
     123          10 :         c.copyMeta (k, "");
     124             : 
     125          22 :         succeed_if (k.getMeta<const ckdb::Key *> ("") == c.getMeta<const ckdb::Key *> (""), "copy meta did not work");
     126             : 
     127           8 :         k.setMeta<int> ("a", 420);
     128             : 
     129          12 :         succeed_if (k.getMeta<int> ("a") == 420, "could not get value set before");
     130             : 
     131           8 :         c.copyMeta (k, "a");
     132          12 :         succeed_if (c.getMeta<int> ("a") == 420, "could not get value copied before");
     133          22 :         succeed_if (k.getMeta<const ckdb::Key *> ("a") == c.getMeta<const ckdb::Key *> ("a"), "copy meta did not work");
     134             : 
     135          10 :         c.copyMeta (k, "a");
     136          12 :         succeed_if (c.getMeta<int> ("a") == 420, "could not get value copied before (again)");
     137          22 :         succeed_if (k.getMeta<const ckdb::Key *> ("a") == c.getMeta<const ckdb::Key *> ("a"), "copy meta did not work (again)");
     138             : 
     139           4 :         Key d;
     140           4 :         Key meta;
     141             : 
     142             :         k.rewindMeta ();
     143          48 :         while ((meta = k.nextMeta ()))
     144             :         {
     145          24 :                 d.copyMeta (k, meta.getName ());
     146             :         }
     147             : 
     148          16 :         succeed_if (d.getMeta<std::string> ("a") == "420", "did not copy metavalue in the loop");
     149          22 :         succeed_if (d.getMeta<const ckdb::Key *> ("a") == d.getMeta<const ckdb::Key *> ("a"), "copy meta did not work in the loop");
     150             : 
     151          16 :         succeed_if (d.getMeta<std::string> ("") == "metavalue", "did not copy metavalue in the loop");
     152          22 :         succeed_if (d.getMeta<const ckdb::Key *> ("") == d.getMeta<const ckdb::Key *> (""), "copy meta did not work in the loop");
     153             : 
     154          16 :         succeed_if (d.getMeta<std::string> ("b") == "b metavalue", "did not copy metavalue in the loop");
     155          22 :         succeed_if (d.getMeta<const ckdb::Key *> ("b") == d.getMeta<const ckdb::Key *> ("b"), "copy meta did not work in the loop");
     156             : 
     157          16 :         succeed_if (d.getMeta<std::string> ("c") == "c metavalue", "did not copy metavalue in the loop");
     158          22 :         succeed_if (d.getMeta<const ckdb::Key *> ("c") == d.getMeta<const ckdb::Key *> ("c"), "copy meta did not work in the loop");
     159             : }
     160             : 
     161          20 : TEST (meta, string)
     162             : {
     163             :         Key k ("user/anything", KEY_META, "", "metavalue", KEY_META, "a", "a metavalue", KEY_META, "b", "b metavalue", KEY_META, "c",
     164           4 :                "c metavalue", KEY_END);
     165             : 
     166          16 :         succeed_if (k.getMeta<string> ("a") == "a metavalue", "could not get metavalue");
     167             : 
     168          12 :         Key m = k.getMeta<const Key> ("a");
     169           4 :         succeed_if (m, "could not get metakey");
     170          10 :         succeed_if (m.getString () == "a metavalue", "could not get meta string");
     171             : 
     172          12 :         Key m1 = k.getMeta<const Key> ("x");
     173           6 :         succeed_if (!m1, "got not existing metakey");
     174             : }
     175             : 
     176          20 : TEST (meta, copyAll)
     177             : {
     178             :         Key k ("user/metakey", KEY_META, "", "metavalue", KEY_META, "a", "a metavalue", KEY_META, "b", "b metavalue", KEY_META, "c",
     179           4 :                "c metavalue", KEY_META, "i", "420", KEY_END);
     180           4 :         Key c;
     181             : 
     182           2 :         c.copyAllMeta (k);
     183             : 
     184          22 :         succeed_if (c.getMeta<const ckdb::Key *> ("a") == c.getMeta<const ckdb::Key *> ("a"), "copy meta did not work");
     185          22 :         succeed_if (c.getMeta<const ckdb::Key *> ("") == c.getMeta<const ckdb::Key *> (""), "copy meta did not work");
     186          12 :         succeed_if (c.getMeta<int> ("i") == 420, "could not get value copied before");
     187          16 :         succeed_if (c.getMeta<std::string> ("i") == "420", "did not copy metavalue in the loop");
     188          22 :         succeed_if (c.getMeta<const ckdb::Key *> ("a") == c.getMeta<const ckdb::Key *> ("a"), "copy meta did not work");
     189             : 
     190           4 :         Key d;
     191             : 
     192           2 :         d.copyAllMeta (k);
     193          16 :         succeed_if (d.getMeta<std::string> ("i") == "420", "did not copy metavalue in the loop");
     194          12 :         succeed_if (d.getMeta<int> ("i") == 420, "could not get value copied before");
     195          22 :         succeed_if (k.getMeta<const ckdb::Key *> ("a") == d.getMeta<const ckdb::Key *> ("a"), "copy meta did not work in the loop");
     196             : 
     197          16 :         succeed_if (d.getMeta<std::string> ("") == "metavalue", "did not copy metavalue in the loop");
     198          22 :         succeed_if (k.getMeta<const ckdb::Key *> ("") == d.getMeta<const ckdb::Key *> (""), "copy meta did not work in the loop");
     199             : 
     200          16 :         succeed_if (d.getMeta<std::string> ("b") == "b metavalue", "did not copy metavalue in the loop");
     201          22 :         succeed_if (k.getMeta<const ckdb::Key *> ("b") == d.getMeta<const ckdb::Key *> ("b"), "copy meta did not work in the loop");
     202             : 
     203          16 :         succeed_if (d.getMeta<std::string> ("c") == "c metavalue", "did not copy metavalue in the loop");
     204          22 :         succeed_if (k.getMeta<const ckdb::Key *> ("c") == d.getMeta<const ckdb::Key *> ("c"), "copy meta did not work in the loop");
     205           6 : }

Generated by: LCOV version 1.13