cscg24-guacamole

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

ckd_sub_or_die.c (2736B)


      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 "assert-signal.h"
     21
     22#include <CUnit/CUnit.h>
     23#include <guacamole/mem.h>
     24#include <stdint.h>
     25
     26/**
     27 * Test which verifies that guac_mem_ckd_sub_or_die() calculates results
     28 * correctly for all inputs involving at least one zero value.
     29 */
     30void test_mem__ckd_sub_or_die_zero() {
     31
     32    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(0), 0);
     33    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(0, 0), 0);
     34    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(0, 0, 0), 0);
     35    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(0, 0, 0, 0), 0);
     36    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(0, 0, 0, 0, 0), 0);
     37    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(1, 0), 1);
     38    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(3, 2, 0), 3 - 2);
     39    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(8, 5, 0, 1), 8 - 5 - 1);
     40    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(99, 99, 0), 0);
     41
     42}
     43
     44/**
     45 * Test which verifies that guac_mem_ckd_sub_or_die() successfully calculates
     46 * expected values for relatively small integer inputs, including inputs that
     47 * cause overflow beyond zero.
     48 */
     49void test_mem__ckd_sub_or_die_small() {
     50
     51    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(123), 123);
     52    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(456, 123), 456 - 123);
     53    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(789, 456, 123), 789 - 456 - 123);
     54    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(123, 123), 0);
     55
     56    ASSERT_SIGNALLED(SIGABRT, guac_mem_ckd_sub_or_die(123, 123, 1));
     57
     58}
     59
     60/**
     61 * Test which verifies that guac_mem_ckd_sub_or_die() behaves as expected for
     62 * relatively large integer inputs, including inputs that cause overflow beyond
     63 * zero.
     64 */
     65void test_mem__ckd_sub_or_die_large() {
     66
     67    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(SIZE_MAX), SIZE_MAX);
     68    CU_ASSERT_EQUAL(guac_mem_ckd_sub_or_die(SIZE_MAX, SIZE_MAX / 2), SIZE_MAX - (SIZE_MAX / 2));
     69
     70    ASSERT_SIGNALLED(SIGABRT, guac_mem_ckd_sub_or_die(SIZE_MAX, SIZE_MAX, 1));
     71    ASSERT_SIGNALLED(SIGABRT, guac_mem_ckd_sub_or_die(0, SIZE_MAX));
     72
     73}
     74