From c3bb1540f6a5f9e869af1c928e8d66f93afc4327 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Thu, 19 Oct 2023 16:58:48 +0200 Subject: [PATCH] add test --- .../jfuse/api/util/MemoryUtilsTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 jfuse-api/src/test/java/org/cryptomator/jfuse/api/util/MemoryUtilsTest.java diff --git a/jfuse-api/src/test/java/org/cryptomator/jfuse/api/util/MemoryUtilsTest.java b/jfuse-api/src/test/java/org/cryptomator/jfuse/api/util/MemoryUtilsTest.java new file mode 100644 index 0000000..ad4c2ab --- /dev/null +++ b/jfuse-api/src/test/java/org/cryptomator/jfuse/api/util/MemoryUtilsTest.java @@ -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); + } +}