Home | History | Annotate | Download | only in unord.hash
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <functional>
     11 
     12 // make sure that we can hash enumeration values
     13 // Not very portable
     14 
     15 #include <__config>
     16 
     17 #if _LIBCPP_STD_VER > 11
     18 
     19 #include <functional>
     20 #include <cassert>
     21 #include <type_traits>
     22 #include <limits>
     23 
     24 enum class Colors { red, orange, yellow, green, blue, indigo, violet };
     25 enum class Cardinals { zero, one, two, three, five=5 };
     26 enum class LongColors : short { red, orange, yellow, green, blue, indigo, violet };
     27 enum class ShortColors : long { red, orange, yellow, green, blue, indigo, violet };
     28 enum class EightBitColors : uint8_t { red, orange, yellow, green, blue, indigo, violet };
     29 
     30 enum Fruits { apple, pear, grape, mango, cantaloupe };
     31 
     32 template <class T>
     33 void
     34 test()
     35 {
     36     static_assert((std::is_base_of<std::unary_function<T, std::size_t>,
     37                                    std::hash<T> >::value), "");
     38     typedef typename std::underlying_type<T>::type under_type;
     39 
     40     std::hash<T> h1;
     41     std::hash<under_type> h2;
     42     for (int i = 0; i <= 5; ++i)
     43     {
     44         T t(static_cast<T> (i));
     45         if (sizeof(T) <= sizeof(std::size_t))
     46             assert(h1(t) == h2(static_cast<under_type>(i)));
     47     }
     48 }
     49 
     50 int main()
     51 {
     52     test<Cardinals>();
     53 
     54     test<Colors>();
     55     test<ShortColors>();
     56     test<LongColors>();
     57     test<EightBitColors>();
     58 
     59     test<Fruits>();
     60 }
     61 #else
     62 int main () {}
     63 #endif
     64