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 16function fooAnd(x: String|null, y: String|null): string { 17 if (x != null && y != null) { 18 return x + " " + y; 19 } else if (x == null && y == null) { 20 return "null"; 21 } else if (x != null && y == null) { 22 return x; 23 } else if (x == null && y != null) { 24 return y; 25 } else { 26 throw new Error("Unreachable"); 27 } 28} 29 30function fooOr1(x: String|null, y: String|null): string { 31 if (x != null || y != null) { 32 return "case 1"; 33 } else if (x == null && y == null) { 34 return "null"; 35 } else { 36 throw new Error("Unreachable"); 37 } 38} 39 40function fooOr2(x: String|null, y: String|null): string { 41 if (x == null || y == null) { 42 return "case 1"; 43 } else if (x != null && y != null) { 44 return x + " " + y; 45 } else { 46 throw new Error("Unreachable"); 47 } 48} 49 50function main(): void { 51 assert(fooAnd("Test", "string") == "Test string"); 52 assert(fooAnd("Test", null) == "Test"); 53 assert(fooAnd(null, "string") == "string"); 54 assert(fooAnd(null, null) == "null"); 55 56 assert(fooOr1("Test", "string") == "case 1"); 57 assert(fooOr1("Test", null) == "case 1"); 58 assert(fooOr1(null, "string") == "case 1"); 59 assert(fooOr1(null, null) == "null"); 60 61 assert(fooOr2("Test", "string") == "Test string"); 62 assert(fooOr2("Test", null) == "case 1"); 63 assert(fooOr2(null, "string") == "case 1"); 64 assert(fooOr2(null, null) == "case 1"); 65} 66