Ho Cheung | b1dba17 | 2023-05-16 22:58:01 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <algorithm> |
| 6 | #include <tuple> |
| 7 | |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
| 10 | namespace { |
| 11 | |
| 12 | struct OneType { |
| 13 | int some_int; |
| 14 | }; |
| 15 | |
| 16 | bool operator<(const OneType& lhs, const OneType& rhs) { |
| 17 | return lhs.some_int < rhs.some_int; |
| 18 | } |
| 19 | |
| 20 | struct AnotherType { |
| 21 | int some_other_int; |
| 22 | }; |
| 23 | |
danakj | 08de6c7 | 2023-09-26 17:57:50 | [diff] [blame^] | 24 | // Verify libc++ hardening terminates instead of UB with invalid clamp args. |
Ho Cheung | b1dba17 | 2023-05-16 22:58:01 | [diff] [blame] | 25 | TEST(ClampTest, Death) { |
| 26 | EXPECT_DEATH_IF_SUPPORTED(std::ignore = std::clamp(3, 10, 0), ""); |
| 27 | EXPECT_DEATH_IF_SUPPORTED(std::ignore = std::clamp(3.0, 10.0, 0.0), ""); |
| 28 | |
| 29 | OneType one_type_0{0}; |
| 30 | OneType one_type_3{3}; |
| 31 | OneType one_type_10{10}; |
| 32 | AnotherType another_type_0{0}; |
| 33 | AnotherType another_type_3{3}; |
| 34 | AnotherType another_type_10{10}; |
| 35 | auto compare_another_type = [](const auto& lhs, const auto& rhs) { |
| 36 | return lhs.some_other_int < rhs.some_other_int; |
| 37 | }; |
| 38 | |
| 39 | EXPECT_DEATH_IF_SUPPORTED( |
| 40 | std::ignore = std::clamp(one_type_3, one_type_10, one_type_0), ""); |
| 41 | EXPECT_DEATH_IF_SUPPORTED( |
| 42 | std::ignore = std::clamp(another_type_3, another_type_10, another_type_0, |
| 43 | compare_another_type), |
| 44 | ""); |
| 45 | } |
| 46 | |
| 47 | } // namespace |