Skip to content

Commit

Permalink
#308 bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedran authored and Vedran committed May 5, 2018
1 parent 7a4e6a5 commit e98ec3a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/scalar/Equality.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Integer value() throws Exception {
() -> lft.length == rght.length,
() -> {
int result = 0;
for (int idx = rght.length - 1; idx > 0; --idx) {
for (int idx = rght.length - 1; idx >= 0; --idx) {
result = lft[idx] - rght[idx];
if (result != 0) {
break;
Expand Down
41 changes: 22 additions & 19 deletions src/test/java/org/cactoos/scalar/EqualityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.scalar;

import java.nio.ByteBuffer;
import org.cactoos.Bytes;
import org.cactoos.matchers.ScalarHasValue;
import org.hamcrest.MatcherAssert;
Expand All @@ -43,7 +42,7 @@ public final class EqualityTest {
public void notEqualLeft() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Weight(0), new EqualityTest.Weight(500)
new EqualityTest.Letters("A"), new EqualityTest.Letters("AB")
),
new ScalarHasValue<>(-1)
);
Expand All @@ -53,7 +52,7 @@ public void notEqualLeft() throws Exception {
public void notEqualRight() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Weight(500), new EqualityTest.Weight(0)
new EqualityTest.Letters("AB"), new EqualityTest.Letters("A")
),
new ScalarHasValue<>(1)
);
Expand All @@ -63,7 +62,7 @@ public void notEqualRight() throws Exception {
public void notEqualLeftWithSameSize() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Weight(400), new EqualityTest.Weight(500)
new EqualityTest.Letters("A"), new EqualityTest.Letters("B")
),
new ScalarHasValue<>(-1)
);
Expand All @@ -73,7 +72,7 @@ public void notEqualLeftWithSameSize() throws Exception {
public void notEqualRightWithSameSize() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Weight(500), new EqualityTest.Weight(400)
new EqualityTest.Letters("B"), new EqualityTest.Letters("A")
),
new ScalarHasValue<>(1)
);
Expand All @@ -83,7 +82,17 @@ public void notEqualRightWithSameSize() throws Exception {
public void equal() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Weight(500), new EqualityTest.Weight(500)
new EqualityTest.Letters("A"), new EqualityTest.Letters("A")
),
new ScalarHasValue<>(0)
);
}

@Test
public void compareEmptyArrays() throws Exception {
MatcherAssert.assertThat(
new Equality<>(
new EqualityTest.Letters(""), new EqualityTest.Letters("")
),
new ScalarHasValue<>(0)
);
Expand All @@ -92,30 +101,24 @@ public void equal() throws Exception {
/**
* Weight.
*/
private static final class Weight implements Bytes {
private static final class Letters implements Bytes {

/**
* Kilos.
* Bytes.
*/
private final int kilos;
private final String text;

/**
* Ctor.
* @param kls Kilos
* @param txt Text
*/
Weight(final int kls) {
this.kilos = kls;
Letters(final String txt) {
this.text = txt;
}

@Override
public byte[] asBytes() {
return new UncheckedScalar<>(
new Ternary<>(
this.kilos == 0,
new byte[]{0, 0},
ByteBuffer.allocate(4).putInt(this.kilos).array()
)
).value();
return this.text.getBytes();
}
}
}

0 comments on commit e98ec3a

Please sign in to comment.