kshareddatacachetest.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. This file is part of the KDE libraries
  3. SPDX-FileCopyrightText: 2012 David Faure <faure@kde.org>
  4. SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
  5. */
  6. #include <kshareddatacache.h>
  7. #include <QStandardPaths>
  8. #include <QTest>
  9. #include <QObject>
  10. #include <QStandardPaths>
  11. #include <QString>
  12. #include <string.h> // strcpy
  13. class KSharedDataCacheTest : public QObject
  14. {
  15. Q_OBJECT
  16. private Q_SLOTS:
  17. void initTestCase();
  18. void simpleInsert();
  19. };
  20. void KSharedDataCacheTest::initTestCase()
  21. {
  22. }
  23. void KSharedDataCacheTest::simpleInsert()
  24. {
  25. const QLatin1String cacheName("myTestCache");
  26. const QLatin1String key("mypic");
  27. // clear the cache
  28. QString cacheFile = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/") + cacheName + QLatin1String(".kcache");
  29. QFile file(cacheFile);
  30. if (file.exists()) {
  31. QVERIFY(file.remove());
  32. }
  33. // insert something into it
  34. KSharedDataCache cache(cacheName, 5 * 1024 * 1024);
  35. #ifndef Q_OS_WIN // the windows implementation is currently only memory based and not really shared
  36. QVERIFY(file.exists()); // make sure we got the cache filename right
  37. #endif
  38. QByteArray data;
  39. data.resize(9228);
  40. strcpy(data.data(), "Hello world");
  41. QVERIFY(cache.insert(key, data));
  42. // read it out again
  43. QByteArray result;
  44. QVERIFY(cache.find(key, &result));
  45. QCOMPARE(result, data);
  46. // another insert
  47. strcpy(data.data(), "Hello KDE");
  48. QVERIFY(cache.insert(key, data));
  49. // and another read
  50. QVERIFY(cache.find(key, &result));
  51. QCOMPARE(result, data);
  52. }
  53. QTEST_MAIN(KSharedDataCacheTest)
  54. #include "kshareddatacachetest.moc"