cscg24-guacamole

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

ckd_sub.c (3310B)


      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_ckd_sub() calculates results correctly for
     26 * all inputs involving at least one zero value.
     27 */
     28void test_mem__ckd_sub_zero() {
     29
     30    size_t result = SIZE_MAX;
     31
     32    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 0));
     33    CU_ASSERT_EQUAL(result, 0);
     34
     35    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 0, 0));
     36    CU_ASSERT_EQUAL(result, 0);
     37
     38    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 0, 0, 0));
     39    CU_ASSERT_EQUAL(result, 0);
     40
     41    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 0, 0, 0, 0));
     42    CU_ASSERT_EQUAL(result, 0);
     43
     44    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 0, 0, 0, 0, 0));
     45    CU_ASSERT_EQUAL(result, 0);
     46
     47    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 1, 0));
     48    CU_ASSERT_EQUAL(result, 1);
     49
     50    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 3, 2, 0));
     51    CU_ASSERT_EQUAL(result, 3 - 2);
     52
     53    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 8, 5, 0, 1));
     54    CU_ASSERT_EQUAL(result, 8 - 5 - 1);
     55
     56    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 99, 99, 0));
     57    CU_ASSERT_EQUAL(result, 0);
     58
     59}
     60
     61/**
     62 * Test which verifies that guac_mem_ckd_sub() successfully calculates expected
     63 * values for relatively small integer inputs, including inputs that cause
     64 * overflow beyond zero.
     65 */
     66void test_mem__ckd_sub_small() {
     67
     68    size_t result = SIZE_MAX;
     69
     70    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 123));
     71    CU_ASSERT_EQUAL(result, 123);
     72
     73    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 456, 123));
     74    CU_ASSERT_EQUAL(result, 456 - 123);
     75
     76    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 789, 456, 123));
     77    CU_ASSERT_EQUAL(result, 789 - 456 - 123);
     78
     79    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, 123, 123));
     80    CU_ASSERT_EQUAL(result, 0);
     81
     82    CU_ASSERT_TRUE_FATAL(guac_mem_ckd_sub(&result, 123, 123, 1));
     83
     84}
     85
     86/**
     87 * Test which verifies that guac_mem_ckd_sub() behaves as expected for
     88 * relatively large integer inputs, including inputs that cause overflow beyond
     89 * zero.
     90 */
     91void test_mem__ckd_sub_large() {
     92
     93    size_t result = 0;
     94
     95    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, SIZE_MAX));
     96    CU_ASSERT_EQUAL(result, SIZE_MAX);
     97
     98    CU_ASSERT_FALSE_FATAL(guac_mem_ckd_sub(&result, SIZE_MAX, SIZE_MAX / 2));
     99    CU_ASSERT_EQUAL(result, SIZE_MAX - (SIZE_MAX / 2));
    100
    101    CU_ASSERT_TRUE_FATAL(guac_mem_ckd_sub(&result, SIZE_MAX, SIZE_MAX, 1));
    102    CU_ASSERT_TRUE_FATAL(guac_mem_ckd_sub(&result, 0, SIZE_MAX));
    103
    104}
    105