Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

private let int32BufferStorage: [Int32] = [10, 20, 30, 40]
private let emptyInt32BufferStorage: [Int32] = []
private var mutableInt32BufferStorage: [Int32] = [5, 10, 15]

public func makeInt32Buffer() -> UnsafeBufferPointer<Int32> {
int32BufferStorage.withUnsafeBufferPointer { $0 }
}

public func makeEmptyInt32Buffer() -> UnsafeBufferPointer<Int32> {
emptyInt32BufferStorage.withUnsafeBufferPointer { $0 }
}

public func sumInt32Buffer(data: UnsafeBufferPointer<Int32>) -> Int64 {
data.reduce(0) { $0 + Int64($1) }
}

public func makeMutableInt32Buffer() -> UnsafeMutableBufferPointer<Int32> {
mutableInt32BufferStorage.withUnsafeMutableBufferPointer { $0 }
}

public func sumMutableInt32Buffer(data: UnsafeMutableBufferPointer<Int32>) -> Int64 {
data.reduce(0) { $0 + Int64($1) }
}

public func mutableInt32BufferElement(data: UnsafeMutableBufferPointer<Int32>, index: Int32) -> Int32 {
data[Int(index)]
}

public func incrementMutableInt32Buffer(data: UnsafeMutableBufferPointer<Int32>) {
for index in data.indices {
data[index] += 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ public func sumOfBytes(data: UnsafeRawBufferPointer) -> Int64 {
public func bufferCount(data: UnsafeRawBufferPointer) -> Int64 {
Int64(data.count)
}

private let rawBufferStorage: [UInt8] = [10, 20, 30, 40]
private let emptyRawBufferStorage: [UInt8] = []
private var mutableRawBufferStorage: [UInt8] = [5, 10, 15]

public func makeRawBuffer() -> UnsafeRawBufferPointer {
rawBufferStorage.withUnsafeBytes { $0 }
}

public func makeEmptyRawBuffer() -> UnsafeRawBufferPointer {
emptyRawBufferStorage.withUnsafeBytes { $0 }
}

public func makeMutableRawBuffer() -> UnsafeMutableRawBufferPointer {
mutableRawBufferStorage.withUnsafeMutableBytes { $0 }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package com.example.swift;

import org.junit.jupiter.api.Test;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

public class UnsafeBufferPointerTest {
@Test
void nullMemorySegment_isEmptyTypedBuffer() {
assertEquals(0, MySwiftLibrary.sumInt32Buffer(MemorySegment.NULL));
}

@Test
void sumInt32Buffer() {
try (Arena arena = Arena.ofConfined()) {
MemorySegment input = arena.allocateFrom(ValueLayout.JAVA_INT, new int[] {10, 20, 30, 40});

assertEquals(100, MySwiftLibrary.sumInt32Buffer(input));
}
}

@Test
void sumInt32Buffer_empty() {
try (Arena arena = Arena.ofConfined()) {
MemorySegment input = arena.allocate(0, ValueLayout.JAVA_INT.byteAlignment());

assertEquals(0, MySwiftLibrary.sumInt32Buffer(input));
}
}

@Test
void returnInt32Buffer() {
MemorySegment result = MySwiftLibrary.makeInt32Buffer();

assertEquals(16, result.byteSize());
assertEquals(10, result.get(ValueLayout.JAVA_INT, 0));
assertEquals(40, result.get(ValueLayout.JAVA_INT, 12));
}

@Test
void returnEmptyInt32Buffer() {
assertEquals(0, MySwiftLibrary.makeEmptyInt32Buffer().byteSize());
}

@Test
void returnMutableInt32Buffer() {
MemorySegment result = MySwiftLibrary.makeMutableInt32Buffer();

assertEquals(12, result.byteSize());
assertEquals(5, result.get(ValueLayout.JAVA_INT, 0));
assertEquals(15, result.get(ValueLayout.JAVA_INT, 8));
}

@Test
void mutableInt32Buffer_isInitialized() {
MemorySegment buffer = MySwiftLibrary.makeMutableInt32Buffer();

assertEquals(5, buffer.get(ValueLayout.JAVA_INT, 0));
assertEquals(10, buffer.get(ValueLayout.JAVA_INT, 4));
assertEquals(15, buffer.get(ValueLayout.JAVA_INT, 8));
}

@Test
void incrementMutableInt32Buffer_modifiesElements() {
try (Arena arena = Arena.ofConfined()) {
MemorySegment buffer = arena.allocateFrom(ValueLayout.JAVA_INT, new int[] {5, 10, 15});

MySwiftLibrary.incrementMutableInt32Buffer(buffer);

assertEquals(6, buffer.get(ValueLayout.JAVA_INT, 0));
assertEquals(11, buffer.get(ValueLayout.JAVA_INT, 4));
assertEquals(16, buffer.get(ValueLayout.JAVA_INT, 8));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package com.example.swift;

import org.junit.jupiter.api.Test;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

public class UnsafeRawBufferPointerTest {
@Test
void sumOfBytes() {
try (Arena arena = Arena.ofConfined()) {
MemorySegment input = arena.allocateFrom(ValueLayout.JAVA_BYTE, new byte[] {1, 2, 3, 4, 5});

assertEquals(15, MySwiftLibrary.sumOfBytes(input));
}
}

@Test
void sumOfBytes_empty() {
try (Arena arena = Arena.ofConfined()) {
MemorySegment input = arena.allocate(0, 1);

assertEquals(0, MySwiftLibrary.sumOfBytes(input));
}
}

@Test
void bufferCount() {
try (Arena arena = Arena.ofConfined()) {
MemorySegment input = arena.allocateFrom(ValueLayout.JAVA_BYTE, new byte[] {10, 20, 30, 40});

assertEquals(4, MySwiftLibrary.bufferCount(input));
}
}

@Test
void bufferCount_empty() {
try (Arena arena = Arena.ofConfined()) {
MemorySegment input = arena.allocate(0, 1);

assertEquals(0, MySwiftLibrary.bufferCount(input));
}
}

@Test
void returnRawBuffer() {
MemorySegment result = MySwiftLibrary.makeRawBuffer();

assertEquals(4, result.byteSize());
assertEquals(10, result.get(ValueLayout.JAVA_BYTE, 0));
assertEquals(40, result.get(ValueLayout.JAVA_BYTE, 3));
}

@Test
void returnEmptyRawBuffer() {
assertEquals(0, MySwiftLibrary.makeEmptyRawBuffer().byteSize());
}

@Test
void returnMutableRawBuffer() {
MemorySegment result = MySwiftLibrary.makeMutableRawBuffer();

assertEquals(3, result.byteSize());
assertEquals(5, result.get(ValueLayout.JAVA_BYTE, 0));
assertEquals(15, result.get(ValueLayout.JAVA_BYTE, 2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import SwiftJava

private let int32BufferStorage: [Int32] = [10, 20, 30, 40]
private let emptyInt32BufferStorage: [Int32] = []
private var mutableInt32BufferStorage: [Int32] = [5, 10, 15]

public func makeInt32Buffer() -> UnsafeBufferPointer<Int32> {
int32BufferStorage.withUnsafeBufferPointer { $0 }
}

public func makeEmptyInt32Buffer() -> UnsafeBufferPointer<Int32> {
emptyInt32BufferStorage.withUnsafeBufferPointer { $0 }
}

public func sumInt32Buffer(data: UnsafeBufferPointer<Int32>) -> Int64 {
data.reduce(0) { $0 + Int64($1) }
}

public func makeMutableInt32Buffer() -> UnsafeMutableBufferPointer<Int32> {
mutableInt32BufferStorage.withUnsafeMutableBufferPointer { $0 }
}

public func sumMutableInt32Buffer(data: UnsafeMutableBufferPointer<Int32>) -> Int64 {
data.reduce(0) { $0 + Int64($1) }
}

public func mutableInt32BufferElement(data: UnsafeMutableBufferPointer<Int32>, index: Int32) -> Int32 {
data[Int(index)]
}

public func incrementMutableInt32Buffer(data: UnsafeMutableBufferPointer<Int32>) {
for index in data.indices {
data[index] += 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,20 @@ public func sumOfBytes(data: UnsafeRawBufferPointer) -> Int64 {
public func bufferCount(data: UnsafeRawBufferPointer) -> Int64 {
Int64(data.count)
}
// snippet.end

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would break existing snippets

//snippet.end

private let rawBufferStorage: [UInt8] = [10, 20, 30, 40]
private let emptyRawBufferStorage: [UInt8] = []
private var mutableRawBufferStorage: [UInt8] = [5, 10, 15]

public func makeRawBuffer() -> UnsafeRawBufferPointer {
rawBufferStorage.withUnsafeBytes { $0 }
}

public func makeEmptyRawBuffer() -> UnsafeRawBufferPointer {
emptyRawBufferStorage.withUnsafeBytes { $0 }
}

public func makeMutableRawBuffer() -> UnsafeMutableRawBufferPointer {
mutableRawBufferStorage.withUnsafeMutableBytes { $0 }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

package com.example.swift;

import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.SwiftUnsafeBufferPointer;
import org.swift.swiftkit.core.SwiftUnsafeMutableBufferPointer;

import static org.junit.jupiter.api.Assertions.*;

public class UnsafeBufferPointerTest {
@Test
void returnInt32Buffer() {
SwiftUnsafeBufferPointer buffer = MySwiftLibrary.makeInt32Buffer();

assertNotEquals(0, buffer.getBaseAddress());
assertEquals(4, buffer.getCount());
assertEquals(100, MySwiftLibrary.sumInt32Buffer(buffer));
}

@Test
void returnEmptyInt32Buffer() {
SwiftUnsafeBufferPointer buffer = MySwiftLibrary.makeEmptyInt32Buffer();
assertEquals(0, buffer.getBaseAddress());
assertEquals(0, buffer.getCount());
assertEquals(0, MySwiftLibrary.sumInt32Buffer(buffer));
}

@Test
void returnMutableInt32Buffer() {
SwiftUnsafeMutableBufferPointer buffer = MySwiftLibrary.makeMutableInt32Buffer();

assertNotEquals(0, buffer.getBaseAddress());
assertEquals(3, buffer.getCount());
assertEquals(30, MySwiftLibrary.sumMutableInt32Buffer(buffer));
}


@Test
void mutableInt32Buffer_isInitialized() {
SwiftUnsafeMutableBufferPointer buffer = MySwiftLibrary.makeMutableInt32Buffer();

assertEquals(5, MySwiftLibrary.mutableInt32BufferElement(buffer, 0));
assertEquals(10, MySwiftLibrary.mutableInt32BufferElement(buffer, 1));
assertEquals(15, MySwiftLibrary.mutableInt32BufferElement(buffer, 2));
}

@Test
void incrementMutableInt32Buffer_modifiesElements() {
SwiftUnsafeMutableBufferPointer buffer = MySwiftLibrary.makeMutableInt32Buffer();

MySwiftLibrary.incrementMutableInt32Buffer(buffer);

assertEquals(6, MySwiftLibrary.mutableInt32BufferElement(buffer, 0));
assertEquals(11, MySwiftLibrary.mutableInt32BufferElement(buffer, 1));
assertEquals(16, MySwiftLibrary.mutableInt32BufferElement(buffer, 2));
}
}
Loading
Loading