From ef68f3c8e8ce8a64aa991d4381265287b10f5254 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Tue, 31 Oct 2023 19:18:01 +0300 Subject: [PATCH 1/4] feat(#217): add buggy example that reviels the problem --- src/it/betecode-to-eo/src/main/java/foo/Foo.java | 10 ++++++++++ src/it/betecode-to-eo/verify.groovy | 1 + 2 files changed, 11 insertions(+) create mode 100644 src/it/betecode-to-eo/src/main/java/foo/Foo.java diff --git a/src/it/betecode-to-eo/src/main/java/foo/Foo.java b/src/it/betecode-to-eo/src/main/java/foo/Foo.java new file mode 100644 index 000000000..19c284dac --- /dev/null +++ b/src/it/betecode-to-eo/src/main/java/foo/Foo.java @@ -0,0 +1,10 @@ +package foo; + +class Foo { + int bar(double x) { + if (x > 0.0d) { + return 5; + } + return 8; + } +} \ No newline at end of file diff --git a/src/it/betecode-to-eo/verify.groovy b/src/it/betecode-to-eo/verify.groovy index 8317807bd..1c3503115 100644 --- a/src/it/betecode-to-eo/verify.groovy +++ b/src/it/betecode-to-eo/verify.groovy @@ -26,4 +26,5 @@ String log = new File(basedir, 'build.log').text; assert log.contains("BUILD SUCCESS") //Check that we have generated XMIR object file. assert new File(basedir, 'target/jeo/xmir/org/eolang/jeo/Application.xmir').exists() +assert new File(basedir, 'target/jeo/xmir/foo/Foo.xmir').exists() true \ No newline at end of file From ac5c4617961068b9dfac06d7d0c7ea9e015cb561 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 1 Nov 2023 16:00:58 +0300 Subject: [PATCH 2/4] feat(#217): test HexData types --- .../eolang/jeo/representation/HexData.java | 8 ++- .../jeo/representation/HexDataTest.java | 69 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/eolang/jeo/representation/HexDataTest.java diff --git a/src/main/java/org/eolang/jeo/representation/HexData.java b/src/main/java/org/eolang/jeo/representation/HexData.java index b4827266b..d818955db 100644 --- a/src/main/java/org/eolang/jeo/representation/HexData.java +++ b/src/main/java/org/eolang/jeo/representation/HexData.java @@ -71,8 +71,14 @@ public String type() { final String res; if (this.data instanceof String) { res = "string"; - } else { + } else if (this.data instanceof Integer) { res = "int"; + } else if (this.data instanceof Float || this.data instanceof Double) { + res = "float"; + } else if (this.data instanceof Boolean) { + res = "bool"; + } else { + res = "bytes"; } return res; } diff --git a/src/test/java/org/eolang/jeo/representation/HexDataTest.java b/src/test/java/org/eolang/jeo/representation/HexDataTest.java new file mode 100644 index 000000000..f83eeaf92 --- /dev/null +++ b/src/test/java/org/eolang/jeo/representation/HexDataTest.java @@ -0,0 +1,69 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2023 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.jeo.representation; + +import java.util.stream.Stream; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test cases for {@link org.eolang.jeo.representation.HexData}. + * @since 0.1. + */ +class HexDataTest { + + @MethodSource("types") + @ParameterizedTest + void determinesTypeCorrectly(final Object data, final String type) { + MatcherAssert.assertThat( + String.format( + "Expected and actual types differ, the type for '%s' should be '%s'", + data, + type + ), + new HexData(data).type(), + Matchers.equalTo(type) + ); + } + + /** + * Arguments for {@link HexDataTest#determinesTypeCorrectly(Object, String)} test. + * @return Stream of arguments. + */ + static Stream types() { + return Stream.of( + Arguments.of(1, "int"), + Arguments.of("Hello!", "string"), + Arguments.of(new byte[]{1, 2, 3}, "bytes"), + Arguments.of(true, "bool"), + Arguments.of(0.1d, "float") + ); + } + +} \ No newline at end of file From e046526363de35f94c71a4c72bf1ca6feb405625 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 1 Nov 2023 16:17:13 +0300 Subject: [PATCH 3/4] feat(#217): add unit test for HexData --- .../eolang/jeo/representation/HexData.java | 19 ++++++++++- .../jeo/representation/HexDataTest.java | 33 +++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/eolang/jeo/representation/HexData.java b/src/main/java/org/eolang/jeo/representation/HexData.java index d818955db..cc271c0e4 100644 --- a/src/main/java/org/eolang/jeo/representation/HexData.java +++ b/src/main/java/org/eolang/jeo/representation/HexData.java @@ -54,11 +54,28 @@ public String value() { final byte[] res; if (this.data instanceof String) { res = ((String) this.data).getBytes(StandardCharsets.UTF_8); - } else { + } else if (this.data instanceof Integer) { res = ByteBuffer .allocate(Long.BYTES) .putLong((int) this.data) .array(); + } else if (this.data instanceof Float || this.data instanceof Double) { + res = ByteBuffer + .allocate(Long.BYTES) + .putDouble((double) this.data) + .array(); + } else if (this.data instanceof Boolean) { + if ((boolean) this.data) { + res = new byte[]{0x01}; + } else { + res = new byte[]{0x00}; + } + } else if (this.data instanceof byte[]) { + res = (byte[]) this.data; + } else { + throw new IllegalStateException( + String.format("Can't convert '%s' into hex string", this.data) + ); } return HexData.bytesToHex(res); } diff --git a/src/test/java/org/eolang/jeo/representation/HexDataTest.java b/src/test/java/org/eolang/jeo/representation/HexDataTest.java index f83eeaf92..35ddc4ed8 100644 --- a/src/test/java/org/eolang/jeo/representation/HexDataTest.java +++ b/src/test/java/org/eolang/jeo/representation/HexDataTest.java @@ -29,8 +29,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.junit.jupiter.params.provider.ValueSource; -import static org.junit.jupiter.api.Assertions.*; /** * Test cases for {@link org.eolang.jeo.representation.HexData}. @@ -52,6 +50,22 @@ void determinesTypeCorrectly(final Object data, final String type) { ); } + @MethodSource("values") + @ParameterizedTest + void convertsRawDataIntoHexString(final Object data, final String hex) { + MatcherAssert.assertThat( + String.format( + String.format( + "Expected and actual hex values differ, the value for '%s' should be '%s'", + data, + hex + ) + ), + new HexData(data).value(), + Matchers.equalTo(hex) + ); + } + /** * Arguments for {@link HexDataTest#determinesTypeCorrectly(Object, String)} test. * @return Stream of arguments. @@ -66,4 +80,19 @@ static Stream types() { ); } + /** + * Arguments for {@link HexDataTest#convertsRawDataIntoHexString(Object, String)}. + * @return Stream of arguments. + */ + static Stream values() { + return Stream.of( + Arguments.of(10, "00 00 00 00 00 00 00 0A"), + Arguments.of("Hello!", "48 65 6C 6C 6F 21"), + Arguments.of(new byte[]{1, 2, 3}, "01 02 03"), + Arguments.of(true, "01"), + Arguments.of(false, "00"), + Arguments.of(0.1d, "3F B9 99 99 99 99 99 9A") + ); + } + } \ No newline at end of file From 6cf17ecc9966d62e87b584dc0102edbffde9b10e Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 1 Nov 2023 17:55:55 +0300 Subject: [PATCH 4/4] feat(#217): save hardcoded value about jump label --- .../representation/directives/DirectivesMethod.java | 10 +++++++++- .../org/eolang/jeo/representation/HexDataTest.java | 5 ++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java b/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java index 2a634242c..afde64852 100644 --- a/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java +++ b/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java @@ -82,7 +82,15 @@ public void visitIntInsn(final int opcode, final int operand) { @Override public void visitJumpInsn(final int opcode, final Label label) { - this.opcode(opcode, label); + // @checkstyle MethodBodyCommentsCheck (10 lines) + // @todo #217:90min Save correct label value into XMIR. + // Currently we just save hardcoded value about label to the XMIR, which is wrong, + // of course. The problem here is that we can't directly use Label#getOffset() method + // since label offset will be set later (not here). So we have to handle that problem + // somehow and save correct Label value. Don't forget to write integration tests + // for if/else and loops control-flow statements. The tests should check correct + // transformation bytecode -> XMIR -> bytecode. + this.opcode(opcode, 0); super.visitJumpInsn(opcode, label); } diff --git a/src/test/java/org/eolang/jeo/representation/HexDataTest.java b/src/test/java/org/eolang/jeo/representation/HexDataTest.java index 35ddc4ed8..80020814d 100644 --- a/src/test/java/org/eolang/jeo/representation/HexDataTest.java +++ b/src/test/java/org/eolang/jeo/representation/HexDataTest.java @@ -32,7 +32,7 @@ /** * Test cases for {@link org.eolang.jeo.representation.HexData}. - * @since 0.1. + * @since 0.1 */ class HexDataTest { @@ -94,5 +94,4 @@ static Stream values() { Arguments.of(0.1d, "3F B9 99 99 99 99 99 9A") ); } - -} \ No newline at end of file +}