brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · 05a1932 Raw
78 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS10 11// <functional>12 13// Make sure that we can hash enumeration values.14 15#include <functional>16#include <cassert>17#include <cstddef>18#include <cstdint>19#include <limits>20#include <type_traits>21 22#include "test_macros.h"23 24#if TEST_STD_VER >= 1125#  include "poisoned_hash_helper.h"26#endif27 28enum class Colors { red, orange, yellow, green, blue, indigo, violet };29enum class Cardinals { zero, one, two, three, five=5 };30enum class LongColors : short { red, orange, yellow, green, blue, indigo, violet };31enum class ShortColors : long { red, orange, yellow, green, blue, indigo, violet };32enum class EightBitColors : std::uint8_t { red, orange, yellow, green, blue, indigo, violet };33 34enum Fruits { apple, pear, grape, mango, cantaloupe };35 36template <class T>37void38test()39{40#if TEST_STD_VER >= 1141    test_hash_disabled<const T>();42    test_hash_disabled<volatile T>();43    test_hash_disabled<const volatile T>();44#endif45 46    typedef std::hash<T> H;47#if TEST_STD_VER <= 1748    static_assert((std::is_same<typename H::argument_type, T>::value), "");49    static_assert((std::is_same<typename H::result_type, std::size_t>::value), "");50#endif51    ASSERT_NOEXCEPT(H()(T()));52    typedef typename std::underlying_type<T>::type under_type;53 54    H h1;55    std::hash<under_type> h2;56    for (int i = 0; i <= 5; ++i)57    {58        T t(static_cast<T> (i));59        const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings60        if (small)61            assert(h1(t) == h2(static_cast<under_type>(i)));62    }63}64 65int main(int, char**)66{67    test<Cardinals>();68 69    test<Colors>();70    test<ShortColors>();71    test<LongColors>();72    test<EightBitColors>();73 74    test<Fruits>();75 76  return 0;77}78