Skip to content

Commit

Permalink
70 bad encoding new line char str (#71)
Browse files Browse the repository at this point in the history
Signed-off-by: jasperpotts <[email protected]>
Co-authored-by: jasperpotts <[email protected]>
  • Loading branch information
jasperpotts and jasperpotts authored Jul 13, 2023
1 parent 50bd662 commit bcca428
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ public static int sizeOfString(FieldDefinition field, String value) {
*/
private static int sizeOfStringNoTag(FieldDefinition field, String value) {
// When not a oneOf don't write default value
if (!field.oneOf() && (value == null || value.isBlank())) {
if (!field.oneOf() && (value == null || value.isEmpty())) {
return 0;
}
try {
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public static Stream<NoToStringWrapper<MessageWithString>> createModelTestArgume
"",
"Dude",
"©«",
"\n",
"I need some HBAR to run work on Hedera!",
"I need some ℏ to run work on Hedera!",
SAMPLE_TEXT_BLOCK,
Expand Down

0 comments on commit bcca428

Please sign in to comment.