Audacity 3.2.0
GraphicsDataCacheTests.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 GraphicsDataCacheTests.cpp
7
8 Dmitry Vedenko
9 **********************************************************************/
10
11#include <catch2/catch.hpp>
12
13#include <string>
14#include <string_view>
15#include <type_traits>
16
17#include "GraphicsDataCache.h"
18#include "ZoomInfo.h"
19
20namespace
21{
23{
25 {
26 Key = key;
27 return *this;
28 }
29
30 void Dispose() override
31 {
32 Key = {};
33 }
34
36};
37
38void CheckCacheElementLookup(GraphicsDataCache<CacheElement>& cache, const ZoomInfo& zoomInfo, double t0, double t1, size_t count)
39{
40 auto range = cache.PerformLookup(zoomInfo, t0, t1);
41
42 REQUIRE(count == range.size());
43
44 if (count == 0)
45 return;
46
47 const size_t t0Pixel = zoomInfo.TimeToPosition(t0);
48 const size_t t0Bin = t0Pixel / GraphicsDataCacheBase::CacheElementWidth;
49
50 const size_t leftOffset =
52
53 const size_t rightOffset =
55 zoomInfo.TimeToPosition(t1) - 1;
56
57 const auto age = range.first->LastCacheAccess;
58
59 if (count == 1)
60 {
61 REQUIRE(range.first.GetLeftOffset() == leftOffset);
62 REQUIRE(range.first.GetRightOffset() == rightOffset);
63 }
64 else
65 {
66 REQUIRE(range.first.GetLeftOffset() == leftOffset);
67 REQUIRE(range.first.GetRightOffset() == 0);
68
69 auto it = range.first;
70 ++it;
71
72 while (--count > 1)
73 {
74 REQUIRE(it.GetLeftOffset() == 0);
75 REQUIRE(it.GetRightOffset() == 0);
76 REQUIRE(it->LastCacheAccess == age);
77
78 ++it;
79 }
80
81 REQUIRE(it.GetLeftOffset() == 0);
82 REQUIRE(it.GetRightOffset() == rightOffset);
83 REQUIRE(it->LastCacheAccess == age);
84 }
85}
86} // namespace
87
88TEST_CASE("graphics-data-cache", "")
89{
91
92 GraphicsDataCache<CacheElement> cache(44100, [](){ return std::make_unique<CacheElement>(); });
93
94 REQUIRE(cache.PerformLookup(info, 0, 0).empty());
95 REQUIRE(!cache.PerformLookup(info, 0, 1).empty());
96
97 REQUIRE(cache.PerformLookup(info, 0, 1).first->LastCacheAccess == 2);
98
99 CheckCacheElementLookup(cache, info, 0.0, 1.0, 1);
100 CheckCacheElementLookup(cache, info, 0.0, 2.0, 1);
101 CheckCacheElementLookup(cache, info, 0.0, 3.0, 2);
102 CheckCacheElementLookup(cache, info, 0.0, 4.0, 2);
103 CheckCacheElementLookup(cache, info, 0.0, 5.0, 2);
104 CheckCacheElementLookup(cache, info, 0.0, 6.0, 3);
105
106 CheckCacheElementLookup(cache, info, 0.5, 1.0, 1);
107
108 for (int i = 0; i < 128; ++i)
109 {
110 const double t0 = i;
111 const double t1 = t0 + 2;
112
113 const int64_t itemsCount = 1 +
116
117 CheckCacheElementLookup(cache, info, t0, t1, itemsCount);
118 }
119}
TEST_CASE("graphics-data-cache", "")
static const AudacityProject::AttachedObjects::RegisteredFactory key
static constexpr uint32_t CacheElementWidth
IteratorRange< GraphicsDataCacheIterator< CacheElementType > > PerformLookup(const ZoomInfo &zoomInfo, double t0, double t1)
static double GetDefaultZoom()
Definition: ZoomInfo.h:116
int64 TimeToPosition(double time, int64 origin=0, bool ignoreFisheye=false) const
STM: Converts a project time to screen x position.
Definition: ZoomInfo.cpp:44
void CheckCacheElementLookup(GraphicsDataCache< CacheElement > &cache, const ZoomInfo &zoomInfo, double t0, double t1, size_t count)
A base class for the for cache elements.
A key into the graphics data cache.
void Dispose() override
This method is called when the item is evicted from the cache. Default implementation is empty.