Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization with one element bugfix #30

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cell/src/main/java/org/ton/java/cell/TonHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ void deserialize(CellSlice c, Function<BitString, Object> keyParser, Function<Ce
* @return array either leaf or empty leaf or [left,right] fork
*/
List<Object> splitTree(List<Object> arr) {
if (arr.size() == 1) {
// Return a structure that represents a leaf node
return Arrays.asList(arr.get(0));
}

List<Object> left = new ArrayList<>();
List<Object> right = new ArrayList<>();

Expand Down Expand Up @@ -134,6 +139,11 @@ List<Object> flatten(List<Object> arr, int m) {
return arr;
}

if (arr.size() == 1 && arr.get(0) instanceof Node) {
// This is a leaf node (single element case)
return Arrays.asList("", m, arr.get(0));
}

if (!(arr.get(0) instanceof String)) {
arr.addAll(0, Arrays.asList("", m));
}
Expand Down
15 changes: 14 additions & 1 deletion cell/src/test/java/org/ton/java/hashmaps/TestHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void testHashMapSerialization() {
}

@Test
@Ignore // TODO fails HashMapE serialization with one entry
//@Ignore // TODO fails HashMapE serialization with one entry
public void testHashMapDeserializationOneEntry() {
int keySizeX = 9;
TonHashMap x = new TonHashMap(keySizeX);
Expand All @@ -101,6 +101,19 @@ public void testHashMapDeserializationOneEntry() {
);

log.info("serialized cellX {}", cellX.print());

CellSlice cs = CellSlice.beginParse(cellX);
TonHashMap сx2 = cs.loadDict(keySizeX,
k -> k.readUint(keySizeX),
v -> CellSlice.beginParse(v).loadUint(3));

assertThat((Integer) сx2.elements.entrySet().size()).isEqualTo((Integer) 1);
log.info("Deserialized hashmap length: {}", сx2.elements.entrySet().size());

for (Map.Entry<Object, Object> entry : сx2.elements.entrySet()) {
assertThat((BigInteger) entry.getKey()).isEqualTo(100L);
assertThat((BigInteger) entry.getValue()).isEqualTo((byte) 1);
}
}

@Test
Expand Down
14 changes: 13 additions & 1 deletion cell/src/test/java/org/ton/java/hashmaps/TestHashMapE.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void testEmptyHashMapEDeserialization() {
}

@Test
@Ignore // TODO fails HashMapE serialization with one entry
public void testHashMapEDeserializationOneEntry() {
int keySizeX = 9;
TonHashMapE x = new TonHashMapE(keySizeX);
Expand All @@ -72,6 +71,19 @@ public void testHashMapEDeserializationOneEntry() {
);

log.info("serialized cellX {}", cellX.print());

CellSlice cs = CellSlice.beginParse(cellX);
TonHashMapE сx2 = cs.loadDictE(keySizeX,
k -> k.readUint(keySizeX),
v -> CellSlice.beginParse(v).loadUint(3));

assertThat((Integer) сx2.elements.entrySet().size()).isEqualTo((Integer) 1);
log.info("Deserialized hashmap length: {}", сx2.elements.entrySet().size());

for (Map.Entry<Object, Object> entry : сx2.elements.entrySet()) {
assertThat((BigInteger) entry.getKey()).isEqualTo(100L);
assertThat((BigInteger) entry.getValue()).isEqualTo((byte) 1);
}
}

@Test
Expand Down