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(numeric_cast, same_type_nocast1)
26{
27 int32_t i32 = -1;
28 auto result = numeric_cast<int32_t>(i32);
29 EXPECT_EQ(result, i32);
30}
31
32TEST(numeric_cast, sign_mismatch_32_1)
33{
34 int32_t i32 = -1;
35 EXPECT_THROW(numeric_cast<uint32_t>(i32), numeric_out_of_range);
36}
37
38TEST(numeric_cast, sign_mismatch_32_2)
39{
40 uint32_t u32 = std::numeric_limits<uint32_t>::max();
41 EXPECT_THROW(numeric_cast<int32_t>(u32), numeric_out_of_range);
42}
43
44TEST(numeric_cast, sign_mismatch_32_3)
45{
46 uint32_t u32 = 0;
47 auto result = numeric_cast<int32_t>(u32);
48 EXPECT_EQ(result, 0);
49}
50
51TEST(numeric_cast, sign_mismatch_32_4)
52{
53 uint32_t u32 = 42;
54 auto result = numeric_cast<int32_t>(u32);
55 EXPECT_EQ(result, 42);
56}
57
58TEST(numeric_cast, sign_mismatch_32_5)
59{
60 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(numeric_cast, sign_mismatch_32_6)
66{
67 int32_t s32 = std::numeric_limits<int32_t>::max();
68 EXPECT_THROW(numeric_cast<uint8_t>(s32), numeric_out_of_range);
69}
70
71TEST(numeric_cast, sign_mismatch_32_7)
72{
73 int32_t s32 = 42;
74 auto result = numeric_cast<uint8_t>(s32);
75 EXPECT_EQ(result, 42);
76}
77
78TEST(numeric_cast, s_range_mismatch_16_64_1)
79{
80 int64_t s64 = std::numeric_limits<int64_t>::max();
81 EXPECT_THROW(numeric_cast<int16_t>(s64), numeric_out_of_range);
82}
83
84TEST(numeric_cast, s_range_match_16_64_1)
85{
86 int64_t s64 = 0;
87 auto result = numeric_cast<int16_t>(s64);
88 EXPECT_EQ(result, 0);
89}
90
91TEST(numeric_cast, u_range_mismatch_16_64_1)
92{
93 uint64_t u64 = std::numeric_limits<uint64_t>::max();
94 EXPECT_THROW(numeric_cast<uint16_t>(u64), numeric_out_of_range);
95}
TEST(numeric_cast, same_type_nocast1)