15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
9dc066888SArthur O'Dwyer // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
10dc066888SArthur O'Dwyer 
115a83710eSEric Fiselier // <functional>
125a83710eSEric Fiselier 
13*3f3abaf4SArthur O'Dwyer // Make sure that we can hash enumeration values.
145a83710eSEric Fiselier 
15f2f2a639SEric Fiselier #include "test_macros.h"
16f2f2a639SEric Fiselier 
175a83710eSEric Fiselier #include <functional>
185a83710eSEric Fiselier #include <cassert>
195a83710eSEric Fiselier #include <type_traits>
205a83710eSEric Fiselier #include <limits>
215a83710eSEric Fiselier 
225a83710eSEric Fiselier enum class Colors { red, orange, yellow, green, blue, indigo, violet };
235a83710eSEric Fiselier enum class Cardinals { zero, one, two, three, five=5 };
245a83710eSEric Fiselier enum class LongColors : short { red, orange, yellow, green, blue, indigo, violet };
255a83710eSEric Fiselier enum class ShortColors : long { red, orange, yellow, green, blue, indigo, violet };
265a83710eSEric Fiselier enum class EightBitColors : uint8_t { red, orange, yellow, green, blue, indigo, violet };
275a83710eSEric Fiselier 
285a83710eSEric Fiselier enum Fruits { apple, pear, grape, mango, cantaloupe };
295a83710eSEric Fiselier 
305a83710eSEric Fiselier template <class T>
315a83710eSEric Fiselier void
test()325a83710eSEric Fiselier test()
335a83710eSEric Fiselier {
34d95510ebSMarshall Clow     typedef std::hash<T> H;
35dc066888SArthur O'Dwyer #if TEST_STD_VER <= 17
366855c93cSMarshall Clow     static_assert((std::is_same<typename H::argument_type, T>::value), "");
376855c93cSMarshall Clow     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "");
38dc066888SArthur O'Dwyer #endif
397c803385SMarshall Clow     ASSERT_NOEXCEPT(H()(T()));
405a83710eSEric Fiselier     typedef typename std::underlying_type<T>::type under_type;
415a83710eSEric Fiselier 
42d95510ebSMarshall Clow     H h1;
435a83710eSEric Fiselier     std::hash<under_type> h2;
445a83710eSEric Fiselier     for (int i = 0; i <= 5; ++i)
455a83710eSEric Fiselier     {
465a83710eSEric Fiselier         T t(static_cast<T> (i));
473d26ee29SStephan T. Lavavej         const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings
483d26ee29SStephan T. Lavavej         if (small)
495a83710eSEric Fiselier             assert(h1(t) == h2(static_cast<under_type>(i)));
505a83710eSEric Fiselier     }
515a83710eSEric Fiselier }
525a83710eSEric Fiselier 
main(int,char **)532df59c50SJF Bastien int main(int, char**)
545a83710eSEric Fiselier {
555a83710eSEric Fiselier     test<Cardinals>();
565a83710eSEric Fiselier 
575a83710eSEric Fiselier     test<Colors>();
585a83710eSEric Fiselier     test<ShortColors>();
595a83710eSEric Fiselier     test<LongColors>();
605a83710eSEric Fiselier     test<EightBitColors>();
615a83710eSEric Fiselier 
625a83710eSEric Fiselier     test<Fruits>();
632df59c50SJF Bastien 
642df59c50SJF Bastien   return 0;
655a83710eSEric Fiselier }
66