-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #579 from LimeChain/547-block-production-lottery
Block Production Lottery - Primary Slot
- Loading branch information
Showing
6 changed files
with
164 additions
and
3 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,5 +81,4 @@ public static boolean verifySignature(final VerifySignature signature) { | |
return false; | ||
} | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
src/test/java/com/limechain/utils/LittleEndianUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.limechain.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigInteger; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class LittleEndianUtilsTest { | ||
@Test | ||
void testBytesToFixedLength() { | ||
byte[] bigEndianArray = {0x01, 0x02, 0x03, 0x04}; | ||
int targetLength = 4; | ||
|
||
byte[] expectedLittleEndianArray = {0x04, 0x03, 0x02, 0x01}; | ||
byte[] actualLittleEndianArray = LittleEndianUtils.bytesToFixedLength(bigEndianArray, targetLength); | ||
assertArrayEquals(expectedLittleEndianArray, actualLittleEndianArray); | ||
} | ||
|
||
@Test | ||
void testBytesToFixedLengthWithPadding() { | ||
byte[] bigEndianArray = {0x0A, 0x0B, 0x0C}; | ||
int targetLength = 5; | ||
|
||
byte[] expectedLittleEndianArray = {0x0C, 0x0B, 0x0A, 0x00, 0x00}; | ||
byte[] actualLittleEndianArray = LittleEndianUtils.bytesToFixedLength(bigEndianArray, targetLength); | ||
assertArrayEquals(expectedLittleEndianArray, actualLittleEndianArray); | ||
} | ||
|
||
@Test | ||
void testBytesToFixedLengthWithTruncation() { | ||
byte[] bigEndianArray = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60}; | ||
int targetLength = 4; | ||
|
||
byte[] expectedLittleEndianArray = {0x60, 0x50, 0x40, 0x30}; | ||
byte[] actualLittleEndianArray = LittleEndianUtils.bytesToFixedLength(bigEndianArray, targetLength); | ||
assertArrayEquals(expectedLittleEndianArray, actualLittleEndianArray); | ||
} | ||
|
||
@Test | ||
void testLongToLittleEndianBytesWithMaxLongValue() { | ||
long value = Long.MAX_VALUE; | ||
byte[] expected = new byte[]{-1, -1, -1, -1, -1, -1, -1, 127}; // Expected little-endian bytes for Long.MAX_VALUE | ||
byte[] result = LittleEndianUtils.longToLittleEndianBytes(value); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
void testLongToLittleEndianBytesWithMinLongValue() { | ||
long value = Long.MIN_VALUE; | ||
byte[] expected = new byte[]{0, 0, 0, 0, 0, 0, 0, -128}; // Expected little-endian bytes for Long.MIN_VALUE | ||
byte[] result = LittleEndianUtils.longToLittleEndianBytes(value); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
void testLongToLittleEndianBytesWithZero() { | ||
long value = 0L; | ||
byte[] expected = new byte[]{0, 0, 0, 0, 0, 0, 0, 0}; // Expected little-endian bytes for 0 | ||
byte[] result = LittleEndianUtils.longToLittleEndianBytes(value); | ||
assertArrayEquals(expected, result); | ||
} | ||
|
||
@Test | ||
void testFromLittleEndianByteArray() { | ||
byte[] bytes = {1, 0, 0, 0}; | ||
BigInteger expected = BigInteger.ONE; | ||
BigInteger result = LittleEndianUtils.fromLittleEndianByteArray(bytes); | ||
assertEquals(expected, result); | ||
} | ||
|
||
@Test | ||
void testFromLittleEndianByteArrayWithRandomNumber() { | ||
// Tested with rust build-in 'u128::from_le_bytes' function | ||
// let byte_arr: [i8 ; 16] = [-124, -72, -92, 56, 112, -102, 29, 107, -111, -17, 73, -30, -49, -30, 58, 111]; | ||
// let byte_arr = byte_arr.map(|byte| byte as u8); | ||
// let result = u128::from_le_bytes(byte_arr)) | ||
byte[] bytes = {-124, -72, -92, 56, 112, -102, 29, 107, -111, -17, 73, -30, -49, -30, 58, 111}; | ||
String expected = "147850061044753742757552691558406731908"; | ||
BigInteger result = LittleEndianUtils.fromLittleEndianByteArray(bytes); | ||
assertEquals(expected, result.toString()); | ||
} | ||
|
||
@Test | ||
void testFromLittleEndianByteArrayWithEmptyArray() { | ||
byte[] bytes = {}; | ||
BigInteger expected = BigInteger.ZERO; | ||
BigInteger result = LittleEndianUtils.fromLittleEndianByteArray(bytes); | ||
assertEquals(expected, result); | ||
} | ||
} |