LCOV - code coverage report
Current view: top level - tests/src - test0025.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 96.6 % 59 57
Test Date: 2026-01-12 05:34:38 Functions: 100.0 % 8 8

            Line data    Source code
       1              : #include "sute.h"
       2              : #include <unistd.h>
       3              : 
       4              : // Test-only hook for sysconf() to control outputs in file_buffer_memory().
       5              : // Macro in file_buffer_memory.c (under TESTITALL) aliases sysconf() to
       6              : // test_sysconf() so only that code path is affected. No delegation to
       7              : // the real libc sysconf is needed here.
       8              : static long mock_avphys_pages = 0;   // _SC_AVPHYS_PAGES
       9              : static long mock_page_size = 0;      // _SC_PAGESIZE
      10              : 
      11          164 : long testitall_sysconf(int name)
      12              : {
      13          164 :         switch(name)
      14              :         {
      15              : #ifdef _SC_AVPHYS_PAGES
      16           82 :                 case _SC_AVPHYS_PAGES:
      17              : #elif defined(_SC_PHYS_PAGES)
      18              :                 // Fallback for platforms without _SC_AVPHYS_PAGES — use total pages
      19              :                 case _SC_PHYS_PAGES:
      20              : #endif
      21           82 :                         return mock_avphys_pages;
      22           82 :                 case _SC_PAGESIZE:
      23           82 :                         return mock_page_size;
      24            0 :                 default:
      25              :                         // Unknown names are not expected in these tests
      26            0 :                         return -1;
      27              :         }
      28              : }
      29              : 
      30            1 : static Return pages_failure_returns_default(void)
      31              : {
      32            1 :         INITTEST;
      33            1 :         mock_avphys_pages = -1; // Simulate sysconf failure for pages
      34            1 :         mock_page_size = 4096; // Any value (won't be used)
      35              : 
      36            1 :         size_t result = file_buffer_memory();
      37            1 :         ASSERT(result == (size_t)(1024*1024));
      38            1 :         RETURN_STATUS;
      39              : }
      40              : 
      41            1 : static Return pagesize_failure_returns_default(void)
      42              : {
      43            1 :         INITTEST;
      44            1 :         mock_avphys_pages = 1000;
      45            1 :         mock_page_size = -1;  // Simulate sysconf failure for page size
      46              : 
      47            1 :         size_t result = file_buffer_memory();
      48            1 :         ASSERT(result == (size_t)(1024*1024));
      49            1 :         RETURN_STATUS;
      50              : }
      51              : 
      52            1 : static Return zero_pages_results_in_zero(void)
      53              : {
      54            1 :         INITTEST;
      55            1 :         mock_avphys_pages = 0;
      56            1 :         mock_page_size = 4096;
      57              : 
      58            1 :         size_t result = file_buffer_memory();
      59            1 :         ASSERT(result == (size_t)0);
      60            1 :         RETURN_STATUS;
      61              : }
      62              : 
      63            1 : static Return zero_pagesize_results_in_zero(void)
      64              : {
      65            1 :         INITTEST;
      66            1 :         mock_avphys_pages = 12345;
      67            1 :         mock_page_size = 0;
      68              : 
      69            1 :         size_t result = file_buffer_memory();
      70            1 :         ASSERT(result == (size_t)0);
      71            1 :         RETURN_STATUS;
      72              : }
      73              : 
      74            1 : static Return tiny_values_integer_division(void)
      75              : {
      76            1 :         INITTEST;
      77              :         // 1% of 12,345 is 123 (integer division)
      78            1 :         mock_avphys_pages = 12345;
      79            1 :         mock_page_size = 1;
      80              : 
      81            1 :         size_t result = file_buffer_memory();
      82            1 :         ASSERT(result == (size_t)123);
      83            1 :         RETURN_STATUS;
      84              : }
      85              : 
      86            1 : static Return normal_values_calculation(void)
      87              : {
      88            1 :         INITTEST;
      89              :         // 1% of (1,000,000 * 4096) = 40,960,000
      90            1 :         mock_avphys_pages = 1000000;
      91            1 :         mock_page_size = 4096;
      92              : 
      93            1 :         size_t result = file_buffer_memory();
      94            1 :         ASSERT(result == (size_t)40960000);
      95            1 :         RETURN_STATUS;
      96              : }
      97              : 
      98              : /**
      99              :  * @brief Unit tests for file_buffer_memory().
     100              :  *
     101              :  * @details
     102              :  * - In TESTITALL builds, sysconf() inside file_buffer_memory() is macro-aliased
     103              :  *   to test_sysconf(), which supplies controlled values for _SC_AVPHYS_PAGES and
     104              :  *   _SC_PAGESIZE. Any other name returns -1 and is not used in these tests.
     105              :  * - Each subtest sets mock_avphys_pages and mock_page_size to drive scenarios:
     106              :  *   sysconf failures (expect default 1 MiB), zero inputs (expect 0), tiny product
     107              :  *   to observe integer-division truncation, and a normal 1% calculation for large
     108              :  *   inputs.
     109              :  * - ASSERT checks the returned size against the expected value; TEST aggregates
     110              :  *   subtests and reports status via the testitall harness.
     111              :  */
     112            1 : Return test0025(void)
     113              : {
     114            1 :         INITTEST;
     115              : 
     116            1 :         TEST(pages_failure_returns_default,"file_buffer_memory: returns default on pages failure…");
     117            1 :         TEST(pagesize_failure_returns_default,"file_buffer_memory: returns default on page size failure…");
     118            1 :         TEST(zero_pages_results_in_zero,"file_buffer_memory: 0 pages yields 0…");
     119            1 :         TEST(zero_pagesize_results_in_zero,"file_buffer_memory: 0 page size yields 0…");
     120            1 :         TEST(tiny_values_integer_division,"file_buffer_memory: integer division rounding down…");
     121            1 :         TEST(normal_values_calculation,"file_buffer_memory: normal case computation…");
     122              : 
     123            1 :         RETURN_STATUS;
     124              : }
        

Generated by: LCOV version 2.0-1