|
| 1 | +// Copyright 2015 Samsung Electronics Co., Ltd. |
| 2 | +// Copyright 2015 University of Szeged. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +// check properties |
| 17 | +assert(Object.getOwnPropertyDescriptor(String.prototype.indexOf, 'length').configurable === false); |
| 18 | + |
| 19 | +assert(Object.getOwnPropertyDescriptor(String.prototype.indexOf, 'length').enumerable === false); |
| 20 | + |
| 21 | +assert(Object.getOwnPropertyDescriptor(String.prototype.indexOf, 'length').writable === false); |
| 22 | + |
| 23 | +assert(String.prototype.indexOf.length === 1); |
| 24 | + |
| 25 | +assert("Hello world, welcome to the universe.".indexOf("welcome") === 13); |
| 26 | + |
| 27 | +assert("Hello world, welcome to the universe.".indexOf("Hello world, welcome to the universe.") === 0); |
| 28 | + |
| 29 | +assert("Hello world, welcome to the universe.".indexOf("welcome",10) == 13); |
| 30 | + |
| 31 | +assert("Hello world, welcome to the universe.".indexOf("welcome",-100) == 13); |
| 32 | + |
| 33 | +assert("Hello world, welcome to the universe.".indexOf("welcome", 15) === -1); |
| 34 | + |
| 35 | +assert("Hello world, welcome to the universe.".indexOf("o", 15) === 17); |
| 36 | + |
| 37 | +// check utf8 strings |
| 38 | +assert("\uFFA2".indexOf("\uFFA2") === 0); |
| 39 | + |
| 40 | +assert("\uFFA2".indexOf("A") === -1); |
| 41 | + |
| 42 | +assert("w2\uFFA2A".indexOf("A") === 3); |
| 43 | + |
| 44 | +assert("w2\u1D306A".indexOf("A") === 4); |
| 45 | + |
| 46 | +// check surrogate pairs |
| 47 | +assert("\uD834\uDF06".indexOf("\uDF06") === 1); |
| 48 | + |
| 49 | +assert("\uD834\uDF06w2\u1D306D".indexOf("D") === 6); |
| 50 | + |
| 51 | +assert("\ud800\dc00".indexOf("\dc00") === 1); |
| 52 | + |
| 53 | +// check prefix search |
| 54 | +assert("aaaabaaa".indexOf("aaaba") === 1); |
| 55 | + |
| 56 | +// check empty string |
| 57 | +assert(String.prototype.indexOf.call(new String()) === -1); |
| 58 | + |
| 59 | +assert(String.prototype.indexOf.call("","") === 0); |
| 60 | + |
| 61 | +// check NaN |
| 62 | +assert("Hello world, welcome to the universe.".indexOf(NaN) === -1); |
| 63 | + |
| 64 | +assert("Hello world, welcome to the universe.".indexOf("welcome",NaN) === 13); |
| 65 | + |
| 66 | +// check Object |
| 67 | +assert(String.prototype.indexOf.call({}) === -1); |
| 68 | + |
| 69 | +// check +-Inf |
| 70 | +assert("hello world!".indexOf("world", -Infinity) === 6); |
| 71 | + |
| 72 | +assert("hello world!".indexOf("world", Infinity) === -1); |
| 73 | + |
| 74 | +// check numbers |
| 75 | +assert("hello world!".indexOf(-1) === -1); |
| 76 | + |
| 77 | +assert("hello 0 world!".indexOf(-0) === 6); |
| 78 | + |
| 79 | +// check undefined |
| 80 | +assert("hello world!".indexOf(undefined) === -1); |
| 81 | + |
| 82 | +var undefined_var; |
| 83 | +assert("Hello world, welcome to the universe.".indexOf("welcome", undefined_var) === 13); |
| 84 | + |
| 85 | +// check booleans |
| 86 | +assert("true".indexOf(true, false) === 0); |
| 87 | + |
| 88 | +// check this is undefined |
| 89 | +try { |
| 90 | + String.prototype.indexOf.call(undefined); |
| 91 | + assert(false); |
| 92 | +} catch(e) { |
| 93 | + assert(e instanceof TypeError); |
| 94 | +} |
| 95 | + |
| 96 | +// check this is null |
| 97 | +try { |
| 98 | + String.prototype.indexOf.call(null); |
| 99 | + assert(false); |
| 100 | +} catch(e) { |
| 101 | + assert(e instanceof TypeError); |
| 102 | +} |
| 103 | + |
| 104 | +// check coercible - undefined |
| 105 | +try { |
| 106 | + assert(true.indexOf()); |
| 107 | + assert(false); |
| 108 | +} catch (e) { |
| 109 | + assert(e instanceof TypeError); |
| 110 | +} |
| 111 | + |
| 112 | +// check coercible - null |
| 113 | +try { |
| 114 | + assert(isNaN(String.prototype.indexOf.call(null, 0))); |
| 115 | + assert(false); |
| 116 | +} catch (e) { |
| 117 | + assert(e instanceof TypeError); |
| 118 | +} |
| 119 | + |
| 120 | +// check coercible - Boolean |
| 121 | +assert(String.prototype.indexOf.call(true, "e") === 3); |
| 122 | + |
| 123 | +// check coercible - Object |
| 124 | +var test_object = {firstName:"John", lastName:"Doe"}; |
| 125 | +assert(String.prototype.indexOf.call(test_object, "Obj") === 8); |
| 126 | + |
| 127 | +// check coercible - Number |
| 128 | +assert(String.prototype.indexOf.call(123, "2") === 1); |
0 commit comments