File indexing completed on 2024-04-28 03:45:19
0001 /* 0002 SPDX-FileCopyrightText: 2021 Valentin Boettcher <hiro at protagon.space; @hiro98:tchncs.de> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include <QTest> 0008 #include <QtConcurrent/QtConcurrentRun> 0009 #include <qtestcase.h> 0010 #include "trixelcache.h" 0011 0012 using TestCache = TrixelCache<std::vector<int>>; 0013 class TestTrixelCache : public QObject 0014 { 0015 Q_OBJECT 0016 private: 0017 TestCache m_cache{ 10, 5 }; 0018 private slots: 0019 void init() 0020 { 0021 // a fresh cache for each test 0022 m_cache = { 10, 5 }; 0023 }; 0024 0025 void noop() 0026 { 0027 m_cache = { 10, 10 }; 0028 QVERIFY2(m_cache.noop(), "Is the constructed trixel cache a noop?"); 0029 0030 m_cache.set_size(5); 0031 QVERIFY2(!m_cache.noop(), "Is the cache no noop after resize?"); 0032 0033 m_cache.set_size(10); 0034 QVERIFY2(m_cache.noop(), "Is the cache no noop again after resize?"); 0035 } 0036 0037 void resize() 0038 { 0039 QVERIFY_EXCEPTION_THROWN(m_cache.set_size(20), std::range_error); 0040 m_cache.set_size(1); 0041 QCOMPARE(m_cache.size(), 1); 0042 0043 m_cache.set_size(2); 0044 QCOMPARE(m_cache.size(), 2); 0045 }; 0046 0047 void setting() 0048 { 0049 QCOMPARE(m_cache.current_usage(), 0); 0050 0051 m_cache[0] = { 1, 2, 3 }; 0052 QCOMPARE(m_cache.current_usage(), 1); 0053 QCOMPARE(m_cache.primed_indices(), std::list<size_t>{ 0 }); 0054 QVERIFY(m_cache[0].is_set()); 0055 0056 m_cache[2] = { 1, 2, 3 }; 0057 QCOMPARE(m_cache.current_usage(), 2); 0058 QCOMPARE(m_cache.primed_indices(), (std::list<size_t>{ 2, 0 })); 0059 QVERIFY(m_cache[2].is_set()); 0060 }; 0061 0062 void pruning() 0063 { 0064 m_cache = { 10, 1 }; 0065 m_cache[1] = { 4, 5, 6 }; 0066 m_cache[0] = { 1, 2, 3 }; 0067 0068 QVERIFY2(m_cache[1].is_set(), "Index 1 should be set."); 0069 QVERIFY2(m_cache[0].is_set(), "Index 0 should be set."); 0070 0071 m_cache.prune(); 0072 0073 QVERIFY2(!m_cache[1].is_set(), "Index 1 should be cleared."); 0074 QCOMPARE(m_cache[1].data(), std::vector<int>{}); 0075 QVERIFY2(m_cache[0].is_set(), "Index 0 should be set."); 0076 QCOMPARE(m_cache[0].data(), (std::vector<int>{ 1, 2, 3 })); 0077 0078 m_cache[0] = { 1, 2, 3 }; 0079 m_cache[1] = { 4, 5, 6 }; 0080 m_cache.prune(2); 0081 0082 QVERIFY2(m_cache[1].is_set(), "Index 1 should be set."); 0083 QVERIFY2(m_cache[0].is_set(), "Index 0 should be set."); 0084 }; 0085 0086 void clear() 0087 { 0088 m_cache = { 2, 1 }; 0089 m_cache[1] = { 4, 5, 6 }; 0090 m_cache[0] = { 1, 2, 3 }; 0091 0092 QVERIFY2(m_cache[1].is_set(), "Index 1 should be set."); 0093 QVERIFY2(m_cache[0].is_set(), "Index 0 should be set."); 0094 0095 m_cache.clear(); 0096 0097 QCOMPARE(m_cache.current_usage(), 0); 0098 QCOMPARE(m_cache.primed_indices(), {}); 0099 0100 QVERIFY2(!m_cache[1].is_set(), "Index 1 should be cleared."); 0101 QCOMPARE(m_cache[1].data(), std::vector<int>{}); 0102 QVERIFY2(!m_cache[0].is_set(), "Index 0 should be cleared."); 0103 QCOMPARE(m_cache[0].data(), std::vector<int>{}); 0104 }; 0105 }; 0106 0107 QTEST_GUILESS_MAIN(TestTrixelCache);