-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
70 bad encoding new line char str (#71)
Signed-off-by: jasperpotts <[email protected]> Co-authored-by: jasperpotts <[email protected]>
- Loading branch information
1 parent
50bd662
commit bcca428
Showing
3 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
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
48 changes: 48 additions & 0 deletions
48
pbj-core/pbj-runtime/src/test/java/com/hedera/pbj/runtime/Utf8ToolsTest.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,48 @@ | ||
package com.hedera.pbj.runtime; | ||
|
||
import com.hedera.pbj.runtime.io.buffer.BufferedData; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HexFormat; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class Utf8ToolsTest { | ||
private static Stream<Arguments> provideStringsAndLengths() { | ||
return Stream.of( | ||
Arguments.of("", 0), | ||
Arguments.of(" ", 1), | ||
Arguments.of("a", 1), | ||
Arguments.of("\n", 1), | ||
Arguments.of("not blank", 9) | ||
); | ||
} | ||
@ParameterizedTest | ||
@MethodSource("provideStringsAndLengths") | ||
void encodedLength(String testStr, int expectedLength) { | ||
assertEquals(expectedLength, assertDoesNotThrow(() -> Utf8Tools.encodedLength(testStr))); | ||
assertEquals(testStr.getBytes(StandardCharsets.UTF_8).length, assertDoesNotThrow(() -> Utf8Tools.encodedLength(testStr))); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideStringsAndLengths") | ||
void encodeUtf8(String testStr, int expectedLength) { | ||
BufferedData bufferedData = BufferedData.allocate(1024); | ||
try { | ||
Utf8Tools.encodeUtf8(testStr, bufferedData); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
bufferedData.flip(); | ||
byte[] bytes = new byte[(int)bufferedData.length()]; | ||
bufferedData.getBytes(0, bytes); | ||
assertEquals(HexFormat.of().formatHex(testStr.getBytes(StandardCharsets.UTF_8)), HexFormat.of().formatHex(bytes)); | ||
assertEquals(expectedLength, bytes.length); | ||
} | ||
} |
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