OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_numeric_cast.cpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2023- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12
13
14#include "test_common.hpp"
15
16#include <cstdint>
17#include <cctype>
18
20
21
22using namespace openvpn::numeric_util;
23
24
25TEST(NumericCast, SameTypeNocast1)
26{
27 const int32_t i32 = -1;
28 auto result = numeric_cast<int32_t>(i32);
29 EXPECT_EQ(result, i32);
30}
31
32TEST(NumericCast, SignMismatch321)
33{
34 const int32_t i32 = -1;
35 EXPECT_THROW(numeric_cast<uint32_t>(i32), numeric_out_of_range);
36}
37
38TEST(NumericCast, SignMismatch322)
39{
40 const uint32_t u32 = std::numeric_limits<uint32_t>::max();
41 EXPECT_THROW(numeric_cast<int32_t>(u32), numeric_out_of_range);
42}
43
44TEST(NumericCast, SignMismatch323)
45{
46 const uint32_t u32 = 0;
47 auto result = numeric_cast<int32_t>(u32);
48 EXPECT_EQ(result, 0);
49}
50
51TEST(NumericCast, SignMismatch324)
52{
53 const uint32_t u32 = 42;
54 auto result = numeric_cast<int32_t>(u32);
55 EXPECT_EQ(result, 42);
56}
57
58TEST(NumericCast, SignMismatch325)
59{
60 const uint32_t u32 = uint32_t(std::numeric_limits<int32_t>::max());
61 auto result = numeric_cast<int32_t>(u32);
62 EXPECT_EQ(result, std::numeric_limits<int32_t>::max());
63}
64
65TEST(NumericCast, SignMismatch326)
66{
67 const int32_t s32 = std::numeric_limits<int32_t>::max();
68 EXPECT_THROW(numeric_cast<uint8_t>(s32), numeric_out_of_range);
69}
70
71TEST(NumericCast, SignMismatch327)
72{
73 const int32_t s32 = 42;
74 auto result = numeric_cast<uint8_t>(s32);
75 EXPECT_EQ(result, 42);
76}
77
78TEST(NumericCast, SRangeMismatch16641)
79{
80 const int64_t s64 = std::numeric_limits<int64_t>::max();
81 EXPECT_THROW(numeric_cast<int16_t>(s64), numeric_out_of_range);
82}
83
84TEST(NumericCast, SRangeMatch16641)
85{
86 const int64_t s64 = 0;
87 auto result = numeric_cast<int16_t>(s64);
88 EXPECT_EQ(result, 0);
89}
90
91TEST(NumericCast, URangeMismatch16641)
92{
93 const uint64_t u64 = std::numeric_limits<uint64_t>::max();
94 EXPECT_THROW(numeric_cast<uint16_t>(u64), numeric_out_of_range);
95}
TEST(NumericCast, SameTypeNocast1)