LCOV - code coverage report
Current view: top level - libs/testmocking/src - testmocking_sysconf.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.5 % 21 19
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 3 3
Branches: 80.0 % 5 4

             Branch data     Line data    Source code
       1                 :             : #include "testmocking.h"
       2                 :             : 
       3                 :             : #include <unistd.h>
       4                 :             : 
       5                 :             : static size_t testmocking_sysconf_remaining = 0;
       6                 :             : static long testmocking_sysconf_avphys_pages = 0;
       7                 :             : static long testmocking_sysconf_page_size = 0;
       8                 :             : 
       9                 :             : /**
      10                 :             :  * @brief Arm sysconf mock to return controlled memory values for the next n calls
      11                 :             :  *
      12                 :             :  * @param[in] n Number of consecutive sysconf calls that must use controlled values
      13                 :             :  * @param[in] avphys_pages Value returned for the available-pages selector
      14                 :             :  * @param[in] page_size Value returned for the page-size selector
      15                 :             :  */
      16                 :           6 : void testmocking_sysconf_return_next(
      17                 :             :         size_t n,
      18                 :             :         long   avphys_pages,
      19                 :             :         long   page_size)
      20                 :             : {
      21                 :           6 :         testmocking_sysconf_remaining = n;
      22                 :           6 :         testmocking_sysconf_avphys_pages = avphys_pages;
      23                 :           6 :         testmocking_sysconf_page_size = page_size;
      24                 :           6 : }
      25                 :             : 
      26                 :             : /**
      27                 :             :  * @brief Disable sysconf mock unconditionally
      28                 :             :  */
      29                 :           7 : void testmocking_sysconf_disable(void)
      30                 :             : {
      31                 :           7 :         testmocking_sysconf_remaining = 0;
      32                 :           7 :         testmocking_sysconf_avphys_pages = 0;
      33                 :           7 :         testmocking_sysconf_page_size = 0;
      34                 :           7 : }
      35                 :             : 
      36                 :             : /**
      37                 :             :  * @brief Test-only entry point for sysconf-compatible controlled values
      38                 :             :  *
      39                 :             :  * When the mock is armed each call decrements the remaining counter and
      40                 :             :  * returns a configured value for memory-size selectors. When the counter
      41                 :             :  * reaches zero the call is forwarded to libc
      42                 :             :  *
      43                 :             :  * @param[in] name sysconf selector passed by the caller
      44                 :             :  * @return Controlled value while the mock is armed, otherwise libc sysconf result
      45                 :             :  */
      46                 :         351 : long testmocking_sysconf(int name)
      47                 :             : {
      48         [ +  + ]:         351 :         if(testmocking_sysconf_remaining > 0)
      49                 :             :         {
      50                 :          11 :                 testmocking_sysconf_remaining--;
      51                 :             : 
      52      [ +  +  - ]:          11 :                 switch(name)
      53                 :             :                 {
      54                 :             : #ifdef _SC_AVPHYS_PAGES
      55                 :           6 :                         case _SC_AVPHYS_PAGES:
      56                 :             : #elif defined(_SC_PHYS_PAGES)
      57                 :             :                         case _SC_PHYS_PAGES:
      58                 :             : #endif
      59                 :           6 :                                 return(testmocking_sysconf_avphys_pages);
      60                 :             : #ifdef _SC_PAGESIZE
      61                 :           5 :                         case _SC_PAGESIZE:
      62                 :             : #elif defined(_SC_PAGE_SIZE)
      63                 :             :                         case _SC_PAGE_SIZE:
      64                 :             : #endif
      65                 :           5 :                                 return(testmocking_sysconf_page_size);
      66                 :           0 :                         default:
      67                 :           0 :                                 return(-1);
      68                 :             :                 }
      69                 :             :         }
      70                 :             : 
      71                 :         340 :         return(sysconf(name));
      72                 :             : }
        

Generated by: LCOV version 2.0-1