LCOV - code coverage report
Current view: top level - src/plugins/hexnumber - testmod_hexnumber.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 80 80 100.0 %
Date: 2019-09-12 12:28:41 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for hexnumber plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <stdlib.h>
      11             : #include <string.h>
      12             : 
      13             : #include <kdbconfig.h>
      14             : 
      15             : #include <tests_plugin.h>
      16             : 
      17             : #include "hexnumber.h"
      18             : 
      19             : #define CREATE_TEST_KEY(HEX) (keyNew ("user/tests/hexnumber/" #HEX, KEY_VALUE, #HEX, KEY_META, "type", "long", KEY_END))
      20             : #define CREATE_TEST_KEY_UNITBASE(HEX) (keyNew ("user/tests/hexnumber/" #HEX, KEY_VALUE, #HEX, KEY_META, "unit/base", "hex", KEY_END))
      21             : #define CREATE_TEST_KEY_CUSTOM(HEX, TYPE) (keyNew ("user/tests/hexnumber/" #HEX, KEY_VALUE, #HEX, KEY_META, "type", TYPE, KEY_END))
      22             : #define CHECK_TEST_KEY(HEX, DEC) succeed_if_same_string (keyString (ksLookupByName (ks, "user/tests/hexnumber/" #HEX, 0)), #DEC)
      23             : 
      24             : 
      25           2 : static void test_basics (void)
      26             : {
      27           2 :         Key * parentKey = keyNew ("user/tests/hexnumber", KEY_END);
      28           2 :         KeySet * conf = ksNew (0, KS_END);
      29           2 :         PLUGIN_OPEN ("hexnumber");
      30             : 
      31           2 :         KeySet * ks = ksNew (0, KS_END);
      32             : 
      33           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_NO_UPDATE, "call to kdbGet was not successful");
      34             : 
      35           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_NO_UPDATE, "call to kdbSet was not successful");
      36             : 
      37           2 :         keyDel (parentKey);
      38           2 :         ksDel (ks);
      39           2 :         PLUGIN_CLOSE ();
      40           2 : }
      41             : 
      42             : 
      43           2 : static void test_default (void)
      44             : {
      45           2 :         Key * parentKey = keyNew ("user/tests/hexnumber", KEY_END);
      46           2 :         KeySet * conf = ksNew (0, KS_END);
      47           2 :         PLUGIN_OPEN ("hexnumber");
      48           2 :         KeySet * ks = ksNew (30, CREATE_TEST_KEY (0xF), CREATE_TEST_KEY (0xf), CREATE_TEST_KEY (0x14), CREATE_TEST_KEY (0xFFFFFFFFFFFFFFFF),
      49             :                              CREATE_TEST_KEY (0x0), CREATE_TEST_KEY (0x2), CREATE_TEST_KEY (-0x2), CREATE_TEST_KEY (-0x1),
      50             :                              CREATE_TEST_KEY (test), KS_END);
      51           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
      52           2 :         CHECK_TEST_KEY (0xF, 15);
      53           2 :         CHECK_TEST_KEY (0xf, 15);
      54           2 :         CHECK_TEST_KEY (0x14, 20);
      55           2 :         CHECK_TEST_KEY (0xFFFFFFFFFFFFFFFF, 18446744073709551615);
      56           2 :         CHECK_TEST_KEY (0x0, 0);
      57           2 :         CHECK_TEST_KEY (0x2, 2);
      58           2 :         CHECK_TEST_KEY (-0x2, -0x2);
      59           2 :         CHECK_TEST_KEY (-0x1, -0x1);
      60           2 :         CHECK_TEST_KEY (test, test);
      61             : 
      62           2 :         ksDel (ks);
      63           2 :         keyDel (parentKey);
      64             : 
      65           2 :         PLUGIN_CLOSE ();
      66           2 : }
      67             : 
      68           2 : static void test_customint (void)
      69             : {
      70           2 :         Key * parentKey = keyNew ("user/tests/hexnumber", KEY_END);
      71           2 :         KeySet * conf = ksNew (10, keyNew ("system/accept/type/#0", KEY_VALUE, "customint", KEY_END),
      72             :                                keyNew ("system/accept/type/#1", KEY_VALUE, "othercustomint", KEY_END), KS_END);
      73           2 :         PLUGIN_OPEN ("hexnumber");
      74           2 :         KeySet * ks = ksNew (30, CREATE_TEST_KEY_CUSTOM (0x1F, "customint"), CREATE_TEST_KEY_CUSTOM (0xFF, "othercustomint"),
      75             :                              CREATE_TEST_KEY_CUSTOM (0x22, "string"), CREATE_TEST_KEY_CUSTOM (0x11, "long"), KS_END);
      76           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
      77           2 :         CHECK_TEST_KEY (0x1F, 31);
      78           2 :         CHECK_TEST_KEY (0xFF, 255);
      79           2 :         CHECK_TEST_KEY (0x22, 0x22);
      80           2 :         CHECK_TEST_KEY (0x11, 17);
      81             : 
      82           2 :         ksDel (ks);
      83           2 :         keyDel (parentKey);
      84             : 
      85           2 :         PLUGIN_CLOSE ();
      86           2 : }
      87             : 
      88           2 : static void test_unitbase (void)
      89             : {
      90           2 :         Key * parentKey = keyNew ("user/tests/hexnumber", KEY_END);
      91           2 :         KeySet * conf = ksNew (0, KS_END);
      92           2 :         PLUGIN_OPEN ("hexnumber");
      93           2 :         KeySet * ks = ksNew (30, CREATE_TEST_KEY_UNITBASE (0x1F), CREATE_TEST_KEY_UNITBASE (0xFF), KS_END);
      94           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
      95           2 :         CHECK_TEST_KEY (0x1F, 31);
      96           2 :         CHECK_TEST_KEY (0xFF, 255);
      97             : 
      98           2 :         ksDel (ks);
      99           2 :         keyDel (parentKey);
     100             : 
     101           2 :         PLUGIN_CLOSE ();
     102           2 : }
     103             : 
     104           2 : static void test_force (void)
     105             : {
     106           2 :         Key * parentKey = keyNew ("user/tests/hexnumber", KEY_END);
     107           2 :         KeySet * conf = ksNew (10, keyNew ("system/integertypes", KEY_VALUE, "customint;othercustomint", KEY_END),
     108             :                                keyNew ("system/force", KEY_VALUE, "1", KEY_END), KS_END);
     109           2 :         PLUGIN_OPEN ("hexnumber");
     110           2 :         KeySet * ks = ksNew (30, CREATE_TEST_KEY_CUSTOM (0x1F, "customint"), CREATE_TEST_KEY_CUSTOM (0xFF, "othercustomint"),
     111             :                              CREATE_TEST_KEY_CUSTOM (0x22, "string"), KS_END);
     112           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
     113           2 :         CHECK_TEST_KEY (0x1F, 31);
     114           2 :         CHECK_TEST_KEY (0xFF, 255);
     115           2 :         CHECK_TEST_KEY (0x22, 34);
     116             : 
     117           2 :         ksDel (ks);
     118           2 :         keyDel (parentKey);
     119             : 
     120           2 :         PLUGIN_CLOSE ();
     121           2 : }
     122             : 
     123           2 : int main (int argc, char ** argv)
     124             : {
     125           2 :         printf ("HEXNUMBER    TESTS\n");
     126           2 :         printf ("==================\n\n");
     127             : 
     128           2 :         init (argc, argv);
     129             : 
     130           2 :         test_basics ();
     131           2 :         test_default ();
     132           2 :         test_customint ();
     133           2 :         test_unitbase ();
     134           2 :         test_force ();
     135             : 
     136           2 :         print_result ("testmod_hexnumber");
     137             : 
     138           2 :         return nbError;
     139             : }

Generated by: LCOV version 1.13