Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / TypeConversion / 9.2.js
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
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is Mozilla Communicator client code, released
16  * March 31, 1998.
17  *
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.
22  *
23  * Contributor(s): christine@netscape.com
24  *                 Jesse Ruderman
25  *
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.
37  *
38  * ***** END LICENSE BLOCK ***** */
39
40
41 /**
42    File Name:          9.2.js
43    ECMA Section:       9.2  Type Conversion:  ToBoolean
44    Description:        rules for converting an argument to a boolean.
45    undefined           false
46    Null                false
47    Boolean             input argument( no conversion )
48    Number              returns false for 0, -0, and NaN
49    otherwise return true
50    String              return false if the string is empty
51    (length is 0) otherwise the result is
52    true
53    Object              all return true
54
55    Author:             christine@netscape.com
56    Date:               14 july 1997
57 */
58 var SECTION = "9.2";
59 var VERSION = "ECMA_1";
60 startTest();
61 var TITLE   = "ToBoolean";
62
63 writeHeaderToLog( SECTION + " "+ TITLE);
64
65 // special cases here
66
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("") );
77
78 // normal test cases here
79
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") );
88
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("")) );
92
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()) );
96
97 new TestCase( SECTION,   "Boolean(new Array())",          true,   Boolean(new Array()) );
98
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)) );
103
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)) );
107
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) );
113
114 // bug 375793
115 new TestCase( SECTION,
116               "NaN ? true : false",
117               false,
118               (NaN ? true : false) );
119 new TestCase( SECTION,
120               "1000 % 0 ? true : false",
121               false,
122               (1000 % 0 ? true : false) );
123 new TestCase( SECTION,
124               "(function(a,b){ return a % b ? true : false })(1000, 0)",
125               false,
126               ((function(a,b){ return a % b ? true : false })(1000, 0)) );
127
128 new TestCase( SECTION,
129               "(function(x) { return !(x) })(0/0)",
130               true,
131               ((function(x) { return !(x) })(0/0)) );
132 new TestCase( SECTION,
133               "!(0/0)",
134               true,
135               (!(0/0)) );
136 test();
137