| 936 | }; |
| 937 | |
| 938 | void THashTest::TestAllocation() { |
| 939 | TAllocatorCounters counters; |
| 940 | |
| 941 | using int_set = THashSet<int, THash<int>, TEqualTo<int>, TCountingAllocator<int>>; |
| 942 | |
| 943 | { |
| 944 | int_set set0(&counters); |
| 945 | int_set set1(set0); |
| 946 | set0.clear(); |
| 947 | int_set set2(&counters); |
| 948 | set2 = set1; |
| 949 | UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 0); /* Copying around null sets should not trigger allocations. */ |
| 950 | |
| 951 | set0.insert(0); |
| 952 | UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 2); /* One for buckets array, one for a new node. */ |
| 953 | |
| 954 | set0.clear(); |
| 955 | set1 = set0; |
| 956 | int_set set3(set0); |
| 957 | UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, 2); /* Copying from an empty set with allocated buckets should not trigger allocations. */ |
| 958 | |
| 959 | for (int i = 0; i < 1000; i++) { |
| 960 | set0.insert(i); |
| 961 | } |
| 962 | size_t allocations = counters.Allocations; |
| 963 | set0.clear(); |
| 964 | UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, allocations); /* clear() should not trigger allocations. */ |
| 965 | } |
| 966 | |
| 967 | UNIT_ASSERT_VALUES_EQUAL(counters.Allocations, counters.Deallocations); |
| 968 | } |
| 969 | |
| 970 | template <int Value> |
| 971 | class TNonCopyableInt { |