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