Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Nov 1, 2023
2 parents c71e53d + 6cf17ec commit b29e6ed
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/it/betecode-to-eo/src/main/java/foo/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package foo;

class Foo {
int bar(double x) {
if (x > 0.0d) {
return 5;
}
return 8;
}
}
1 change: 1 addition & 0 deletions src/it/betecode-to-eo/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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/generated-sources/xmir/org/eolang/jeo/Application.xmir').exists()
assert new File(basedir, 'target/generated-sources/xmir/foo/Foo.xmir').exists()
true
27 changes: 25 additions & 2 deletions src/main/java/org/eolang/jeo/representation/HexData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -71,8 +88,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
97 changes: 97 additions & 0 deletions src/test/java/org/eolang/jeo/representation/HexDataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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;

/**
* 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)
);
}

@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.
*/
static Stream<Arguments> 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")
);
}

/**
* Arguments for {@link HexDataTest#convertsRawDataIntoHexString(Object, String)}.
* @return Stream of arguments.
*/
static Stream<Arguments> 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")
);
}
}

1 comment on commit b29e6ed

@0pdd
Copy link

@0pdd 0pdd commented on b29e6ed Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 217-e6d26aba discovered in src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java) and submitted as #226. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.