blob: 603378e1383b8d2ebcd4c35f0993f435f471c1a1 [file] [log] [blame]
Ho Cheungb1dba172023-05-16 22:58:011// 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
10namespace {
11
12struct OneType {
13 int some_int;
14};
15
16bool operator<(const OneType& lhs, const OneType& rhs) {
17 return lhs.some_int < rhs.some_int;
18}
19
20struct AnotherType {
21 int some_other_int;
22};
23
danakj08de6c72023-09-26 17:57:5024// Verify libc++ hardening terminates instead of UB with invalid clamp args.
Ho Cheungb1dba172023-05-16 22:58:0125TEST(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