kwiberg | 7885d3f | 2017-04-25 12:35:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <algorithm> |
| 12 | #include <limits> |
| 13 | |
| 14 | #include "webrtc/base/safe_minmax.h" |
| 15 | #include "webrtc/test/gtest.h" |
| 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Functions that check that SafeMin() and SafeMax() return the specified type. |
| 22 | // The functions that end in "R" use an explicitly given return type. |
| 23 | |
| 24 | template <typename T1, typename T2, typename Tmin, typename Tmax> |
| 25 | constexpr bool TypeCheckMinMax() { |
| 26 | return std::is_same<decltype(SafeMin(std::declval<T1>(), std::declval<T2>())), |
| 27 | Tmin>::value && |
| 28 | std::is_same<decltype(SafeMax(std::declval<T1>(), std::declval<T2>())), |
| 29 | Tmax>::value; |
| 30 | } |
| 31 | |
| 32 | template <typename T1, typename T2, typename R> |
| 33 | constexpr bool TypeCheckMinR() { |
| 34 | return std::is_same< |
| 35 | decltype(SafeMin<R>(std::declval<T1>(), std::declval<T2>())), R>::value; |
| 36 | } |
| 37 | |
| 38 | template <typename T1, typename T2, typename R> |
| 39 | constexpr bool TypeCheckMaxR() { |
| 40 | return std::is_same< |
| 41 | decltype(SafeMax<R>(std::declval<T1>(), std::declval<T2>())), R>::value; |
| 42 | } |
| 43 | |
| 44 | // clang-format off |
| 45 | |
| 46 | // SafeMin/SafeMax: Check that all combinations of signed/unsigned 8/64 bits |
| 47 | // give the correct default result type. |
| 48 | static_assert(TypeCheckMinMax< int8_t, int8_t, int8_t, int8_t>(), ""); |
| 49 | static_assert(TypeCheckMinMax< int8_t, uint8_t, int8_t, uint8_t>(), ""); |
| 50 | static_assert(TypeCheckMinMax< int8_t, int64_t, int64_t, int64_t>(), ""); |
| 51 | static_assert(TypeCheckMinMax< int8_t, uint64_t, int8_t, uint64_t>(), ""); |
| 52 | static_assert(TypeCheckMinMax< uint8_t, int8_t, int8_t, uint8_t>(), ""); |
| 53 | static_assert(TypeCheckMinMax< uint8_t, uint8_t, uint8_t, uint8_t>(), ""); |
| 54 | static_assert(TypeCheckMinMax< uint8_t, int64_t, int64_t, int64_t>(), ""); |
| 55 | static_assert(TypeCheckMinMax< uint8_t, uint64_t, uint8_t, uint64_t>(), ""); |
| 56 | static_assert(TypeCheckMinMax< int64_t, int8_t, int64_t, int64_t>(), ""); |
| 57 | static_assert(TypeCheckMinMax< int64_t, uint8_t, int64_t, int64_t>(), ""); |
| 58 | static_assert(TypeCheckMinMax< int64_t, int64_t, int64_t, int64_t>(), ""); |
| 59 | static_assert(TypeCheckMinMax< int64_t, uint64_t, int64_t, uint64_t>(), ""); |
| 60 | static_assert(TypeCheckMinMax<uint64_t, int8_t, int8_t, uint64_t>(), ""); |
| 61 | static_assert(TypeCheckMinMax<uint64_t, uint8_t, uint8_t, uint64_t>(), ""); |
| 62 | static_assert(TypeCheckMinMax<uint64_t, int64_t, int64_t, uint64_t>(), ""); |
| 63 | static_assert(TypeCheckMinMax<uint64_t, uint64_t, uint64_t, uint64_t>(), ""); |
| 64 | |
kwiberg | 7885d3f | 2017-04-25 12:35:07 -0700 | [diff] [blame] | 65 | enum DefaultE { kFoo = -17 }; |
| 66 | enum UInt8E : uint8_t { kBar = 17 }; |
kwiberg | dbb497a | 2017-06-02 04:24:11 -0700 | [diff] [blame^] | 67 | |
| 68 | // SafeMin/SafeMax: Check that we can use enum types. |
| 69 | static_assert(TypeCheckMinMax<unsigned, unsigned, unsigned, unsigned>(), ""); |
| 70 | static_assert(TypeCheckMinMax<unsigned, DefaultE, int, unsigned>(), ""); |
| 71 | static_assert(TypeCheckMinMax<unsigned, UInt8E, uint8_t, unsigned>(), ""); |
| 72 | static_assert(TypeCheckMinMax<DefaultE, unsigned, int, unsigned>(), ""); |
| 73 | static_assert(TypeCheckMinMax<DefaultE, DefaultE, int, int>(), ""); |
| 74 | static_assert(TypeCheckMinMax<DefaultE, UInt8E, int, int>(), ""); |
| 75 | static_assert(TypeCheckMinMax< UInt8E, unsigned, uint8_t, unsigned>(), ""); |
| 76 | static_assert(TypeCheckMinMax< UInt8E, DefaultE, int, int>(), ""); |
| 77 | static_assert(TypeCheckMinMax< UInt8E, UInt8E, uint8_t, uint8_t>(), ""); |
kwiberg | 7885d3f | 2017-04-25 12:35:07 -0700 | [diff] [blame] | 78 | |
| 79 | using ld = long double; |
| 80 | |
| 81 | // SafeMin/SafeMax: Check that all floating-point combinations give the |
| 82 | // correct result type. |
| 83 | static_assert(TypeCheckMinMax< float, float, float, float>(), ""); |
| 84 | static_assert(TypeCheckMinMax< float, double, double, double>(), ""); |
| 85 | static_assert(TypeCheckMinMax< float, ld, ld, ld>(), ""); |
| 86 | static_assert(TypeCheckMinMax<double, float, double, double>(), ""); |
| 87 | static_assert(TypeCheckMinMax<double, double, double, double>(), ""); |
| 88 | static_assert(TypeCheckMinMax<double, ld, ld, ld>(), ""); |
| 89 | static_assert(TypeCheckMinMax< ld, float, ld, ld>(), ""); |
| 90 | static_assert(TypeCheckMinMax< ld, double, ld, ld>(), ""); |
| 91 | static_assert(TypeCheckMinMax< ld, ld, ld, ld>(), ""); |
| 92 | |
| 93 | // clang-format on |
| 94 | |
| 95 | // SafeMin/SafeMax: Check some cases of explicitly specified return type. The |
| 96 | // commented-out lines give compilation errors due to the requested return type |
| 97 | // being too small or requiring an int<->float conversion. |
| 98 | static_assert(TypeCheckMinR<int8_t, int8_t, int16_t>(), ""); |
| 99 | // static_assert(TypeCheckMinR<int8_t, int8_t, float>(), ""); |
| 100 | static_assert(TypeCheckMinR<uint32_t, uint64_t, uint32_t>(), ""); |
| 101 | // static_assert(TypeCheckMaxR<uint64_t, float, float>(), ""); |
| 102 | // static_assert(TypeCheckMaxR<uint64_t, double, float>(), ""); |
| 103 | static_assert(TypeCheckMaxR<uint32_t, int32_t, uint32_t>(), ""); |
| 104 | // static_assert(TypeCheckMaxR<uint32_t, int32_t, int32_t>(), ""); |
| 105 | |
| 106 | template <typename T1, typename T2, typename Tmin, typename Tmax> |
| 107 | constexpr bool CheckMinMax(T1 a, T2 b, Tmin min, Tmax max) { |
| 108 | return TypeCheckMinMax<T1, T2, Tmin, Tmax>() && SafeMin(a, b) == min && |
| 109 | SafeMax(a, b) == max; |
| 110 | } |
| 111 | |
| 112 | // SafeMin/SafeMax: Check a few values. |
| 113 | static_assert(CheckMinMax(int8_t{1}, int8_t{-1}, int8_t{-1}, int8_t{1}), ""); |
| 114 | static_assert(CheckMinMax(uint8_t{1}, int8_t{-1}, int8_t{-1}, uint8_t{1}), ""); |
| 115 | static_assert(CheckMinMax(uint8_t{5}, uint64_t{2}, uint8_t{2}, uint64_t{5}), |
| 116 | ""); |
| 117 | static_assert(CheckMinMax(std::numeric_limits<int32_t>::min(), |
| 118 | std::numeric_limits<uint32_t>::max(), |
| 119 | std::numeric_limits<int32_t>::min(), |
| 120 | std::numeric_limits<uint32_t>::max()), |
| 121 | ""); |
| 122 | static_assert(CheckMinMax(std::numeric_limits<int32_t>::min(), |
| 123 | std::numeric_limits<uint16_t>::max(), |
| 124 | std::numeric_limits<int32_t>::min(), |
| 125 | int32_t{std::numeric_limits<uint16_t>::max()}), |
| 126 | ""); |
| 127 | // static_assert(CheckMinMax(1.f, 2, 1.f, 2.f), ""); |
| 128 | static_assert(CheckMinMax(1.f, 0.0, 0.0, 1.0), ""); |
| 129 | |
| 130 | } // namespace |
| 131 | |
| 132 | // clang-format off |
| 133 | |
| 134 | // These functions aren't used in the tests, but it's useful to look at the |
| 135 | // compiler output for them, and verify that (1) the same-signedness *Safe |
| 136 | // functions result in exactly the same code as their *Ref counterparts, and |
| 137 | // that (2) the mixed-signedness *Safe functions have just a few extra |
| 138 | // arithmetic and logic instructions (but no extra control flow instructions). |
| 139 | int32_t TestMinRef( int32_t a, int32_t b) { return std::min(a, b); } |
| 140 | uint32_t TestMinRef( uint32_t a, uint32_t b) { return std::min(a, b); } |
| 141 | int32_t TestMinSafe( int32_t a, int32_t b) { return SafeMin(a, b); } |
| 142 | int32_t TestMinSafe( int32_t a, uint32_t b) { return SafeMin(a, b); } |
| 143 | int32_t TestMinSafe(uint32_t a, int32_t b) { return SafeMin(a, b); } |
| 144 | uint32_t TestMinSafe(uint32_t a, uint32_t b) { return SafeMin(a, b); } |
| 145 | |
| 146 | // clang-format on |
| 147 | |
| 148 | } // namespace rtc |