done with BList linked list logic, write tests. override equals on
BValue, thus making comparison by actual contents of nodes.
This commit is contained in:
parent
84e82e9e07
commit
bc99ebf3d5
6 changed files with 85 additions and 2 deletions
|
|
@ -7,6 +7,11 @@ public class BInteger extends BValue {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
@Override()
|
||||
public boolean compare(BValue other) {
|
||||
return get() == ((BInteger)other).get();
|
||||
}
|
||||
|
||||
@Override()
|
||||
public BType getType() {
|
||||
return BType.INTEGER;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,11 @@ public class BList extends BValue {
|
|||
return BType.LIST;
|
||||
}
|
||||
|
||||
@Override()
|
||||
public boolean compare(BValue other) {
|
||||
throw new UnsupportedOperationException("Not yet implemented");
|
||||
}
|
||||
|
||||
protected BValue getFirst() {
|
||||
return getChild();
|
||||
}
|
||||
|
|
@ -30,11 +35,11 @@ public class BList extends BValue {
|
|||
// builder
|
||||
public BList append(BValue item) {
|
||||
BValue first = getFirst();
|
||||
BValue last = getLast();
|
||||
if (first == null) {
|
||||
setFirst(item);
|
||||
setLast(item);
|
||||
} else {
|
||||
BValue last = getLast();
|
||||
last.setNext(item);
|
||||
setLast(item);
|
||||
}
|
||||
|
|
@ -53,7 +58,7 @@ public class BList extends BValue {
|
|||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
while (--index > 0) {
|
||||
while (index-- > 0) {
|
||||
it.next();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.mykola2312.retracker.bencode;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BString extends BValue {
|
||||
private byte[] bytes;
|
||||
|
|
@ -9,6 +10,11 @@ public class BString extends BValue {
|
|||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
@Override()
|
||||
public boolean compare(BValue other) {
|
||||
return Arrays.equals(((BString)other).get(), get());
|
||||
}
|
||||
|
||||
@Override()
|
||||
public BType getType() {
|
||||
return BType.STRING;
|
||||
|
|
|
|||
|
|
@ -15,6 +15,20 @@ abstract public class BValue implements Iterable<BValue> {
|
|||
|
||||
abstract public BType getType();
|
||||
|
||||
// other is guaranteed to be same BType
|
||||
abstract public boolean compare(BValue other);
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof BValue)) return false;
|
||||
|
||||
BValue other = (BValue)o;
|
||||
if (other.getType() != getType()) return false;
|
||||
|
||||
return compare(other);
|
||||
}
|
||||
|
||||
public BValue getNext() {
|
||||
return next;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@ package com.mykola2312.retracker.bencode;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
|
@ -13,4 +16,40 @@ public class BListTest {
|
|||
assertEquals(0, empty.getLength());
|
||||
assertNull(empty.getChild());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppend() {
|
||||
BList list = new BList();
|
||||
list.append(new BInteger(1));
|
||||
list.append(new BInteger(2));
|
||||
list.append(new BInteger(3));
|
||||
|
||||
System.out.println("manual iteration:");
|
||||
{
|
||||
BValue cur = list.getChild();
|
||||
do {
|
||||
System.out.println(((BInteger)cur).get());
|
||||
cur = cur.getNext();
|
||||
} while (cur != null);
|
||||
}
|
||||
System.out.println("iterator:");
|
||||
{
|
||||
Iterator<BValue> it = list.iterator();
|
||||
while (it.hasNext()) {
|
||||
System.out.println(((BInteger)it.next()).get());
|
||||
}
|
||||
}
|
||||
System.out.println("get:");
|
||||
{
|
||||
System.out.println(((BInteger)list.get(0)).get());
|
||||
System.out.println(((BInteger)list.get(1)).get());
|
||||
System.out.println(((BInteger)list.get(2)).get());
|
||||
}
|
||||
|
||||
assertEquals(new BInteger(1), list.get(0));
|
||||
assertEquals(new BInteger(2), list.get(1));
|
||||
assertEquals(new BInteger(3), list.get(2));
|
||||
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> list.get(3));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
package com.mykola2312.retracker.bencode;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class EqualsTest {
|
||||
@Test
|
||||
public void testIntegerEquals() {
|
||||
assertEquals(new BInteger(1), new BInteger(1));
|
||||
assertNotEquals(new BInteger(2), new BInteger(3));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue