|
| 1 | +#include <TfTest/TfTest> |
| 2 | +#include "tsharedmemory.h" |
| 3 | +#include "tglobal.h" |
| 4 | +#include <iostream> |
| 5 | + |
| 6 | + |
| 7 | +namespace { |
| 8 | +TSharedMemory sharedMomory("testshared.shm"); |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +class TestSharedMemory : public QObject |
| 13 | +{ |
| 14 | + Q_OBJECT |
| 15 | +private slots: |
| 16 | + void initTestCase(); |
| 17 | + void cleanupTestCase(); |
| 18 | + |
| 19 | + void attach(); |
| 20 | + void test1(); |
| 21 | + void test2_data(); |
| 22 | + void test2(); |
| 23 | +}; |
| 24 | + |
| 25 | + |
| 26 | +static QByteArray randomString(int length) |
| 27 | +{ |
| 28 | + constexpr auto ch = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-+/^=_[]@:;!#$%()~? \t\n"; |
| 29 | + QByteArray ret; |
| 30 | + int max = (int)strlen(ch) - 1; |
| 31 | + |
| 32 | + for (int i = 0; i < length; ++i) { |
| 33 | + ret += ch[Tf::random(max)]; |
| 34 | + } |
| 35 | + return ret; |
| 36 | +} |
| 37 | + |
| 38 | +void TestSharedMemory::initTestCase() |
| 39 | +{ |
| 40 | + sharedMomory.create(100 * 1024 * 1024); |
| 41 | +} |
| 42 | + |
| 43 | +void TestSharedMemory::cleanupTestCase() |
| 44 | +{ |
| 45 | + sharedMomory.unlink(); |
| 46 | +} |
| 47 | + |
| 48 | +void TestSharedMemory::attach() |
| 49 | +{ |
| 50 | + bool res = sharedMomory.attach(); |
| 51 | + Q_ASSERT(res); |
| 52 | + Tf::msleep(1000); |
| 53 | + res = sharedMomory.detach(); |
| 54 | + Q_ASSERT(res); |
| 55 | + Tf::msleep(1000); |
| 56 | + res = sharedMomory.attach(); |
| 57 | + Q_ASSERT(res); |
| 58 | + res = sharedMomory.detach(); |
| 59 | + Q_ASSERT(res); |
| 60 | +} |
| 61 | + |
| 62 | +void TestSharedMemory::test1() |
| 63 | +{ |
| 64 | + sharedMomory.attach(); |
| 65 | + QVERIFY(sharedMomory.size() == 100 * 1024 * 1024); |
| 66 | + |
| 67 | + // Lock and unlock |
| 68 | + auto ptr = sharedMomory.data(); |
| 69 | + QVERIFY(ptr != nullptr); |
| 70 | + bool res = sharedMomory.lockForRead(); |
| 71 | + Q_ASSERT(res); |
| 72 | + res = sharedMomory.lockForWrite(); |
| 73 | + Q_ASSERT(!res); |
| 74 | + res = sharedMomory.unlock(); |
| 75 | + Q_ASSERT(res); |
| 76 | + res = sharedMomory.lockForWrite(); |
| 77 | + Q_ASSERT(res); |
| 78 | + res = sharedMomory.unlock(); |
| 79 | + Q_ASSERT(res); |
| 80 | + QVERIFY(ptr == sharedMomory.data()); |
| 81 | +} |
| 82 | + |
| 83 | +void TestSharedMemory::test2_data() |
| 84 | +{ |
| 85 | + QTest::addColumn<QByteArray>("string"); |
| 86 | + |
| 87 | + QTest::newRow("1") << QUuid::createUuid().toByteArray(); |
| 88 | + QTest::newRow("2") << QUuid::createUuid().toByteArray(); |
| 89 | + QTest::newRow("3") << randomString(128); |
| 90 | + QTest::newRow("4") << randomString(256); |
| 91 | + QTest::newRow("5") << randomString(1024); |
| 92 | +} |
| 93 | + |
| 94 | +void TestSharedMemory::test2() |
| 95 | +{ |
| 96 | + QFETCH(QByteArray, string); |
| 97 | + |
| 98 | + bool res = sharedMomory.attach(); |
| 99 | + Q_ASSERT(res); |
| 100 | + auto ptr = sharedMomory.data(); |
| 101 | + res = sharedMomory.lockForWrite(); |
| 102 | + Q_ASSERT(res); |
| 103 | + std::memcpy(ptr, string.data(), string.length()); |
| 104 | + sharedMomory.unlock(); |
| 105 | + res = sharedMomory.detach(); |
| 106 | + Q_ASSERT(res); |
| 107 | +} |
| 108 | + |
| 109 | +TF_TEST_MAIN(TestSharedMemory) |
| 110 | +#include "sharedmemory.moc" |
0 commit comments