| 107 | } |
| 108 | |
| 109 | void TestIndexedString::testSwap() |
| 110 | { |
| 111 | QFETCH(bool, lhsRcEnabled); |
| 112 | QFETCH(bool, rhsRcEnabled); |
| 113 | |
| 114 | class OptionallyRcString |
| 115 | { |
| 116 | public: |
| 117 | explicit OptionallyRcString(bool rcEnabled, const QString& initText) |
| 118 | : m_rcEnabled{rcEnabled} |
| 119 | , m_initText{initText} |
| 120 | , m_notRefCounted{m_initText} |
| 121 | , m_refCountedData{} |
| 122 | , m_rcEnabler{m_refCountedData, sizeof(IndexedString)} |
| 123 | , m_string{m_rcEnabled ? new (m_refCountedData) IndexedString(m_initText) : &m_notRefCounted} |
| 124 | { |
| 125 | QCOMPARE(m_string->str(), m_initText); |
| 126 | } |
| 127 | |
| 128 | ~OptionallyRcString() |
| 129 | { |
| 130 | if (m_rcEnabled) { |
| 131 | m_string->~IndexedString(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | const QString& initText() const { return m_initText; } |
| 136 | IndexedString& string() { return *m_string; } |
| 137 | |
| 138 | private: |
| 139 | const bool m_rcEnabled; |
| 140 | const QString m_initText; |
| 141 | |
| 142 | IndexedString m_notRefCounted; |
| 143 | std::byte m_refCountedData[sizeof(IndexedString)]; |
| 144 | const DUChainReferenceCountingEnabler m_rcEnabler; |
| 145 | |
| 146 | IndexedString* const m_string = nullptr; |
| 147 | }; |
| 148 | |
| 149 | OptionallyRcString lhs(lhsRcEnabled, QStringLiteral("1st text")); |
| 150 | OptionallyRcString rhs(rhsRcEnabled, QStringLiteral("another string")); |
| 151 | |
| 152 | using std::swap; |
| 153 | |
| 154 | swap(lhs.string(), rhs.string()); |
| 155 | QCOMPARE(lhs.string().str(), rhs.initText()); |
| 156 | QCOMPARE(rhs.string().str(), lhs.initText()); |
| 157 | |
| 158 | swap(lhs.string(), rhs.string()); |
| 159 | QCOMPARE(lhs.string().str(), lhs.initText()); |
| 160 | QCOMPARE(rhs.string().str(), rhs.initText()); |
| 161 | } |
| 162 | |
| 163 | void TestIndexedString::testSwap_data() |
| 164 | { |