From ef07eeb2e857ec8316613ab4b33802a2982cc4b9 Mon Sep 17 00:00:00 2001 From: Jabba Hutt Date: Mon, 31 Mar 2025 20:14:35 +0300 Subject: [PATCH] Create CTS tests for chapter 6.4.5 Created tests for the chapter 6.4.5 Casting Conversions to Type Parameter Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/IBXREC Test: $STATIC_CORE_DIR/tests/tests-u-runner/runner.sh --ets-cts --force-generate --filter "06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/*" --build-dir=$BUILD_DIR Signed-off-by: Jabba Hutt --- .../cast.ets | 52 +++++++++ .../cast.params.yaml | 103 ++++++++++++++++++ .../cast_neg.ets | 65 +++++++++++ .../cast_neg.params.yaml | 83 ++++++++++++++ 4 files changed, 303 insertions(+) create mode 100644 static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast.ets create mode 100644 static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast.params.yaml create mode 100644 static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast_neg.ets create mode 100644 static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast_neg.params.yaml diff --git a/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast.ets b/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast.ets new file mode 100644 index 0000000000..cfd533a96e --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast.ets @@ -0,0 +1,52 @@ +/* + * 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. + */ + +{%- for c in cases %} + {%- for from in c.from %} + +/*--- +desc: >- + Casting conversion to type parameter attempts to convert an expression + of any type to type parameter type which is to be specified as target type. +---*/ + +function cast(p: Object|null|undefined) { + return p as T // attempt to convert of any type to type parameter +} + +{{from.decl}} + +cast<{{c.to}}>({{from.arg}}) + + +// Auxiliary stuff + +class A {} +class B {} +class C {} + +interface I {} +interface J {} + +class CI implements I {} +class CJ implements J {} + +class D extends C {} + +enum EN { a, b, c } +enum ES { a = "a", b = "b", c = "c" } + + {%- endfor %} +{%- endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast.params.yaml b/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast.params.yaml new file mode 100644 index 0000000000..f988286172 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast.params.yaml @@ -0,0 +1,103 @@ +# 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. +--- +cases: + - to: 'Object' + from: + - arg: 'new Object' + - arg: 'new C' + - arg: 'new CI' + - arg: '[ true ]' + - arg: '[ 0 ]' + - arg: '[ new C ]' + - arg: '[ new CI ]' + - arg: 't' + decl: 'const t: [number, boolean] = [ 0, true ]' + - arg: 'f' + decl: 'const f = () => {}' + - arg: 'new C' + - arg: 'new CI' + - arg: '0' + - arg: 'true' + - arg: '"just a test"' + - arg: '0n' + - arg: 'EN.a' + - arg: 'ES.a' + - arg: 'null' + - arg: 'undefined' + - to: 'C' + from: + - arg: 'new C' + - arg: 'new D' + - to: 'I' + from: + - arg: 'new CI' + - to: 'byte[]' + from: + - arg: 'array' + decl: 'const array: byte[] = [ 0 ]' + - to: 'C[]' + from: + - arg: '[ new C ]' + - arg: '[ new D ]' + - to: 'I[]' + from: + - arg: '[ new CI ]' + - to: '() => void' + from: + - arg: 'f' + decl: 'const f = () => {}' + - to: 'B|J' + from: + - arg: 'new B' + - arg: 'new CJ' + - to: 'A|boolean' + from: + - arg: 'new A' + - arg: 'true' + - to: 'string' + from: + - arg: '"just a test"' + - arg: 'c' + decl: 'const c: char = 0' + - to: 'bigint' + from: + - arg: '0n' + - arg: 'n' + decl: 'const n: char = 0' + - arg: 'n' + decl: 'const n: byte = 0' + - arg: 'n' + decl: 'const n: short = 0' + - arg: 'n' + decl: 'const n: int = 0' + - arg: 'n' + decl: 'const n: long = 0' + - arg: 'n' + decl: 'const n: float = 0' + - arg: 'n' + decl: 'const n: double = 0' + - arg: 'n' + decl: 'const n: number = 0' + - to: 'EN' + from: + - arg: 'EN.a' + - to: 'ES' + from: + - arg: 'ES.a' + - to: 'null' + from: + - arg: 'null' + - to: 'undefined' + from: + - arg: 'undefined' diff --git a/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast_neg.ets b/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast_neg.ets new file mode 100644 index 0000000000..3eeddd7fc1 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast_neg.ets @@ -0,0 +1,65 @@ +/* + * 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. + */ + +{%- for c1 in cases %} + {%- for c2 in cases %} + {%- if c1.to != c2.to and c1.to != 'Object' %} + {%- for from in c2.from %} +/*--- +desc: >- + This conversion causes a runtime error (ClassCastError) + if the runtime type of an expression is not the target type. +---*/ + +function cast(p: Object|null|undefined) { + return p as T // attempt to convert of any type to type parameter +} + +{{from.decl}} + +function main() { + try { + cast<{{c1.to}}>({{from.arg}}) + } + catch (error: Error) { + if (error instanceof ClassCastError) // OK + return + + throw error // Unknown error + } + throw new AssertionError("Expected a ClassCastError") +} + +// Auxiliary stuff + +class A {} +class B {} +class C {} + +interface I {} +interface J {} + +class CI implements I {} +class CJ implements J {} + +class D extends C {} + +enum EN { a, b, c } +enum ES { a = "a", b = "b", c = "c" } + + {%- endfor %} + {%- endif %} + {%- endfor %} +{%- endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast_neg.params.yaml b/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast_neg.params.yaml new file mode 100644 index 0000000000..67c6239189 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/06.contexts_and_conversions/04.casting_contexts_and_conversions/05.casting_conversions_to_type_parameter/cast_neg.params.yaml @@ -0,0 +1,83 @@ +# 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. +--- +cases: + - to: 'Object' + from: + - arg: 'new Object' + - to: 'C' + from: + - arg: 'new C' + - arg: 'new D' + - to: 'I' + from: + - arg: 'new CI' + - to: 'byte[]' + from: + - arg: 'array' + decl: 'const array: byte[] = [ 0 ]' + - to: 'C[]' + from: + - arg: '[ new C ]' + - arg: '[ new D ]' + - to: 'I[]' + from: + - arg: '[ new CI ]' + - to: '() => void' + from: + - arg: 'f' + decl: 'const f = () => {}' + - to: 'B|J' + from: + - arg: 'new B' + - arg: 'new CJ' + - to: 'A|boolean' + from: + - arg: 'new A' + - arg: 'true' + - to: 'string' + from: + - arg: '"just a test"' + - arg: 'c' + decl: 'const c: char = 0' + - to: 'bigint' + from: + - arg: '0n' + - arg: 'n' + decl: 'const n: char = 0' + - arg: 'n' + decl: 'const n: byte = 0' + - arg: 'n' + decl: 'const n: short = 0' + - arg: 'n' + decl: 'const n: int = 0' + - arg: 'n' + decl: 'const n: long = 0' + - arg: 'n' + decl: 'const n: float = 0' + - arg: 'n' + decl: 'const n: double = 0' + - arg: 'n' + decl: 'const n: number = 0' + - to: 'EN' + from: + - arg: 'EN.a' + - to: 'ES' + from: + - arg: 'ES.a' + - to: 'null' + from: + - arg: 'null' + - to: 'undefined' + from: + - arg: 'undefined' -- Gitee