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