1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/* 17 * @tc.name:setobjectwithproto 18 * @tc.desc:test set object with proto 19 * @tc.type: FUNC 20 * @tc.require: issueI5NO8G 21 */ 22var object = { 23 __proto__: null 24}; 25 26assert_equal(Object.getPrototypeOf(object),null); 27 28class C32 extends String { 29 30} 31let obj1 = new C32(); 32assert_equal(obj1.__proto__ == C32.prototype,true); 33assert_equal(C32.__proto__ == String,true); 34C32.__proto__ = Array; 35let obj2 = new C32(); 36assert_equal(obj2.__proto__ == C32.prototype,true); 37assert_equal(C32.__proto__ == Array,true); 38 39Float64Array.__proto__ = Float32Array 40new Float64Array(); 41 42var normalValues = [ 43 1, 44 true, 45 'string', 46 Symbol() 47]; 48 49function getObjects() { 50 function func() {} 51 return [ 52 func, 53 new func(), 54 {x: 5}, 55 /regexp/, 56 ['array'], 57 new Date(), 58 new Number(1), 59 new Boolean(true), 60 new String('str'), 61 Object(Symbol()) 62 ]; 63} 64 65function TestSetPrototypeOf1() { 66 var object = {}; 67 var oldProto = { 68 x: 'old x', 69 y: 'old y' 70 }; 71 Object.setPrototypeOf(object, oldProto); 72 assert_equal(object.x,"old x"); 73 assert_equal(object.y,"old y"); 74 var newProto = { 75 x: 'new x' 76 }; 77 Object.setPrototypeOf(object, newProto); 78 assert_equal(object.x,'new x'); 79} 80TestSetPrototypeOf1(); 81 82function TestSetPrototypeOf2() { 83 for (var i = 0; i < normalValues.length; i++) { 84 var value = normalValues[i]; 85 var proto = Object.getPrototypeOf(value); 86 if (Object.setPrototypeOf(value, {}) != value) { 87 return false; 88 } 89 if (proto != Object.getPrototypeOf(value)) { 90 return false; 91 } 92 } 93 return true; 94} 95 96assert_equal(TestSetPrototypeOf2(),true); 97 98function TestSetPrototypeOf(object, proto) { 99 return (Object.setPrototypeOf(object, proto) === object) && 100 (Object.getPrototypeOf(object) === proto); 101} 102 103function TestSetPrototypeOf3() { 104 var objects1 = getObjects(); 105 var objects2 = getObjects(); 106 for (var i = 0; i < objects1.length; i++) { 107 for (var j = 0; j < objects2.length; j++) { 108 if (!TestSetPrototypeOf(objects1[i], objects2[j])) { 109 return false; 110 } 111 } 112 } 113 return true; 114} 115assert_equal(TestSetPrototypeOf3(),true); 116 117test_end();