Debian Codegraph @

tests/benchmarks/script/qscriptengine/tst_qscriptengine.cpp

raw symbols SCIP
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 #include <QtScript/private/qscriptdeclarativeclass_p.h>
33
34 Q_DECLARE_METATYPE(QScriptValue)
35
36 class tst_QScriptEngine : public QObject
37 {
38 Q_OBJECT
39
40 public:
41 tst_QScriptEngine();
42 virtual ~tst_QScriptEngine();
43
44 public slots:
45 void init();
46 void cleanup();
47
48 private slots:
49 void constructor();
50 void defaultPrototype();
51 void setDefaultPrototype();
52 void evaluate_data();
53 void evaluate();
54 void evaluateProgram_data();
55 void evaluateProgram();
56 void connectAndDisconnect();
57 void globalObject();
58 void hasUncaughtException();
59 void isEvaluating();
60 void newArray_data();
61 void newArray();
62 void newDate();
63 void newDateFromMs();
64 void newObject();
65 void newObjectWithScriptClass();
66 void newQMetaObject();
67 void newQObject();
68 void newFunction();
69 void newRegExp();
70 void newRegExpFromString();
71 void newVariant();
72 void nullValue();
73 void undefinedValue();
74 void collectGarbage();
75 void currentContext();
76 void pushAndPopContext();
77 void availableExtensions();
78 void importedExtensions();
79 void toObject_data();
80 void toObject();
81 void toStringHandle();
82 void castValueToQreal();
83 void nativeCall();
84 void installTranslatorFunctions();
85 void translation_data();
86 void translation();
87 void readScopeProperty_data();
88 void readScopeProperty();
89
90 private:
91 void defineStandardTestValues();
92 void newEngine()
93 {
94 delete m_engine;
95 m_engine = new QScriptEngine;
96 }
97
98 QScriptEngine *m_engine;
99 };
100
101 tst_QScriptEngine::tst_QScriptEngine()
102 : m_engine(0)
103 {
104 }
105
106 tst_QScriptEngine::~tst_QScriptEngine()
107 {
108 delete m_engine;
109 }
110
111 void tst_QScriptEngine::init()
112 {
113 }
114
115 void tst_QScriptEngine::cleanup()
116 {
117 }
118
119 void tst_QScriptEngine::constructor()
120 {
121 QBENCHMARK {
122 QScriptEngine engine;
123 (void)engine.parent();
124 }
125 }
126
127 void tst_QScriptEngine::defaultPrototype()
128 {
129 newEngine();
130 int type = qMetaTypeId<int>();
131 m_engine->setDefaultPrototype(type, m_engine->newObject());
132 QBENCHMARK {
133 m_engine->defaultPrototype(type);
134 }
135 }
136
137 void tst_QScriptEngine::setDefaultPrototype()
138 {
139 newEngine();
140 int type = qMetaTypeId<int>();
141 QScriptValue proto = m_engine->newObject();
142 QBENCHMARK {
143 m_engine->setDefaultPrototype(type, proto);
144 }
145 }
146
147 void tst_QScriptEngine::evaluate_data()
148 {
149 QTest::addColumn<QString>("code");
150 QTest::newRow("empty script") << QString::fromLatin1("");
151 QTest::newRow("number literal") << QString::fromLatin1("123");
152 QTest::newRow("string literal") << QString::fromLatin1("'ciao'");
153 QTest::newRow("regexp literal") << QString::fromLatin1("/foo/gim");
154 QTest::newRow("null literal") << QString::fromLatin1("null");
155 QTest::newRow("undefined literal") << QString::fromLatin1("undefined");
156 QTest::newRow("null literal") << QString::fromLatin1("null");
157 QTest::newRow("empty object literal") << QString::fromLatin1("{}");
158 QTest::newRow("this") << QString::fromLatin1("this");
159 QTest::newRow("object literal with one property") << QString::fromLatin1("{ foo: 123 }");
160 QTest::newRow("object literal with two properties") << QString::fromLatin1("{ foo: 123, bar: 456 }");
161 QTest::newRow("object literal with many properties") << QString::fromLatin1("{ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }");
162 QTest::newRow("empty array literal") << QString::fromLatin1("[]");
163 QTest::newRow("array literal with one element") << QString::fromLatin1("[1]");
164 QTest::newRow("array literal with two elements") << QString::fromLatin1("[1,2]");
165 QTest::newRow("array literal with many elements") << QString::fromLatin1("[1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1]");
166 QTest::newRow("empty function definition") << QString::fromLatin1("function foo() { }");
167 QTest::newRow("function definition") << QString::fromLatin1("function foo() { return 123; }");
168 QTest::newRow("for loop with empty body (1000 iterations)") << QString::fromLatin1("for (i = 0; i < 1000; ++i) {}");
169 QTest::newRow("for loop with empty body (10000 iterations)") << QString::fromLatin1("for (i = 0; i < 10000; ++i) {}");
170 QTest::newRow("for loop with empty body (100000 iterations)") << QString::fromLatin1("for (i = 0; i < 100000; ++i) {}");
171 QTest::newRow("for loop with empty body (1000000 iterations)") << QString::fromLatin1("for (i = 0; i < 1000000; ++i) {}");
172 QTest::newRow("for loop (1000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 1000; ++i) { j += i; }; j");
173 QTest::newRow("for loop (10000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 10000; ++i) { j += i; }; j");
174 QTest::newRow("for loop (100000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 100000; ++i) { j += i; }; j");
175 QTest::newRow("for loop (1000000 iterations)") << QString::fromLatin1("j = 0; for (i = 0; i < 1000000; ++i) { j += i; }; j");
176 QTest::newRow("assignments") << QString::fromLatin1("a = 1; b = 2; c = 3; d = 4");
177 QTest::newRow("while loop (1000 iterations)") << QString::fromLatin1("i = 0; while (i < 1000) { ++i; }; i");
178 QTest::newRow("while loop (10000 iterations)") << QString::fromLatin1("i = 0; while (i < 10000) { ++i; }; i");
179 QTest::newRow("while loop (100000 iterations)") << QString::fromLatin1("i = 0; while (i < 100000) { ++i; }; i");
180 QTest::newRow("while loop (1000000 iterations)") << QString::fromLatin1("i = 0; while (i < 1000000) { ++i; }; i");
181 QTest::newRow("function expression") << QString::fromLatin1("(function(a, b, c){ return a + b + c; })(1, 2, 3)");
182 }
183
184 void tst_QScriptEngine::evaluate()
185 {
186 QFETCH(QString, code);
187 newEngine();
188
189 QBENCHMARK {
190 (void)m_engine->evaluate(code);
191 }
192 }
193
194 void tst_QScriptEngine::connectAndDisconnect()
195 {
196 newEngine();
197 QScriptValue fun = m_engine->evaluate("(function() { })");
198 QBENCHMARK {
199 qScriptConnect(m_engine, SIGNAL(destroyed()), QScriptValue(), fun);
200 qScriptDisconnect(m_engine, SIGNAL(destroyed()), QScriptValue(), fun);
201 }
202 }
203
204 void tst_QScriptEngine::evaluateProgram_data()
205 {
206 evaluate_data();
207 }
208
209 void tst_QScriptEngine::evaluateProgram()
210 {
211 QFETCH(QString, code);
212 QScriptProgram program(code);
213 newEngine();
214
215 QBENCHMARK {
216 (void)m_engine->evaluate(program);
217 }
218 }
219
220 void tst_QScriptEngine::globalObject()
221 {
222 newEngine();
223 QBENCHMARK {
224 m_engine->globalObject();
225 }
226 }
227
228 void tst_QScriptEngine::hasUncaughtException()
229 {
230 newEngine();
231 QBENCHMARK {
232 m_engine->hasUncaughtException();
233 }
234 }
235
236 void tst_QScriptEngine::isEvaluating()
237 {
238 newEngine();
239 QBENCHMARK {
240 m_engine->isEvaluating();
241 }
242 }
243
244 void tst_QScriptEngine::newArray_data()
245 {
246 QTest::addColumn<int>("size");
247 QTest::newRow("size=0") << 0;
248 QTest::newRow("size=10") << 10;
249 QTest::newRow("size=100") << 0;
250 QTest::newRow("size=1000") << 0;
251 QTest::newRow("size=10000") << 0;
252 QTest::newRow("size=50000") << 0;
253 }
254
255 void tst_QScriptEngine::newArray()
256 {
257 QFETCH(int, size);
258 newEngine();
259 QBENCHMARK {
260 m_engine->newArray(size);
261 }
262 }
263
264 void tst_QScriptEngine::newDate()
265 {
266 newEngine();
267 QDateTime dt = QDateTime::currentDateTime();
268 QBENCHMARK {
269 m_engine->newDate(dt);
270 }
271 }
272
273 void tst_QScriptEngine::newDateFromMs()
274 {
275 newEngine();
276 QBENCHMARK {
277 m_engine->newDate(0);
278 }
279 }
280
281 void tst_QScriptEngine::newObject()
282 {
283 newEngine();
284 QBENCHMARK {
285 (void)m_engine->newObject();
286 }
287 }
288
289 void tst_QScriptEngine::newObjectWithScriptClass()
290 {
291 newEngine();
292 QScriptClass cls(m_engine);
293 QBENCHMARK {
294 m_engine->newObject(&cls);
295 }
296 }
297
298 void tst_QScriptEngine::newQMetaObject()
299 {
300 newEngine();
301 QBENCHMARK {
302 m_engine->newQMetaObject(&QScriptEngine::staticMetaObject);
303 }
304 }
305
306 void tst_QScriptEngine::newQObject()
307 {
308 newEngine();
309 QBENCHMARK {
310 (void)m_engine->newQObject(QCoreApplication::instance());
311 }
312 }
313
314 static QScriptValue testFunction(QScriptContext *, QScriptEngine *)
315 {
316 return 0;
317 }
318
319 void tst_QScriptEngine::newFunction()
320 {
321 newEngine();
322 QBENCHMARK {
323 (void)m_engine->newFunction(testFunction);
324 }
325 }
326
327 void tst_QScriptEngine::newRegExp()
328 {
329 newEngine();
330 QRegExp re = QRegExp("foo");
331 QBENCHMARK {
332 m_engine->newRegExp(re);
333 }
334 }
335
336 void tst_QScriptEngine::newRegExpFromString()
337 {
338 newEngine();
339 QString pattern("foo");
340 QString flags("gim");
341 QBENCHMARK {
342 m_engine->newRegExp(pattern, flags);
343 }
344 }
345
346 void tst_QScriptEngine::newVariant()
347 {
348 newEngine();
349 QVariant var(123);
350 QBENCHMARK {
351 (void)m_engine->newVariant(var);
352 }
353 }
354
355 void tst_QScriptEngine::nullValue()
356 {
357 newEngine();
358 QBENCHMARK {
359 m_engine->nullValue();
360 }
361 }
362
363 void tst_QScriptEngine::undefinedValue()
364 {
365 newEngine();
366 QBENCHMARK {
367 m_engine->undefinedValue();
368 }
369 }
370
371 void tst_QScriptEngine::collectGarbage()
372 {
373 newEngine();
374 QBENCHMARK {
375 m_engine->collectGarbage();
376 }
377 }
378
379 void tst_QScriptEngine::availableExtensions()
380 {
381 newEngine();
382 QBENCHMARK {
383 m_engine->availableExtensions();
384 }
385 }
386
387 void tst_QScriptEngine::importedExtensions()
388 {
389 newEngine();
390 QBENCHMARK {
391 m_engine->importedExtensions();
392 }
393 }
394
395 void tst_QScriptEngine::currentContext()
396 {
397 newEngine();
398 QBENCHMARK {
399 m_engine->currentContext();
400 }
401 }
402
403 void tst_QScriptEngine::pushAndPopContext()
404 {
405 newEngine();
406 QBENCHMARK {
407 (void)m_engine->pushContext();
408 m_engine->popContext();
409 }
410 }
411
412 void tst_QScriptEngine::toObject_data()
413 {
414 newEngine();
415 QTest::addColumn<QScriptValue>("val");
416 QTest::newRow("bool") << m_engine->evaluate("true");
417 QTest::newRow("number") << m_engine->evaluate("123");
418 QTest::newRow("string") << m_engine->evaluate("'ciao'");
419 QTest::newRow("null") << m_engine->evaluate("null");
420 QTest::newRow("undefined") << m_engine->evaluate("undefined");
421 QTest::newRow("object") << m_engine->evaluate("({foo:123})");
422 QTest::newRow("array") << m_engine->evaluate("[10,20,30]");
423 QTest::newRow("function") << m_engine->evaluate("(function foo(a, b, c) { return a + b + c; })");
424 QTest::newRow("date") << m_engine->evaluate("new Date");
425 QTest::newRow("regexp") << m_engine->evaluate("new RegExp('foo')");
426 QTest::newRow("error") << m_engine->evaluate("new Error");
427
428 QTest::newRow("qobject") << m_engine->newQObject(this);
429 QTest::newRow("qmetaobject") << m_engine->newQMetaObject(&QScriptEngine::staticMetaObject);
430 QTest::newRow("variant") << m_engine->newVariant(123);
431 QTest::newRow("qscriptclassobject") << m_engine->newObject(new QScriptClass(m_engine));
432
433 QTest::newRow("invalid") << QScriptValue();
434 QTest::newRow("bool-no-engine") << QScriptValue(true);
435 QTest::newRow("number-no-engine") << QScriptValue(123.0);
436 QTest::newRow("string-no-engine") << QScriptValue(QString::fromLatin1("hello"));
437 QTest::newRow("null-no-engine") << QScriptValue(QScriptValue::NullValue);
438 QTest::newRow("undefined-no-engine") << QScriptValue(QScriptValue::UndefinedValue);
439 }
440
441 void tst_QScriptEngine::toObject()
442 {
443 QFETCH(QScriptValue, val);
444 QBENCHMARK {
445 m_engine->toObject(val);
446 }
447 }
448
449 void tst_QScriptEngine::toStringHandle()
450 {
451 newEngine();
452 QString str = QString::fromLatin1("foobarbaz");
453 QBENCHMARK {
454 (void)m_engine->toStringHandle(str);
455 }
456 }
457
458 void tst_QScriptEngine::castValueToQreal()
459 {
460 QScriptValue val(123);
461 QBENCHMARK {
462 (void)qscriptvalue_cast<qreal>(val);
463 }
464 }
465
466 static QScriptValue native_function(QScriptContext *, QScriptEngine *)
467 {
468 return 42;
469 }
470
471 void tst_QScriptEngine::nativeCall()
472 {
473 newEngine();
474 m_engine->globalObject().setProperty("fun", m_engine->newFunction(native_function));
475 QBENCHMARK{
476 m_engine->evaluate("var w = 0; for (i = 0; i < 100000; ++i) {\n"
477 " w += fun() + fun(); w -= fun(); fun(); w -= fun(); }");
478 }
479 }
480
481 void tst_QScriptEngine::installTranslatorFunctions()
482 {
483 newEngine();
484 QBENCHMARK {
485 m_engine->installTranslatorFunctions();
486 }
487 }
488
489 void tst_QScriptEngine::translation_data()
490 {
491 QTest::addColumn<QString>("text");
492 QTest::addColumn<QString>("fileName");
493 QTest::newRow("no translation") << "\"hello world\"" << "";
494 QTest::newRow("qsTr") << "qsTr(\"hello world\")" << "";
495 QTest::newRow("qsTranslate") << "qsTranslate(\"\", \"hello world\")" << "";
496 QTest::newRow("qsTr:script.js") << "qsTr(\"hello world\")" << "script.js";
497 }
498
499 void tst_QScriptEngine::translation()
500 {
501 QFETCH(QString, text);
502 QFETCH(QString, fileName);
503 newEngine();
504 m_engine->installTranslatorFunctions();
505
506 QBENCHMARK {
507 (void)m_engine->evaluate(text, fileName);
508 }
509 }
510
511 void tst_QScriptEngine::readScopeProperty_data()
512 {
513 QTest::addColumn<bool>("staticScope");
514 QTest::addColumn<bool>("nestedScope");
515 QTest::newRow("single dynamic scope") << false << false;
516 QTest::newRow("single static scope") << true << false;
517 QTest::newRow("double dynamic scope") << false << true;
518 QTest::newRow("double static scope") << true << true;
519 }
520
521 void tst_QScriptEngine::readScopeProperty()
522 {
523 QFETCH(bool, staticScope);
524 QFETCH(bool, nestedScope);
525
526 newEngine();
527 QScriptContext *ctx = m_engine->pushContext();
528
529 QScriptValue scope;
530 if (staticScope)
531 scope = QScriptDeclarativeClass::newStaticScopeObject(m_engine);
532 else
533 scope = m_engine->newObject();
534 scope.setProperty("foo", 123);
535 ctx->pushScope(scope);
536
537 if (nestedScope) {
538 QScriptValue scope2;
539 if (staticScope)
540 scope2 = QScriptDeclarativeClass::newStaticScopeObject(m_engine);
541 else
542 scope2 = m_engine->newObject();
543 scope2.setProperty("bar", 456); // ensure a miss in inner scope
544 ctx->pushScope(scope2);
545 }
546
547 QScriptValue fun = m_engine->evaluate("(function() {\n"
548 " for (var i = 0; i < 10000; ++i) {\n"
549 " foo; foo; foo; foo; foo; foo; foo; foo;\n"
550 " }\n"
551 "})");
552 m_engine->popContext();
553 QVERIFY(fun.isFunction());
554 QBENCHMARK {
555 fun.call();
556 }
557 }
558
559 QTEST_MAIN(tst_QScriptEngine)
560 #include "tst_qscriptengine.moc"