MCPcopy Create free account
hub / github.com/catboost/catboost / TestInsertOrAssign

Method TestInsertOrAssign

util/generic/hash_ut.cpp:1087–1136  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1085}
1086
1087void THashTest::TestInsertOrAssign() {
1088 static int constructorCounter = 0;
1089 static int assignmentCounter = 0;
1090
1091 struct TCountConstruct {
1092 explicit TCountConstruct(int v)
1093 : Value(v)
1094 {
1095 ++constructorCounter;
1096 }
1097
1098 TCountConstruct& operator=(int v) {
1099 Value = v;
1100 ++assignmentCounter;
1101 return *this;
1102 }
1103
1104 TCountConstruct(const TCountConstruct&) = delete;
1105 int Value;
1106 };
1107
1108 THashMap<int, TCountConstruct> hash;
1109 {
1110 auto r = hash.insert_or_assign(TNonCopyableInt<4>(4), 1);
1111 UNIT_ASSERT(r.second);
1112 UNIT_ASSERT_VALUES_EQUAL(1, hash.size());
1113 UNIT_ASSERT_VALUES_EQUAL(1, constructorCounter);
1114 UNIT_ASSERT_VALUES_EQUAL(0, assignmentCounter);
1115 UNIT_ASSERT_VALUES_EQUAL(1, r.first->second.Value);
1116 }
1117 {
1118 auto r = hash.insert_or_assign(TNonCopyableInt<4>(4), 5);
1119 UNIT_ASSERT(!r.second);
1120 UNIT_ASSERT_VALUES_EQUAL(1, hash.size());
1121 UNIT_ASSERT_VALUES_EQUAL(1, constructorCounter);
1122 UNIT_ASSERT_VALUES_EQUAL(1, assignmentCounter);
1123 UNIT_ASSERT_VALUES_EQUAL(5, r.first->second.Value);
1124 }
1125 {
1126 constexpr int iterations = 200;
1127 for (int iteration = 0; iteration < iterations; ++iteration) {
1128 hash.insert_or_assign(iteration, iteration);
1129 }
1130 UNIT_ASSERT_VALUES_EQUAL(iterations, hash.size());
1131 UNIT_ASSERT_VALUES_EQUAL(iterations, constructorCounter);
1132 UNIT_ASSERT_VALUES_EQUAL(2, assignmentCounter);
1133 UNIT_ASSERT_VALUES_EQUAL(4, hash.at(4).Value);
1134 UNIT_ASSERT_VALUES_EQUAL(44, hash.at(44).Value);
1135 }
1136}
1137
1138void THashTest::TestHMMapEmplace() {
1139 using hash_t = THashMultiMap<int, TNonCopyableInt<0>>;

Callers

nothing calls this directly

Calls 3

insert_or_assignMethod · 0.45
sizeMethod · 0.45
atMethod · 0.45

Tested by

no test coverage detected