1/* 2 * Copyright (c) 2024 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 16class C { 17 readonly a: boolean 18 constructor(a_: boolean = false) { 19 this.a = a_; 20 } 21} 22 23function foo1(x: C|null|undefined): string { 24 if (x == null || !x.a) { 25 return x != null ? "false1" : "null"; 26 } else { 27 return x.a ? "true2" : "false2"; 28 } 29} 30 31function foo2(x: C|null|undefined): string { 32 if (x != null && x.a) { 33 return "true"; 34 } else { 35 return x != null ? "false" : "null"; 36 } 37} 38 39function bar(x: C|null|undefined, y: boolean, z: boolean): string { 40 if ((x instanceof C && y) || (x instanceof C && z)) { 41 return (x.a ? "true1" : "false1") + y + z; 42 } else { 43 return (x != null ? (x.a ? "true2" : "false2") : "null") + y + z; 44 } 45} 46 47function main(): void { 48 assert(foo1(null) == "null"); 49 assert(foo2(null) == "null"); 50 assert(bar(null, true, true) == "nulltruetrue"); 51 assert(bar(null, true, false) == "nulltruefalse"); 52 assert(bar(null, false, true) == "nullfalsetrue"); 53 assert(bar(null, false, false) == "nullfalsefalse"); 54 55 assert(foo1(undefined) == "null"); 56 assert(foo2(undefined) == "null"); 57 assert(bar(undefined, true, true) == "nulltruetrue"); 58 assert(bar(undefined, true, false) == "nulltruefalse"); 59 assert(bar(undefined, false, true) == "nullfalsetrue"); 60 assert(bar(undefined, false, false) == "nullfalsefalse"); 61 62 let c = new C(); 63 assert(foo1(c) == "false1"); 64 assert(foo2(c) == "false"); 65 assert(bar(c, true, true) == "false1truetrue"); 66 assert(bar(c, true, false) == "false1truefalse"); 67 assert(bar(c, false, true) == "false1falsetrue"); 68 assert(bar(c, false, false) == "false2falsefalse"); 69 70 c = new C(true); 71 assert(foo1(c) == "true2"); 72 assert(foo2(c) == "true"); 73 assert(bar(c, true, true) == "true1truetrue"); 74 assert(bar(c, true, false) == "true1truefalse"); 75 assert(bar(c, false, true) == "true1falsetrue"); 76 assert(bar(c, false, false) == "true2falsefalse"); 77} 78