LCOV - code coverage report
Current view: top level - src/plugins/rgbcolor - testmod_rgbcolor.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 70 70 100.0 %
Date: 2022-05-21 16:19:22 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for rgbcolor plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <kdbconfig.h>
      11             : #include <kdbtypes.h>
      12             : #include <stdio.h>
      13             : #include <stdlib.h>
      14             : #include <string.h>
      15             : 
      16             : #include <tests_plugin.h>
      17             : 
      18          33 : static void test_normalize_color (const char * color, kdb_unsigned_long_t colorValue)
      19             : {
      20          33 :         Key * parentKey = keyNew ("user:/tests/rgbcolor", KEY_END);
      21          33 :         Key * hexkey = keyNew ("user:/test/rgbcolor/testcolor", KEY_VALUE, color, KEY_META, "check/rgbcolor", "any", KEY_END);
      22          33 :         KeySet * conf = ksNew (0, KS_END);
      23          33 :         KeySet * ks = ksNew (20, KS_END);
      24             :         // KeySet * ksGet = ks;
      25             : 
      26          33 :         ksAppendKey (ks, hexkey);
      27             : 
      28          33 :         PLUGIN_OPEN ("rgbcolor");
      29             : 
      30          33 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 1, "kdbSet did not succeed");
      31          33 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "kdbGet did not succeed");
      32             : 
      33          33 :         Key * foundKey = ksLookupByName (ks, "user:/test/rgbcolor/testcolor", 0);
      34             : 
      35          33 :         char colorStr[11];
      36          33 :         snprintf (colorStr, 11, "%u", colorValue);
      37             : 
      38          33 :         printf ("Test Color Normalization %s, returned value: %s, expected value: %s\n", color, keyString (foundKey), colorStr);
      39          33 :         succeed_if (!strcmp (keyString (foundKey), colorStr), "Values dont match");
      40             : 
      41          33 :         const Key * origValueKey = keyGetMeta (foundKey, "origvalue");
      42          33 :         succeed_if (origValueKey != NULL, "origvalue is not set");
      43          33 :         succeed_if (!strcmp (keyString (origValueKey), color), "origvalue does not match actual original value");
      44             : 
      45          33 :         ksDel (ks);
      46          33 :         keyDel (parentKey);
      47             : 
      48          33 :         PLUGIN_CLOSE ();
      49          33 : }
      50             : 
      51             : 
      52          51 : static void test_color (const char * color, const int expected_ret)
      53             : {
      54          51 :         Key * parentKey = keyNew ("user:/tests/rgbcolor", KEY_END);
      55          51 :         Key * hexkey = keyNew ("user:/test/rgbcolor/testcolor", KEY_VALUE, color, KEY_META, "check/rgbcolor", "any", KEY_END);
      56          51 :         KeySet * conf = ksNew (0, KS_END);
      57          51 :         KeySet * ks = ksNew (20, KS_END);
      58             : 
      59          51 :         ksAppendKey (ks, hexkey);
      60             : 
      61          51 :         PLUGIN_OPEN ("rgbcolor");
      62             : 
      63          51 :         int ret = plugin->kdbSet (plugin, ks, parentKey);
      64             : 
      65          51 :         printf ("Test Color Validity %s, returned value: %d, expected value: %d\n", color, ret, expected_ret);
      66          51 :         succeed_if (ret == expected_ret, "failed");
      67             : 
      68          51 :         ksDel (ks);
      69          51 :         keyDel (parentKey);
      70             : 
      71          51 :         PLUGIN_CLOSE ();
      72          51 : }
      73             : 
      74           3 : static void test_rgbcolor (void)
      75             : {
      76           3 :         test_color ("#0fb", 1);
      77           3 :         test_color ("#fff", 1);
      78           3 :         test_color ("#111", 1);
      79             : 
      80           3 :         test_color ("#fff111", 1);
      81           3 :         test_color ("#a1C2bF", 1);
      82             : 
      83           3 :         test_color ("#abcd", 1);
      84             : 
      85           3 :         test_color ("#aabbccdd", 1);
      86           3 :         test_color ("#ffffffff", 1);
      87           3 :         test_color ("#00000000", 1);
      88             : 
      89           3 :         test_color ("aliceblue", 1);
      90           3 :         test_color ("orange", 1);
      91           3 :         test_color ("red", 1);
      92             : 
      93           3 :         test_color ("nonexistentcolor", -1);
      94           3 :         test_color ("1110", -1);
      95           3 :         test_color ("a1C2bF", -1);
      96           3 :         test_color ("#a1C2bZ", -1);
      97           3 :         test_color ("#00000", -1);
      98             : 
      99           3 :         test_normalize_color ("lightcyan", 0xe0ffffff);
     100           3 :         test_normalize_color ("white", 0xffffffff);
     101           3 :         test_normalize_color ("black", 0xff);
     102           3 :         test_normalize_color ("#fff", 0xffffffff);
     103           3 :         test_normalize_color ("#ffff", 0xffffffff);
     104           3 :         test_normalize_color ("#ffffff", 0xffffffff);
     105           3 :         test_normalize_color ("#ffffffff", 0xffffffff);
     106             : 
     107           3 :         test_normalize_color ("#aabbccdd", 0xaabbccdd);
     108           3 :         test_normalize_color ("#abcd", 0xaabbccdd);
     109             : 
     110           3 :         test_normalize_color ("#abc", 0xaabbccff);
     111           3 :         test_normalize_color ("#aabbcc", 0xaabbccff);
     112           3 : }
     113             : 
     114           3 : int main (int argc, char ** argv)
     115             : {
     116           3 :         init (argc, argv);
     117             : 
     118           3 :         test_rgbcolor ();
     119             : 
     120           9 :         print_result ("testmod_rgbcolor");
     121             : 
     122           3 :         return nbError;
     123             : }

Generated by: LCOV version 1.13