diff options
Diffstat (limited to 'runtime/base/variant_map_test.cc')
-rw-r--r-- | runtime/base/variant_map_test.cc | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/runtime/base/variant_map_test.cc b/runtime/base/variant_map_test.cc index 827de46249..f306a48e53 100644 --- a/runtime/base/variant_map_test.cc +++ b/runtime/base/variant_map_test.cc @@ -38,10 +38,12 @@ namespace { static const Key<int> Apple; static const Key<double> Orange; + static const Key<std::string> Label; }; const FruitMap::Key<int> FruitMap::Apple; const FruitMap::Key<double> FruitMap::Orange; + const FruitMap::Key<std::string> FruitMap::Label; } // namespace TEST(VariantMaps, BasicReadWrite) { @@ -67,6 +69,7 @@ TEST(VariantMaps, BasicReadWrite) { EXPECT_DOUBLE_EQ(555.0, *fm.Get(FruitMap::Orange)); EXPECT_EQ(size_t(2), fm.Size()); + // Simple remove fm.Remove(FruitMap::Apple); EXPECT_FALSE(fm.Exists(FruitMap::Apple)); @@ -75,6 +78,24 @@ TEST(VariantMaps, BasicReadWrite) { EXPECT_FALSE(fm.Exists(FruitMap::Orange)); } +TEST(VariantMaps, SetPreviousValue) { + FruitMap fm; + + // Indirect remove by setting yourself again + fm.Set(FruitMap::Label, std::string("hello_world")); + auto* ptr = fm.Get(FruitMap::Label); + ASSERT_TRUE(ptr != nullptr); + *ptr = "foobar"; + + // Set the value to the same exact pointer which we got out of the map. + // This should cleanly 'just work' and not try to delete the value too early. + fm.Set(FruitMap::Label, *ptr); + + auto* new_ptr = fm.Get(FruitMap::Label); + ASSERT_TRUE(ptr != nullptr); + EXPECT_EQ(std::string("foobar"), *new_ptr); +} + TEST(VariantMaps, RuleOfFive) { // Test empty constructor FruitMap fmEmpty; |