Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Oct 19, 2023
1 parent a0b9c0b commit c3bb154
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.cryptomator.jfuse.api.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;

public class MemoryUtilsTest {


@Test
@DisplayName("On MemorySegment != NULL pointer, method returns utf8 string in the memory region")
void testValidSegmentReturnsString() {
try (var arena = Arena.ofConfined()) {
var address = arena.allocate(4);
address.setUtf8String(0, "abc");
String result = MemoryUtils.toUtf8StringOrNull(address);
Assertions.assertEquals("abc", result);
}
}

@Test
@DisplayName("With offset, on MemorySegment != NULL pointer, method returns utf8 string in the memory region")
void testValidSegmentReturnsStringAtOffset() {
try (var arena = Arena.ofConfined()) {
var address = arena.allocate(10);
address.setUtf8String(5, "abc");
String result = MemoryUtils.toUtf8StringOrNull(address, 5);
Assertions.assertEquals("abc", result);
}
}

@Test
@DisplayName("When MemorySegment == NULL pointer, method returns null")
void testNullPointerSegmentReturnsNull() {
String result = MemoryUtils.toUtf8StringOrNull(MemorySegment.NULL, 0);
Assertions.assertNull(result);
}
}

0 comments on commit c3bb154

Please sign in to comment.