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 #if __cplusplus >= 201402L
     16 
     17 #include <functional>
     18 #include <cassert>
     19 #include <type_traits>
     20 #include <limits>
     21 
     22 enum class Colors { red, orange, yellow, green, blue, indigo, violet };
     23 enum class Cardinals { zero, one, two, three, five=5 };
     24 enum class LongColors : short { red, orange, yellow, green, blue, indigo, violet };
     25 enum class ShortColors : long { red, orange, yellow, green, blue, indigo, violet };
     26 enum class EightBitColors : uint8_t { red, orange, yellow, green, blue, indigo, violet };
     27 
     28 enum Fruits { apple, pear, grape, mango, cantaloupe };
     29 
     30 template <class T>
     31 void
     32 test()
     33 {
     34     typedef std::hash<T> H;
     35     static_assert((std::is_same<typename H::argument_type, T>::value), "" );
     36     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
     37     typedef typename std::underlying_type<T>::type under_type;
     38 
     39     H h1;
     40     std::hash<under_type> h2;
     41     for (int i = 0; i <= 5; ++i)
     42     {
     43         T t(static_cast<T> (i));
     44         if (sizeof(T) <= sizeof(std::size_t))
     45             assert(h1(t) == h2(static_cast<under_type>(i)));
     46     }
     47 }
     48 
     49 int main()
     50 {
     51     test<Cardinals>();
     52 
     53     test<Colors>();
     54     test<ShortColors>();
     55     test<LongColors>();
     56     test<EightBitColors>();
     57 
     58     test<Fruits>();
     59 }
     60 #else
     61 int main () {}
     62 #endif
     63