LCOV - code coverage report
Current view: top level - src/tools/kdb - test.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 5 315 1.6 %
Date: 2019-09-12 12:28:41 Functions: 4 13 30.8 %

          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 <test.hpp>
      10             : 
      11             : #include <cmdline.hpp>
      12             : #include <kdb.hpp>
      13             : 
      14             : #include <algorithm>
      15             : #include <iostream>
      16             : #include <vector>
      17             : 
      18             : using namespace kdb;
      19             : using namespace std;
      20             : 
      21          78 : TestCommand::TestCommand ()
      22             : : root (), nrTest (0), nrError (0),
      23             :   // XXX add here the name if you add a test
      24             :   // (spaces at begin/end needed for check)
      25         312 :   testNames (" basic string umlauts binary naming meta ")
      26             : {
      27          78 : }
      28             : 
      29           0 : void TestCommand::doBasicTest ()
      30             : {
      31             :         {
      32           0 :                 KDB kdb;
      33           0 :                 Key t = root.dup ();
      34           0 :                 t.addBaseName ("basic");
      35           0 :                 t.setString ("BasicString");
      36           0 :                 KeySet basic;
      37           0 :                 basic.append (t);
      38             : 
      39           0 :                 KeySet test;
      40           0 :                 kdb.get (test, root);
      41           0 :                 kdb.set (basic, root);
      42             :         }
      43             : 
      44             :         {
      45           0 :                 KDB kdb;
      46           0 :                 Key t = root.dup ();
      47           0 :                 t.addBaseName ("basic");
      48           0 :                 t.setString ("BasicString");
      49             : 
      50           0 :                 KeySet test;
      51           0 :                 kdb.get (test, root);
      52             : 
      53           0 :                 nrTest++;
      54           0 :                 if (!test.lookup (t))
      55             :                 {
      56           0 :                         nrError++;
      57           0 :                         cerr << "Basic test failed" << endl;
      58             :                 }
      59             :         }
      60           0 : }
      61             : 
      62           0 : void TestCommand::doStringTest ()
      63             : {
      64           0 :         vector<string> teststrings;
      65           0 :         teststrings.push_back ("");
      66           0 :         teststrings.push_back ("value");
      67           0 :         teststrings.push_back ("value with spaces");
      68           0 :         teststrings.push_back (" a very long value with many spaces and basically very very long, but only text ... ");
      69           0 :         for (int i = 1; i < 256; ++i)
      70           0 :                 teststrings.back () += " very very long, but only text ... ";
      71             : 
      72             : 
      73           0 :         for (auto & teststring : teststrings)
      74             :         {
      75             :                 {
      76           0 :                         KDB kdb;
      77           0 :                         Key t = root.dup ();
      78           0 :                         t.addBaseName ("string");
      79           0 :                         t.setString (teststring);
      80             : 
      81           0 :                         KeySet basic;
      82           0 :                         basic.append (t);
      83             : 
      84           0 :                         KeySet test;
      85           0 :                         kdb.get (test, root);
      86           0 :                         kdb.set (basic, root);
      87             :                 }
      88             : 
      89             :                 {
      90           0 :                         KDB kdb;
      91             : 
      92           0 :                         KeySet test;
      93           0 :                         kdb.get (test, root);
      94             : 
      95           0 :                         Key t = root.dup ();
      96           0 :                         t.addBaseName ("string");
      97             : 
      98           0 :                         Key res = test.lookup (t);
      99             : 
     100           0 :                         nrTest++;
     101           0 :                         if (!res)
     102             :                         {
     103           0 :                                 nrError++;
     104           0 :                                 cerr << "String test failed (key not found)" << t.getName () << endl;
     105           0 :                                 continue;
     106             :                         }
     107             : 
     108           0 :                         nrTest++;
     109           0 :                         if (res.getString () != teststring)
     110             :                         {
     111           0 :                                 nrError++;
     112           0 :                                 cerr << "String test failed (value is not equal)" << endl;
     113           0 :                                 cerr << "We got: \"" << res.getString () << "\"" << endl;
     114           0 :                                 cerr << "We wanted: \"" << teststring << "\"" << endl;
     115             :                         }
     116             :                 }
     117             :         }
     118           0 : }
     119             : 
     120           0 : void TestCommand::doUmlautsTest ()
     121             : {
     122           0 :         vector<string> teststrings;
     123           0 :         teststrings.push_back ("ascii umlauts !\"§$%&/()=?`\\}][{");
     124           0 :         teststrings.push_back ("utf8 umlauts ¸¬½¼³²¹ł€¶øæßð𳽫»¢“”nµ─·");
     125           0 :         teststrings.push_back ("all chars:");
     126           0 :         for (int i = 1; i < 256; ++i)
     127           0 :                 teststrings.back ().push_back (i);
     128           0 :         teststrings.push_back ("€");
     129           0 :         for (int i = 1; i < 256; ++i)
     130             :         {
     131           0 :                 string s;
     132           0 :                 s.push_back (i);
     133           0 :                 teststrings.push_back (s);
     134             :         }
     135             : 
     136             : 
     137           0 :         for (auto & teststring : teststrings)
     138             :         {
     139             :                 {
     140           0 :                         KDB kdb;
     141           0 :                         Key t = root.dup ();
     142           0 :                         t.addBaseName ("string");
     143           0 :                         t.setString (teststring);
     144             : 
     145           0 :                         KeySet basic;
     146           0 :                         basic.append (t);
     147             : 
     148           0 :                         KeySet test;
     149           0 :                         kdb.get (test, root);
     150           0 :                         kdb.set (basic, root);
     151             :                 }
     152             : 
     153             :                 {
     154           0 :                         KDB kdb;
     155             : 
     156           0 :                         KeySet test;
     157           0 :                         kdb.get (test, root);
     158             : 
     159           0 :                         Key t = root.dup ();
     160           0 :                         t.addBaseName ("string");
     161             : 
     162           0 :                         Key res = test.lookup (t);
     163             : 
     164           0 :                         nrTest++;
     165           0 :                         if (!res)
     166             :                         {
     167           0 :                                 nrError++;
     168           0 :                                 cerr << "String test failed (key not found)" << t.getName () << endl;
     169           0 :                                 continue;
     170             :                         }
     171             : 
     172           0 :                         nrTest++;
     173           0 :                         if (res.getString () != teststring)
     174             :                         {
     175           0 :                                 nrError++;
     176           0 :                                 cerr << "String test failed (value is not equal)" << endl;
     177           0 :                                 cerr << "We got: \"" << res.getString () << "\"" << endl;
     178           0 :                                 cerr << "We wanted: \"" << teststring << "\"" << endl;
     179             :                         }
     180             :                 }
     181             :         }
     182           0 : }
     183             : 
     184           0 : void TestCommand::doBinaryTest ()
     185             : {
     186           0 :         vector<string> teststrings;
     187           0 :         teststrings.push_back ("binary value");
     188           0 :         teststrings.push_back ("binary value with null");
     189           0 :         teststrings.back ().push_back ('\0');
     190             : 
     191           0 :         teststrings.push_back ("binary value with null");
     192           0 :         teststrings.back ().push_back ('\0');
     193           0 :         teststrings.back () += "in the middle";
     194             : 
     195           0 :         teststrings.push_back (" a very long value with many spaces and basically very very long, but only binary text ... ");
     196           0 :         for (int i = 0; i < 256; ++i)
     197             :         {
     198           0 :                 teststrings.back ().push_back ('\0');
     199           0 :                 teststrings.back () += " very very long, but only binary text ... ";
     200             :         }
     201             : 
     202           0 :         teststrings.push_back ("all chars:");
     203           0 :         for (int i = 0; i < 256; ++i)
     204           0 :                 teststrings.back ().push_back (i);
     205           0 :         teststrings.push_back ("€");
     206           0 :         for (int i = 0; i < 256; ++i)
     207             :         {
     208           0 :                 string s;
     209           0 :                 s.push_back (i);
     210           0 :                 teststrings.push_back (s);
     211             :         }
     212             : 
     213             : 
     214           0 :         for (auto & teststring : teststrings)
     215             :         {
     216             :                 {
     217           0 :                         KDB kdb;
     218           0 :                         Key t = root.dup ();
     219           0 :                         t.addBaseName ("binary");
     220           0 :                         t.setBinary (teststring.c_str (), teststring.length ());
     221             : 
     222           0 :                         KeySet basic;
     223           0 :                         basic.append (t);
     224             : 
     225           0 :                         KeySet test;
     226           0 :                         kdb.get (test, root);
     227           0 :                         kdb.set (basic, root);
     228             :                 }
     229             : 
     230             :                 {
     231           0 :                         KDB kdb;
     232             : 
     233           0 :                         KeySet test;
     234           0 :                         kdb.get (test, root);
     235             : 
     236           0 :                         Key t = root.dup ();
     237           0 :                         t.addBaseName ("binary");
     238             : 
     239           0 :                         Key res = test.lookup (t);
     240           0 :                         nrTest++;
     241           0 :                         if (!res)
     242             :                         {
     243           0 :                                 nrError++;
     244           0 :                                 cerr << "Binary test failed (key not found)" << t.getName () << endl;
     245           0 :                                 continue;
     246             :                         }
     247             : 
     248           0 :                         nrTest++;
     249           0 :                         if (res.getBinarySize () > 0 && static_cast<size_t> (res.getBinarySize ()) != teststring.length ())
     250             :                         {
     251           0 :                                 nrError++;
     252           0 :                                 cerr << "Binary test failed (length is not equal)" << endl;
     253           0 :                                 cerr << "We got: \"" << res.getBinary () << "\"" << endl;
     254           0 :                                 cerr << "We wanted: \"" << teststring << "\"" << endl;
     255             :                         }
     256             : 
     257           0 :                         nrTest++;
     258           0 :                         if (res.getBinary () != teststring)
     259             :                         {
     260           0 :                                 nrError++;
     261           0 :                                 cerr << "Binary test failed (value is not equal)" << endl;
     262           0 :                                 cerr << "We got: \"" << res.getBinary () << "\"" << endl;
     263           0 :                                 cerr << "We wanted: \"" << teststring << "\"" << endl;
     264             :                         }
     265             :                 }
     266             :         }
     267           0 : }
     268             : 
     269           0 : void TestCommand::doNamingTest ()
     270             : {
     271           0 :         vector<string> teststrings;
     272           0 :         teststrings.push_back ("keyname");
     273           0 :         teststrings.push_back ("deep/below/keyname");
     274           0 :         teststrings.push_back ("keyname with spaces");
     275           0 :         teststrings.push_back ("deep/belowkeyname with spaces");
     276           0 :         teststrings.push_back (" a very long value with many spaces and basically very very long, but only text ");
     277           0 :         for (int i = 1; i < 256; ++i)
     278           0 :                 teststrings.back () += "/ very very long, but only text ... ";
     279           0 :         teststrings.push_back ("ascii umlauts !\"§$%&/()=?`\\}][{");
     280           0 :         teststrings.push_back ("utf8 umlauts ¸¬½¼³²¹ł€¶øæßð𳽫»¢“”nµ─·");
     281           0 :         teststrings.push_back ("all chars:");
     282           0 :         for (int i = 1; i < 256; ++i)
     283           0 :                 teststrings.back ().push_back (i);
     284           0 :         teststrings.push_back ("€");
     285           0 :         for (int i = 1; i < 256; ++i)
     286             :         {
     287           0 :                 if (i == '.') continue;
     288           0 :                 string s;
     289           0 :                 s.push_back (i);
     290           0 :                 teststrings.push_back (s);
     291             :         }
     292             : 
     293             : 
     294           0 :         for (auto & teststring : teststrings)
     295             :         {
     296             :                 {
     297           0 :                         KDB kdb;
     298           0 :                         Key t = root.dup ();
     299           0 :                         t.addBaseName (teststring);
     300             : 
     301           0 :                         KeySet basic;
     302           0 :                         basic.append (t);
     303             : 
     304           0 :                         KeySet test;
     305           0 :                         kdb.get (test, root);
     306           0 :                         kdb.set (basic, root);
     307             :                 }
     308             : 
     309             :                 {
     310           0 :                         KDB kdb;
     311             : 
     312           0 :                         KeySet test;
     313           0 :                         kdb.get (test, root);
     314             : 
     315           0 :                         test.rewind ();
     316           0 :                         Key res = test.next ();
     317             : 
     318           0 :                         nrTest++;
     319           0 :                         if (!res)
     320             :                         {
     321           0 :                                 nrError++;
     322           0 :                                 cerr << "Naming test failed (no key in keyset)" << endl;
     323           0 :                                 continue;
     324             :                         }
     325             : 
     326           0 :                         nrTest++;
     327           0 :                         Key cmp = root.dup ();
     328           0 :                         cmp.addBaseName (teststring);
     329           0 :                         if (res != cmp)
     330             :                         {
     331           0 :                                 nrError++;
     332           0 :                                 cerr << "Naming test failed (name is not equal)" << endl;
     333           0 :                                 cerr << "We got: \"" << res.getName () << "\"" << endl;
     334           0 :                                 cerr << "We wanted: \"" << cmp.getName () << "\"" << endl;
     335             :                         }
     336             :                 }
     337             :         }
     338           0 : }
     339             : 
     340           0 : void TestCommand::doMetaTest ()
     341             : {
     342           0 :         vector<string> teststrings;
     343           0 :         teststrings.push_back ("");
     344           0 :         teststrings.push_back ("value");
     345           0 :         teststrings.push_back ("value with spaces");
     346           0 :         teststrings.push_back (" a very long value with many spaces and basically very very long, but only text ... ");
     347           0 :         for (int i = 1; i < 256; ++i)
     348           0 :                 teststrings.back () += " very very long, but only text ... ";
     349           0 :         teststrings.push_back ("ascii umlauts !\"§$%&/()=?`\\}][{");
     350           0 :         teststrings.push_back ("utf8 umlauts ¸¬½¼³²¹ł€¶øæßð𳽫»¢“”nµ─·");
     351           0 :         teststrings.push_back ("all chars:");
     352           0 :         for (int i = 1; i < 256; ++i)
     353           0 :                 teststrings.back ().push_back (i);
     354           0 :         teststrings.push_back ("€");
     355           0 :         for (int i = 1; i < 256; ++i)
     356             :         {
     357           0 :                 string s;
     358           0 :                 s.push_back (i);
     359           0 :                 teststrings.push_back (s);
     360             :         }
     361             : 
     362           0 :         vector<string> testnames;
     363           0 :         testnames.push_back ("keyname");
     364           0 :         testnames.push_back ("deep/below/keyname");
     365           0 :         testnames.push_back ("keyname with spaces");
     366           0 :         testnames.push_back ("deep/belowkeyname with spaces");
     367           0 :         testnames.push_back (" a very long value with many spaces and basically very very long, but only text ");
     368           0 :         for (int i = 1; i < 256; ++i)
     369           0 :                 testnames.back () += "/ very very long, but only text ... ";
     370           0 :         testnames.push_back ("ascii umlauts !\"§$%&/()=?`\\}][{");
     371           0 :         testnames.push_back ("utf8 umlauts ¸¬½¼³²¹ł€¶øæßð𳽫»¢“”nµ─·");
     372           0 :         testnames.push_back ("all chars:");
     373           0 :         for (int i = 1; i < 256; ++i)
     374           0 :                 testnames.back ().push_back (i);
     375           0 :         testnames.push_back ("€");
     376           0 :         for (int i = 1; i < 256; ++i)
     377             :         {
     378           0 :                 if (i == 46) continue; // ignore .
     379           0 :                 string s;
     380           0 :                 s.push_back (i);
     381           0 :                 testnames.push_back (s);
     382             :         }
     383             : 
     384             : 
     385           0 :         for (auto & testname : testnames)
     386           0 :                 for (auto & teststring : teststrings)
     387             :                 {
     388             :                         {
     389           0 :                                 KDB kdb;
     390           0 :                                 Key t = root.dup ();
     391           0 :                                 t.addBaseName (testname);
     392           0 :                                 t.setMeta<string> ("key", teststring);
     393             : 
     394           0 :                                 KeySet basic;
     395           0 :                                 basic.append (t);
     396             : 
     397           0 :                                 KeySet test;
     398           0 :                                 kdb.get (test, root);
     399           0 :                                 kdb.set (basic, root);
     400             :                         }
     401             : 
     402             :                         {
     403           0 :                                 KDB kdb;
     404             : 
     405           0 :                                 KeySet test;
     406           0 :                                 kdb.get (test, root);
     407             : 
     408           0 :                                 Key t = root.dup ();
     409           0 :                                 t.addBaseName (testname);
     410           0 :                                 Key res = test.lookup (t);
     411             : 
     412           0 :                                 nrTest++;
     413           0 :                                 if (!res)
     414             :                                 {
     415           0 :                                         nrError++;
     416           0 :                                         cerr << "Meta test failed (key not found)" << t.getName () << endl;
     417           0 :                                         continue;
     418             :                                 }
     419             : 
     420           0 :                                 std::string meta = res.getMeta<std::string> ("key");
     421             : 
     422           0 :                                 nrTest++;
     423           0 :                                 if (meta != teststring)
     424             :                                 {
     425           0 :                                         nrError++;
     426           0 :                                         cerr << "Meta test failed (name is not equal)" << endl;
     427           0 :                                         cerr << "We got: \"" << meta << "\"" << endl;
     428           0 :                                         cerr << "We wanted: \"" << teststring << "\"" << endl;
     429             :                                 }
     430             :                         }
     431             :                 }
     432           0 : }
     433             : 
     434             : namespace
     435             : {
     436           0 : bool checkArgument (std::vector<std::string> const & arguments, std::string testname)
     437             : {
     438           0 :         return arguments.size () == 1 || find (arguments.begin (), arguments.end (), testname) != arguments.end ();
     439             : }
     440             : } // namespace
     441             : 
     442           0 : void TestCommand::doTests (std::vector<std::string> const & arguments)
     443             : {
     444           0 :         if (checkArgument (arguments, "basic"))
     445             :         {
     446           0 :                 cout << "Doing basic tests" << std::endl;
     447           0 :                 doBasicTest ();
     448             :         }
     449           0 :         if (checkArgument (arguments, "string"))
     450             :         {
     451           0 :                 cout << "Doing string tests" << std::endl;
     452           0 :                 doStringTest ();
     453             :         }
     454           0 :         if (checkArgument (arguments, "umlauts"))
     455             :         {
     456           0 :                 cout << "Doing umlauts tests" << std::endl;
     457           0 :                 doUmlautsTest ();
     458             :         }
     459           0 :         if (checkArgument (arguments, "binary"))
     460             :         {
     461           0 :                 cout << "Doing binary tests" << std::endl;
     462           0 :                 doBinaryTest ();
     463             :         }
     464           0 :         if (checkArgument (arguments, "naming"))
     465             :         {
     466           0 :                 cout << "Doing naming tests" << std::endl;
     467           0 :                 doNamingTest ();
     468             :         }
     469           0 :         if (checkArgument (arguments, "meta"))
     470             :         {
     471           0 :                 cout << "Doing meta tests" << std::endl;
     472           0 :                 doMetaTest ();
     473             :         }
     474             :         // XXX add here a new test execution (as above)
     475           0 : }
     476             : 
     477           0 : int TestCommand::execute (Cmdline const & cl)
     478             : {
     479           0 :         if (cl.arguments.size () < 1)
     480             :         {
     481           0 :                 throw invalid_argument ("need at least one argument");
     482             :         }
     483             : 
     484             :         // do a basic check on every argument
     485           0 :         for (size_t i = 1; i < cl.arguments.size (); ++i)
     486             :         {
     487           0 :                 string name = " ";
     488           0 :                 name += cl.arguments[i];
     489           0 :                 name += " ";
     490           0 :                 if (testNames.find (name) == std::string::npos)
     491             :                 {
     492           0 :                         throw invalid_argument ("test name " + cl.arguments[i] + " does not exist in:" + testNames);
     493             :                 }
     494             :         }
     495             : 
     496           0 :         printWarnings (cerr, root, cl.verbose, cl.debug);
     497             : 
     498           0 :         root = cl.createKey (0);
     499             : 
     500           0 :         KDB kdb;
     501           0 :         KeySet original;
     502           0 :         kdb.get (original, root);
     503           0 :         original.rewind ();
     504             : 
     505           0 :         doTests (cl.arguments);
     506             : 
     507           0 :         cerr << "We got " << nrError << " errors in " << nrTest << " test cases." << endl;
     508             : 
     509           0 :         cout << "Test suite is now finished." << endl;
     510           0 :         cout << "Now restoring the original keyset." << endl;
     511           0 :         kdb.set (original, root);
     512             : 
     513           0 :         printWarnings (cerr, root, cl.verbose, cl.debug);
     514             : 
     515           0 :         return nrError;
     516             : }
     517             : 
     518         312 : TestCommand::~TestCommand ()
     519             : {
     520        7320 : }

Generated by: LCOV version 1.13