1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s): christine@netscape.com
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
43 ECMA Section: 9.2 Type Conversion: ToBoolean
44 Description: rules for converting an argument to a boolean.
47 Boolean input argument( no conversion )
48 Number returns false for 0, -0, and NaN
50 String return false if the string is empty
51 (length is 0) otherwise the result is
53 Object all return true
55 Author: christine@netscape.com
59 var VERSION = "ECMA_1";
61 var TITLE = "ToBoolean";
63 writeHeaderToLog( SECTION + " "+ TITLE);
67 new TestCase( SECTION, "Boolean()", false, Boolean() );
68 new TestCase( SECTION, "Boolean(var x)", false, Boolean(eval("var x")) );
69 new TestCase( SECTION, "Boolean(void 0)", false, Boolean(void 0) );
70 new TestCase( SECTION, "Boolean(null)", false, Boolean(null) );
71 new TestCase( SECTION, "Boolean(false)", false, Boolean(false) );
72 new TestCase( SECTION, "Boolean(true)", true, Boolean(true) );
73 new TestCase( SECTION, "Boolean(0)", false, Boolean(0) );
74 new TestCase( SECTION, "Boolean(-0)", false, Boolean(-0) );
75 new TestCase( SECTION, "Boolean(NaN)", false, Boolean(Number.NaN) );
76 new TestCase( SECTION, "Boolean('')", false, Boolean("") );
78 // normal test cases here
80 new TestCase( SECTION, "Boolean(Infinity)", true, Boolean(Number.POSITIVE_INFINITY) );
81 new TestCase( SECTION, "Boolean(-Infinity)", true, Boolean(Number.NEGATIVE_INFINITY) );
82 new TestCase( SECTION, "Boolean(Math.PI)", true, Boolean(Math.PI) );
83 new TestCase( SECTION, "Boolean(1)", true, Boolean(1) );
84 new TestCase( SECTION, "Boolean(-1)", true, Boolean(-1) );
85 new TestCase( SECTION, "Boolean([tab])", true, Boolean("\t") );
86 new TestCase( SECTION, "Boolean('0')", true, Boolean("0") );
87 new TestCase( SECTION, "Boolean('string')", true, Boolean("string") );
89 // ToBoolean (object) should always return true.
90 new TestCase( SECTION, "Boolean(new String() )", true, Boolean(new String()) );
91 new TestCase( SECTION, "Boolean(new String('') )", true, Boolean(new String("")) );
93 new TestCase( SECTION, "Boolean(new Boolean(true))", true, Boolean(new Boolean(true)) );
94 new TestCase( SECTION, "Boolean(new Boolean(false))", true, Boolean(new Boolean(false)) );
95 new TestCase( SECTION, "Boolean(new Boolean() )", true, Boolean(new Boolean()) );
97 new TestCase( SECTION, "Boolean(new Array())", true, Boolean(new Array()) );
99 new TestCase( SECTION, "Boolean(new Number())", true, Boolean(new Number()) );
100 new TestCase( SECTION, "Boolean(new Number(-0))", true, Boolean(new Number(-0)) );
101 new TestCase( SECTION, "Boolean(new Number(0))", true, Boolean(new Number(0)) );
102 new TestCase( SECTION, "Boolean(new Number(NaN))", true, Boolean(new Number(Number.NaN)) );
104 new TestCase( SECTION, "Boolean(new Number(-1))", true, Boolean(new Number(-1)) );
105 new TestCase( SECTION, "Boolean(new Number(Infinity))", true, Boolean(new Number(Number.POSITIVE_INFINITY)) );
106 new TestCase( SECTION, "Boolean(new Number(-Infinity))",true, Boolean(new Number(Number.NEGATIVE_INFINITY)) );
108 new TestCase( SECTION, "Boolean(new Object())", true, Boolean(new Object()) );
109 new TestCase( SECTION, "Boolean(new Function())", true, Boolean(new Function()) );
110 new TestCase( SECTION, "Boolean(new Date())", true, Boolean(new Date()) );
111 new TestCase( SECTION, "Boolean(new Date(0))", true, Boolean(new Date(0)) );
112 new TestCase( SECTION, "Boolean(Math)", true, Boolean(Math) );
115 new TestCase( SECTION,
116 "NaN ? true : false",
118 (NaN ? true : false) );
119 new TestCase( SECTION,
120 "1000 % 0 ? true : false",
122 (1000 % 0 ? true : false) );
123 new TestCase( SECTION,
124 "(function(a,b){ return a % b ? true : false })(1000, 0)",
126 ((function(a,b){ return a % b ? true : false })(1000, 0)) );
128 new TestCase( SECTION,
129 "(function(x) { return !(x) })(0/0)",
131 ((function(x) { return !(x) })(0/0)) );
132 new TestCase( SECTION,