12fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// Copyright 2013 the V8 project authors. All rights reserved.
22fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
32fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma//
42fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// Redistribution and use in source and binary forms, with or without
52fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// modification, are permitted provided that the following conditions
62fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// are met:
72fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// 1.  Redistributions of source code must retain the above copyright
82fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma//     notice, this list of conditions and the following disclaimer.
92fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// 2.  Redistributions in binary form must reproduce the above copyright
102fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma//     notice, this list of conditions and the following disclaimer in the
112fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma//     documentation and/or other materials provided with the distribution.
122fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma//
132fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
142fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
152fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
162fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
172fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
182fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
192fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
202fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
212fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
222fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
232fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma
242fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharmadescription(
252fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma"This test checks that using the typeof operator on a JavaScript value and comparing it to a constant string works as expected."
2640afb6667d93946d0631c07d2bc3e1bff2865082Arun Sharma);
272fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma
282fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharmafunction isUndefined(a)
292fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma{
302fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma    return typeof a == "undefined";
312fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma}
322fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma
332fce54102cdb03aa2d3105c750685dc7cf2677b1Arun SharmashouldBeTrue("isUndefined(undefined)");
342fce54102cdb03aa2d3105c750685dc7cf2677b1Arun SharmashouldBeFalse("isUndefined(1)");
352fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma
362fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharmafunction isUndefinedStrict(a)
372fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma{
382fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma    return typeof a === "undefined";
392fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma}
402fce54102cdb03aa2d3105c750685dc7cf2677b1Arun Sharma
412fce54102cdb03aa2d3105c750685dc7cf2677b1Arun SharmashouldBeTrue("isUndefinedStrict(undefined)");
422fce54102cdb03aa2d3105c750685dc7cf2677b1Arun SharmashouldBeFalse("isUndefinedStrict(1)");
43
44function isBoolean(a)
45{
46    return typeof a == "boolean";
47}
48
49shouldBeTrue("isBoolean(true)");
50shouldBeTrue("isBoolean(false)");
51shouldBeFalse("isBoolean(1)");
52
53function isBooleanStrict(a)
54{
55    return typeof a === "boolean";
56}
57
58shouldBeTrue("isBooleanStrict(true)");
59shouldBeTrue("isBooleanStrict(false)");
60shouldBeFalse("isBooleanStrict(1)");
61
62function isNumber(a)
63{
64    return typeof a == "number";
65}
66
67shouldBeTrue("isNumber(1)");
68shouldBeFalse("isNumber(undefined)");
69
70function isNumberStrict(a)
71{
72    return typeof a === "number";
73}
74
75shouldBeTrue("isNumberStrict(1)");
76shouldBeFalse("isNumberStrict(undefined)");
77
78function isString(a)
79{
80    return typeof a == "string";
81}
82
83shouldBeTrue("isString('string')");
84shouldBeFalse("isString(1)");
85
86function isStringStrict(a)
87{
88    return typeof a === "string";
89}
90
91shouldBeTrue("isStringStrict('string')");
92shouldBeFalse("isStringStrict(1)");
93
94function isObject(a)
95{
96    return typeof a == "object";
97}
98
99shouldBeTrue("isObject({ })");
100shouldBeFalse("isObject(1)");
101
102function isObjectStrict(a)
103{
104    return typeof a === "object";
105}
106
107shouldBeTrue("isObjectStrict({ })");
108shouldBeFalse("isObjectStrict(1)");
109
110function isFunction(a)
111{
112    return typeof a == "function";
113}
114
115shouldBeTrue("isFunction(function () { })");
116shouldBeFalse("isFunction(1)");
117
118function isFunctionStrict(a)
119{
120    return typeof a === "function";
121}
122
123shouldBeTrue("isFunctionStrict(function () { })");
124shouldBeFalse("isFunctionStrict(1)");
125
126function complexIsUndefinedTest()
127{
128    function replace_formats() {
129        var o = ["text", 0];
130        if (typeof o == "string") {
131        } else if (typeof o == "undefined") {
132        } else if (typeof o == "object" && typeof o[0] == "string") {
133            return "PASS";
134        }
135        return "FAIL";
136    };
137
138    return "%d".replace(/%d/, replace_formats);
139}
140shouldBe("complexIsUndefinedTest()", "'PASS'");
141
142function complexIsBooleanTest()
143{
144    function replace_formats() {
145        var o = ["text", 0];
146        if (typeof o == "string") {
147        } else if (typeof o == "boolean") {
148        } else if (typeof o == "object" && typeof o[0] == "string") {
149            return "PASS";
150        }
151        return "FAIL";
152    };
153
154    return "%d".replace(/%d/, replace_formats);
155}
156shouldBe("complexIsBooleanTest()", "'PASS'");
157
158function complexIsNumberTest()
159{
160    function replace_formats() {
161        var o = ["text", 0];
162        if (typeof o == "string") {
163        } else if (typeof o == "number") {
164        } else if (typeof o == "object" && typeof o[0] == "string") {
165            return "PASS";
166        }
167        return "FAIL";
168    };
169
170    return "%d".replace(/%d/, replace_formats);
171}
172shouldBe("complexIsNumberTest()", "'PASS'");
173
174function complexIsStringTest()
175{
176    function replace_formats() {
177        var o = ["text", 0];
178        if (typeof o == "string") {
179        } else if (typeof o == "string") {
180        } else if (typeof o == "object" && typeof o[0] == "string") {
181            return "PASS";
182        }
183        return "FAIL";
184    };
185
186    return "%d".replace(/%d/, replace_formats);
187}
188shouldBe("complexIsStringTest()", "'PASS'");
189
190function complexIsObjectTest()
191{
192    var a = ["text", 0];
193    function replace_formats() {
194        var o = function () { };
195        if (typeof o == "string") {
196        } else if (typeof o == "object") {
197        } else if (typeof o == "function" && typeof a[0] == "string") {
198            return "PASS";
199        }
200        return "FAIL";
201    };
202
203    return "%d".replace(/%d/, replace_formats);
204}
205shouldBe("complexIsObjectTest()", "'PASS'");
206
207function complexIsFunctionTest()
208{
209    function replace_formats() {
210        var o = ["text", 0];
211        if (typeof o == "string") {
212        } else if (typeof o == "function") {
213        } else if (typeof o == "object" && typeof o[0] == "string") {
214            return "PASS";
215        }
216        return "FAIL";
217    };
218
219    return "%d".replace(/%d/, replace_formats);
220}
221shouldBe("complexIsFunctionTest()", "'PASS'");
222
223function complexIsUndefinedStrictTest()
224{
225    function replace_formats() {
226        var o = ["text", 0];
227        if (typeof o == "string") {
228        } else if (typeof o === "undefined") {
229        } else if (typeof o == "object" && typeof o[0] == "string") {
230            return "PASS";
231        }
232        return "FAIL";
233    };
234
235    return "%d".replace(/%d/, replace_formats);
236}
237shouldBe("complexIsUndefinedStrictTest()", "'PASS'");
238
239function complexIsBooleanStrictTest()
240{
241    function replace_formats() {
242        var o = ["text", 0];
243        if (typeof o == "string") {
244        } else if (typeof o === "boolean") {
245        } else if (typeof o == "object" && typeof o[0] == "string") {
246            return "PASS";
247        }
248        return "FAIL";
249    };
250
251    return "%d".replace(/%d/, replace_formats);
252}
253shouldBe("complexIsBooleanStrictTest()", "'PASS'");
254
255function complexIsNumberStrictTest()
256{
257    function replace_formats() {
258        var o = ["text", 0];
259        if (typeof o == "string") {
260        } else if (typeof o === "number") {
261        } else if (typeof o == "object" && typeof o[0] == "string") {
262            return "PASS";
263        }
264        return "FAIL";
265    };
266
267    return "%d".replace(/%d/, replace_formats);
268}
269shouldBe("complexIsNumberStrictTest()", "'PASS'");
270
271function complexIsStringStrictTest()
272{
273    function replace_formats() {
274        var o = ["text", 0];
275        if (typeof o == "string") {
276        } else if (typeof o === "string") {
277        } else if (typeof o == "object" && typeof o[0] == "string") {
278            return "PASS";
279        }
280        return "FAIL";
281    };
282
283    return "%d".replace(/%d/, replace_formats);
284}
285shouldBe("complexIsStringStrictTest()", "'PASS'");
286
287function complexIsObjectStrictTest()
288{
289    var a = ["text", 0];
290    function replace_formats() {
291        var o = function () { };
292        if (typeof o == "string") {
293        } else if (typeof o === "object") {
294        } else if (typeof o == "function" && typeof a[0] == "string") {
295            return "PASS";
296        }
297        return "FAIL";
298    };
299
300    return "%d".replace(/%d/, replace_formats);
301}
302shouldBe("complexIsObjectStrictTest()", "'PASS'");
303
304function complexIsFunctionStrictTest()
305{
306    function replace_formats() {
307        var o = ["text", 0];
308        if (typeof o == "string") {
309        } else if (typeof o === "function") {
310        } else if (typeof o == "object" && typeof o[0] == "string") {
311            return "PASS";
312        }
313        return "FAIL";
314    };
315
316    return "%d".replace(/%d/, replace_formats);
317}
318shouldBe("complexIsFunctionStrictTest()", "'PASS'");
319