File indexing completed on 2024-04-28 16:08:40
0001 /*************************************************************************** 0002 * Copyright (C) 2013 by Linuxstopmotion contributors; * 0003 * see the AUTHORS file for details. * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 0021 #include "tcache.h" 0022 0023 #include "src/presentation/loadcache.h" 0024 0025 #include <QtTest/QtTest> 0026 0027 namespace { 0028 const char* TestPath1 = "1"; 0029 const char* TestPath2 = "2"; 0030 const char* TestPath3 = "3"; 0031 const char* TestPath4 = "4"; 0032 } 0033 0034 const char* TestLoader::lastFreed; 0035 const char* TestLoader::lastLoaded; 0036 int TestLoader::freeCount; 0037 0038 TestCache::TestCache() : cache(0) { 0039 cache = new LoadCache<TestLoader>(3); 0040 } 0041 0042 TestCache::~TestCache() { 0043 delete cache; 0044 } 0045 0046 void TestCache::gettingTwiceReturnsSameInstance() { 0047 cache->clear(); 0048 TestLoader::lastLoaded = 0; 0049 const char* r = cache->get(TestPath1); 0050 QVERIFY2(TestLoader::lastLoaded == TestPath1, "Did not load"); 0051 QVERIFY2(r == TestPath1, "Did not get the correct value"); 0052 TestLoader::lastLoaded = 0; 0053 r = cache->get(TestPath1); 0054 QVERIFY2(TestLoader::lastLoaded == 0, 0055 "Loaded item that should have been cache-resident"); 0056 QVERIFY2(r == TestPath1, "Did not get the correct value"); 0057 } 0058 0059 void TestCache::leastRecentlyUsedIsFreed() { 0060 cache->clear(); 0061 cache->get(TestPath1); 0062 cache->get(TestPath2); 0063 cache->get(TestPath3); 0064 TestLoader::lastFreed = 0; 0065 cache->get(TestPath4); 0066 QVERIFY2(TestLoader::lastFreed == TestPath1, 0067 "Least Recently Used not freed"); 0068 TestLoader::lastFreed = 0; 0069 cache->get(TestPath2); 0070 QVERIFY2(TestLoader::lastFreed == 0, 0071 "Freed when requested item should have been cache-resident"); 0072 TestLoader::lastFreed = 0; 0073 cache->get(TestPath1); 0074 QVERIFY2(TestLoader::lastFreed == TestPath3, 0075 "Least Recently Used not freed"); 0076 } 0077 0078 void TestCache::clearFrees() { 0079 cache->clear(); 0080 cache->get(TestPath1); 0081 cache->get(TestPath2); 0082 cache->get(TestPath3); 0083 TestLoader::freeCount = 0; 0084 cache->clear(); 0085 QCOMPARE(TestLoader::freeCount, 3); 0086 cache->get(TestPath2); 0087 TestLoader::lastFreed = 0; 0088 TestLoader::freeCount = 0; 0089 cache->clear(); 0090 QCOMPARE(TestLoader::freeCount, 1); 0091 QVERIFY2(TestLoader::lastFreed == TestPath2, 0092 "clear() did not free resident item"); 0093 } 0094 0095 void TestCache::dropFrees() { 0096 cache->clear(); 0097 cache->get(TestPath1); 0098 cache->get(TestPath2); 0099 cache->get(TestPath3); 0100 TestLoader::lastFreed = 0; 0101 TestLoader::freeCount = 0; 0102 cache->drop(TestPath2); 0103 QCOMPARE(TestLoader::freeCount, 1); 0104 QVERIFY2(TestLoader::lastFreed == TestPath2, 0105 "drop() did not free the correct item"); 0106 TestLoader::lastFreed = 0; 0107 TestLoader::freeCount = 0; 0108 cache->drop(TestPath2); 0109 QCOMPARE(TestLoader::freeCount, 0); 0110 } 0111 0112 void TestCache::droppedItemMustBeReloaded() { 0113 cache->clear(); 0114 cache->get(TestPath1); 0115 cache->get(TestPath2); 0116 cache->get(TestPath3); 0117 cache->drop(TestPath2); 0118 TestLoader::lastLoaded = 0; 0119 const char* r = cache->get(TestPath2); 0120 QVERIFY2(TestLoader::lastLoaded == TestPath2, 0121 "Did not load previously dropped item"); 0122 QVERIFY2(r == TestPath2, "Did not get the correct value"); 0123 }