Files

159 lines
5.0 KiB
C++
Raw Permalink Normal View History

2019-04-01 18:34:39 +02:00
/*
* This file is part of the Flowee project
2021-06-20 22:44:44 +02:00
* Copyright (C) 2019 Tom Zander <tom@flowee.org>
2019-04-01 18:34:39 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "test_hashstorage.h"
#include "../../indexer/HashStorage.h"
#include "../../indexer/HashStorage_p.h"
2019-04-01 18:34:39 +02:00
#include <utiltime.h>
class OpenHashStorage {
public:
HashStoragePrivate *d;
};
2019-04-01 18:34:39 +02:00
void TestHashStorage::init()
{
m_testPath = strprintf("test_flowee_%lu", (unsigned long)GetTime());
boost::filesystem::remove_all(m_testPath);
boost::filesystem::create_directories(m_testPath);
}
void TestHashStorage::cleanup()
{
boost::filesystem::remove_all(m_testPath);
}
void TestHashStorage::basic()
{
uint256 hash1 = uint256S("00001e397a22a7262111111111111ae899550d85ae9cb4ac3145");
uint256 hash2 = uint256S("5123d8a19c8815f9311111111111195cd63abc796289ee790013");
uint256 hash3 = uint256S("00001e397a22a7262111111111111ae899450d85ae9cb4ac3155");
2019-04-01 18:34:39 +02:00
HashIndexPoint index1, index2, index3;
{
HashStorage hs(m_testPath);
index1 = hs.append(hash1);
index2 = hs.append(hash2);
QCOMPARE(index1.db, index2.db);
QVERIFY(index1.row != index2.row);
}
{
HashStorage hs(m_testPath);
2019-04-05 13:50:41 +02:00
QCOMPARE(hash1, hs.find(index1));
QCOMPARE(hash2, hs.find(index2));
2019-04-01 18:34:39 +02:00
index3 = hs.append(hash3);
2019-04-05 13:50:41 +02:00
QCOMPARE(hash1, hs.find(index1));
QCOMPARE(hash2, hs.find(index2));
QCOMPARE(hash3, hs.find(index3));
2019-04-01 18:34:39 +02:00
}
{
HashStorage hs(m_testPath);
2019-04-08 13:19:54 +02:00
QCOMPARE(hs.find(index1), hash1);
QCOMPARE(hs.find(index2), hash2);
QCOMPARE(hs.find(index3), hash3);
2019-04-01 18:34:39 +02:00
2019-04-08 13:19:54 +02:00
QCOMPARE(index1, hs.lookup(hash1));
2019-04-01 21:15:24 +02:00
hs.finalize();
2019-04-08 13:19:54 +02:00
QCOMPARE(hs.find(index1), hash1);
QCOMPARE(hs.find(index2), hash2);
QCOMPARE(hs.find(index3), hash3);
2019-04-01 21:15:24 +02:00
2019-04-05 13:50:41 +02:00
QCOMPARE(hs.lookup(hash1), index1);
2019-04-01 21:15:24 +02:00
}
2019-04-01 22:53:39 +02:00
uint256 hash4 = uint256S("0e3e2357e806b6cdb1111111111111f70b54c3a3a17b6714ee1f");
2019-04-01 22:53:39 +02:00
HashIndexPoint index4;
2019-04-01 21:15:24 +02:00
{
HashStorage hs(m_testPath);
2019-04-08 13:19:54 +02:00
QCOMPARE(hs.find(index1), hash1);
QCOMPARE(hs.find(index2), hash2);
QCOMPARE(hs.find(index3), hash3);
2019-04-05 13:50:41 +02:00
QCOMPARE(hs.lookup(hash1), index1);
2019-04-01 21:15:24 +02:00
// insert more and finalize again.
2019-04-01 22:53:39 +02:00
index4 = hs.append(hash4);
2019-04-08 13:19:54 +02:00
QCOMPARE(hs.find(index1), hash1);
QCOMPARE(hs.find(index2), hash2);
QCOMPARE(hs.find(index3), hash3);
QCOMPARE(hs.find(index4), hash4);
2019-04-05 13:50:41 +02:00
QCOMPARE(hs.lookup(hash3), index3);
2019-04-01 22:53:39 +02:00
// second round.
hs.finalize();
2019-04-08 13:19:54 +02:00
QCOMPARE(hs.find(index1), hash1);
QCOMPARE(hs.find(index2), hash2);
QCOMPARE(hs.find(index3), hash3);
QCOMPARE(hs.find(index4), hash4);
2019-04-05 13:50:41 +02:00
QCOMPARE(hs.lookup(hash3), index3);
2019-04-02 12:07:08 +02:00
}
2019-04-01 22:53:39 +02:00
{
HashStorage hs(m_testPath);
2019-04-05 13:50:41 +02:00
QCOMPARE(hash1, hs.find(index1));
QCOMPARE(hash2, hs.find(index2));
QCOMPARE(hash3, hs.find(index3));
QCOMPARE(hash4, hs.find(index4));
QCOMPARE(hs.lookup(hash1), index1);
2019-04-01 18:34:39 +02:00
}
}
2019-04-03 18:55:33 +02:00
void TestHashStorage::multipleDbs()
{
uint256 hash1 = uint256S("00001e397a22a7261111111111112ae899550d85ae9cb4ac3145");
uint256 hash2 = uint256S("5123d8a19c8815f9111111111111395cd63abc796289ee790013");
uint256 hash3 = uint256S("00001e397a22a7261111111111112ae899450d85ae9cb4ac3155");
uint256 hash4 = uint256S("0e3e2357e806b6cd111111111111b1f70b54c3a3a17b6714ee1f");
2019-04-09 11:19:03 +02:00
{
HashStorage hs(m_testPath);
HashStoragePrivate *d = reinterpret_cast<OpenHashStorage*>(&hs)->d;
Q_ASSERT(d);
QCOMPARE(d->dbs.length(), 1);
hs.append(hash1);
hs.append(hash2);
hs.append(hash3);
QCOMPARE(d->dbs.size(), 1);
auto db = d->dbs.first();
QCOMPARE(db->m_parts.size(), 0);
db->stabilize();
QCOMPARE(db->m_parts.size(), 1);
hs.append(hash4);
QCOMPARE(hs.lookup(hash1).db, 0);
QCOMPARE(hs.lookup(hash2).db, 0);
QCOMPARE(hs.lookup(hash3).db, 0);
QCOMPARE(hs.lookup(hash4).db, 0);
}
HashStorage hs(m_testPath);
2019-04-09 11:19:03 +02:00
QCOMPARE(hs.lookup(hash1).db, 0);
QCOMPARE(hs.lookup(hash2).db, 0);
QCOMPARE(hs.lookup(hash3).db, 0);
QCOMPARE(hs.lookup(hash4).db, 0);
hs.finalize();
HashStoragePrivate *d = reinterpret_cast<OpenHashStorage*>(&hs)->d;
Q_ASSERT(d);
2019-04-09 11:19:03 +02:00
QCOMPARE(d->dbs.length(), 2);
for (auto db : d->dbs) {
QCOMPARE(db->m_parts.size(), 0);
}
2019-04-01 18:34:39 +02:00
}
QTEST_MAIN(TestHashStorage)