ckd_add_or_die.c (2734B)
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_add_or_die() calculates results 28 * correctly for all inputs involving at least one zero value. 29 */ 30void test_mem__ckd_add_or_die_zero() { 31 32 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(0), 0); 33 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(0, 0), 0); 34 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(0, 0, 0), 0); 35 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(0, 0, 0, 0), 0); 36 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(0, 0, 0, 0, 0), 0); 37 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(0, 1), 1); 38 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(1, 0), 1); 39 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(3, 2, 0), 3 + 2); 40 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(5, 0, 8, 9), 5 + 8 + 9); 41 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(99, 99, 99, 0, 99), 99 + 99 + 99 + 99); 42 43} 44 45/** 46 * Test which verifies that guac_mem_ckd_add_or_die() successfully calculates 47 * expected values for relatively small integer inputs. 48 */ 49void test_mem__ckd_add_or_die_small() { 50 51 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(123), 123); 52 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(123, 456), 123 + 456); 53 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(123, 456, 789), 123 + 456 + 789); 54 55} 56 57/** 58 * Test which verifies that guac_mem_ckd_add_or_die() behaves as expected for 59 * relatively large integer inputs, including inputs that cause overflow beyond 60 * the capacity of a size_t. 61 */ 62void test_mem__ckd_add_or_die_large() { 63 64 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(SIZE_MAX), SIZE_MAX); 65 CU_ASSERT_EQUAL(guac_mem_ckd_add_or_die(SIZE_MAX / 2, SIZE_MAX / 2), (SIZE_MAX / 2) * 2); 66 67 ASSERT_SIGNALLED(SIGABRT, guac_mem_ckd_add_or_die(SIZE_MAX, 1)); 68 ASSERT_SIGNALLED(SIGABRT, guac_mem_ckd_add_or_die(123, 456, SIZE_MAX)); 69 ASSERT_SIGNALLED(SIGABRT, guac_mem_ckd_add_or_die(SIZE_MAX / 2, SIZE_MAX / 2, 2)); 70 71} 72