Files
swift-mirror/unittests/Basic/MultiMapCacheTest.cpp
Michael Gottesman 00d4576977 [multimapcache] Add an efficient CRTP based write-once multimap cache that can be small.
The properties of this multimap cache are:

1. Values are stored (inline if Small) in a Vector and our map internally maps
   keys to (start, length) of slices of the Vector. This is done instead of
   storing arrays refs to ensure that if our array goes from small -> large, we
   do not have stale pointers.

2. Values are only allowed to be inserted all at once. This is ok, since this is
   a cache.

3. One is not storing individual small vectors in a map (or state storing
   SmallVectors). This can inadvertantly add up to using a lot of memory and is
   not needed for homogenous data.
2020-03-31 15:12:37 -07:00

62 lines
1.7 KiB
C++

//===--- MultiMapCacheTest.cpp --------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/MultiMapCache.h"
#include "swift/Basic/Range.h"
#include "gtest/gtest.h"
#include <random>
using namespace swift;
namespace {
/// A multimap cache that caches the initial 4 powers of each key.
struct PowerMultiMapCache
: public MultiMapCache<PowerMultiMapCache, unsigned, unsigned> {
bool constructValuesForKey(unsigned key, std::vector<unsigned> &data) {
// Construct the first 3 powers of key.
data.push_back(key);
data.push_back(key * key);
data.push_back(key * key * key);
return true;
}
};
} // end anonymous namespace
TEST(MultiMapCache, powersTest) {
PowerMultiMapCache cache;
EXPECT_TRUE(cache.empty());
EXPECT_EQ(cache.size(), 0u);
for (unsigned index : range(1, 256)) {
auto array = *cache.get(index);
for (unsigned power : array) {
EXPECT_EQ(power % index, 0);
}
}
EXPECT_FALSE(cache.empty());
EXPECT_EQ(cache.size(), 255);
for (unsigned index : range(1, 256)) {
auto array = *cache.get(index);
for (unsigned power : array) {
EXPECT_EQ(power % index, 0);
}
}
EXPECT_FALSE(cache.empty());
EXPECT_EQ(cache.size(), 255);
cache.clear();
EXPECT_TRUE(cache.empty());
EXPECT_EQ(cache.size(), 0);
}