Debian Codegraph @

libraries/lib-wave-track-paint/tests/GraphicsDataCacheTests.cpp

raw
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
20 namespace
21 {
22 struct CacheElement : GraphicsDataCacheElementBase
23 {
24 CacheElement& operator=(GraphicsDataCacheKey key)
25 {
26 Key = key;
27 return *this;
28 }
29
30 void Dispose() override
31 {
32 Key = {};
33 }
34
35 GraphicsDataCacheKey Key;
36 };
37
38 void 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 =
51 t0Pixel - t0Bin * GraphicsDataCacheBase::CacheElementWidth;
52
53 const size_t rightOffset =
54 (t0Bin + count) * GraphicsDataCacheBase::CacheElementWidth -
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
88 TEST_CASE("graphics-data-cache", "")
89 {
90 ZoomInfo info(0.0, ZoomInfo::GetDefaultZoom());
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 +
114 info.TimeToPosition(t1) / GraphicsDataCacheBase::CacheElementWidth -
115 info.TimeToPosition(t0) / GraphicsDataCacheBase::CacheElementWidth;
116
117 CheckCacheElementLookup(cache, info, t0, t1, itemsCount);
118 }
119 }