LCOV - code coverage report
Current view: top level - tests/ctest - test_size.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 65 65 100.0 %
Date: 2019-09-12 12:28:41 Functions: 2 2 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             : #define NAME_SIZE 250
      12             : 
      13           2 : static void test_ksResize (void)
      14             : {
      15             :         int i;
      16           2 :         KeySet * ks = 0;
      17           2 :         KeySet * copy = ksNew (0, KS_END);
      18             :         char name[NAME_SIZE];
      19             : 
      20           2 :         ks = ksNew (20, keyNew ("user/test01", KEY_END), keyNew ("user/test02", KEY_END), keyNew ("user/test03", KEY_END),
      21             :                     keyNew ("user/test04", KEY_END), keyNew ("user/test05", KEY_END), keyNew ("user/test11", KEY_END),
      22             :                     keyNew ("user/test12", KEY_END), keyNew ("user/test13", KEY_END), keyNew ("user/test14", KEY_END),
      23             :                     keyNew ("user/test15", KEY_END), keyNew ("user/test21", KEY_END), keyNew ("user/test22", KEY_END),
      24             :                     keyNew ("user/test23", KEY_END), keyNew ("user/test24", KEY_END), keyNew ("user/test25", KEY_END),
      25             :                     keyNew ("user/test31", KEY_END), keyNew ("user/test32", KEY_END), keyNew ("user/test33", KEY_END),
      26             :                     keyNew ("user/test34", KEY_END), keyNew ("user/test35", KEY_END), KS_END);
      27           2 :         succeed_if (ksGetAlloc (ks) == 20, "20 keys with alloc 20 should work");
      28           2 :         ksDel (ks);
      29             : 
      30           2 :         printf ("Test resize of keyset\n");
      31           2 :         exit_if_fail ((ks = ksNew (0, KS_END)) != 0, "could not create new keyset");
      32         200 :         for (i = 0; i < 100; i++)
      33             :         {
      34         200 :                 snprintf (name, NAME_SIZE, "user/test%d", i);
      35         200 :                 ksAppendKey (ks, keyNew (name, KEY_END));
      36         200 :                 if (i >= 63)
      37             :                 {
      38          74 :                         succeed_if (ksGetAlloc (ks) == 127, "allocation size wrong");
      39             :                 }
      40         126 :                 else if (i >= 31)
      41             :                 {
      42          64 :                         succeed_if (ksGetAlloc (ks) == 63, "allocation size wrong");
      43             :                 }
      44          62 :                 else if (i >= 15)
      45             :                 {
      46          32 :                         succeed_if (ksGetAlloc (ks) == 31, "allocation size wrong");
      47             :                 }
      48          30 :                 else if (i >= 0)
      49             :                 {
      50          30 :                         succeed_if (ksGetAlloc (ks) == 15, "allocation size wrong");
      51             :                 }
      52             :         }
      53           2 :         succeed_if (ksGetSize (ks) == 100, "could not append 100 keys");
      54           2 :         succeed_if (ksGetAlloc (ks) == 127, "allocation size wrong");
      55         202 :         for (i = 100; i >= 0; i--)
      56             :         {
      57         202 :                 keyDel (ksPop (ks));
      58         202 :                 if (i >= 64)
      59             :                 {
      60          74 :                         succeed_if (ksGetAlloc (ks) == 127, "allocation size wrong");
      61             :                 }
      62         128 :                 else if (i >= 32)
      63             :                 {
      64          64 :                         succeed_if (ksGetAlloc (ks) == 63, "allocation size wrong");
      65             :                 }
      66          64 :                 else if (i >= 16)
      67             :                 {
      68          32 :                         succeed_if (ksGetAlloc (ks) == 31, "allocation size wrong");
      69             :                 }
      70             :                 else if (i >= 0)
      71             :                 {
      72          32 :                         succeed_if (ksGetAlloc (ks) == 15, "allocation size wrong");
      73             :                 }
      74             :         }
      75           2 :         succeed_if (ksGetSize (ks) == 0, "could not pop 100 keys");
      76           2 :         succeed_if (ksGetAlloc (ks) == 15, "allocation size wrong");
      77           2 :         ksDel (ks);
      78             : 
      79           2 :         exit_if_fail ((ks = ksNew (0, KS_END)) != 0, "could not create new keyset");
      80           2 :         ksResize (ks, 100);
      81           2 :         succeed_if (ksGetAlloc (ks) == 100, "allocation size wrong");
      82         200 :         for (i = 0; i < 100; i++)
      83             :         {
      84         200 :                 snprintf (name, NAME_SIZE, "user/test%d", i);
      85         200 :                 ksAppendKey (ks, keyNew (name, KEY_END));
      86         200 :                 succeed_if (ksGetAlloc (ks) == 100, "allocation size wrong");
      87             :         }
      88           2 :         succeed_if (ksGetSize (ks) == 100, "could not append 100 keys");
      89           2 :         succeed_if (ksGetAlloc (ks) == 100, "allocation size wrong");
      90           2 :         ksDel (ks);
      91             : 
      92           2 :         ks =
      93             : #include "data_keyset.c"
      94             : 
      95           2 :                 succeed_if (ksGetSize (ks) == 102, "Problem loading keyset with 102 keys");
      96           2 :         succeed_if (ksGetAlloc (ks) == 102, "alloc size wrong");
      97             : 
      98           2 :         ksCopy (copy, ks);
      99           2 :         succeed_if (ksGetSize (copy) == 102, "Problem copy keyset with 102 keys");
     100           2 :         succeed_if (ksGetAlloc (copy) == 127, "alloc of copy size wrong");
     101             : 
     102           2 :         compare_keyset (copy, ks);
     103             : 
     104           2 :         ksClear (copy); // useless, just test for double free
     105           2 :         ksCopy (copy, ks);
     106             : 
     107           2 :         succeed_if (ksGetSize (copy) == 102, "Problem copy keyset with 102 keys");
     108           2 :         succeed_if (ksGetAlloc (copy) == 127, "alloc of copy size wrong");
     109           2 :         compare_keyset (copy, ks);
     110             : 
     111           2 :         ksDel (copy);
     112           2 :         ksDel (ks);
     113           2 : }
     114             : 
     115           2 : int main (int argc, char ** argv)
     116             : {
     117           2 :         printf ("KEYSET SIZE  TESTS\n");
     118           2 :         printf ("==================\n\n");
     119             : 
     120           2 :         init (argc, argv);
     121             : 
     122           2 :         test_ksResize ();
     123             : 
     124           2 :         printf ("\ntest_size RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
     125             : 
     126           2 :         return nbError;
     127             : }

Generated by: LCOV version 1.13