Skip to content

Commit

Permalink
#2433: EOint has hashcode even without delta attribute initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky committed Aug 24, 2023
1 parent 6d1f43b commit 2eebcc0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ SOFTWARE.
<xsl:apply-templates select="//meta[head='package']" mode="head"/>
<xsl:text>import org.eolang.*;</xsl:text>
<xsl:value-of select="eo:eol(0)"/>
<xsl:text>import java.util.concurrent.atomic.AtomicBoolean;</xsl:text>
<xsl:value-of select="eo:eol(0)"/>
<xsl:apply-templates select="//meta[head='junit' or head='tests']" mode="head"/>
<xsl:apply-templates select="." mode="body"/>
</xsl:element>
Expand Down Expand Up @@ -158,6 +160,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 +177,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 AtomicBoolean initialized = new 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 Down Expand Up @@ -223,14 +235,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>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.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
62 changes: 62 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,62 @@
/*
* 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)
);
}
}

0 comments on commit 2eebcc0

Please sign in to comment.