38#include <gtest/gtest.h>
57namespace fs = std::filesystem;
71 return static_cast<long long>(
_getpid());
73 return static_cast<long long>(
getpid());
88 static std::atomic<unsigned long long>
counter{0};
89 const auto now = std::chrono::steady_clock::now().time_since_epoch().count();
90 const auto id = std::to_string(
now) +
"_" + std::to_string(process_id()) +
91 "_" + std::to_string(
counter++);
93 const fs::path dir = fs::temp_directory_path() /
"aleph_ringcache_tests";
94 fs::create_directories(dir);
95 return {dir / (
id +
".pars"), dir / (
id +
".cache")};
101 explicit TempFiles(TempPaths p) : paths(
std::move(p)) {}
105 fs::remove(paths.pars,
ec);
106 fs::remove(paths.cache,
ec);
114 for (
size_t i = 0; i < arr.
size(); ++i)
115 out.push_back(arr[i]);
138 int buf[4] = {0, 0, 0, 0};
140 EXPECT_EQ(std::vector<int>(buf, buf + 4), std::vector<int>({1, 2, 3, 4}));
165 for (
int v : {10, 20, 30, 40})
168 auto sub = cache.read_from(1, 2);
173 auto sub_ptr = cache.read_from(ptr, 3);
177 for (
auto it = cache.get_it(); it.has_curr(); it.next_ne())
178 iterated.push_back(it.get_curr_ne());
185 TempFiles
tmp(paths);
189 for (
int v : {1, 2, 3})
220 auto all = cache.read_all();
230 auto all = cache.read_all();
245 EXPECT_EQ(ptr.get_pos_respect_to_head(), 0u);
247 auto first_two = cache.read_from(ptr, 2);
251 EXPECT_EQ(ptr.get_pos_respect_to_head(), 1u);
254 EXPECT_EQ(ptr.get_pos_respect_to_head(), 0u);
261 auto wrapped = cache.read_from(head_ptr, 3);
265 for (
auto it = cache.get_it(); it.has_curr(); it.next_ne())
266 iterated.push_back(it.get_curr_ne());
273 TempFiles
tmp(paths);
277 cache.
init(
tmp.paths.pars.string());
320 for (
int i = 0; i < 4; ++i)
325 for (
int i = 0; i < 4; ++i)
449 cache.
init(
tmp.paths.pars.string());
462 for (
int i = 0; i < 5; ++i)
492 for (
int i = 0; i < 4; ++i)
498 auto ptr2 = ptr + (-1);
515 std::strncmp(tag,
other.tag, 8) == 0;
523 tmp.paths.cache.string(), 3);
526 TestRecord
r1{1, 3.14,
"alpha"};
527 TestRecord r2{2, 2.71,
"beta"};
528 TestRecord
r3{3, 1.41,
"gamma"};
590 TempFiles
tmp(paths);
bool operator==(const Time &l, const Time &r)
size_t size_t int32_t value
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Defines a pointer to a specific location in the cache.
Persistent ring buffer cache with file-backed storage.
T read(const Pointer &ptr)
Read the entry pointed to by ptr
T read_first()
Read the oldest entry in the cache.
Array< T > read_all()
Read all entries in insertion order.
bool is_empty() const noexcept
Returns true if the cache is empty.
Iterator get_it()
Returns a forward iterator starting at the oldest entry.
void clear() noexcept
Removes all entries from the cache.
bool put(const T &item)
Insert an item into the cache.
void init(const std::string &pars_fname)
Initialize a cache constructed with the default constructor.
size_t size() const noexcept
Returns the number of entries stored in the cache.
void empty() noexcept
Empties the cache; all entries are logically deleted.
size_t capacity() const noexcept
Returns the maximum capacity.
bool is_full() const noexcept
Returns true if the cache is full (no more entries can be inserted)
bool is_initialized() const
True if the cache has been initialized with a valid parameters file.
Array< T > read_from(const size_t pos, const size_t m)
Read up to m entries starting from the pos-th oldest position.
bool get(const size_t m=1) noexcept
Extracts (deletes) from the cache the m oldest inserted items.
void flush()
Flushes the current cache state to disk.
void close()
Flushes state and closes the underlying streams.
static void create(const std::string &pars_file_name, const std::string &cache_file_name, const size_t num_entries)
Create a brand new on-disk ring cache.
void resize(const size_t sz)
Resize the maximum capacity of the cache.
size_t avail() const noexcept
Returns the number of available entries.
T oldest()
Alias for read_first().
T read_last()
Read the youngest (most recently inserted) entry in the cache.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
std::vector< typename C::Item_Type > to_vector(const C &c)
Convert a container to a std::vector.
double sub(double a, double b)
File-backed ring buffer cache for persistent storage.