cscg24-guacamole

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

alloc.c (2275B)


      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_alloc() returns NULL for all inputs involving
     26 * at least one zero value.
     27 */
     28void test_mem__alloc_fail_zero() {
     29
     30    CU_ASSERT_PTR_NULL(guac_mem_alloc(0));
     31    CU_ASSERT_PTR_NULL(guac_mem_alloc(0, 0));
     32    CU_ASSERT_PTR_NULL(guac_mem_alloc(0, 0, 0));
     33    CU_ASSERT_PTR_NULL(guac_mem_alloc(0, 0, 0, 0));
     34    CU_ASSERT_PTR_NULL(guac_mem_alloc(0, 0, 0, 0, 0));
     35
     36    CU_ASSERT_PTR_NULL(guac_mem_alloc(1, 0));
     37    CU_ASSERT_PTR_NULL(guac_mem_alloc(3, 2, 0));
     38    CU_ASSERT_PTR_NULL(guac_mem_alloc(5, 0, 8, 9));
     39    CU_ASSERT_PTR_NULL(guac_mem_alloc(99, 99, 99, 0, 99));
     40
     41}
     42
     43/**
     44 * Test which verifies that guac_mem_alloc() successfully allocates blocks of
     45 * memory for inputs that can reasonably be expected to succeed.
     46 */
     47void test_mem__alloc_success() {
     48
     49    void* ptr;
     50
     51    ptr = guac_mem_alloc(123);
     52    CU_ASSERT_PTR_NOT_NULL(ptr);
     53    guac_mem_free(ptr);
     54
     55    ptr = guac_mem_alloc(123, 456);
     56    CU_ASSERT_PTR_NOT_NULL(ptr);
     57    guac_mem_free(ptr);
     58
     59    ptr = guac_mem_alloc(123, 456, 789);
     60    CU_ASSERT_PTR_NOT_NULL(ptr);
     61    guac_mem_free(ptr);
     62
     63}
     64
     65/**
     66 * Test which verifies that guac_mem_alloc() fails to allocate blocks of memory
     67 * that exceed the capacity of a size_t.
     68 */
     69void test_mem__alloc_fail_large() {
     70    CU_ASSERT_PTR_NULL(guac_mem_alloc(123, 456, SIZE_MAX));
     71    CU_ASSERT_PTR_NULL(guac_mem_alloc(SIZE_MAX / 2, SIZE_MAX / 2));
     72}
     73