root/lib/common/tests/utils/pcmk__realloc_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. bad_size
  2. realloc_fails
  3. realloc_succeeds

   1 /*
   2  * Copyright 2024 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <crm/common/unittest_internal.h>
  13 
  14 #include "mock_private.h"
  15 
  16 static void
  17 bad_size(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  18 {
  19     char *ptr = NULL;
  20 
  21     pcmk__assert_asserts(pcmk__realloc(ptr, 0));
  22 }
  23 
  24 static void
  25 realloc_fails(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  26 {
  27     char *ptr = NULL;
  28 
  29     pcmk__assert_aborts(
  30         {
  31             pcmk__mock_realloc = true;   // realloc() will return NULL
  32             expect_any(__wrap_realloc, ptr);
  33             expect_value(__wrap_realloc, size, 1000);
  34             pcmk__realloc(ptr, 1000);
  35             pcmk__mock_realloc = false;  // Use real realloc()
  36         }
  37     );
  38 }
  39 
  40 static void
  41 realloc_succeeds(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  42 {
  43     char *ptr = NULL;
  44 
  45     /* We can't really test that the resulting pointer is the size we asked
  46      * for - it might be larger if that's what the memory allocator decides
  47      * to do.  And anyway, testing realloc isn't really the point.  All we
  48      * want to do here is make sure the function works when given good input.
  49      */
  50 
  51     /* Allocate new memory */
  52     ptr = pcmk__realloc(ptr, 1000);
  53     assert_non_null(ptr);
  54 
  55     /* Grow previously allocated memory */
  56     ptr = pcmk__realloc(ptr, 2000);
  57     assert_non_null(ptr);
  58 
  59     /* Shrink previously allocated memory */
  60     ptr = pcmk__realloc(ptr, 500);
  61     assert_non_null(ptr);
  62 
  63     free(ptr);
  64 }
  65 
  66 PCMK__UNIT_TEST(NULL, NULL,
  67                 cmocka_unit_test(bad_size),
  68                 cmocka_unit_test(realloc_fails),
  69                 cmocka_unit_test(realloc_succeeds))

/* [previous][next][first][last][top][bottom][index][help] */