From 63176f2628f1ba01224654d895fd99c60a390cf2 Mon Sep 17 00:00:00 2001 From: "rogerl%netscape.com" Date: Fri, 16 Jun 2000 01:37:47 +0000 Subject: [PATCH] Added tests for scripts as strings. git-svn-id: svn://10.0.0.236/trunk@72338 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/js/js2/js2.cpp | 33 +++++++++++++++++++++++++++-- mozilla/js2/tests/cpp/js2_shell.cpp | 33 +++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/mozilla/js/js2/js2.cpp b/mozilla/js/js2/js2.cpp index 534216899e1..19403a4a5ad 100644 --- a/mozilla/js/js2/js2.cpp +++ b/mozilla/js/js2/js2.cpp @@ -109,8 +109,8 @@ static void genCode(World &world, Context &cx, StmtNode *p) p = p->next; } icg.returnStmt(ret); - stdOut << '\n'; - stdOut << icg; +// stdOut << '\n'; +// stdOut << icg; JSValue result = cx.interpret(icg.complete(), JSValues()); stdOut << "result = " << result << "\n"; } @@ -290,13 +290,41 @@ static float64 testFactorial(World &world, float64 n) return result.f64; } + +char * tests[] = { + "function fact(n) { if (n > 1) return n * fact(n-1); else return 1; } print(fact(6), \" should be 720\"); return;" , + "a = { f1: 1, f2: 2}; print(a.f2++, \" should be 2\"); print(a.f2 <<= 1, \" should be 6\"); return;" +}; + +void testCompile() +{ + JSScope glob; + Context cx(world, &glob); + StringAtom& printName = world.identifiers[widenCString("print")]; + glob.defineNativeFunction(printName, print); + + for (int i = 0; i < sizeof(tests) / sizeof(char *); i++) { + String testScript = widenCString(tests[i]); + Arena a; + Parser p(world, a, testScript, widenCString("testCompile")); + StmtNode *parsedStatements = p.parseProgram(); + ICodeGenerator icg(&world); + icg.isScript(); + while (parsedStatements) { + icg.genStmt(parsedStatements); + parsedStatements = parsedStatements->next; + } + cx.interpret(icg.complete(), JSValues()); + } +} } /* namespace Shell */ } /* namespace JavaScript */ + int main(int argc, char **argv) { #if defined(XP_MAC) && !defined(XP_MAC_MPW) @@ -306,6 +334,7 @@ int main(int argc, char **argv) using namespace Shell; #if 0 assert(testFactorial(world, 5) == 120); + testCompile(); #endif readEvalPrint(stdin, world); return 0; diff --git a/mozilla/js2/tests/cpp/js2_shell.cpp b/mozilla/js2/tests/cpp/js2_shell.cpp index 534216899e1..19403a4a5ad 100644 --- a/mozilla/js2/tests/cpp/js2_shell.cpp +++ b/mozilla/js2/tests/cpp/js2_shell.cpp @@ -109,8 +109,8 @@ static void genCode(World &world, Context &cx, StmtNode *p) p = p->next; } icg.returnStmt(ret); - stdOut << '\n'; - stdOut << icg; +// stdOut << '\n'; +// stdOut << icg; JSValue result = cx.interpret(icg.complete(), JSValues()); stdOut << "result = " << result << "\n"; } @@ -290,13 +290,41 @@ static float64 testFactorial(World &world, float64 n) return result.f64; } + +char * tests[] = { + "function fact(n) { if (n > 1) return n * fact(n-1); else return 1; } print(fact(6), \" should be 720\"); return;" , + "a = { f1: 1, f2: 2}; print(a.f2++, \" should be 2\"); print(a.f2 <<= 1, \" should be 6\"); return;" +}; + +void testCompile() +{ + JSScope glob; + Context cx(world, &glob); + StringAtom& printName = world.identifiers[widenCString("print")]; + glob.defineNativeFunction(printName, print); + + for (int i = 0; i < sizeof(tests) / sizeof(char *); i++) { + String testScript = widenCString(tests[i]); + Arena a; + Parser p(world, a, testScript, widenCString("testCompile")); + StmtNode *parsedStatements = p.parseProgram(); + ICodeGenerator icg(&world); + icg.isScript(); + while (parsedStatements) { + icg.genStmt(parsedStatements); + parsedStatements = parsedStatements->next; + } + cx.interpret(icg.complete(), JSValues()); + } +} } /* namespace Shell */ } /* namespace JavaScript */ + int main(int argc, char **argv) { #if defined(XP_MAC) && !defined(XP_MAC_MPW) @@ -306,6 +334,7 @@ int main(int argc, char **argv) using namespace Shell; #if 0 assert(testFactorial(world, 5) == 120); + testCompile(); #endif readEvalPrint(stdin, world); return 0;