diff options
Diffstat (limited to 'js/src/tests/ecma_2/Statements')
33 files changed, 2492 insertions, 0 deletions
diff --git a/js/src/tests/ecma_2/Statements/browser.js b/js/src/tests/ecma_2/Statements/browser.js new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/js/src/tests/ecma_2/Statements/browser.js diff --git a/js/src/tests/ecma_2/Statements/dowhile-001.js b/js/src/tests/ecma_2/Statements/dowhile-001.js new file mode 100644 index 000000000..d9a7a033d --- /dev/null +++ b/js/src/tests/ecma_2/Statements/dowhile-001.js @@ -0,0 +1,44 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: dowhile-001 + * ECMA Section: + * Description: do...while statements + * + * + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "dowhile-002"; +var VERSION = "ECMA_2"; +var TITLE = "do...while with a labeled continue statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +LabeledContinue( 0, 1 ); +LabeledContinue( 1, 1 ); +LabeledContinue( -1, 1 ); +LabeledContinue( 5, 5 ); + +test(); + +function LabeledContinue( limit, expect ) { + i = 0; +woohoo: + do { + i++; + continue woohoo; + } while ( i < limit ); + + new TestCase( + SECTION, + "do while ( " + i +" < " + limit +" )", + expect, + i ); +} diff --git a/js/src/tests/ecma_2/Statements/dowhile-002.js b/js/src/tests/ecma_2/Statements/dowhile-002.js new file mode 100644 index 000000000..e25557e70 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/dowhile-002.js @@ -0,0 +1,71 @@ +/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: dowhile-002 + * ECMA Section: + * Description: do...while statements + * + * Verify that code after a labeled break is not executed. Verify that + * a labeled break breaks you out of the whole labeled block, and not + * just the current iteration statement. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "dowhile-002"; +var VERSION = "ECMA_2"; +var TITLE = "do...while with a labeled continue statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +LabeledContinue( 0, 1 ); +LabeledContinue( 1, 1 ); +LabeledContinue( -1, 1 ); +LabeledContinue( 5, 5 ); + +test(); + +// The labeled statement contains statements after the labeled break. +// Verify that the statements after the break are not executed. + +function LabeledContinue( limit, expect ) { + i = 0; + result1 = "pass"; + result2 = "pass"; + +woohoo: { + do { + i++; + if ( ! (i < limit) ) { + break woohoo; + result1 = "fail: evaluated statement after a labeled break"; + } + } while ( true ); + + result2 = "failed: broke out of loop, but not out of labeled block"; + } + + new TestCase( + SECTION, + "do while ( " + i +" < " + limit +" )", + expect, + i ); + + new TestCase( + SECTION, + "breaking out of a do... while loop", + "pass", + result1 ); + + + new TestCase( + SECTION, + "breaking out of a labeled do...while loop", + "pass", + result2 ); +} diff --git a/js/src/tests/ecma_2/Statements/dowhile-003.js b/js/src/tests/ecma_2/Statements/dowhile-003.js new file mode 100644 index 000000000..b482823e1 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/dowhile-003.js @@ -0,0 +1,63 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: dowhile-003 + * ECMA Section: + * Description: do...while statements + * + * Test do while, when the while expression is a JavaScript Number object. + * + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "dowhile-003"; +var VERSION = "ECMA_2"; +var TITLE = "do...while with a labeled continue statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +DoWhile( new DoWhileObject( 1, 1, 0 )); +DoWhile( new DoWhileObject( 1000, 1000, 0 )); +DoWhile( new DoWhileObject( 1001, 1001, 0 )); +DoWhile( new DoWhileObject( 1002, 1001, 1 )); +DoWhile( new DoWhileObject( -1, 1001, -1002 )); + +test(); + +function DoWhileObject( value, iterations, endvalue ) { + this.value = value; + this.iterations = iterations; + this.endvalue = endvalue; +} + +function DoWhile( object ) { + var i = 0; + + do { + object.value = --object.value; + i++; + if ( i > 1000 ) + break; + } while( object.value ); + + new TestCase( + SECTION, + "loop iterations", + object.iterations, + i + ); + + new TestCase( + SECTION, + "object.value", + object.endvalue, + Number( object.value ) + ); + +} diff --git a/js/src/tests/ecma_2/Statements/dowhile-004.js b/js/src/tests/ecma_2/Statements/dowhile-004.js new file mode 100644 index 000000000..90e376fde --- /dev/null +++ b/js/src/tests/ecma_2/Statements/dowhile-004.js @@ -0,0 +1,67 @@ +/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: dowhile-004 + * ECMA Section: + * Description: do...while statements + * + * Test a labeled do...while. Break out of the loop with no label + * should break out of the loop, but not out of the label. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "dowhile-004"; +var VERSION = "ECMA_2"; +var TITLE = "do...while with a labeled continue statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +DoWhile( 0, 1 ); +DoWhile( 1, 1 ); +DoWhile( -1, 1 ); +DoWhile( 5, 5 ); + +test(); + +function DoWhile( limit, expect ) { + i = 0; + result1 = "pass"; + result2 = "failed: broke out of labeled statement unexpectedly"; + +foo: { + do { + i++; + if ( ! (i < limit) ) { + break; + result1 = "fail: evaluated statement after a labeled break"; + } + } while ( true ); + + result2 = "pass"; + } + + new TestCase( + SECTION, + "do while ( " + i +" < " + limit +" )", + expect, + i ); + + new TestCase( + SECTION, + "breaking out of a do... while loop", + "pass", + result1 ); + + + new TestCase( + SECTION, + "breaking out of a labeled do...while loop", + "pass", + result2 ); +} diff --git a/js/src/tests/ecma_2/Statements/dowhile-005.js b/js/src/tests/ecma_2/Statements/dowhile-005.js new file mode 100644 index 000000000..3c339f493 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/dowhile-005.js @@ -0,0 +1,73 @@ +/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: dowhile-005 + * ECMA Section: + * Description: do...while statements + * + * Test a labeled do...while. Break out of the loop with no label + * should break out of the loop, but not out of the label. + * + * Currently causes an infinite loop in the monkey. Uncomment the + * print statement below and it works OK. + * + * Author: christine@netscape.com + * Date: 26 August 1998 + */ +var SECTION = "dowhile-005"; +var VERSION = "ECMA_2"; +var TITLE = "do...while with a labeled continue statement"; +var BUGNUMBER = "316293"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +NestedLabel(); + + +test(); + +function NestedLabel() { + i = 0; + result1 = "pass"; + result2 = "fail: did not hit code after inner loop"; + result3 = "pass"; + +outer: { + do { + inner: { +// print( i ); + break inner; + result1 = "fail: did break out of inner label"; + } + result2 = "pass"; + break outer; + print(i); + } while ( i++ < 100 ); + + } + + result3 = "fail: did not break out of outer label"; + + new TestCase( + SECTION, + "number of loop iterations", + 0, + i ); + + new TestCase( + SECTION, + "break out of inner loop", + "pass", + result1 ); + + new TestCase( + SECTION, + "break out of outer loop", + "pass", + result2 ); +} diff --git a/js/src/tests/ecma_2/Statements/dowhile-006.js b/js/src/tests/ecma_2/Statements/dowhile-006.js new file mode 100644 index 000000000..0a1aa701a --- /dev/null +++ b/js/src/tests/ecma_2/Statements/dowhile-006.js @@ -0,0 +1,89 @@ +/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: dowhile-006 + * ECMA Section: + * Description: do...while statements + * + * A general do...while test. + * + * Author: christine@netscape.com + * Date: 26 August 1998 + */ +var SECTION = "dowhile-006"; +var VERSION = "ECMA_2"; +var TITLE = "do...while"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +DoWhile( new DoWhileObject( false, false, 10 ) ); +DoWhile( new DoWhileObject( true, false, 2 ) ); +DoWhile( new DoWhileObject( false, true, 3 ) ); +DoWhile( new DoWhileObject( true, true, 4 ) ); + +test(); + +function looping( object ) { + object.iterations--; + + if ( object.iterations <= 0 ) { + return false; + } else { + return true; + } +} +function DoWhileObject( breakOut, breakIn, iterations, loops ) { + this.iterations = iterations; + this.loops = loops; + this.breakOut = breakOut; + this.breakIn = breakIn; + this.looping = looping; +} +function DoWhile( object ) { + var result1 = false; + var result2 = false; + +outie: { + innie: { + do { + if ( object.breakOut ) + break outie; + + if ( object.breakIn ) + break innie; + + } while ( looping(object) ); + + // statements should be executed if: + // do...while exits normally + // do...while exits abruptly with no label + + result1 = true; + + } + +// statements should be executed if: +// do...while breaks out with label "innie" +// do...while exits normally +// do...while does not break out with "outie" + + result2 = true; + } + + new TestCase( + SECTION, + "hit code after loop in inner loop", + ( object.breakIn || object.breakOut ) ? false : true , + result1 ); + + new TestCase( + SECTION, + "hit code after loop in outer loop", + ( object.breakOut ) ? false : true, + result2 ); +} diff --git a/js/src/tests/ecma_2/Statements/dowhile-007.js b/js/src/tests/ecma_2/Statements/dowhile-007.js new file mode 100644 index 000000000..44adbcafd --- /dev/null +++ b/js/src/tests/ecma_2/Statements/dowhile-007.js @@ -0,0 +1,97 @@ +/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: dowhile-007 + * ECMA Section: + * Description: do...while statements + * + * A general do...while test. + * + * Author: christine@netscape.com + * Date: 26 August 1998 + */ +var SECTION = "dowhile-007"; +var VERSION = "ECMA_2"; +var TITLE = "do...while"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +DoWhile( new DoWhileObject( false, false, false, false )); +DoWhile( new DoWhileObject( true, false, false, false )); +DoWhile( new DoWhileObject( true, true, false, false )); +DoWhile( new DoWhileObject( true, true, true, false )); +DoWhile( new DoWhileObject( true, true, true, true )); +DoWhile( new DoWhileObject( false, false, false, true )); +DoWhile( new DoWhileObject( false, false, true, true )); +DoWhile( new DoWhileObject( false, true, true, true )); +DoWhile( new DoWhileObject( false, false, true, false )); + +test(); + +function DoWhileObject( out1, out2, out3, in1 ) { + this.breakOutOne = out1; + this.breakOutTwo = out2; + this.breakOutThree = out3; + this.breakIn = in1; +} +function DoWhile( object ) { + result1 = false; + result2 = false; + result3 = false; + result4 = false; + +outie: + do { + if ( object.breakOutOne ) { + break outie; + } + result1 = true; + + innie: + do { + if ( object.breakOutTwo ) { + break outie; + } + result2 = true; + + if ( object.breakIn ) { + break innie; + } + result3 = true; + + } while ( false ); + if ( object.breakOutThree ) { + break outie; + } + result4 = true; + } while ( false ); + + new TestCase( + SECTION, + "break one: ", + (object.breakOutOne) ? false : true, + result1 ); + + new TestCase( + SECTION, + "break two: ", + (object.breakOutOne||object.breakOutTwo) ? false : true, + result2 ); + + new TestCase( + SECTION, + "break three: ", + (object.breakOutOne||object.breakOutTwo||object.breakIn) ? false : true, + result3 ); + + new TestCase( + SECTION, + "break four: ", + (object.breakOutOne||object.breakOutTwo||object.breakOutThree) ? false: true, + result4 ); +} diff --git a/js/src/tests/ecma_2/Statements/forin-001.js b/js/src/tests/ecma_2/Statements/forin-001.js new file mode 100644 index 000000000..215de6c4c --- /dev/null +++ b/js/src/tests/ecma_2/Statements/forin-001.js @@ -0,0 +1,297 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: forin-001.js + * ECMA Section: + * Description: The forin-001 statement + * + * Verify that the property name is assigned to the property on the left + * hand side of the for...in expression. + * + * Author: christine@netscape.com + * Date: 28 August 1998 + */ +var SECTION = "forin-001"; +var VERSION = "ECMA_2"; +var TITLE = "The for...in statement"; +var BUGNUMBER="330890"; +var BUGNUMBER="http://scopus.mcom.com/bugsplat/show_bug.cgi?id=344855"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +ForIn_1( { length:4, company:"netscape", year:2000, 0:"zero" } ); +ForIn_2( { length:4, company:"netscape", year:2000, 0:"zero" } ); +ForIn_3( { length:4, company:"netscape", year:2000, 0:"zero" } ); + +// ForIn_6({ length:4, company:"netscape", year:2000, 0:"zero" }); +// ForIn_7({ length:4, company:"netscape", year:2000, 0:"zero" }); +ForIn_8({ length:4, company:"netscape", year:2000, 0:"zero" }); + +test(); + +/** + * Verify that the left side argument is evaluated with every iteration. + * Verify that the name of each property of the object is assigned to a + * a property. + * + */ +function ForIn_1( object ) { + PropertyArray = new Array(); + ValueArray = new Array(); + + for ( PropertyArray[PropertyArray.length] in object ) { + ValueArray[ValueArray.length] = + object[PropertyArray[PropertyArray.length-1]]; + } + + for ( var i = 0; i < PropertyArray.length; i++ ) { + new TestCase( + SECTION, + "object[" + PropertyArray[i] +"]", + object[PropertyArray[i]], + ValueArray[i] + ); + } + + new TestCase( + SECTION, + "object.length", + PropertyArray.length, + object.length ); +} + +/** + * Similar to ForIn_1, except it should increment the counter variable + * every time the left hand expression is evaluated. + */ +function ForIn_2( object ) { + PropertyArray = new Array(); + ValueArray = new Array(); + var i = 0; + + for ( PropertyArray[i++] in object ) { + ValueArray[ValueArray.length] = + object[PropertyArray[PropertyArray.length-1]]; + } + + for ( i = 0; i < PropertyArray.length; i++ ) { + new TestCase( + SECTION, + "object[" + PropertyArray[i] +"]", + object[PropertyArray[i]], + ValueArray[i] + ); + } + + new TestCase( + SECTION, + "object.length", + PropertyArray.length, + object.length ); +} + +/** + * Break out of a for...in loop + * + * + */ +function ForIn_3( object ) { + var checkBreak = "pass"; + var properties = new Array(); + var values = new Array(); + + for ( properties[properties.length] in object ) { + values[values.length] = object[properties[properties.length-1]]; + break; + checkBreak = "fail"; + } + + new TestCase( + SECTION, + "check break out of for...in", + "pass", + checkBreak ); + + new TestCase( + SECTION, + "properties.length", + 1, + properties.length ); + + new TestCase( + SECTION, + "object["+properties[0]+"]", + values[0], + object[properties[0]] ); +} + +/** + * Break out of a labeled for...in loop. + */ +function ForIn_4( object ) { + var result1 = 0; + var result2 = 0; + var result3 = 0; + var result4 = 0; + var i = 0; + var property = new Array(); + +butterbean: { + result1++; + + for ( property[i++] in object ) { + result2++; + break; + result4++; + } + result3++; + } + + new TestCase( + SECTION, + "verify labeled statement is only executed once", + true, + result1 == 1 ); + + new TestCase( + SECTION, + "verify statements in for loop are evaluated", + true, + result2 == i ); + + new TestCase( + SECTION, + "verify break out of labeled for...in loop", + true, + result4 == 0 ); + + new TestCase( + SECTION, + "verify break out of labeled block", + true, + result3 == 0 ); +} + +/** + * Labeled break out of a labeled for...in loop. + */ +function ForIn_5 (object) { + var result1 = 0; + var result2 = 0; + var result3 = 0; + var result4 = 0; + var i = 0; + var property = new Array(); + +bigredbird: { + result1++; + for ( property[i++] in object ) { + result2++; + break bigredbird; + result4++; + } + result3++; + } + + new TestCase( + SECTION, + "verify labeled statement is only executed once", + true, + result1 == 1 ); + + new TestCase( + SECTION, + "verify statements in for loop are evaluated", + true, + result2 == i ); + + new TestCase( + SECTION, + "verify break out of labeled for...in loop", + true, + result4 == 0 ); + + new TestCase( + SECTION, + "verify break out of labeled block", + true, + result3 == 0 ); +} + +/** + * Labeled continue from a labeled for...in loop + */ +function ForIn_7( object ) { + var result1 = 0; + var result2 = 0; + var result3 = 0; + var result4 = 0; + var i = 0; + var property = new Array(); + +bigredbird: + for ( property[i++] in object ) { + result2++; + continue bigredbird; + result4++; + } + + new TestCase( + SECTION, + "verify statements in for loop are evaluated", + true, + result2 == i ); + + new TestCase( + SECTION, + "verify break out of labeled for...in loop", + true, + result4 == 0 ); + + new TestCase( + SECTION, + "verify break out of labeled block", + true, + result3 == 1 ); +} + + +/** + * continue in a for...in loop + * + */ +function ForIn_8( object ) { + var checkBreak = "pass"; + var properties = new Array(); + var values = new Array(); + + for ( properties[properties.length] in object ) { + values[values.length] = object[properties[properties.length-1]]; + break; + checkBreak = "fail"; + } + + new TestCase( + SECTION, + "check break out of for...in", + "pass", + checkBreak ); + + new TestCase( + SECTION, + "properties.length", + 1, + properties.length ); + + new TestCase( + SECTION, + "object["+properties[0]+"]", + values[0], + object[properties[0]] ); +} + diff --git a/js/src/tests/ecma_2/Statements/forin-002.js b/js/src/tests/ecma_2/Statements/forin-002.js new file mode 100644 index 000000000..7d898d6bc --- /dev/null +++ b/js/src/tests/ecma_2/Statements/forin-002.js @@ -0,0 +1,77 @@ +// |reftest| skip-if(Android) -- bug - nsIDOMWindow.crypto throws NS_ERROR_NOT_IMPLEMENTED on Android +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: forin-002.js + * ECMA Section: + * Description: The forin-001 statement + * + * Verify that the property name is assigned to the property on the left + * hand side of the for...in expression. + * + * Author: christine@netscape.com + * Date: 28 August 1998 + */ +var SECTION = "forin-002"; +var VERSION = "ECMA_2"; +var TITLE = "The for...in statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +function MyObject( value ) { + this.value = value; + this.valueOf = new Function ( "return this.value" ); + this.toString = new Function ( "return this.value + \"\"" ); + this.toNumber = new Function ( "return this.value + 0" ); + this.toBoolean = new Function ( "return Boolean( this.value )" ); +} + +ForIn_1(this); +ForIn_2(this); + +ForIn_1(new MyObject(true)); +ForIn_2(new MyObject(new Boolean(true))); + +ForIn_2(3); + +test(); + +/** + * For ... In in a With Block + * + */ +function ForIn_1( object) { + with ( object ) { + for ( property in object ) { + new TestCase( + SECTION, + "with loop in a for...in loop. ("+object+")["+property +"] == "+ + "eval ( " + property +" )", + true, + object[property] == eval(property) ); + } + } +} + +/** + * With block in a For...In loop + * + */ +function ForIn_2(object) { + for ( property in object ) { + with ( object ) { + new TestCase( + SECTION, + "with loop in a for...in loop. ("+object+")["+property +"] == "+ + "eval ( " + property +" )", + true, + object[property] == eval(property) ); + } + } +} + diff --git a/js/src/tests/ecma_2/Statements/if-001.js b/js/src/tests/ecma_2/Statements/if-001.js new file mode 100644 index 000000000..ffc621a32 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/if-001.js @@ -0,0 +1,42 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: if-001.js + * ECMA Section: + * Description: The if statement + * + * Verify that assignment in the if expression is evaluated correctly. + * Verifies the fix for bug http://scopus/bugsplat/show_bug.cgi?id=148822. + * + * Author: christine@netscape.com + * Date: 28 August 1998 + */ +var SECTION = "for-001"; +var VERSION = "ECMA_2"; +var TITLE = "The if statement"; +var BUGNUMBER="148822"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +var a = 0; +var b = 0; +var result = "passed"; + +if ( a = b ) { + result = "failed: a = b should return 0"; +} + +new TestCase( + SECTION, + "if ( a = b ), where a and b are both equal to 0", + "passed", + result ); + + +test(); + diff --git a/js/src/tests/ecma_2/Statements/label-001.js b/js/src/tests/ecma_2/Statements/label-001.js new file mode 100644 index 000000000..391732a2d --- /dev/null +++ b/js/src/tests/ecma_2/Statements/label-001.js @@ -0,0 +1,42 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: label-001.js + * ECMA Section: + * Description: Labeled statements + * + * Labeled break and continue within a for loop. + * + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "label-003"; +var VERSION = "ECMA_2"; +var TITLE = "Labeled statements"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +LabelTest(0, 0); +LabelTest(1, 1) + LabelTest(-1, 1000); +LabelTest(false, 0); +LabelTest(true, 1); + +test(); + +function LabelTest( limit, expect) { +woo: for ( var result = 0; result < 1000; result++ ) { if (result == limit) { break woo; } else { continue woo; } }; + + new TestCase( + SECTION, + "break out of a labeled for loop: "+ limit, + expect, + result ); +} + diff --git a/js/src/tests/ecma_2/Statements/label-002.js b/js/src/tests/ecma_2/Statements/label-002.js new file mode 100644 index 000000000..1e2b63adc --- /dev/null +++ b/js/src/tests/ecma_2/Statements/label-002.js @@ -0,0 +1,56 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: label-002.js + * ECMA Section: + * Description: Labeled statements + * + * Labeled break and continue within a for-in loop. + * + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "label-002"; +var VERSION = "ECMA_2"; +var TITLE = "Labeled statements"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +LabelTest( { p1:"hi,", p2:" norris" }, "hi, norris", " norrishi," ); +LabelTest( { 0:"zero", 1:"one" }, "zeroone", "onezero" ); + +LabelTest2( { p1:"hi,", p2:" norris" }, "hi,", " norris" ); +LabelTest2( { 0:"zero", 1:"one" }, "zero", "one" ); + +test(); + +function LabelTest( object, expect1, expect2 ) { + result = ""; + +yoohoo: { for ( property in object ) { result += object[property]; }; break yoohoo }; + + new TestCase( + SECTION, + "yoohoo: for ( property in object ) { result += object[property]; } break yoohoo }", + true, + result == expect1 || result == expect2 ); +} + +function LabelTest2( object, expect1, expect2 ) { + result = ""; + +yoohoo: { for ( property in object ) { result += object[property]; break yoohoo } }; ; + + new TestCase( + SECTION, + "yoohoo: for ( property in object ) { result += object[property]; break yoohoo }}", + true, + result == expect1 || result == expect2 ); +} + diff --git a/js/src/tests/ecma_2/Statements/label-003.js b/js/src/tests/ecma_2/Statements/label-003.js new file mode 100644 index 000000000..934732949 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/label-003.js @@ -0,0 +1,15 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// The colon for a labeled statement may be on a separate line. +var x; +label +: { + x = 1; + break label; + x = 2; +} +assertEq(x, 1); +reportCompare(0, 0, 'ok'); diff --git a/js/src/tests/ecma_2/Statements/shell.js b/js/src/tests/ecma_2/Statements/shell.js new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/js/src/tests/ecma_2/Statements/shell.js diff --git a/js/src/tests/ecma_2/Statements/switch-001.js b/js/src/tests/ecma_2/Statements/switch-001.js new file mode 100644 index 000000000..44e72985d --- /dev/null +++ b/js/src/tests/ecma_2/Statements/switch-001.js @@ -0,0 +1,65 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: switch-001.js + * ECMA Section: + * Description: The switch Statement + * + * A simple switch test with no abrupt completions. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + * + */ +var SECTION = "switch-001"; +var VERSION = "ECMA_2"; +var TITLE = "The switch statement"; + +var BUGNUMBER="315767"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +SwitchTest( 0, 126 ); +SwitchTest( 1, 124 ); +SwitchTest( 2, 120 ); +SwitchTest( 3, 112 ); +SwitchTest( 4, 64 ); +SwitchTest( 5, 96 ); +SwitchTest( true, 96 ); +SwitchTest( false, 96 ); +SwitchTest( null, 96 ); +SwitchTest( void 0, 96 ); +SwitchTest( "0", 96 ); + +test(); + +function SwitchTest( input, expect ) { + var result = 0; + + switch ( input ) { + case 0: + result += 2; + case 1: + result += 4; + case 2: + result += 8; + case 3: + result += 16; + default: + result += 32; + case 4: + result +=64; + } + + new TestCase( + SECTION, + "switch with no breaks, case expressions are numbers. input is "+ + input, + expect, + result ); +} diff --git a/js/src/tests/ecma_2/Statements/switch-002.js b/js/src/tests/ecma_2/Statements/switch-002.js new file mode 100644 index 000000000..3e08630be --- /dev/null +++ b/js/src/tests/ecma_2/Statements/switch-002.js @@ -0,0 +1,63 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: switch-002.js + * ECMA Section: + * Description: The switch Statement + * + * A simple switch test with no abrupt completions. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + * + */ +var SECTION = "switch-002"; +var VERSION = "ECMA_2"; +var TITLE = "The switch statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +SwitchTest( 0, 6 ); +SwitchTest( 1, 4 ); +SwitchTest( 2, 56 ); +SwitchTest( 3, 48 ); +SwitchTest( 4, 64 ); +SwitchTest( true, 32 ); +SwitchTest( false, 32 ); +SwitchTest( null, 32 ); +SwitchTest( void 0, 32 ); +SwitchTest( "0", 32 ); + +test(); + +function SwitchTest( input, expect ) { + var result = 0; + + switch ( input ) { + case 0: + result += 2; + case 1: + result += 4; + break; + case 2: + result += 8; + case 3: + result += 16; + default: + result += 32; + break; + case 4: + result += 64; + } + + new TestCase( + SECTION, + "switch with no breaks: input is " + input, + expect, + result ); +} diff --git a/js/src/tests/ecma_2/Statements/switch-003.js b/js/src/tests/ecma_2/Statements/switch-003.js new file mode 100644 index 000000000..5ceaade69 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/switch-003.js @@ -0,0 +1,57 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: switch-003.js + * ECMA Section: + * Description: The switch Statement + * + * Attempt to verify that case statements are evaluated in source order + * + * Author: christine@netscape.com + * Date: 11 August 1998 + * + */ +var SECTION = "switch-003"; +var VERSION = "ECMA_2"; +var TITLE = "The switch statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +SwitchTest( "a", "abc" ); +SwitchTest( "b", "bc" ); +SwitchTest( "c", "c" ); +SwitchTest( "d", "*abc" ); +SwitchTest( "v", "*abc" ); +SwitchTest( "w", "w*abc" ); +SwitchTest( "x", "xw*abc" ); +SwitchTest( "y", "yxw*abc" ); +SwitchTest( "z", "zyxw*abc" ); +// SwitchTest( new java.lang.String("z"), "*abc" ); + +test(); + +function SwitchTest( input, expect ) { + var result = ""; + + switch ( input ) { + case "z": result += "z"; + case "y": result += "y"; + case "x": result += "x"; + case "w": result += "w"; + default: result += "*"; + case "a": result += "a"; + case "b": result += "b"; + case "c": result += "c"; + } + + new TestCase( + SECTION, + "switch with no breaks: input is " + input, + expect, + result ); +} diff --git a/js/src/tests/ecma_2/Statements/switch-004.js b/js/src/tests/ecma_2/Statements/switch-004.js new file mode 100644 index 000000000..0572874a6 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/switch-004.js @@ -0,0 +1,94 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: switch-003.js + * ECMA Section: + * Description: The switch Statement + * + * This uses variables and objects as case expressions in switch statements. + * This verifies a bunch of bugs: + * + * http://scopus.mcom.com/bugsplat/show_bug.cgi?id=315988 + * http://scopus.mcom.com/bugsplat/show_bug.cgi?id=315975 + * http://scopus.mcom.com/bugsplat/show_bug.cgi?id=315954 + * + * Author: christine@netscape.com + * Date: 11 August 1998 + * + */ +var SECTION = "switch-003"; +var VERSION = "ECMA_2"; +var TITLE = "The switch statement"; +var BUGNUMBER= "315988"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +ONE = new Number(1); +ZERO = new Number(0); +var A = new String("A"); +var B = new String("B"); +TRUE = new Boolean( true ); +FALSE = new Boolean( false ); +UNDEFINED = void 0; +NULL = null; + +SwitchTest( ZERO, "ZERO" ); +SwitchTest( NULL, "NULL" ); +SwitchTest( UNDEFINED, "UNDEFINED" ); +SwitchTest( FALSE, "FALSE" ); +SwitchTest( false, "false" ); +SwitchTest( 0, "0" ); + +SwitchTest ( TRUE, "TRUE" ); +SwitchTest( 1, "1" ); +SwitchTest( ONE, "ONE" ); +SwitchTest( true, "true" ); + +SwitchTest( "a", "a" ); +SwitchTest( A, "A" ); +SwitchTest( "b", "b" ); +SwitchTest( B, "B" ); + +SwitchTest( new Boolean( true ), "default" ); +SwitchTest( new Boolean(false ), "default" ); +SwitchTest( new String( "A" ), "default" ); +SwitchTest( new Number( 0 ), "default" ); + +test(); + +function SwitchTest( input, expect ) { + var result = ""; + + switch ( input ) { + default: result += "default"; break; + case "a": result += "a"; break; + case "b": result += "b"; break; + case A: result += "A"; break; + case B: result += "B"; break; + case new Boolean(true): result += "new TRUE"; break; + case new Boolean(false): result += "new FALSE"; break; + case NULL: result += "NULL"; break; + case UNDEFINED: result += "UNDEFINED"; break; + case true: result += "true"; break; + case false: result += "false"; break; + case TRUE: result += "TRUE"; break; + case FALSE: result += "FALSE"; break; + case 0: result += "0"; break; + case 1: result += "1"; break; + case new Number(0) : result += "new ZERO"; break; + case new Number(1) : result += "new ONE"; break; + case ONE: result += "ONE"; break; + case ZERO: result += "ZERO"; break; + } + + new TestCase( + SECTION, + "switch with no breaks: input is " + input, + expect, + result ); +} diff --git a/js/src/tests/ecma_2/Statements/try-001.js b/js/src/tests/ecma_2/Statements/try-001.js new file mode 100644 index 000000000..931837cd3 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/try-001.js @@ -0,0 +1,83 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: try-001.js + * ECMA Section: + * Description: The try statement + * + * This test contains try, catch, and finally blocks. An exception is + * sometimes thrown by a function called from within the try block. + * + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = ""; +var VERSION = "ECMA_2"; +var TITLE = "The try statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +var INVALID_JAVA_INTEGER_VALUE = "Invalid value for java.lang.Integer constructor"; + +TryNewJavaInteger( "3.14159", INVALID_JAVA_INTEGER_VALUE ); +TryNewJavaInteger( NaN, INVALID_JAVA_INTEGER_VALUE ); +TryNewJavaInteger( 0, 0 ); +TryNewJavaInteger( -1, -1 ); +TryNewJavaInteger( 1, 1 ); +TryNewJavaInteger( Infinity, Infinity ); + +test(); + +/** + * Check to see if the input is valid for java.lang.Integer. If it is + * not valid, throw INVALID_JAVA_INTEGER_VALUE. If input is valid, + * return Number( v ) + * + */ + +function newJavaInteger( v ) { + value = Number( v ); + if ( Math.floor(value) != value || isNaN(value) ) { + throw ( INVALID_JAVA_INTEGER_VALUE ); + } else { + return value; + } +} + +/** + * Call newJavaInteger( value ) from within a try block. Catch any + * exception, and store it in result. Verify that we got the right + * return value from newJavaInteger in cases in which we do not expect + * exceptions, and that we got the exception in cases where an exception + * was expected. + */ +function TryNewJavaInteger( value, expect ) { + var finalTest = false; + + try { + result = newJavaInteger( value ); + } catch ( e ) { + result = String( e ); + } finally { + finalTest = true; + } + new TestCase( + SECTION, + "newJavaValue( " + value +" )", + expect, + result); + + new TestCase( + SECTION, + "newJavaValue( " + value +" ) hit finally block", + true, + finalTest); + +} + diff --git a/js/src/tests/ecma_2/Statements/try-003.js b/js/src/tests/ecma_2/Statements/try-003.js new file mode 100644 index 000000000..9244fcb5d --- /dev/null +++ b/js/src/tests/ecma_2/Statements/try-003.js @@ -0,0 +1,82 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: try-003.js + * ECMA Section: + * Description: The try statement + * + * This test has a try with no catch, and a finally. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "try-003"; +var VERSION = "ECMA_2"; +var TITLE = "The try statement"; +var BUGNUMBER="http://scopus.mcom.com/bugsplat/show_bug.cgi?id=313585"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +// Tests start here. + +TrySomething( "x = \"hi\"", false ); +TrySomething( "throw \"boo\"", true ); +TrySomething( "throw 3", true ); + +test(); + +/** + * This function contains a try block with no catch block, + * but it does have a finally block. Try to evaluate expressions + * that do and do not throw exceptions. + */ + +function TrySomething( expression, throwing ) { + innerFinally = "FAIL: DID NOT HIT INNER FINALLY BLOCK"; + if (throwing) { + outerCatch = "FAILED: NO EXCEPTION CAUGHT"; + } else { + outerCatch = "PASS"; + } + outerFinally = "FAIL: DID NOT HIT OUTER FINALLY BLOCK"; + + try { + try { + eval( expression ); + } finally { + innerFinally = "PASS"; + } + } catch ( e ) { + if (throwing) { + outerCatch = "PASS"; + } else { + outerCatch = "FAIL: HIT OUTER CATCH BLOCK"; + } + } finally { + outerFinally = "PASS"; + } + + + new TestCase( + SECTION, + "eval( " + expression +" )", + "PASS", + innerFinally ); + new TestCase( + SECTION, + "eval( " + expression +" )", + "PASS", + outerCatch ); + new TestCase( + SECTION, + "eval( " + expression +" )", + "PASS", + outerFinally ); + + +} diff --git a/js/src/tests/ecma_2/Statements/try-004.js b/js/src/tests/ecma_2/Statements/try-004.js new file mode 100644 index 000000000..dbc554559 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/try-004.js @@ -0,0 +1,54 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: try-004.js + * ECMA Section: + * Description: The try statement + * + * This test has a try with one catch block but no finally. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "try-004"; +var VERSION = "ECMA_2"; +var TITLE = "The try statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +TryToCatch( "Math.PI", Math.PI ); +TryToCatch( "Thrower(5)", "Caught 5" ); +TryToCatch( "Thrower(\"some random exception\")", "Caught some random exception" ); + +test(); + +function Thrower( v ) { + throw "Caught " + v; +} + +/** + * Evaluate a string. Catch any exceptions thrown. If no exception is + * expected, verify the result of the evaluation. If an exception is + * expected, verify that we got the right exception. + */ + +function TryToCatch( value, expect ) { + try { + result = eval( value ); + } catch ( e ) { + result = e; + } + + new TestCase( + SECTION, + "eval( " + value +" )", + expect, + result ); +} + + diff --git a/js/src/tests/ecma_2/Statements/try-005.js b/js/src/tests/ecma_2/Statements/try-005.js new file mode 100644 index 000000000..b50c5bf53 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/try-005.js @@ -0,0 +1,57 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: try-005.js + * ECMA Section: + * Description: The try statement + * + * This test has a try with one catch block but no finally. Same + * as try-004, but the eval statement is called from a function, not + * directly from within the try block. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "try-005"; +var VERSION = "ECMA_2"; +var TITLE = "The try statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +TryToCatch( "Math.PI", Math.PI ); +TryToCatch( "Thrower(5)", "Caught 5" ); +TryToCatch( "Thrower(\"some random exception\")", "Caught some random exception" ); + +test(); + +function Thrower( v ) { + throw "Caught " + v; +} +function Eval( v ) { + return eval( v ); +} + +/** + * Evaluate a string. Catch any exceptions thrown. If no exception is + * expected, verify the result of the evaluation. If an exception is + * expected, verify that we got the right exception. + */ + +function TryToCatch( value, expect ) { + try { + result = Eval( value ); + } catch ( e ) { + result = e; + } + + new TestCase( + SECTION, + "eval( " + value +" )", + expect, + result ); +} diff --git a/js/src/tests/ecma_2/Statements/try-006.js b/js/src/tests/ecma_2/Statements/try-006.js new file mode 100644 index 000000000..0a85200a1 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/try-006.js @@ -0,0 +1,87 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: try-006.js + * ECMA Section: + * Description: The try statement + * + * Throw an exception from within a With block in a try block. Verify + * that any expected exceptions are caught. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "try-006"; +var VERSION = "ECMA_2"; +var TITLE = "The try statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +/** + * This is the "check" function for test objects that will + * throw an exception. + */ +function throwException() { + throw EXCEPTION_STRING +": " + this.valueOf(); +} +var EXCEPTION_STRING = "Exception thrown:"; + +/** + * This is the "check" function for test objects that do not + * throw an exception + */ +function noException() { + return this.valueOf(); +} + +/** + * Add test cases here + */ +TryWith( new TryObject( "hello", throwException, true )); +TryWith( new TryObject( "hola", noException, false )); + +/** + * Run the test. + */ + +test(); + +/** + * This is the object that will be the "this" in a with block. + */ +function TryObject( value, fun, exception ) { + this.value = value; + this.exception = exception; + + this.valueOf = new Function ( "return this.value" ); + this.check = fun; +} + +/** + * This function has the try block that has a with block within it. + * Test cases are added in this function. Within the with block, the + * object's "check" function is called. If the test object's exception + * property is true, we expect the result to be the exception value. + * If exception is false, then we expect the result to be the value of + * the object. + */ +function TryWith( object ) { + try { + with ( object ) { + result = check(); + } + } catch ( e ) { + result = e; + } + + new TestCase( + SECTION, + "TryWith( " + object.value +" )", + (object.exception ? EXCEPTION_STRING +": " + object.valueOf() : object.valueOf()), + result ); +} diff --git a/js/src/tests/ecma_2/Statements/try-007.js b/js/src/tests/ecma_2/Statements/try-007.js new file mode 100644 index 000000000..93e08a0a1 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/try-007.js @@ -0,0 +1,92 @@ +/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: try-007.js + * ECMA Section: + * Description: The try statement + * + * This test has a for-in statement within a try block. + * + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "try-007"; +var VERSION = "ECMA_2"; +var TITLE = "The try statement: for-in"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +/** + * This is the "check" function for test objects that will + * throw an exception. + */ +function throwException() { + throw EXCEPTION_STRING +": " + this.valueOf(); +} +var EXCEPTION_STRING = "Exception thrown:"; + +/** + * This is the "check" function for test objects that do not + * throw an exception + */ +function noException() { + return this.valueOf(); +} + +/** + * Add test cases here + */ +TryForIn( new TryObject( "hello", throwException, true )); +TryForIn( new TryObject( "hola", noException, false )); + +/** + * Run the test. + */ + +test(); + +/** + * This is the object that will be the "this" in a with block. + * The check function is either throwException() or noException(). + * See above. + * + */ +function TryObject( value, fun, exception ) { + this.value = value; + this.exception = exception; + + this.check = fun; + this.valueOf = function () { return this.value; } +} + +/** + * This function has a for-in statement within a try block. Test cases + * are added after the try-catch-finally statement. Within the for-in + * block, call a function that can throw an exception. Verify that any + * exceptions are properly caught. + */ + +function TryForIn( object ) { + try { + for ( p in object ) { + if ( typeof object[p] == "function" ) { + result = object[p](); + } + } + } catch ( e ) { + result = e; + } + + new TestCase( + SECTION, + "TryForIn( " + object+ " )", + (object.exception ? EXCEPTION_STRING +": " + object.value : object.value), + result ); + +} diff --git a/js/src/tests/ecma_2/Statements/try-008.js b/js/src/tests/ecma_2/Statements/try-008.js new file mode 100644 index 000000000..d47ced0ae --- /dev/null +++ b/js/src/tests/ecma_2/Statements/try-008.js @@ -0,0 +1,59 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: try-008.js + * ECMA Section: + * Description: The try statement + * + * This test has a try block in a constructor. + * + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "try-008"; +var VERSION = "ECMA_2"; +var TITLE = "The try statement: try in a constructor"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +function Integer( value, exception ) { + try { + this.value = checkValue( value ); + } catch ( e ) { + this.value = e.toString(); + } + + new TestCase( + SECTION, + "Integer( " + value +" )", + (exception ? INVALID_INTEGER_VALUE +": " + value : this.value), + this.value ); +} + +var INVALID_INTEGER_VALUE = "Invalid value for java.lang.Integer constructor"; + +function checkValue( value ) { + if ( Math.floor(value) != value || isNaN(value) ) { + throw ( INVALID_INTEGER_VALUE +": " + value ); + } else { + return value; + } +} + +// add test cases + +new Integer( 3, false ); +new Integer( NaN, true ); +new Integer( 0, false ); +new Integer( Infinity, false ); +new Integer( -2.12, true ); +new Integer( Math.LN2, true ); + + +test(); diff --git a/js/src/tests/ecma_2/Statements/try-009.js b/js/src/tests/ecma_2/Statements/try-009.js new file mode 100644 index 000000000..d3e0ebaa2 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/try-009.js @@ -0,0 +1,66 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: try-009.js + * ECMA Section: + * Description: The try statement + * + * This test has a try block within a while block. Verify that an exception + * breaks out of the while. I don't really know why this is an interesting + * test case but Mike Shaver had two of these so what the hey. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "try-009"; +var VERSION = "ECMA_2"; +var TITLE = "The try statement: try in a while block"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +var EXCEPTION_STRING = "Exception thrown: "; +var NO_EXCEPTION_STRING = "No exception thrown: "; + + +TryInWhile( new TryObject( "hello", ThrowException, true ) ); +TryInWhile( new TryObject( "aloha", NoException, false )); + +test(); + +function TryObject( value, throwFunction, result ) { + this.value = value; + this.thrower = throwFunction; + this.result = result; +} +function ThrowException() { + throw EXCEPTION_STRING + this.value; +} +function NoException() { + return NO_EXCEPTION_STRING + this.value; +} +function TryInWhile( object ) { + result = null; + while ( true ) { + try { + object.thrower(); + result = NO_EXCEPTION_STRING + object.value; + break; + } catch ( e ) { + result = e; + break; + } + } + + new TestCase( + SECTION, + "( "+ object +".thrower() )", + (object.result + ? EXCEPTION_STRING + object.value : + NO_EXCEPTION_STRING + object.value), + result ); +} diff --git a/js/src/tests/ecma_2/Statements/try-010.js b/js/src/tests/ecma_2/Statements/try-010.js new file mode 100644 index 000000000..5ee472c3b --- /dev/null +++ b/js/src/tests/ecma_2/Statements/try-010.js @@ -0,0 +1,73 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: try-010.js + * ECMA Section: + * Description: The try statement + * + * This has a try block nested in the try block. Verify that the + * exception is caught by the right try block, and all finally blocks + * are executed. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "try-010"; +var VERSION = "ECMA_2"; +var TITLE = "The try statement: try in a tryblock"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +var EXCEPTION_STRING = "Exception thrown: "; +var NO_EXCEPTION_STRING = "No exception thrown: "; + + +NestedTry( new TryObject( "No Exceptions Thrown", NoException, NoException, 43 ) ); +NestedTry( new TryObject( "Throw Exception in Outer Try", ThrowException, NoException, 48 )); +NestedTry( new TryObject( "Throw Exception in Inner Try", NoException, ThrowException, 45 )); +NestedTry( new TryObject( "Throw Exception in Both Trys", ThrowException, ThrowException, 48 )); + +test(); + +function TryObject( description, tryOne, tryTwo, result ) { + this.description = description; + this.tryOne = tryOne; + this.tryTwo = tryTwo; + this.result = result; +} +function ThrowException() { + throw EXCEPTION_STRING + this.value; +} +function NoException() { + return NO_EXCEPTION_STRING + this.value; +} +function NestedTry( object ) { + result = 0; + try { + object.tryOne(); + result += 1; + try { + object.tryTwo(); + result += 2; + } catch ( e ) { + result +=4; + } finally { + result += 8; + } + } catch ( e ) { + result += 16; + } finally { + result += 32; + } + + new TestCase( + SECTION, + object.description, + object.result, + result ); +} diff --git a/js/src/tests/ecma_2/Statements/try-012.js b/js/src/tests/ecma_2/Statements/try-012.js new file mode 100644 index 000000000..3ee7d1026 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/try-012.js @@ -0,0 +1,95 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: try-012.js + * ECMA Section: + * Description: The try statement + * + * This test has a try with no catch, and a finally. This is like try-003, + * but throws from a finally block, not the try block. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "try-012"; +var VERSION = "ECMA_2"; +var TITLE = "The try statement"; +var BUGNUMBER="336872"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +// Tests start here. + +TrySomething( "x = \"hi\"", true ); +TrySomething( "throw \"boo\"", true ); +TrySomething( "throw 3", true ); + +test(); + +/** + * This function contains a try block with no catch block, + * but it does have a finally block. Try to evaluate expressions + * that do and do not throw exceptions. + * + * The productioni TryStatement Block Finally is evaluated as follows: + * 1. Evaluate Block + * 2. Evaluate Finally + * 3. If Result(2).type is normal return result 1 (in the test case, result 1 has + * the completion type throw) + * 4. return result 2 (does not get hit in this case) + * + */ + +function TrySomething( expression, throwing ) { + innerFinally = "FAIL: DID NOT HIT INNER FINALLY BLOCK"; + if (throwing) { + outerCatch = "FAILED: NO EXCEPTION CAUGHT"; + } else { + outerCatch = "PASS"; + } + outerFinally = "FAIL: DID NOT HIT OUTER FINALLY BLOCK"; + + + // If the inner finally does not throw an exception, the result + // of the try block should be returned. (Type of inner return + // value should be throw if finally executes correctly + + try { + try { + throw 0; + } finally { + innerFinally = "PASS"; + eval( expression ); + } + } catch ( e ) { + if (throwing) { + outerCatch = "PASS"; + } else { + outerCatch = "FAIL: HIT OUTER CATCH BLOCK"; + } + } finally { + outerFinally = "PASS"; + } + + + new TestCase( + SECTION, + "eval( " + expression +" ): evaluated inner finally block", + "PASS", + innerFinally ); + new TestCase( + SECTION, + "eval( " + expression +" ): evaluated outer catch block ", + "PASS", + outerCatch ); + new TestCase( + SECTION, + "eval( " + expression +" ): evaluated outer finally block", + "PASS", + outerFinally ); +} diff --git a/js/src/tests/ecma_2/Statements/while-001.js b/js/src/tests/ecma_2/Statements/while-001.js new file mode 100644 index 000000000..88fd8dfe8 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/while-001.js @@ -0,0 +1,42 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: while-001 + * ECMA Section: + * Description: while statement + * + * Verify that the while statement is not executed if the while expression is + * false + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "while-001"; +var VERSION = "ECMA_2"; +var TITLE = "while statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +DoWhile(); +test(); + +function DoWhile() { + result = "pass"; + + while (false) { + result = "fail"; + break; + } + + new TestCase( + SECTION, + "while statement: don't evaluate statement is expression is false", + "pass", + result ); + +} diff --git a/js/src/tests/ecma_2/Statements/while-002.js b/js/src/tests/ecma_2/Statements/while-002.js new file mode 100644 index 000000000..a3c315bf2 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/while-002.js @@ -0,0 +1,86 @@ +/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: while-002 + * ECMA Section: + * Description: while statement + * + * Verify that the while statement is not executed if the while expression is + * false + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "while-002"; +var VERSION = "ECMA_2"; +var TITLE = "while statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +DoWhile( new DoWhileObject( + "while expression is null", + null, + "result = \"fail: should not have evaluated statements in while block;break" + ) ); + +DoWhile( new DoWhileObject( + "while expression is undefined", + void 0, + "result = \"fail: should not have evaluated statements in while block; break" + )); + +DoWhile( new DoWhileObject( + "while expression is 0", + 0, + "result = \"fail: should not have evaluated statements in while block; break;" + )); + +DoWhile( new DoWhileObject( + "while expression is eval(\"\")", + eval(""), + "result = \"fail: should not have evaluated statements in while block; break" + )); + +DoWhile( new DoWhileObject( + "while expression is NaN", + NaN, + "result = \"fail: should not have evaluated statements in while block; break" + )); + +test(); + +function DoWhileObject( d, e, s ) { + this.description = d; + this.whileExpression = e; + this.statements = s; +} + +function DoWhile( object ) { + result = "pass"; + + while ( expression = object.whileExpression ) { + eval( object.statements ); + } + + // verify that the while expression was evaluated + + new TestCase( + SECTION, + "verify that while expression was evaluated (should be "+ + object.whileExpression +")", + "pass", + (object.whileExpression == expression || + ( isNaN(object.whileExpression) && isNaN(expression) ) + ) ? "pass" : "fail" ); + + new TestCase( + SECTION, + object.description, + "pass", + result ); +} diff --git a/js/src/tests/ecma_2/Statements/while-003.js b/js/src/tests/ecma_2/Statements/while-003.js new file mode 100644 index 000000000..b47d09a7a --- /dev/null +++ b/js/src/tests/ecma_2/Statements/while-003.js @@ -0,0 +1,87 @@ +/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: while-003 + * ECMA Section: + * Description: while statement + * + * The while expression evaluates to true, Statement returns abrupt completion. + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "while-003"; +var VERSION = "ECMA_2"; +var TITLE = "while statement"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +DoWhile( new DoWhileObject( + "while expression is true", + true, + "result = \"pass\";" )); + +DoWhile( new DoWhileObject( + "while expression is 1", + 1, + "result = \"pass\";" )); + +DoWhile( new DoWhileObject( + "while expression is new Boolean(false)", + new Boolean(false), + "result = \"pass\";" )); + +DoWhile( new DoWhileObject( + "while expression is new Object()", + new Object(), + "result = \"pass\";" )); + +DoWhile( new DoWhileObject( + "while expression is \"hi\"", + "hi", + "result = \"pass\";" )); +/* + DoWhile( new DoWhileObject( + "while expression has a continue in it", + "true", + "if ( i == void 0 ) i = 0; result=\"pass\"; if ( ++i == 1 ) {continue;} else {break;} result=\"fail\";" + )); +*/ +test(); + +function DoWhileObject( d, e, s ) { + this.description = d; + this.whileExpression = e; + this.statements = s; +} + +function DoWhile( object ) { + result = "fail: statements in while block were not evaluated"; + + while ( expression = object.whileExpression ) { + eval( object.statements ); + break; + } + + // verify that the while expression was evaluated + + new TestCase( + SECTION, + "verify that while expression was evaluated (should be "+ + object.whileExpression +")", + "pass", + (object.whileExpression == expression || + ( isNaN(object.whileExpression) && isNaN(expression) ) + ) ? "pass" : "fail" ); + + new TestCase( + SECTION, + object.description, + "pass", + result ); +} diff --git a/js/src/tests/ecma_2/Statements/while-004.js b/js/src/tests/ecma_2/Statements/while-004.js new file mode 100644 index 000000000..393e680e5 --- /dev/null +++ b/js/src/tests/ecma_2/Statements/while-004.js @@ -0,0 +1,217 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +/** + * File Name: while-004 + * ECMA Section: + * Description: while statement + * + * Author: christine@netscape.com + * Date: 11 August 1998 + */ +var SECTION = "while-004"; +var VERSION = "ECMA_2"; +var TITLE = "while statement"; +var BUGNUMBER="316725"; + +startTest(); +writeHeaderToLog( SECTION + " "+ TITLE); + +DoWhile_1(); +DoWhile_2(); +DoWhile_3(); +DoWhile_4(); +DoWhile_5(); + +test(); + +/** + * Break out of a while by calling return. + * + * Tests: 12.6.2 step 6. + */ +function dowhile() { + result = "pass"; + + while (true) { + return result; + result = "fail: hit code after return statement"; + break; + } +} + +function DoWhile_1() { + description = "return statement in a while block"; + + result = dowhile(); + + new TestCase( + SECTION, + "DoWhile_1" + description, + "pass", + result ); +} + +/** + * While with a labeled continue statement. Verify that statements + * after the continue statement are not evaluated. + * + * Tests: 12.6.2 step 8. + * + */ +function DoWhile_2() { + var description = "while with a labeled continue statement"; + var result1 = "pass"; + var result2 = "fail: did not execute code after loop, but inside label"; + var i = 0; + var j = 0; + +theloop: + while( i++ < 10 ) { + j++; + continue theloop; + result1 = "failed: hit code after continue statement"; + } + result2 = "pass"; + + new TestCase( + SECTION, + "DoWhile_2: " +description + " - code inside the loop, before the continue should be executed ("+j+")", + true, + j == 10 ); + + new TestCase( + SECTION, + "DoWhile_2: " +description +" - code after labeled continue should not be executed", + "pass", + result1 ); + + new TestCase( + SECTION, + "DoWhile_2: " +description +" - code after loop but inside label should be executed", + "pass", + result2 ); +} + +/** + * While with a labeled break. + * + */ +function DoWhile_3() { + var description = "while with a labeled break statement"; + var result1 = "pass"; + var result2 = "pass"; + var result3 = "fail: did not get to code after label"; + +woohoo: { + while( true ) { + break woohoo; + result1 = "fail: got to code after a break"; + } + result2 = "fail: got to code outside of loop but inside label"; + } + + result3 = "pass"; + + new TestCase( + SECTION, + "DoWhile_3: " +description +" - verify break out of loop", + "pass", + result1 ); + + + new TestCase( + SECTION, + "DoWhile_3: " +description +" - verify break out of label", + "pass", + result2 ); + + new TestCase( + SECTION, + "DoWhile_3: " +description + " - verify correct exit from label", + "pass", + result3 ); +} + + +/** + * Labled while with an unlabeled break + * + */ +function DoWhile_4() { + var description = "labeled while with an unlabeled break"; + var result1 = "pass"; + var result2 = "pass"; + var result3 = "fail: did not evaluate statement after label"; + +woohooboy: { + while( true ) { + break woohooboy; + result1 = "fail: got to code after the break"; + } + result2 = "fail: broke out of while, but not out of label"; + } + result3 = "pass"; + + new TestCase( + SECTION, + "DoWhile_4: " +description +" - verify break out of while loop", + "pass", + result1 ); + + new TestCase( + SECTION, + "DoWhile_4: " +description + " - verify break out of label", + "pass", + result2 ); + + new TestCase( + SECTION, + "DoWhile_4: " +description +" - verify that statements after label are evaluated", + "pass", + result3 ); +} + +/** + * in this case, should behave the same way as + * + * + */ +function DoWhile_5() { + var description = "while with a labeled continue statement"; + var result1 = "pass"; + var result2 = "fail: did not execute code after loop, but inside label"; + var i = 0; + var j = 0; + +theloop: { + j++; + while( i++ < 10 ) { + continue; + result1 = "failed: hit code after continue statement"; + } + result2 = "pass"; + } + + new TestCase( + SECTION, + "DoWhile_5: " +description + " - continue should not execute statements above the loop", + true, + ( j == 1 ) ); + + new TestCase( + SECTION, + "DoWhile_5: " +description +" - code after labeled continue should not be executed", + "pass", + result1 ); + + new TestCase( + SECTION, + "DoWhile_5: " +description +" - code after loop but inside label should be executed", + "pass", + result2 ); +} + |