|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +package com.example.swift; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.lang.foreign.Arena; |
| 20 | +import java.lang.foreign.MemorySegment; |
| 21 | +import java.lang.foreign.ValueLayout; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | + |
| 25 | +public class UnsafeBufferPointerTest { |
| 26 | + @Test |
| 27 | + void nullMemorySegment_isEmptyTypedBuffer() { |
| 28 | + assertEquals(0, MySwiftLibrary.sumInt32Buffer(MemorySegment.NULL)); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void sumInt32Buffer() { |
| 33 | + try (Arena arena = Arena.ofConfined()) { |
| 34 | + MemorySegment input = arena.allocateFrom(ValueLayout.JAVA_INT, new int[] {10, 20, 30, 40}); |
| 35 | + |
| 36 | + assertEquals(100, MySwiftLibrary.sumInt32Buffer(input)); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void sumInt32Buffer_empty() { |
| 42 | + try (Arena arena = Arena.ofConfined()) { |
| 43 | + MemorySegment input = arena.allocate(0, ValueLayout.JAVA_INT.byteAlignment()); |
| 44 | + |
| 45 | + assertEquals(0, MySwiftLibrary.sumInt32Buffer(input)); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void returnInt32Buffer() { |
| 51 | + MemorySegment result = MySwiftLibrary.makeInt32Buffer(); |
| 52 | + |
| 53 | + assertEquals(16, result.byteSize()); |
| 54 | + assertEquals(10, result.get(ValueLayout.JAVA_INT, 0)); |
| 55 | + assertEquals(40, result.get(ValueLayout.JAVA_INT, 12)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void returnEmptyInt32Buffer() { |
| 60 | + assertEquals(0, MySwiftLibrary.makeEmptyInt32Buffer().byteSize()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void returnMutableInt32Buffer() { |
| 65 | + MemorySegment result = MySwiftLibrary.makeMutableInt32Buffer(); |
| 66 | + |
| 67 | + assertEquals(12, result.byteSize()); |
| 68 | + assertEquals(5, result.get(ValueLayout.JAVA_INT, 0)); |
| 69 | + assertEquals(15, result.get(ValueLayout.JAVA_INT, 8)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void mutableInt32Buffer_isInitialized() { |
| 74 | + MemorySegment buffer = MySwiftLibrary.makeMutableInt32Buffer(); |
| 75 | + |
| 76 | + assertEquals(5, buffer.get(ValueLayout.JAVA_INT, 0)); |
| 77 | + assertEquals(10, buffer.get(ValueLayout.JAVA_INT, 4)); |
| 78 | + assertEquals(15, buffer.get(ValueLayout.JAVA_INT, 8)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void incrementMutableInt32Buffer_modifiesElements() { |
| 83 | + try (Arena arena = Arena.ofConfined()) { |
| 84 | + MemorySegment buffer = arena.allocateFrom(ValueLayout.JAVA_INT, new int[] {5, 10, 15}); |
| 85 | + |
| 86 | + MySwiftLibrary.incrementMutableInt32Buffer(buffer); |
| 87 | + |
| 88 | + assertEquals(6, buffer.get(ValueLayout.JAVA_INT, 0)); |
| 89 | + assertEquals(11, buffer.get(ValueLayout.JAVA_INT, 4)); |
| 90 | + assertEquals(16, buffer.get(ValueLayout.JAVA_INT, 8)); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments