| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <qtest.h> |
| 30 | #include <QtScript> |
| 31 | |
| 32 | class tst_QScriptValueIterator : public QObject |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | |
| 36 | public: |
| 37 | tst_QScriptValueIterator(); |
| 38 | virtual ~tst_QScriptValueIterator(); |
| 39 | |
| 40 | void dataHelper(); |
| 41 | |
| 42 | private slots: |
| 43 | void init(); |
| 44 | void cleanup(); |
| 45 | |
| 46 | void hasNextAndNext(); |
| 47 | |
| 48 | void constructAndNext_data(); |
| 49 | void constructAndNext(); |
| 50 | |
| 51 | void name_data(); |
| 52 | void name(); |
| 53 | void scriptName_data(); |
| 54 | void scriptName(); |
| 55 | |
| 56 | void value_data(); |
| 57 | void value(); |
| 58 | void setValue_data(); |
| 59 | void setValue(); |
| 60 | |
| 61 | void flags(); |
| 62 | |
| 63 | void iterateArrayAndConvertNameToIndex(); |
| 64 | void iterateArrayAndDoubleElements(); |
| 65 | void iterateArrayAndRemoveAllElements(); |
| 66 | }; |
| 67 | |
| 68 | tst_QScriptValueIterator::tst_QScriptValueIterator() |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | tst_QScriptValueIterator::~tst_QScriptValueIterator() |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | void tst_QScriptValueIterator::init() |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | void tst_QScriptValueIterator::cleanup() |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | void tst_QScriptValueIterator::dataHelper() |
| 85 | { |
| 86 | QTest::addColumn<QString>("code"); |
| 87 | QTest::newRow("{ foo: 123 }") << QString::fromLatin1("({ foo: 123 })"); |
| 88 | QTest::newRow("Math") << QString::fromLatin1("Math"); |
| 89 | QTest::newRow("Array.prototype") << QString::fromLatin1("Array.prototype"); |
| 90 | QTest::newRow("Global Object") << QString::fromLatin1("this"); |
| 91 | QTest::newRow("['foo']") << QString::fromLatin1("['foo']"); |
| 92 | QTest::newRow("array with 1000 elements") |
| 93 | << QString::fromLatin1("(function() {" |
| 94 | " var a = new Array;" |
| 95 | " for (i = 0; i < 1000; ++i)" |
| 96 | " a[i] = i;" |
| 97 | " return a;" |
| 98 | "})()"); |
| 99 | } |
| 100 | |
| 101 | void tst_QScriptValueIterator::hasNextAndNext() |
| 102 | { |
| 103 | QScriptEngine engine; |
| 104 | QScriptValue object = engine.newObject(); |
| 105 | for (int i = 0; i < 2000; ++i) |
| 106 | object.setProperty(i, i); |
| 107 | QScriptValueIterator it(object); |
| 108 | QBENCHMARK { |
| 109 | for (int i = 0; i < 1000; ++i) { |
| 110 | it.toFront(); |
| 111 | while (it.hasNext()) |
| 112 | it.next(); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void tst_QScriptValueIterator::constructAndNext_data() |
| 118 | { |
| 119 | dataHelper(); |
| 120 | } |
| 121 | |
| 122 | void tst_QScriptValueIterator::constructAndNext() |
| 123 | { |
| 124 | QFETCH(QString, code); |
| 125 | QScriptEngine engine; |
| 126 | QScriptValue object = engine.evaluate(code); |
| 127 | Q_ASSERT(object.isObject()); |
| 128 | |
| 129 | QBENCHMARK { |
| 130 | for (int i = 0; i < 100; ++i) { |
| 131 | QScriptValueIterator it(object); |
| 132 | it.next(); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void tst_QScriptValueIterator::name_data() |
| 138 | { |
| 139 | dataHelper(); |
| 140 | } |
| 141 | |
| 142 | void tst_QScriptValueIterator::name() |
| 143 | { |
| 144 | QFETCH(QString, code); |
| 145 | QScriptEngine engine; |
| 146 | QScriptValue object = engine.evaluate(code); |
| 147 | Q_ASSERT(object.isObject()); |
| 148 | |
| 149 | QScriptValueIterator it(object); |
| 150 | it.next(); |
| 151 | QBENCHMARK { |
| 152 | for (int i = 0; i < 200000; ++i) |
| 153 | it.name(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void tst_QScriptValueIterator::scriptName_data() |
| 158 | { |
| 159 | dataHelper(); |
| 160 | } |
| 161 | |
| 162 | void tst_QScriptValueIterator::scriptName() |
| 163 | { |
| 164 | QFETCH(QString, code); |
| 165 | QScriptEngine engine; |
| 166 | QScriptValue object = engine.evaluate(code); |
| 167 | Q_ASSERT(object.isObject()); |
| 168 | |
| 169 | QScriptValueIterator it(object); |
| 170 | it.next(); |
| 171 | QBENCHMARK { |
| 172 | for (int i = 0; i < 50000; ++i) |
| 173 | it.scriptName(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void tst_QScriptValueIterator::value_data() |
| 178 | { |
| 179 | dataHelper(); |
| 180 | } |
| 181 | |
| 182 | void tst_QScriptValueIterator::value() |
| 183 | { |
| 184 | QFETCH(QString, code); |
| 185 | QScriptEngine engine; |
| 186 | QScriptValue object = engine.evaluate(code); |
| 187 | Q_ASSERT(object.isObject()); |
| 188 | |
| 189 | QScriptValueIterator it(object); |
| 190 | it.next(); |
| 191 | QBENCHMARK { |
| 192 | for (int i = 0; i < 50000; ++i) |
| 193 | it.value(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void tst_QScriptValueIterator::setValue_data() |
| 198 | { |
| 199 | dataHelper(); |
| 200 | } |
| 201 | |
| 202 | void tst_QScriptValueIterator::setValue() |
| 203 | { |
| 204 | QFETCH(QString, code); |
| 205 | QScriptEngine engine; |
| 206 | QScriptValue object = engine.evaluate(code); |
| 207 | Q_ASSERT(object.isObject()); |
| 208 | |
| 209 | QScriptValueIterator it(object); |
| 210 | it.next(); |
| 211 | QScriptValue newValue(&engine, 456); |
| 212 | QBENCHMARK { |
| 213 | for (int i = 0; i < 50000; ++i) |
| 214 | it.setValue(newValue); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void tst_QScriptValueIterator::flags() |
| 219 | { |
| 220 | QScriptEngine engine; |
| 221 | QScriptValue object = engine.newObject(); |
| 222 | QScriptValue::PropertyFlags flags = flags; |
| 223 | object.setProperty("foo", 123, QScriptValue::SkipInEnumeration | QScriptValue::ReadOnly | QScriptValue::Undeletable); |
| 224 | QScriptValueIterator it(object); |
| 225 | it.next(); |
| 226 | QBENCHMARK { |
| 227 | for (int i = 0; i < 50000; ++i) |
| 228 | it.flags(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void tst_QScriptValueIterator::iterateArrayAndConvertNameToIndex() |
| 233 | { |
| 234 | QScriptEngine engine; |
| 235 | QScriptValue array = engine.newArray(); |
| 236 | for (int i = 0; i < 20000; ++i) |
| 237 | array.setProperty(i, i); |
| 238 | QBENCHMARK { |
| 239 | QScriptValueIterator it(array); |
| 240 | while (it.hasNext()) { |
| 241 | it.next(); |
| 242 | it.scriptName().toArrayIndex(); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void tst_QScriptValueIterator::iterateArrayAndDoubleElements() |
| 248 | { |
| 249 | QScriptEngine engine; |
| 250 | QScriptValue array = engine.newArray(); |
| 251 | for (int i = 0; i < 20000; ++i) |
| 252 | array.setProperty(i, i); |
| 253 | QBENCHMARK { |
| 254 | QScriptValueIterator it(array); |
| 255 | while (it.hasNext()) { |
| 256 | it.next(); |
| 257 | it.setValue(QScriptValue(&engine, it.value().toNumber() * 2)); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void tst_QScriptValueIterator::iterateArrayAndRemoveAllElements() |
| 263 | { |
| 264 | QScriptEngine engine; |
| 265 | QScriptValue array = engine.newArray(); |
| 266 | for (int i = 0; i < 20000; ++i) |
| 267 | array.setProperty(i, i); |
| 268 | QBENCHMARK { |
| 269 | QScriptValueIterator it(array); |
| 270 | while (it.hasNext()) { |
| 271 | it.next(); |
| 272 | it.remove(); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | QTEST_MAIN(tst_QScriptValueIterator) |
| 278 | #include "tst_qscriptvalueiterator.moc" |