Skip to content

Commit

Permalink
add hashmap deserialization tests;
Browse files Browse the repository at this point in the history
add getKeyByIndex() and getValueByIndex in TonHashMaps
  • Loading branch information
neodiX committed Aug 29, 2024
1 parent 769de57 commit dd7c80f
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 31 deletions.
35 changes: 26 additions & 9 deletions cell/src/main/java/org/ton/java/cell/TonHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import org.ton.java.utils.Utils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -110,13 +106,13 @@ PatriciaTreeNode splitTree(List<Node> nodes) {
PatriciaTreeNode leftNode = left.size() > 1
? splitTree(left)
: left.isEmpty()
? null
: new PatriciaTreeNode("", 0, left.get(0), null, null);
? null
: new PatriciaTreeNode("", 0, left.get(0), null, null);
PatriciaTreeNode rightNode = right.size() > 1
? splitTree(right)
: right.isEmpty()
? null
: new PatriciaTreeNode("", 0, right.get(0), null, null);
? null
: new PatriciaTreeNode("", 0, right.get(0), null, null);

return new PatriciaTreeNode("", keySize, null, leftNode, rightNode);
}
Expand Down Expand Up @@ -308,4 +304,25 @@ public String toString() {
sb.append(")");
return sb.toString();
}


public Object getKeyByIndex(long index) {
long i = 0;
for (Map.Entry<Object, Object> entry : elements.entrySet()) {
if (i++ == index) {
return entry.getKey();
}
}
throw new Error("key not found at index " + index);
}

public Object getValueByIndex(long index) {
long i = 0;
for (Map.Entry<Object, Object> entry : elements.entrySet()) {
if (i++ == index) {
return entry.getValue();
}
}
throw new Error("value not found at index " + index);
}
}
36 changes: 31 additions & 5 deletions cell/src/main/java/org/ton/java/cell/TonHashMapAug.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
import org.ton.java.utils.Utils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;

Expand Down Expand Up @@ -282,4 +278,34 @@ public String toString() {
sb.append(")");
return sb.toString();
}

public Object getKeyByIndex(long index) {
long i = 0;
for (Map.Entry<Object, Pair<Object, Object>> entry : elements.entrySet()) {
if (i == index) {
return entry.getKey();
}
}
throw new Error("key not found at index " + index);
}

public Object getValueByIndex(long index) {
long i = 0;
for (Map.Entry<Object, Pair<Object, Object>> entry : elements.entrySet()) {
if (i++ == index) {
return entry.getValue().getLeft();
}
}
throw new Error("value not found at index " + index);
}

public Object getEdgeByIndex(long index) {
long i = 0;
for (Map.Entry<Object, Pair<Object, Object>> entry : elements.entrySet()) {
if (i++ == index) {
return entry.getValue().getRight();
}
}
throw new Error("edge not found at index " + index);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
package org.ton.java;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.ton.java.address.Address;
import org.ton.java.bitstring.BitString;
import org.ton.java.cell.*;

import java.io.IOException;
import java.math.BigInteger;
import java.net.URL;
import java.nio.charset.Charset;

import static org.assertj.core.api.Assertions.assertThat;

@Slf4j
@RunWith(JUnit4.class)
public class TestTonSdkTestCasesHashmapDeserialization {
public static final String numbersTestFileUrl = "https://raw.githubusercontent.com/neodix42/ton-sdk-test-cases/main/hashmap-deserialization.json";
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
String fileContentWithUseCases = IOUtils.toString(new URL(numbersTestFileUrl), Charset.defaultCharset());
TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

public TestTonSdkTestCasesHashmapDeserialization() throws IOException {
}

@Test
public void testHashmapDeserialization1() {

String testId = "hashmap-deserialization-1";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

int keySizeBits = Integer.parseInt(testCase.getInput().get("keySizeBits").toString());
int uintValueSizeBits = Integer.parseInt(testCase.getInput().get("uintValueSizeBits").toString());
String bocHex = testCase.getInput().get("bocHex").toString();


Cell cell = CellBuilder.beginCell().fromBoc(bocHex).endCell();
CellSlice cs = CellSlice.beginParse(cell);
TonHashMap x = cs.loadDict(keySizeBits,
k -> k.readUint(keySizeBits),
v -> CellSlice.beginParse(v).loadUint(uintValueSizeBits));

int elementsCount = Integer.parseInt(testCase.getExpectedOutput().get("elementsCount").toString());
int firstElementValue = Integer.parseInt(testCase.getExpectedOutput().get("firstElementValue").toString());

assertThat(x.elements.size()).isEqualTo(elementsCount);
assertThat((BigInteger) x.elements.get(BigInteger.ONE)).isEqualTo(firstElementValue);
}

@Test
public void testHashmapDeserialization2() {

String testId = "hashmap-deserialization-2";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

int keySizeBits = Integer.parseInt(testCase.getInput().get("keySizeBits").toString());
int uintValueSizeBits = Integer.parseInt(testCase.getInput().get("uintValueSizeBits").toString());
String bocHex = testCase.getInput().get("bocHex").toString();


Cell cell = CellBuilder.beginCell().fromBoc(bocHex).endCell();
CellSlice cs = CellSlice.beginParse(cell);
TonHashMap x = cs.loadDict(keySizeBits,
k -> k.readUint(keySizeBits),
v -> CellSlice.beginParse(v).loadUint(uintValueSizeBits));

int elementsCount = Integer.parseInt(testCase.getExpectedOutput().get("elementsCount").toString());
int thirdElementKey = Integer.parseInt(testCase.getExpectedOutput().get("thirdElementKey").toString());
int thirdElementValue = Integer.parseInt(testCase.getExpectedOutput().get("thirdElementValue").toString());

assertThat(x.elements.size()).isEqualTo(elementsCount);
assertThat((BigInteger) x.elements.get(BigInteger.valueOf(thirdElementKey))).isEqualTo(thirdElementValue);
}

@Test
public void testHashmapDeserialization3() {

String testId = "hashmap-deserialization-3";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

int keySizeBits = Integer.parseInt(testCase.getInput().get("keySizeBits").toString());
int intValueSizeBits = Integer.parseInt(testCase.getInput().get("intValueSizeBits").toString());
String bocBase64 = testCase.getInput().get("bocBase64").toString();


Cell cell = CellBuilder.beginCell().fromBocBase64(bocBase64).endCell();
CellSlice cs = CellSlice.beginParse(cell);
TonHashMap x = cs.loadDict(keySizeBits,
k -> k.readInt(intValueSizeBits),
v -> v);

int elementsCount = Integer.parseInt(testCase.getExpectedOutput().get("elementsCount").toString());
BigInteger thirdElementKey = new BigInteger(testCase.getExpectedOutput().get("thirdElementKey").toString());
BigInteger lastElementKey = new BigInteger(testCase.getExpectedOutput().get("lastElementKey").toString());

assertThat(x.elements.size()).isEqualTo(elementsCount);
assertThat((BigInteger) x.getKeyByIndex(2)).isEqualTo(thirdElementKey);
assertThat((BigInteger) x.getKeyByIndex(13)).isEqualTo(lastElementKey);
}

@Test
public void testHashmapDeserialization4() {

String testId = "hashmap-deserialization-4";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

int keySizeBits = Integer.parseInt(testCase.getInput().get("keySizeBits").toString());
int uintValueSizeBits = Integer.parseInt(testCase.getInput().get("uintValueSizeBits").toString());
String bocHex = testCase.getInput().get("bocHex").toString();


Cell cell = CellBuilder.beginCell().fromBoc(bocHex).endCell();
CellSlice cs = CellSlice.beginParse(cell);
TonHashMapE x = cs.loadDictE(keySizeBits,
k -> k.readUint(keySizeBits),
v -> CellSlice.beginParse(v).loadUint(uintValueSizeBits));

int elementsCount = Integer.parseInt(testCase.getExpectedOutput().get("elementsCount").toString());
BigInteger firstElementValue = new BigInteger(testCase.getExpectedOutput().get("firstElementValue").toString());

assertThat(x.elements.size()).isEqualTo(elementsCount);
assertThat((BigInteger) x.getValueByIndex(0)).isEqualTo(firstElementValue);
}

@Test
public void testHashmapDeserialization5() {

String testId = "hashmap-deserialization-5";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

int keySizeBits = Integer.parseInt(testCase.getInput().get("keySizeBits").toString());
int uintValueSizeBits = Integer.parseInt(testCase.getInput().get("booleanValueSizeBits").toString());
String bocHex = testCase.getInput().get("bocHex").toString();


Cell cell = CellBuilder.beginCell().fromBoc(bocHex).endCell();
CellSlice cs = CellSlice.beginParse(cell);
TonPfxHashMap x = cs.loadDictPfx(keySizeBits,
BitString::readAddress,
v -> true);

int elementsCount = Integer.parseInt(testCase.getExpectedOutput().get("elementsCount").toString());
String firstElementKey = testCase.getExpectedOutput().get("firstElementKey").toString();

assertThat(x.elements.size()).isEqualTo(elementsCount);
assertThat(((Address) x.getKeyByIndex(0)).toBounceable()).isEqualTo(firstElementKey);
}

@Test
public void testHashmapDeserialization6() {

String testId = "hashmap-deserialization-6";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

int keySizeBits = Integer.parseInt(testCase.getInput().get("keySizeBits").toString());
int uintValueSizeBits = Integer.parseInt(testCase.getInput().get("uintValueSizeBits").toString());
int uintEdgeSizeBits = Integer.parseInt(testCase.getInput().get("uintEdgeSizeBits").toString());
String bocHex = testCase.getInput().get("bocHex").toString();


Cell cell = CellBuilder.beginCell().fromBoc(bocHex).endCell();
CellSlice cs = CellSlice.beginParse(cell);
TonHashMapAug x = cs.loadDictAug(keySizeBits,
k -> k.readUint(keySizeBits),
v -> CellSlice.beginParse(v).loadUint(uintValueSizeBits),
e -> CellSlice.beginParse(e).loadUint(uintEdgeSizeBits));

int elementsCount = Integer.parseInt(testCase.getExpectedOutput().get("elementsCount").toString());
BigInteger firstElementKey = new BigInteger(testCase.getExpectedOutput().get("firstElementKey").toString());
BigInteger firstElementValue = new BigInteger(testCase.getExpectedOutput().get("firstElementValue").toString());
BigInteger firstElementEdge = new BigInteger(testCase.getExpectedOutput().get("firstElementEdge").toString());

assertThat(x.elements.size()).isEqualTo(elementsCount);
assertThat((BigInteger) x.getKeyByIndex(0)).isEqualTo(firstElementKey);
assertThat((BigInteger) x.getValueByIndex(0)).isEqualTo(firstElementValue);
assertThat((BigInteger) x.getEdgeByIndex(0)).isEqualTo(firstElementEdge);
}

@Test
public void testHashmapDeserialization7() {

String testId = "hashmap-deserialization-7";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

int keySizeBits = Integer.parseInt(testCase.getInput().get("keySizeBits").toString());
int uintValueSizeBits = Integer.parseInt(testCase.getInput().get("uintValueSizeBits").toString());
int uintEdgeSizeBits = Integer.parseInt(testCase.getInput().get("uintEdgeSizeBits").toString());
String bocHex = testCase.getInput().get("bocHex").toString();


Cell cell = CellBuilder.beginCell().fromBoc(bocHex).endCell();
CellSlice cs = CellSlice.beginParse(cell);
TonHashMapAugE x = cs.loadDictAugE(keySizeBits,
k -> k.readUint(keySizeBits),
v -> CellSlice.beginParse(v).loadUint(uintValueSizeBits),
e -> CellSlice.beginParse(e).loadUint(uintEdgeSizeBits));

int elementsCount = Integer.parseInt(testCase.getExpectedOutput().get("elementsCount").toString());
BigInteger firstElementKey = new BigInteger(testCase.getExpectedOutput().get("firstElementKey").toString());
BigInteger firstElementValue = new BigInteger(testCase.getExpectedOutput().get("firstElementValue").toString());
BigInteger firstElementEdge = new BigInteger(testCase.getExpectedOutput().get("firstElementEdge").toString());

assertThat(x.elements.size()).isEqualTo(elementsCount);
assertThat((BigInteger) x.getKeyByIndex(0)).isEqualTo(firstElementKey);
assertThat((BigInteger) x.getValueByIndex(0)).isEqualTo(firstElementValue);
assertThat((BigInteger) x.getEdgeByIndex(0)).isEqualTo(firstElementEdge);
}
}
Loading

0 comments on commit dd7c80f

Please sign in to comment.