/* * * Copyright (c) 2020 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @file * This file implements a unit test suite for CHIP SafeInt functions * */ #include #include #include using namespace chip; TEST(TestSafeInt, TestCanCastTo_Int8) { EXPECT_TRUE(CanCastTo(0)); EXPECT_TRUE(CanCastTo(127)); EXPECT_TRUE(CanCastTo(128)); EXPECT_TRUE(CanCastTo(129)); EXPECT_TRUE(CanCastTo(255)); EXPECT_FALSE(CanCastTo(256)); EXPECT_FALSE(CanCastTo(32767)); EXPECT_FALSE(CanCastTo(32768)); EXPECT_FALSE(CanCastTo(32769)); EXPECT_FALSE(CanCastTo(65535)); EXPECT_FALSE(CanCastTo(65536)); EXPECT_FALSE(CanCastTo(2147483647ll)); EXPECT_FALSE(CanCastTo(2147483648ll)); EXPECT_FALSE(CanCastTo(2147483649ll)); EXPECT_FALSE(CanCastTo(4294967295ll)); EXPECT_FALSE(CanCastTo(4294967296ll)); EXPECT_FALSE(CanCastTo(9223372036854775807ull)); EXPECT_FALSE(CanCastTo(9223372036854775808ull)); EXPECT_FALSE(CanCastTo(-1)); EXPECT_FALSE(CanCastTo(-127)); EXPECT_FALSE(CanCastTo(-128)); EXPECT_FALSE(CanCastTo(-129)); EXPECT_FALSE(CanCastTo(-255)); EXPECT_FALSE(CanCastTo(-256)); EXPECT_FALSE(CanCastTo(-32767)); EXPECT_FALSE(CanCastTo(-32768)); EXPECT_FALSE(CanCastTo(-32769)); EXPECT_FALSE(CanCastTo(-65535)); EXPECT_FALSE(CanCastTo(-65536)); EXPECT_FALSE(CanCastTo(-2147483647ll)); EXPECT_FALSE(CanCastTo(-2147483648ll)); EXPECT_FALSE(CanCastTo(-2147483649ll)); EXPECT_FALSE(CanCastTo(-4294967295ll)); EXPECT_FALSE(CanCastTo(-4294967296ll)); EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); EXPECT_TRUE(CanCastTo(0)); EXPECT_TRUE(CanCastTo(127)); EXPECT_FALSE(CanCastTo(128)); EXPECT_FALSE(CanCastTo(129)); EXPECT_FALSE(CanCastTo(255)); EXPECT_FALSE(CanCastTo(256)); EXPECT_FALSE(CanCastTo(32767)); EXPECT_FALSE(CanCastTo(32768)); EXPECT_FALSE(CanCastTo(32769)); EXPECT_FALSE(CanCastTo(65535)); EXPECT_FALSE(CanCastTo(65536)); EXPECT_FALSE(CanCastTo(2147483647ll)); EXPECT_FALSE(CanCastTo(2147483648ll)); EXPECT_FALSE(CanCastTo(2147483649ll)); EXPECT_FALSE(CanCastTo(4294967295ll)); EXPECT_FALSE(CanCastTo(4294967296ll)); EXPECT_FALSE(CanCastTo(9223372036854775807ull)); EXPECT_FALSE(CanCastTo(9223372036854775808ull)); EXPECT_TRUE(CanCastTo(-1)); EXPECT_TRUE(CanCastTo(-127)); EXPECT_TRUE(CanCastTo(-128)); EXPECT_FALSE(CanCastTo(-129)); EXPECT_FALSE(CanCastTo(-255)); EXPECT_FALSE(CanCastTo(-256)); EXPECT_FALSE(CanCastTo(-32767)); EXPECT_FALSE(CanCastTo(-32768)); EXPECT_FALSE(CanCastTo(-32769)); EXPECT_FALSE(CanCastTo(-65535)); EXPECT_FALSE(CanCastTo(-65536)); EXPECT_FALSE(CanCastTo(-2147483647ll)); EXPECT_FALSE(CanCastTo(-2147483648ll)); EXPECT_FALSE(CanCastTo(-2147483649ll)); EXPECT_FALSE(CanCastTo(-4294967295ll)); EXPECT_FALSE(CanCastTo(-4294967296ll)); EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); } TEST(TestSafeInt, TestCanCastTo_Int16) { EXPECT_TRUE(CanCastTo(0)); EXPECT_TRUE(CanCastTo(127)); EXPECT_TRUE(CanCastTo(128)); EXPECT_TRUE(CanCastTo(129)); EXPECT_TRUE(CanCastTo(255)); EXPECT_TRUE(CanCastTo(256)); EXPECT_TRUE(CanCastTo(32767)); EXPECT_TRUE(CanCastTo(32768)); EXPECT_TRUE(CanCastTo(32769)); EXPECT_TRUE(CanCastTo(65535)); EXPECT_FALSE(CanCastTo(65536)); EXPECT_FALSE(CanCastTo(2147483647ll)); EXPECT_FALSE(CanCastTo(2147483648ll)); EXPECT_FALSE(CanCastTo(2147483649ll)); EXPECT_FALSE(CanCastTo(4294967295ll)); EXPECT_FALSE(CanCastTo(4294967296ll)); EXPECT_FALSE(CanCastTo(9223372036854775807ull)); EXPECT_FALSE(CanCastTo(9223372036854775808ull)); EXPECT_FALSE(CanCastTo(-1)); EXPECT_FALSE(CanCastTo(-127)); EXPECT_FALSE(CanCastTo(-128)); EXPECT_FALSE(CanCastTo(-129)); EXPECT_FALSE(CanCastTo(-255)); EXPECT_FALSE(CanCastTo(-256)); EXPECT_FALSE(CanCastTo(-32767)); EXPECT_FALSE(CanCastTo(-32768)); EXPECT_FALSE(CanCastTo(-32769)); EXPECT_FALSE(CanCastTo(-65535)); EXPECT_FALSE(CanCastTo(-65536)); EXPECT_FALSE(CanCastTo(-2147483647ll)); EXPECT_FALSE(CanCastTo(-2147483648ll)); EXPECT_FALSE(CanCastTo(-2147483649ll)); EXPECT_FALSE(CanCastTo(-4294967295ll)); EXPECT_FALSE(CanCastTo(-4294967296ll)); EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); EXPECT_TRUE(CanCastTo(0)); EXPECT_TRUE(CanCastTo(127)); EXPECT_TRUE(CanCastTo(128)); EXPECT_TRUE(CanCastTo(129)); EXPECT_TRUE(CanCastTo(255)); EXPECT_TRUE(CanCastTo(256)); EXPECT_TRUE(CanCastTo(32767)); EXPECT_FALSE(CanCastTo(32768)); EXPECT_FALSE(CanCastTo(32769)); EXPECT_FALSE(CanCastTo(65535)); EXPECT_FALSE(CanCastTo(65536)); EXPECT_FALSE(CanCastTo(2147483647ll)); EXPECT_FALSE(CanCastTo(2147483648ll)); EXPECT_FALSE(CanCastTo(2147483649ll)); EXPECT_FALSE(CanCastTo(4294967295ll)); EXPECT_FALSE(CanCastTo(4294967296ll)); EXPECT_FALSE(CanCastTo(9223372036854775807ull)); EXPECT_FALSE(CanCastTo(9223372036854775808ull)); EXPECT_TRUE(CanCastTo(-1)); EXPECT_TRUE(CanCastTo(-127)); EXPECT_TRUE(CanCastTo(-128)); EXPECT_TRUE(CanCastTo(-129)); EXPECT_TRUE(CanCastTo(-255)); EXPECT_TRUE(CanCastTo(-256)); EXPECT_TRUE(CanCastTo(-32767)); EXPECT_TRUE(CanCastTo(-32768)); EXPECT_FALSE(CanCastTo(-32769)); EXPECT_FALSE(CanCastTo(-65535)); EXPECT_FALSE(CanCastTo(-65536)); EXPECT_FALSE(CanCastTo(-2147483647ll)); EXPECT_FALSE(CanCastTo(-2147483648ll)); EXPECT_FALSE(CanCastTo(-2147483649ll)); EXPECT_FALSE(CanCastTo(-4294967295ll)); EXPECT_FALSE(CanCastTo(-4294967296ll)); EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); } TEST(TestSafeInt, TestCanCastTo_Int32) { EXPECT_TRUE(CanCastTo(0)); EXPECT_TRUE(CanCastTo(127)); EXPECT_TRUE(CanCastTo(128)); EXPECT_TRUE(CanCastTo(129)); EXPECT_TRUE(CanCastTo(255)); EXPECT_TRUE(CanCastTo(256)); EXPECT_TRUE(CanCastTo(32767)); EXPECT_TRUE(CanCastTo(32768)); EXPECT_TRUE(CanCastTo(32769)); EXPECT_TRUE(CanCastTo(65535)); EXPECT_TRUE(CanCastTo(65536)); EXPECT_TRUE(CanCastTo(2147483647ll)); EXPECT_TRUE(CanCastTo(2147483648ll)); EXPECT_TRUE(CanCastTo(2147483649ll)); EXPECT_TRUE(CanCastTo(4294967295ll)); EXPECT_FALSE(CanCastTo(4294967296ll)); EXPECT_FALSE(CanCastTo(9223372036854775807ull)); EXPECT_FALSE(CanCastTo(9223372036854775808ull)); EXPECT_FALSE(CanCastTo(-1)); EXPECT_FALSE(CanCastTo(-127)); EXPECT_FALSE(CanCastTo(-128)); EXPECT_FALSE(CanCastTo(-129)); EXPECT_FALSE(CanCastTo(-255)); EXPECT_FALSE(CanCastTo(-256)); EXPECT_FALSE(CanCastTo(-32767)); EXPECT_FALSE(CanCastTo(-32768)); EXPECT_FALSE(CanCastTo(-32769)); EXPECT_FALSE(CanCastTo(-65535)); EXPECT_FALSE(CanCastTo(-65536)); EXPECT_FALSE(CanCastTo(-2147483647ll)); EXPECT_FALSE(CanCastTo(-2147483648ll)); EXPECT_FALSE(CanCastTo(-2147483649ll)); EXPECT_FALSE(CanCastTo(-4294967295ll)); EXPECT_FALSE(CanCastTo(-4294967296ll)); EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); EXPECT_TRUE(CanCastTo(0)); EXPECT_TRUE(CanCastTo(127)); EXPECT_TRUE(CanCastTo(128)); EXPECT_TRUE(CanCastTo(129)); EXPECT_TRUE(CanCastTo(255)); EXPECT_TRUE(CanCastTo(256)); EXPECT_TRUE(CanCastTo(32767)); EXPECT_TRUE(CanCastTo(32768)); EXPECT_TRUE(CanCastTo(32769)); EXPECT_TRUE(CanCastTo(65535)); EXPECT_TRUE(CanCastTo(65536)); EXPECT_TRUE(CanCastTo(2147483647ll)); EXPECT_FALSE(CanCastTo(2147483648ll)); EXPECT_FALSE(CanCastTo(2147483649ll)); EXPECT_FALSE(CanCastTo(4294967295ll)); EXPECT_FALSE(CanCastTo(4294967296ll)); EXPECT_FALSE(CanCastTo(9223372036854775807ull)); EXPECT_FALSE(CanCastTo(9223372036854775808ull)); EXPECT_TRUE(CanCastTo(-1)); EXPECT_TRUE(CanCastTo(-127)); EXPECT_TRUE(CanCastTo(-128)); EXPECT_TRUE(CanCastTo(-129)); EXPECT_TRUE(CanCastTo(-255)); EXPECT_TRUE(CanCastTo(-256)); EXPECT_TRUE(CanCastTo(-32767)); EXPECT_TRUE(CanCastTo(-32768)); EXPECT_TRUE(CanCastTo(-32769)); EXPECT_TRUE(CanCastTo(-65535)); EXPECT_TRUE(CanCastTo(-65536)); EXPECT_TRUE(CanCastTo(-2147483647ll)); EXPECT_TRUE(CanCastTo(-2147483648ll)); EXPECT_FALSE(CanCastTo(-2147483649ll)); EXPECT_FALSE(CanCastTo(-4294967295ll)); EXPECT_FALSE(CanCastTo(-4294967296ll)); EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); } TEST(TestSafeInt, TestCanCastTo_Int64) { EXPECT_TRUE(CanCastTo(0)); EXPECT_TRUE(CanCastTo(127)); EXPECT_TRUE(CanCastTo(128)); EXPECT_TRUE(CanCastTo(129)); EXPECT_TRUE(CanCastTo(255)); EXPECT_TRUE(CanCastTo(256)); EXPECT_TRUE(CanCastTo(32767)); EXPECT_TRUE(CanCastTo(32768)); EXPECT_TRUE(CanCastTo(32769)); EXPECT_TRUE(CanCastTo(65535)); EXPECT_TRUE(CanCastTo(65536)); EXPECT_TRUE(CanCastTo(2147483647ll)); EXPECT_TRUE(CanCastTo(2147483648ll)); EXPECT_TRUE(CanCastTo(2147483649ll)); EXPECT_TRUE(CanCastTo(4294967295ll)); EXPECT_TRUE(CanCastTo(4294967296ll)); EXPECT_TRUE(CanCastTo(9223372036854775807ull)); EXPECT_TRUE(CanCastTo(9223372036854775808ull)); EXPECT_FALSE(CanCastTo(-1)); EXPECT_FALSE(CanCastTo(-127)); EXPECT_FALSE(CanCastTo(-128)); EXPECT_FALSE(CanCastTo(-129)); EXPECT_FALSE(CanCastTo(-255)); EXPECT_FALSE(CanCastTo(-256)); EXPECT_FALSE(CanCastTo(-32767)); EXPECT_FALSE(CanCastTo(-32768)); EXPECT_FALSE(CanCastTo(-32769)); EXPECT_FALSE(CanCastTo(-65535)); EXPECT_FALSE(CanCastTo(-65536)); EXPECT_FALSE(CanCastTo(-2147483647ll)); EXPECT_FALSE(CanCastTo(-2147483648ll)); EXPECT_FALSE(CanCastTo(-2147483649ll)); EXPECT_FALSE(CanCastTo(-4294967295ll)); EXPECT_FALSE(CanCastTo(-4294967296ll)); EXPECT_FALSE(CanCastTo(-9223372036854775807ll)); EXPECT_TRUE(CanCastTo(0)); EXPECT_TRUE(CanCastTo(127)); EXPECT_TRUE(CanCastTo(128)); EXPECT_TRUE(CanCastTo(129)); EXPECT_TRUE(CanCastTo(255)); EXPECT_TRUE(CanCastTo(256)); EXPECT_TRUE(CanCastTo(32767)); EXPECT_TRUE(CanCastTo(32768)); EXPECT_TRUE(CanCastTo(32769)); EXPECT_TRUE(CanCastTo(65535)); EXPECT_TRUE(CanCastTo(65536)); EXPECT_TRUE(CanCastTo(2147483647ll)); EXPECT_TRUE(CanCastTo(2147483648ll)); EXPECT_TRUE(CanCastTo(2147483649ll)); EXPECT_TRUE(CanCastTo(4294967295ll)); EXPECT_TRUE(CanCastTo(4294967296ll)); EXPECT_TRUE(CanCastTo(9223372036854775807ull)); EXPECT_FALSE(CanCastTo(9223372036854775808ull)); EXPECT_TRUE(CanCastTo(-1)); EXPECT_TRUE(CanCastTo(-127)); EXPECT_TRUE(CanCastTo(-128)); EXPECT_TRUE(CanCastTo(-129)); EXPECT_TRUE(CanCastTo(-255)); EXPECT_TRUE(CanCastTo(-256)); EXPECT_TRUE(CanCastTo(-32767)); EXPECT_TRUE(CanCastTo(-32768)); EXPECT_TRUE(CanCastTo(-32769)); EXPECT_TRUE(CanCastTo(-65535)); EXPECT_TRUE(CanCastTo(-65536)); EXPECT_TRUE(CanCastTo(-2147483647ll)); EXPECT_TRUE(CanCastTo(-2147483648ll)); EXPECT_TRUE(CanCastTo(-2147483649ll)); EXPECT_TRUE(CanCastTo(-4294967295ll)); EXPECT_TRUE(CanCastTo(-4294967296ll)); EXPECT_TRUE(CanCastTo(-9223372036854775807ll)); } TEST(TestSafeInt, TestCastToSigned) { EXPECT_EQ(CastToSigned(static_cast(5)), 5); EXPECT_EQ(CastToSigned(static_cast(-5)), -5); EXPECT_EQ(CastToSigned(static_cast(254)), -2); EXPECT_EQ(CastToSigned(static_cast(65533)), -3); EXPECT_EQ(CastToSigned(static_cast(254)), 254); EXPECT_EQ(CastToSigned(static_cast(65533)), -3); }