1/* 2 * Copyright (c) 2023-2025 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 main(): void { 17 let a : double = 2147483649.0; // 2^31 + 1 18 assertEQ(a as float, 2147483648) // rounded 19 assertEQ(a as long, 2147483649) 20 assertEQ(a as int, 2147483647 ) // 2^31 - 1 == MAX_INT == 0xFFFFFFFF 21 assertEQ(a as short, -1 ) // 0xFFFF 22 assertEQ(a as char, c'\uFFFF') 23 assertEQ(a as byte, -1 )// 0xFF 24 25 a = -2147483649.0 // 2^31 + 1 26 assertEQ(a as float, -2147483648) // rounded 27 assertEQ(a as long, -2147483649) 28 assertEQ(a as int, -2147483648) // -2^31 == MIN_INT == 0x10000000 29 assertEQ(a as short, 0) 30 assertEQ(a as char, c'\u0000') 31 assertEQ(a as byte, 0) 32 33 let b : float = 70000.9921875f; 34 assertEQ(b as double, 70000.9921875) 35 assertEQ(b as long, 70000 ) // rounded, 70000 == 0x11170 36 assertEQ(b as int, 70000) 37 assertEQ(b as short, 4464) // 4464 == 0x1170 38 assertEQ(b as char, c'\u1170') 39 assertEQ(b as byte, 112) // 112 == 0x70 40 41 let c : long = 1193046; // 1193046 == 0x123456 42 assertEQ(c as int, 1193046) 43 assertEQ(c as short, 13398) // 13398 == 0x3456 44 assertEQ(c as char, c'\u3456') 45 assertEQ(c as byte, 86) // 86 == 0x56 46 47 let d : int = 126977; // 65537 == 0x1F001 48 assertEQ(d as short, -4095) // -4095 == 0xF001 49 assertEQ(d as char, c'\uF001') 50 assertEQ(d as byte, 1) // 1 == 0x01 51 52 let e : short = -30875; // -30875 == 0x8765 53 assertEQ(e as double, -30875.0) 54 assertEQ(e as float, -30875.0) 55 assertEQ(e as long, -30875) // -30875 == 0xFFFFFFFFFFFF8765 56 assertEQ(e as int, -30875) // -30875 == 0xFFFF8765 57 assertEQ(e as char, c'\u8765') 58 assertEQ(e as byte, 101) // 101 == 0x65 59 60 let f : char = c'\uF001'; 61 assertEQ(f as double, 61441.0) 62 assertEQ(f as float, 61441.0) 63 assertEQ(f as long, 61441) // 61441 == 0x000000000000F001 64 assertEQ(f as int, 61441) // 61441 == 0x0000F001 65 assertEQ(f as short, 0xf001 as short) // -4095 == 0xF001 66 assertEQ(f as short, -4095) 67 68 let g : byte = -128; 69 assertEQ(g as double, -128.0) 70 assertEQ(g as float, -128.0) 71 assertEQ(g as long, -128) 72 assertEQ(g as int, -128) 73 assertEQ(g as short, -128) 74 assertEQ(g as char, c'\uFF80') 75 assertEQ((-128) as byte, -128) 76 assertEQ((-129) as byte, 127) 77 78 let i : boolean = true; 79 assertEQ(i as boolean, true) 80 81 i = false; 82 assertEQ(i as boolean, false) 83 84 assertEQ(4294967296.0 as byte, -1) 85 assertEQ(4294967296.0 as char, c'\uFFFF') 86 assertEQ(4294967296.0 as short, -1) 87 assertEQ(4294967296.0 as int, Int.MAX_VALUE) 88 assertEQ(4294967296.0 as long, 4294967296) 89 assertEQ(-4294967296.0 as byte, 0) 90 assertEQ(-4294967296.0 as char, c'\u0000') 91 assertEQ(-4294967296.0 as short, 0) 92 assertEQ(-4294967296.0 as int, Int.MIN_VALUE) 93 assertEQ(-4294967296.0 as long, -4294967296) 94 95 return; 96} 97