From 9526cc0eb4b75adb5d84aec99aa23d9c27e35b53 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Fri, 27 Jun 2025 11:33:09 +0800 Subject: [PATCH] widening_numeric_conversions_test https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICI7YR Signed-off-by: yp9522 --- .../widening_numeric_invalid_conversions.ets | 58 +++++++++++++ .../ets/widening_numeric_conversions.ets | 82 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/widening_numeric_invalid_conversions.ets create mode 100755 ets2panda/test/runtime/ets/widening_numeric_conversions.ets diff --git a/ets2panda/test/ast/compiler/ets/widening_numeric_invalid_conversions.ets b/ets2panda/test/ast/compiler/ets/widening_numeric_invalid_conversions.ets new file mode 100644 index 0000000000..69e4532d31 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/widening_numeric_invalid_conversions.ets @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function main() { + let shortVal: short = 30000; + let shortToByte: byte = shortVal; + + let intVal: int = 2147483647; + let intToByte: byte = intVal; + let intToShort: short = intVal; + + let longVal: long = 9223372036854775807; + let longToByte: byte = longVal; + let longToShort: short = longVal; + let longToInt: int = longVal; + + let floatVal: float = 3.14; + let floatToByte: byte = floatVal; + let floatToShort: short = floatVal; + let floatToInt: int = floatVal; + let floatToLong: long = floatVal; + + let doubleVal: double = 3.14; + let doubleToByte: byte = doubleVal; + let doubleToShort: short = doubleVal; + let doubleToInt: int = doubleVal; + let doubleToLong: long = doubleVal; + let doubleToFloat: float = doubleVal; +} +/* @@? 18:27 TypeError: Type 'Short' cannot be assigned to type 'Byte' */ +/* @@? 21:25 TypeError: Type 'Int' cannot be assigned to type 'Byte' */ +/* @@? 22:27 TypeError: Type 'Int' cannot be assigned to type 'Short' */ +/* @@? 25:26 TypeError: Type 'Long' cannot be assigned to type 'Byte' */ +/* @@? 26:28 TypeError: Type 'Long' cannot be assigned to type 'Short' */ +/* @@? 27:24 TypeError: Type 'Long' cannot be assigned to type 'Int' */ +/* @@? 29:25 TypeError: Type 'Double' cannot be assigned to type 'Float' */ +/* @@? 29:25 TypeError: Floating-point value cannot be converted */ +/* @@? 30:27 TypeError: Type 'Float' cannot be assigned to type 'Byte' */ +/* @@? 31:29 TypeError: Type 'Float' cannot be assigned to type 'Short' */ +/* @@? 32:25 TypeError: Type 'Float' cannot be assigned to type 'Int' */ +/* @@? 33:27 TypeError: Type 'Float' cannot be assigned to type 'Long' */ +/* @@? 36:28 TypeError: Type 'Double' cannot be assigned to type 'Byte' */ +/* @@? 37:30 TypeError: Type 'Double' cannot be assigned to type 'Short' */ +/* @@? 38:26 TypeError: Type 'Double' cannot be assigned to type 'Int' */ +/* @@? 39:28 TypeError: Type 'Double' cannot be assigned to type 'Long' */ +/* @@? 40:30 TypeError: Type 'Double' cannot be assigned to type 'Float' */ diff --git a/ets2panda/test/runtime/ets/widening_numeric_conversions.ets b/ets2panda/test/runtime/ets/widening_numeric_conversions.ets new file mode 100755 index 0000000000..35ffca5c0d --- /dev/null +++ b/ets2panda/test/runtime/ets/widening_numeric_conversions.ets @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function main() { + let byteVal: byte = 100; + let byteToShort: short = byteVal; + let byteToInt: int = byteVal; + let byteToLong: long = byteVal; + let byteToFloat: float = byteVal; + let byteToDouble: double = byteVal; + arktest.assertEQ(byteToShort, 100 as short); + arktest.assertEQ(byteToInt, 100 as int); + arktest.assertEQ(byteToLong, 100 as long); + arktest.assertEQ(byteToFloat, 100 as float); + arktest.assertEQ(byteToDouble, 100 as double); + + let shortVal: short = 30000; + let shortToInt: int = shortVal; + let shortToLong: long = shortVal; + let shortToFloat: float = shortVal; + let shortToDouble: double = shortVal; + arktest.assertEQ(shortToInt, 30000 as int); + arktest.assertEQ(shortToLong, 30000 as long); + arktest.assertEQ(shortToFloat, 30000 as float); + arktest.assertEQ(shortToDouble, 30000 as double); + + let intVal: int = 2147483647; + let intToLong: long = intVal; + let intToFloat: float = intVal; + let intToDouble: double = intVal; + arktest.assertEQ(intToLong, 2147483647 as long); + arktest.assertEQ(intToFloat, 2147483647 as float); + arktest.assertEQ(intToDouble, 2147483647 as double); + + let longVal: long = 9223372036854775807; + let longToFloat: float = longVal; + let longToDouble: double = longVal; + arktest.assertEQ(longToFloat, 9223372036854775807 as float); + arktest.assertEQ(longToDouble, 9223372036854775807 as double); + + let floatVal: float = 3; + let floatToDouble: double = floatVal; + arktest.assertEQ(floatToDouble, 3 as double); + + enum EnumInt: int { + Zero = 0, + One = 1, + Max = 2147483647 + } + let enmuIntToInt: int = EnumInt.Max; + let enmuIntToLong: long = EnumInt.Max; + let enmuIntToFloat: float = EnumInt.Max; + let enmuIntToDouble: double = EnumInt.Max; + arktest.assertEQ(enmuIntToInt, 2147483647 as int); + arktest.assertEQ(enmuIntToLong, 2147483647 as long); + arktest.assertEQ(enmuIntToFloat, 2147483647 as float); + arktest.assertEQ(enmuIntToDouble, 2147483647 as double); + + enum EnumLong: long { + Zero = 0, + One = 1, + Max = 9223372036854775807 + } + let enmuLongToLong: long = EnumLong.Max; + let enmuLongToFloat: float = EnumLong.Max; + let enmuLongToDouble: double = EnumLong.Max; + arktest.assertEQ(enmuLongToLong, 9223372036854775807 as long); + arktest.assertEQ(enmuLongToFloat, 9223372036854775807 as float); + arktest.assertEQ(enmuLongToDouble, 9223372036854775807 as double); +} -- Gitee