brintos

brintos / llvm-project-archived public Read only

0
0
Text · 17.6 KiB · be8a96f Raw
463 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// <unordered_set>10 11// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,12//           class Alloc = allocator<Value>>13// class unordered_set14 15// void swap(unordered_set& u);16 17#include <unordered_set>18#include <cassert>19#include <cstddef>20 21#include "test_macros.h"22#include "../../test_compare.h"23#include "../../test_hash.h"24#include "test_allocator.h"25#include "min_allocator.h"26 27int main(int, char**) {28  {29    typedef test_hash<int> Hash;30    typedef test_equal_to<int> Compare;31    typedef test_allocator<int> Alloc;32    typedef std::unordered_set<int, Hash, Compare, Alloc> C;33    C c1(0, Hash(1), Compare(1), Alloc(1, 1));34    C c2(0, Hash(2), Compare(2), Alloc(1, 2));35    c2.max_load_factor(2);36    c1.swap(c2);37 38    LIBCPP_ASSERT(c1.bucket_count() == 0);39    assert(c1.size() == 0);40    assert(c1.hash_function() == Hash(2));41    assert(c1.key_eq() == Compare(2));42    assert(c1.get_allocator().get_id() == 1);43    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());44    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());45    assert(c1.max_load_factor() == 2);46 47    LIBCPP_ASSERT(c2.bucket_count() == 0);48    assert(c2.size() == 0);49    assert(c2.hash_function() == Hash(1));50    assert(c2.key_eq() == Compare(1));51    assert(c2.get_allocator().get_id() == 2);52    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());53    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());54    assert(c2.max_load_factor() == 1);55  }56  {57    typedef test_hash<int> Hash;58    typedef test_equal_to<int> Compare;59    typedef test_allocator<int> Alloc;60    typedef std::unordered_set<int, Hash, Compare, Alloc> C;61    typedef int P;62    P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};63    C c1(0, Hash(1), Compare(1), Alloc(1, 1));64    C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(1, 2));65    c2.max_load_factor(2);66    c1.swap(c2);67 68    assert(c1.bucket_count() >= 8);69    assert(c1.size() == 8);70    assert(*c1.find(10) == 10);71    assert(*c1.find(20) == 20);72    assert(*c1.find(30) == 30);73    assert(*c1.find(40) == 40);74    assert(*c1.find(50) == 50);75    assert(*c1.find(60) == 60);76    assert(*c1.find(70) == 70);77    assert(*c1.find(80) == 80);78    assert(c1.hash_function() == Hash(2));79    assert(c1.key_eq() == Compare(2));80    assert(c1.get_allocator().get_id() == 1);81    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());82    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());83    assert(c1.max_load_factor() == 2);84 85    LIBCPP_ASSERT(c2.bucket_count() == 0);86    assert(c2.size() == 0);87    assert(c2.hash_function() == Hash(1));88    assert(c2.key_eq() == Compare(1));89    assert(c2.get_allocator().get_id() == 2);90    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());91    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());92    assert(c2.max_load_factor() == 1);93  }94  {95    typedef test_hash<int> Hash;96    typedef test_equal_to<int> Compare;97    typedef test_allocator<int> Alloc;98    typedef std::unordered_set<int, Hash, Compare, Alloc> C;99    typedef int P;100    P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};101    C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1, 1));102    C c2(0, Hash(2), Compare(2), Alloc(1, 2));103    c2.max_load_factor(2);104    c1.swap(c2);105 106    LIBCPP_ASSERT(c1.bucket_count() == 0);107    assert(c1.size() == 0);108    assert(c1.hash_function() == Hash(2));109    assert(c1.key_eq() == Compare(2));110    assert(c1.get_allocator().get_id() == 1);111    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());112    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());113    assert(c1.max_load_factor() == 2);114 115    assert(c2.bucket_count() >= 4);116    assert(c2.size() == 4);117    assert(c2.count(1) == 1);118    assert(c2.count(2) == 1);119    assert(c2.count(3) == 1);120    assert(c2.count(4) == 1);121    assert(c2.hash_function() == Hash(1));122    assert(c2.key_eq() == Compare(1));123    assert(c2.get_allocator().get_id() == 2);124    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());125    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());126    assert(c2.max_load_factor() == 1);127  }128  {129    typedef test_hash<int> Hash;130    typedef test_equal_to<int> Compare;131    typedef test_allocator<int> Alloc;132    typedef std::unordered_set<int, Hash, Compare, Alloc> C;133    typedef int P;134    P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};135    P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};136    C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1, 1));137    C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(1, 2));138    c2.max_load_factor(2);139    c1.swap(c2);140 141    assert(c1.bucket_count() >= 8);142    assert(c1.size() == 8);143    assert(*c1.find(10) == 10);144    assert(*c1.find(20) == 20);145    assert(*c1.find(30) == 30);146    assert(*c1.find(40) == 40);147    assert(*c1.find(50) == 50);148    assert(*c1.find(60) == 60);149    assert(*c1.find(70) == 70);150    assert(*c1.find(80) == 80);151    assert(c1.hash_function() == Hash(2));152    assert(c1.key_eq() == Compare(2));153    assert(c1.get_allocator().get_id() == 1);154    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());155    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());156    assert(c1.max_load_factor() == 2);157 158    assert(c2.bucket_count() >= 4);159    assert(c2.size() == 4);160    assert(c2.count(1) == 1);161    assert(c2.count(2) == 1);162    assert(c2.count(3) == 1);163    assert(c2.count(4) == 1);164    assert(c2.hash_function() == Hash(1));165    assert(c2.key_eq() == Compare(1));166    assert(c2.get_allocator().get_id() == 2);167    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());168    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());169    assert(c2.max_load_factor() == 1);170  }171 172  {173    typedef test_hash<int> Hash;174    typedef test_equal_to<int> Compare;175    typedef other_allocator<int> Alloc;176    typedef std::unordered_set<int, Hash, Compare, Alloc> C;177    C c1(0, Hash(1), Compare(1), Alloc(1));178    C c2(0, Hash(2), Compare(2), Alloc(2));179    c2.max_load_factor(2);180    c1.swap(c2);181 182    LIBCPP_ASSERT(c1.bucket_count() == 0);183    assert(c1.size() == 0);184    assert(c1.hash_function() == Hash(2));185    assert(c1.key_eq() == Compare(2));186    assert(c1.get_allocator() == Alloc(2));187    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());188    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());189    assert(c1.max_load_factor() == 2);190 191    LIBCPP_ASSERT(c2.bucket_count() == 0);192    assert(c2.size() == 0);193    assert(c2.hash_function() == Hash(1));194    assert(c2.key_eq() == Compare(1));195    assert(c2.get_allocator() == Alloc(1));196    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());197    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());198    assert(c2.max_load_factor() == 1);199  }200  {201    typedef test_hash<int> Hash;202    typedef test_equal_to<int> Compare;203    typedef other_allocator<int> Alloc;204    typedef std::unordered_set<int, Hash, Compare, Alloc> C;205    typedef int P;206    P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};207    C c1(0, Hash(1), Compare(1), Alloc(1));208    C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));209    c2.max_load_factor(2);210    c1.swap(c2);211 212    assert(c1.bucket_count() >= 8);213    assert(c1.size() == 8);214    assert(*c1.find(10) == 10);215    assert(*c1.find(20) == 20);216    assert(*c1.find(30) == 30);217    assert(*c1.find(40) == 40);218    assert(*c1.find(50) == 50);219    assert(*c1.find(60) == 60);220    assert(*c1.find(70) == 70);221    assert(*c1.find(80) == 80);222    assert(c1.hash_function() == Hash(2));223    assert(c1.key_eq() == Compare(2));224    assert(c1.get_allocator() == Alloc(2));225    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());226    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());227    assert(c1.max_load_factor() == 2);228 229    LIBCPP_ASSERT(c2.bucket_count() == 0);230    assert(c2.size() == 0);231    assert(c2.hash_function() == Hash(1));232    assert(c2.key_eq() == Compare(1));233    assert(c2.get_allocator() == Alloc(1));234    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());235    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());236    assert(c2.max_load_factor() == 1);237  }238  {239    typedef test_hash<int> Hash;240    typedef test_equal_to<int> Compare;241    typedef other_allocator<int> Alloc;242    typedef std::unordered_set<int, Hash, Compare, Alloc> C;243    typedef int P;244    P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};245    C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));246    C c2(0, Hash(2), Compare(2), Alloc(2));247    c2.max_load_factor(2);248    c1.swap(c2);249 250    LIBCPP_ASSERT(c1.bucket_count() == 0);251    assert(c1.size() == 0);252    assert(c1.hash_function() == Hash(2));253    assert(c1.key_eq() == Compare(2));254    assert(c1.get_allocator() == Alloc(2));255    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());256    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());257    assert(c1.max_load_factor() == 2);258 259    assert(c2.bucket_count() >= 4);260    assert(c2.size() == 4);261    assert(c2.count(1) == 1);262    assert(c2.count(2) == 1);263    assert(c2.count(3) == 1);264    assert(c2.count(4) == 1);265    assert(c2.hash_function() == Hash(1));266    assert(c2.key_eq() == Compare(1));267    assert(c2.get_allocator() == Alloc(1));268    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());269    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());270    assert(c2.max_load_factor() == 1);271  }272  {273    typedef test_hash<int> Hash;274    typedef test_equal_to<int> Compare;275    typedef other_allocator<int> Alloc;276    typedef std::unordered_set<int, Hash, Compare, Alloc> C;277    typedef int P;278    P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};279    P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};280    C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));281    C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));282    c2.max_load_factor(2);283    c1.swap(c2);284 285    assert(c1.bucket_count() >= 8);286    assert(c1.size() == 8);287    assert(*c1.find(10) == 10);288    assert(*c1.find(20) == 20);289    assert(*c1.find(30) == 30);290    assert(*c1.find(40) == 40);291    assert(*c1.find(50) == 50);292    assert(*c1.find(60) == 60);293    assert(*c1.find(70) == 70);294    assert(*c1.find(80) == 80);295    assert(c1.hash_function() == Hash(2));296    assert(c1.key_eq() == Compare(2));297    assert(c1.get_allocator() == Alloc(2));298    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());299    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());300    assert(c1.max_load_factor() == 2);301 302    assert(c2.bucket_count() >= 4);303    assert(c2.size() == 4);304    assert(c2.count(1) == 1);305    assert(c2.count(2) == 1);306    assert(c2.count(3) == 1);307    assert(c2.count(4) == 1);308    assert(c2.hash_function() == Hash(1));309    assert(c2.key_eq() == Compare(1));310    assert(c2.get_allocator() == Alloc(1));311    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());312    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());313    assert(c2.max_load_factor() == 1);314  }315#if TEST_STD_VER >= 11316  {317    typedef test_hash<int> Hash;318    typedef test_equal_to<int> Compare;319    typedef min_allocator<int> Alloc;320    typedef std::unordered_set<int, Hash, Compare, Alloc> C;321    C c1(0, Hash(1), Compare(1), Alloc());322    C c2(0, Hash(2), Compare(2), Alloc());323    c2.max_load_factor(2);324    c1.swap(c2);325 326    LIBCPP_ASSERT(c1.bucket_count() == 0);327    assert(c1.size() == 0);328    assert(c1.hash_function() == Hash(2));329    assert(c1.key_eq() == Compare(2));330    assert(c1.get_allocator() == Alloc());331    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());332    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());333    assert(c1.max_load_factor() == 2);334 335    LIBCPP_ASSERT(c2.bucket_count() == 0);336    assert(c2.size() == 0);337    assert(c2.hash_function() == Hash(1));338    assert(c2.key_eq() == Compare(1));339    assert(c2.get_allocator() == Alloc());340    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());341    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());342    assert(c2.max_load_factor() == 1);343  }344  {345    typedef test_hash<int> Hash;346    typedef test_equal_to<int> Compare;347    typedef min_allocator<int> Alloc;348    typedef std::unordered_set<int, Hash, Compare, Alloc> C;349    typedef int P;350    P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};351    C c1(0, Hash(1), Compare(1), Alloc());352    C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());353    c2.max_load_factor(2);354    c1.swap(c2);355 356    assert(c1.bucket_count() >= 8);357    assert(c1.size() == 8);358    assert(*c1.find(10) == 10);359    assert(*c1.find(20) == 20);360    assert(*c1.find(30) == 30);361    assert(*c1.find(40) == 40);362    assert(*c1.find(50) == 50);363    assert(*c1.find(60) == 60);364    assert(*c1.find(70) == 70);365    assert(*c1.find(80) == 80);366    assert(c1.hash_function() == Hash(2));367    assert(c1.key_eq() == Compare(2));368    assert(c1.get_allocator() == Alloc());369    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());370    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());371    assert(c1.max_load_factor() == 2);372 373    LIBCPP_ASSERT(c2.bucket_count() == 0);374    assert(c2.size() == 0);375    assert(c2.hash_function() == Hash(1));376    assert(c2.key_eq() == Compare(1));377    assert(c2.get_allocator() == Alloc());378    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());379    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());380    assert(c2.max_load_factor() == 1);381  }382  {383    typedef test_hash<int> Hash;384    typedef test_equal_to<int> Compare;385    typedef min_allocator<int> Alloc;386    typedef std::unordered_set<int, Hash, Compare, Alloc> C;387    typedef int P;388    P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};389    C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());390    C c2(0, Hash(2), Compare(2), Alloc());391    c2.max_load_factor(2);392    c1.swap(c2);393 394    LIBCPP_ASSERT(c1.bucket_count() == 0);395    assert(c1.size() == 0);396    assert(c1.hash_function() == Hash(2));397    assert(c1.key_eq() == Compare(2));398    assert(c1.get_allocator() == Alloc());399    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());400    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());401    assert(c1.max_load_factor() == 2);402 403    assert(c2.bucket_count() >= 4);404    assert(c2.size() == 4);405    assert(c2.count(1) == 1);406    assert(c2.count(2) == 1);407    assert(c2.count(3) == 1);408    assert(c2.count(4) == 1);409    assert(c2.hash_function() == Hash(1));410    assert(c2.key_eq() == Compare(1));411    assert(c2.get_allocator() == Alloc());412    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());413    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());414    assert(c2.max_load_factor() == 1);415  }416  {417    typedef test_hash<int> Hash;418    typedef test_equal_to<int> Compare;419    typedef min_allocator<int> Alloc;420    typedef std::unordered_set<int, Hash, Compare, Alloc> C;421    typedef int P;422    P a1[] = {P(1), P(2), P(3), P(4), P(1), P(2)};423    P a2[] = {P(10), P(20), P(30), P(40), P(50), P(60), P(70), P(80)};424    C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());425    C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());426    c2.max_load_factor(2);427    c1.swap(c2);428 429    assert(c1.bucket_count() >= 8);430    assert(c1.size() == 8);431    assert(*c1.find(10) == 10);432    assert(*c1.find(20) == 20);433    assert(*c1.find(30) == 30);434    assert(*c1.find(40) == 40);435    assert(*c1.find(50) == 50);436    assert(*c1.find(60) == 60);437    assert(*c1.find(70) == 70);438    assert(*c1.find(80) == 80);439    assert(c1.hash_function() == Hash(2));440    assert(c1.key_eq() == Compare(2));441    assert(c1.get_allocator() == Alloc());442    assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());443    assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());444    assert(c1.max_load_factor() == 2);445 446    assert(c2.bucket_count() >= 4);447    assert(c2.size() == 4);448    assert(c2.count(1) == 1);449    assert(c2.count(2) == 1);450    assert(c2.count(3) == 1);451    assert(c2.count(4) == 1);452    assert(c2.hash_function() == Hash(1));453    assert(c2.key_eq() == Compare(1));454    assert(c2.get_allocator() == Alloc());455    assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());456    assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());457    assert(c2.max_load_factor() == 1);458  }459#endif460 461  return 0;462}463