blob: b25c019688a6aece439618e171b79429bb8778c5 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <unordered_map>
11
12// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
13// class Alloc = allocator<pair<const Key, T>>>
14// class unordered_map
15
16// float load_factor() const
17
18#include <unordered_map>
19#include <string>
20#include <cassert>
Howard Hinnantf836d532011-12-02 21:23:14 +000021#include <cfloat>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000022
Eric Fiselierfd9bbf52015-07-19 03:16:47 +000023#include "test_macros.h"
Marshall Clow061d0cc2013-11-26 20:58:02 +000024#include "min_allocator.h"
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000025
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026int main()
27{
28 {
29 typedef std::unordered_map<int, std::string> C;
30 typedef std::pair<int, std::string> P;
31 P a[] =
32 {
33 P(10, "ten"),
34 P(20, "twenty"),
35 P(30, "thirty"),
Alp Toker08f8ac62014-05-15 11:33:29 +000036 P(40, "forty"),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000037 P(50, "fifty"),
38 P(60, "sixty"),
39 P(70, "seventy"),
40 P(80, "eighty"),
41 };
42 const C c(std::begin(a), std::end(a));
Dan Albert1d4a1ed2016-05-25 22:36:09 -070043 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044 }
45 {
46 typedef std::unordered_map<int, std::string> C;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047 const C c;
48 assert(c.load_factor() == 0);
49 }
Eric Fiselierfd9bbf52015-07-19 03:16:47 +000050#if TEST_STD_VER >= 11
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000051 {
52 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
53 min_allocator<std::pair<const int, std::string>>> C;
54 typedef std::pair<int, std::string> P;
55 P a[] =
56 {
57 P(10, "ten"),
58 P(20, "twenty"),
59 P(30, "thirty"),
Alp Toker08f8ac62014-05-15 11:33:29 +000060 P(40, "forty"),
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000061 P(50, "fifty"),
62 P(60, "sixty"),
63 P(70, "seventy"),
64 P(80, "eighty"),
65 };
66 const C c(std::begin(a), std::end(a));
Dan Albert1d4a1ed2016-05-25 22:36:09 -070067 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000068 }
69 {
70 typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
71 min_allocator<std::pair<const int, std::string>>> C;
Howard Hinnant7a6b7ce2013-06-22 15:21:29 +000072 const C c;
73 assert(c.load_factor() == 0);
74 }
75#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000076}