cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

realloc.c (3512B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include <CUnit/CUnit.h>
     21#include <guacamole/mem.h>
     22#include <stdint.h>
     23
     24/**
     25 * Test which verifies that guac_mem_realloc() returns NULL for all inputs
     26 * involving at least one zero value (reallocation to zero bytes is not an
     27 * error but equivalent freeing the memory).
     28 */
     29void test_mem__realloc_success_zero() {
     30
     31    void* ptr;
     32
     33    ptr = guac_mem_alloc(1);
     34    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     35    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, 0));
     36
     37    ptr = guac_mem_alloc(1);
     38    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     39    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, 0, 0));
     40
     41    ptr = guac_mem_alloc(1);
     42    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     43    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, 0, 0, 0));
     44
     45    ptr = guac_mem_alloc(1);
     46    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     47    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, 0, 0, 0, 0));
     48
     49    ptr = guac_mem_alloc(1);
     50    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     51    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, 0, 0, 0, 0, 0));
     52
     53    ptr = guac_mem_alloc(1);
     54    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     55    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, 1, 0));
     56
     57    ptr = guac_mem_alloc(1);
     58    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     59    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, 3, 2, 0));
     60
     61    ptr = guac_mem_alloc(1);
     62    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     63    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, 5, 0, 8, 9));
     64
     65    ptr = guac_mem_alloc(1);
     66    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     67    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, 99, 99, 99, 0, 99));
     68
     69}
     70
     71/**
     72 * Test which verifies that guac_mem_realloc() successfully allocates blocks of
     73 * memory for inputs that can reasonably be expected to succeed.
     74 */
     75void test_mem__realloc_success() {
     76
     77    void* ptr;
     78
     79    ptr = guac_mem_alloc(1);
     80    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     81    ptr = guac_mem_realloc(ptr, 123);
     82    CU_ASSERT_PTR_NOT_NULL(ptr);
     83    guac_mem_free(ptr);
     84
     85    ptr = guac_mem_alloc(1);
     86    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     87    ptr = guac_mem_realloc(ptr, 123, 456);
     88    CU_ASSERT_PTR_NOT_NULL(ptr);
     89    guac_mem_free(ptr);
     90
     91    ptr = guac_mem_alloc(1);
     92    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     93    ptr = guac_mem_realloc(ptr, 123, 456, 789);
     94    CU_ASSERT_PTR_NOT_NULL(ptr);
     95    guac_mem_free(ptr);
     96
     97}
     98
     99/**
    100 * Test which verifies that guac_mem_realloc() fails to allocate blocks of
    101 * memory that exceed the capacity of a size_t.
    102 */
    103void test_mem__realloc_fail_large() {
    104
    105    void* ptr;
    106
    107    ptr = guac_mem_alloc(1);
    108    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
    109    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, 123, 456, SIZE_MAX));
    110    guac_mem_free(ptr);
    111
    112    ptr = guac_mem_alloc(1);
    113    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
    114    CU_ASSERT_PTR_NULL(guac_mem_realloc(ptr, SIZE_MAX / 2, SIZE_MAX / 2));
    115    guac_mem_free(ptr);
    116
    117}
    118