Skip to content

Commit

Permalink
Merge branch 'master' into dataize-returns-u8-array
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky committed Aug 28, 2023
2 parents afd0707 + 1171786 commit b51922c
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .codacy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# are not available for the project. For example a lot of
# package name contains capital letter and such names are conventional.
exclude_paths:
- "eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java"
- "eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java"
- "eo-runtime/src/test/java/EOorg/EOeolang/EOintTest.java"
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ SOFTWARE.
<xsl:value-of select="eo:class-name(@name, eo:suffix(@line, @pos))"/>
<xsl:text> extends PhDefault {</xsl:text>
<xsl:value-of select="eo:eol(0)"/>
<xsl:apply-templates select="." mode="fields"/>
<xsl:apply-templates select="." mode="ctors"/>
<xsl:apply-templates select="." mode="equals-and-hashCode"/>
<xsl:if test="//meta[head='junit' or head='tests'] and not(@parent)">
Expand All @@ -174,8 +175,17 @@ SOFTWARE.
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
<xsl:template match="class" mode="fields">
<xsl:value-of select="eo:tabs(1)"/>
<xsl:variable name="type" select="concat(//meta[head='package']/tail, '.', @name)"/>
<xsl:if test="$literal-objects[text()=$type]">
<xsl:value-of select="eo:eol(1)"/>
<xsl:text>private final java.util.concurrent.atomic.AtomicBoolean initialized = new java.util.concurrent.atomic.AtomicBoolean(false);</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="class" mode="ctors">
<xsl:value-of select="eo:tabs(1)"/>
<xsl:value-of select="eo:eol(1)"/>
<xsl:choose>
<xsl:when test="//meta[head='junit' or head='tests'] and not(@parent)">
<xsl:text>public </xsl:text>
Expand All @@ -200,7 +210,7 @@ SOFTWARE.
<xsl:variable name="type" select="concat(//meta[head='package']/tail, '.', @name)"/>
<xsl:if test="$literal-objects[text()=$type]">
<xsl:value-of select="eo:eol(2)"/>
<xsl:text>this.add("Δ", new AtFree());</xsl:text>
<xsl:text>this.add("Δ", new AtFree(new AtSimple(), this.initialized));</xsl:text>
</xsl:if>
<xsl:if test="$type='org.eolang.tuple'">
<xsl:value-of select="eo:eol(2)"/>
Expand All @@ -223,8 +233,24 @@ SOFTWARE.
<xsl:text>@Override</xsl:text>
<xsl:value-of select="eo:eol(1)"/>
<xsl:text>public int hashCode() {</xsl:text>
<xsl:value-of select="eo:eol(2)"/>
<xsl:text>return this.attr("Δ").get().hashCode();</xsl:text>
<xsl:choose>
<xsl:when test="$type='org.eolang.tuple'">
<xsl:value-of select="eo:eol(1)"/>
<xsl:text>return this.attr("Δ").get().hashCode();</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="eo:eol(2)"/>
<xsl:text>if (this.initialized.get()) {</xsl:text>
<xsl:value-of select="eo:eol(3)"/>
<xsl:text>return this.attr("Δ").get().hashCode();</xsl:text>
<xsl:value-of select="eo:eol(2)"/>
<xsl:text>} else {</xsl:text>
<xsl:value-of select="eo:eol(3)"/>
<xsl:text>return this.vertex;</xsl:text>
<xsl:value-of select="eo:eol(2)"/>
<xsl:text>}</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="eo:eol(1)"/>
<xsl:text>}</xsl:text>
<xsl:value-of select="eo:eol(1)"/>
Expand Down
20 changes: 19 additions & 1 deletion eo-runtime/src/main/java/org/eolang/Universe.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,27 @@ public Universe() {
* @return Vertex of the object to find.
*/
public int find(final String name) {
Phi accum = this.connector;
if (name == null) {
throw new IllegalArgumentException(
"Argument name is null"
);
}
Phi accum;
final String[] atts = Universe.replace(name)
.split("\\.");
if ("Q".equals(atts[0])) {
accum = Phi.Φ;
} else if ("$".equals(atts[0])) {
accum = this.connector;
} else {
throw new ExFailure(
String.format(
"Universe.find starts with %s, but it should start with Q or $ only",
atts[0]
)
);
}
atts[0] = "";
for (final String att: atts) {
if (!"".equals(att)) {
accum = accum.attr(att).get();
Expand Down
16 changes: 8 additions & 8 deletions eo-runtime/src/test/eo/org/eolang/rust-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
use eo_env::eo_enum::EO::{EOInt};

pub fn foo(env: &mut EOEnv) -> EO {
EOInt(env.find("^.a").into())
EOInt(env.find("$.^.a").into())
}
"""
*
Expand All @@ -129,7 +129,7 @@
use eo_env::eo_enum::EO::{EOVertex};

pub fn foo(env: &mut EOEnv) -> EO {
let v = env.find("^.book") as u32;
let v = env.find("$.^.book") as u32;
EOVertex(v)
}
"""
Expand Down Expand Up @@ -168,8 +168,8 @@
use eo_env::eo_enum::EO;
use eo_env::eo_enum::EO::{EOInt};
pub fn foo(env: &mut EOEnv) -> EO {
let v1 = env.find("^.a") as u32;
let v2 = env.find("^.b") as u32;
let v1 = env.find("$.^.a") as u32;
let v2 = env.find("$.^.b") as u32;
env.bind(v1 , v2, "EO-att");
return EOInt(v1 as i64);
}
Expand All @@ -190,7 +190,7 @@
use eo_env::eo_enum::EO;
use eo_env::eo_enum::EO::{EOInt};
pub fn foo(env: &mut EOEnv) -> EO {
let v = env.find("^.a") as u32;
let v = env.find("$.^.a") as u32;
let copy = env.copy(v).unwrap();
EOInt(copy as i64)
}
Expand All @@ -211,7 +211,7 @@
use eo_env::eo_enum::EO;
use eo_env::eo_enum::EO::{EOInt};
pub fn foo(env: &mut EOEnv) -> EO {
let v = env.find("^.a") as u32;
let v = env.find("$.^.a") as u32;
let _bytes = env.dataize(v).unwrap();
EOInt(v as i64)
}
Expand All @@ -236,11 +236,11 @@
use byteorder::{BigEndian, ReadBytesExt};

pub fn foo(env: &mut EOEnv) -> EO {
let a = env.find("^.a") as u32;
let a = env.find("$.^.a") as u32;
let bytes_a = env.dataize(a).unwrap();
let a = bytes_a.as_slice().read_i64::<BigEndian>().unwrap();

let b = env.find("^.b") as u32;
let b = env.find("$.^.b") as u32;
let bytes_b = env.dataize(b).unwrap();
let b = bytes_b.as_slice().read_i64::<BigEndian>().unwrap();
println!("sum 5 + 10 = {}", a + b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ void addsNumbers() {
);
}
}

72 changes: 72 additions & 0 deletions eo-runtime/src/test/java/EOorg/EOeolang/EOintTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.
*/

/*
* @checkstyle PackageNameCheck (10 lines)
*/
package EOorg.EOeolang;

import org.eolang.Data;
import org.eolang.Phi;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link EOint}.
*
* @since 0.1
* @checkstyle TypeNameCheck (4 lines)
*/
public class EOintTest {

@Test
void hasEqualHashes() {
final Phi left = new Data.ToPhi(42L);
final Phi right = new Data.ToPhi(42L);
MatcherAssert.assertThat(
left.hashCode(),
Matchers.equalTo(right.hashCode())
);
}

@Test
void hasHashEvenWithoutData() {
final Phi phi = new EOint(Phi.Φ);
MatcherAssert.assertThat(
phi.hashCode(),
Matchers.greaterThan(0)
);
}

@Test
void hasDifferentHash() {
final Phi raw = new EOint(Phi.Φ);
final Phi initialized = new Data.ToPhi(0L);
MatcherAssert.assertThat(
raw.hashCode(),
Matchers.not(initialized.hashCode())
);
}
}
22 changes: 18 additions & 4 deletions eo-runtime/src/test/java/org/eolang/UniverseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package org.eolang;

import EOorg.EOeolang.EOseq;
import java.util.HashMap;
import java.util.Map;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
Expand All @@ -44,7 +47,7 @@ void findsSimpleAtt() {
final Phi phi = new DummyWithAt(Phi.Φ);
final Universe universe = new Universe(phi);
MatcherAssert.assertThat(
universe.find(UniverseTest.ATT),
universe.find("$.".concat(UniverseTest.ATT)),
Matchers.equalTo(
phi.attr(UniverseTest.ATT).get().hashCode()
)
Expand All @@ -58,7 +61,7 @@ void findsLongAtt() {
MatcherAssert.assertThat(
universe.find(
String.format(
"%s.%s",
"$.%s.%s",
UniverseTest.ATT,
UniverseTest.ATT
)
Expand All @@ -69,13 +72,24 @@ void findsLongAtt() {
);
}

@Test
void findsByAbsoluteLoc() {
final Map<Integer, Phi> indexed = new HashMap<>();
final Universe universe = new Universe(Phi.Φ, indexed);
final int vertex = universe.find("Q.org.eolang.seq");
MatcherAssert.assertThat(
indexed.get(vertex).getClass(),
Matchers.equalTo(EOseq.class)
);
}

@Test
void throwsIfWrongFind() {
Assertions.assertThrows(
ExAbstract.class,
() -> new Universe(
new DummyWithStructure(Phi.Φ)
).find("wrong-name")
).find("$.wrong-name")
);
}

Expand All @@ -85,7 +99,7 @@ void dataizesIndexed() {
new DummyWithAt(Phi.Φ)
);
final int vertex = universe.find(
String.format(UniverseTest.ATT)
"$.".concat(UniverseTest.ATT)
);
MatcherAssert.assertThat(
universe.dataize(vertex),
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ SOFTWARE.
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.1</version>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
Expand Down

0 comments on commit b51922c

Please sign in to comment.