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 ***** */
44 ECMA Section: 9.2 Type Conversion: ToBoolean
45 Description: rules for converting an argument to a boolean.
48 Boolean input argument( no conversion )
49 Number returns false for 0, -0, and NaN
51 String return false if the string is empty
52 (length is 0) otherwise the result is
54 Object all return true
56 Author: christine@netscape.com
60 var VERSION = "ECMA_1";
62 var TITLE = "ToBoolean";
64 writeHeaderToLog( SECTION + " "+ TITLE);
68 new TestCase( SECTION, "Boolean()", false, Boolean() );
69 new TestCase( SECTION, "Boolean(var x)", false, Boolean(eval("var x")) );
70 new TestCase( SECTION, "Boolean(void 0)", false, Boolean(void 0) );
71 new TestCase( SECTION, "Boolean(null)", false, Boolean(null) );
72 new TestCase( SECTION, "Boolean(false)", false, Boolean(false) );
73 new TestCase( SECTION, "Boolean(true)", true, Boolean(true) );
74 new TestCase( SECTION, "Boolean(0)", false, Boolean(0) );
75 new TestCase( SECTION, "Boolean(-0)", false, Boolean(-0) );
76 new TestCase( SECTION, "Boolean(NaN)", false, Boolean(Number.NaN) );
77 new TestCase( SECTION, "Boolean('')", false, Boolean("") );
79 // normal test cases here
81 new TestCase( SECTION, "Boolean(Infinity)", true, Boolean(Number.POSITIVE_INFINITY) );
82 new TestCase( SECTION, "Boolean(-Infinity)", true, Boolean(Number.NEGATIVE_INFINITY) );
83 new TestCase( SECTION, "Boolean(Math.PI)", true, Boolean(Math.PI) );
84 new TestCase( SECTION, "Boolean(1)", true, Boolean(1) );
85 new TestCase( SECTION, "Boolean(-1)", true, Boolean(-1) );
86 new TestCase( SECTION, "Boolean([tab])", true, Boolean("\t") );
87 new TestCase( SECTION, "Boolean('0')", true, Boolean("0") );
88 new TestCase( SECTION, "Boolean('string')", true, Boolean("string") );
90 // ToBoolean (object) should always return true.
91 new TestCase( SECTION, "Boolean(new String() )", true, Boolean(new String()) );
92 new TestCase( SECTION, "Boolean(new String('') )", true, Boolean(new String("")) );
94 new TestCase( SECTION, "Boolean(new Boolean(true))", true, Boolean(new Boolean(true)) );
95 new TestCase( SECTION, "Boolean(new Boolean(false))", true, Boolean(new Boolean(false)) );
96 new TestCase( SECTION, "Boolean(new Boolean() )", true, Boolean(new Boolean()) );
98 new TestCase( SECTION, "Boolean(new Array())", true, Boolean(new Array()) );
100 new TestCase( SECTION, "Boolean(new Number())", true, Boolean(new Number()) );
101 new TestCase( SECTION, "Boolean(new Number(-0))", true, Boolean(new Number(-0)) );
102 new TestCase( SECTION, "Boolean(new Number(0))", true, Boolean(new Number(0)) );
103 new TestCase( SECTION, "Boolean(new Number(NaN))", true, Boolean(new Number(Number.NaN)) );
105 new TestCase( SECTION, "Boolean(new Number(-1))", true, Boolean(new Number(-1)) );
106 new TestCase( SECTION, "Boolean(new Number(Infinity))", true, Boolean(new Number(Number.POSITIVE_INFINITY)) );
107 new TestCase( SECTION, "Boolean(new Number(-Infinity))",true, Boolean(new Number(Number.NEGATIVE_INFINITY)) );
109 new TestCase( SECTION, "Boolean(new Object())", true, Boolean(new Object()) );
110 new TestCase( SECTION, "Boolean(new Function())", true, Boolean(new Function()) );
111 new TestCase( SECTION, "Boolean(new Date())", true, Boolean(new Date()) );
112 new TestCase( SECTION, "Boolean(new Date(0))", true, Boolean(new Date(0)) );
113 new TestCase( SECTION, "Boolean(Math)", true, Boolean(Math) );
116 new TestCase( SECTION,
117 "NaN ? true : false",
119 (NaN ? true : false) );
120 new TestCase( SECTION,
121 "1000 % 0 ? true : false",
123 (1000 % 0 ? true : false) );
124 new TestCase( SECTION,
125 "(function(a,b){ return a % b ? true : false })(1000, 0)",
127 ((function(a,b){ return a % b ? true : false })(1000, 0)) );
129 new TestCase( SECTION,
130 "(function(x) { return !(x) })(0/0)",
132 ((function(x) { return !(x) })(0/0)) );
133 new TestCase( SECTION,