Skip to content
Merged
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
393 changes: 192 additions & 201 deletions Tests/WASITests/IntegrationTests.swift

Large diffs are not rendered by default.

65 changes: 34 additions & 31 deletions Tests/WASITests/Platform/SandboxPrimitives/OpenParentTests.swift
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
import SystemPackage
import XCTest
#if canImport(Testing)
import Testing
import SystemPackage

@testable import WASI
@testable import WASI

final class OpenParentTests: XCTestCase {
func testSplitParent() {
func XCTCheck(
_ lhs: (FilePath, FilePath.Component)?,
_ rhs: (FilePath, FilePath.Component)?,
file: StaticString = #file,
line: UInt = #line
) {
switch (lhs, rhs) {
case (.none, .none): return
case let (.some(lhs), .some(rhs)):
XCTAssertEqual(lhs.0, rhs.0, file: file, line: line)
XCTAssertEqual(lhs.1, rhs.1, file: file, line: line)
default:
XCTFail("\(String(describing: lhs)) and \(String(describing: rhs)) are not equal", file: file, line: line)
@Suite
struct OpenParentTests {
@Test
func testSplitParent() {
func check(
_ lhs: (FilePath, FilePath.Component)?,
_ rhs: (FilePath, FilePath.Component)?,
sourceLocation: SourceLocation = #_sourceLocation
) {
switch (lhs, rhs) {
case (.none, .none): return
case let (.some(lhs), .some(rhs)):
#expect(lhs.0 == rhs.0, sourceLocation: sourceLocation)
#expect(lhs.1 == rhs.1, sourceLocation: sourceLocation)
default:
#expect((false), "\(String(describing: lhs)) and \(String(describing: rhs)) are not equal", sourceLocation: sourceLocation)
}
}
}

XCTCheck(splitParent(path: ""), nil)
check(splitParent(path: ""), nil)

XCTCheck(splitParent(path: "/"), (FilePath("/"), FilePath.Component(".")))
XCTCheck(splitParent(path: "/."), (FilePath("/."), FilePath.Component(".")))
XCTCheck(splitParent(path: "/a"), (FilePath("/"), FilePath.Component("a")))
XCTCheck(splitParent(path: "/a/"), (FilePath("/a"), FilePath.Component(".")))
XCTCheck(splitParent(path: "/a/."), (FilePath("/a/."), FilePath.Component(".")))
XCTCheck(splitParent(path: "/a/.."), (FilePath("/a/.."), FilePath.Component(".")))
check(splitParent(path: "/"), (FilePath("/"), FilePath.Component(".")))
check(splitParent(path: "/."), (FilePath("/."), FilePath.Component(".")))
check(splitParent(path: "/a"), (FilePath("/"), FilePath.Component("a")))
check(splitParent(path: "/a/"), (FilePath("/a"), FilePath.Component(".")))
check(splitParent(path: "/a/."), (FilePath("/a/."), FilePath.Component(".")))
check(splitParent(path: "/a/.."), (FilePath("/a/.."), FilePath.Component(".")))

XCTCheck(splitParent(path: "b"), (FilePath(""), FilePath.Component("b")))
XCTCheck(splitParent(path: "b/."), (FilePath("b/."), FilePath.Component(".")))
XCTCheck(splitParent(path: "b/.."), (FilePath("b/.."), FilePath.Component(".")))
check(splitParent(path: "b"), (FilePath(""), FilePath.Component("b")))
check(splitParent(path: "b/."), (FilePath("b/."), FilePath.Component(".")))
check(splitParent(path: "b/.."), (FilePath("b/.."), FilePath.Component(".")))

XCTCheck(splitParent(path: "../c"), (FilePath(".."), FilePath.Component("c")))
check(splitParent(path: "../c"), (FilePath(".."), FilePath.Component("c")))
}
}
}
#endif
61 changes: 33 additions & 28 deletions Tests/WASITests/RandomBufferGeneratorTests.swift
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
import XCTest
#if canImport(Testing)
import Testing

@testable import WASI
@testable import WASI

final class RandomBufferGeneratorTests: XCTestCase {
struct DeterministicGenerator: RandomNumberGenerator, RandomBufferGenerator {
var items: [UInt64]
@Suite
struct RandomBufferGeneratorTests {
struct DeterministicGenerator: RandomNumberGenerator, RandomBufferGenerator {
var items: [UInt64]

mutating func next() -> UInt64 {
items.removeFirst()
mutating func next() -> UInt64 {
items.removeFirst()
}
}
}
func testDefaultFill() {
var generator = DeterministicGenerator(items: [
0x0123_4567_89ab_cdef, 0xfedc_ba98_7654_3210, 0xdead_beef_badd_cafe,
])
for (bufferSize, expectedBytes): (Int, [UInt8]) in [
(10, [0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, 0x10, 0x32]),
(2, [0xfe, 0xca]),
(0, []),
] {
var buffer: [UInt8] = Array(repeating: 0, count: bufferSize)
buffer.withUnsafeMutableBufferPointer {
generator.fill(buffer: $0)
@Test
func defaultFill() {
var generator = DeterministicGenerator(items: [
0x0123_4567_89ab_cdef, 0xfedc_ba98_7654_3210, 0xdead_beef_badd_cafe,
])
for (bufferSize, expectedBytes): (Int, [UInt8]) in [
(10, [0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, 0x10, 0x32]),
(2, [0xfe, 0xca]),
(0, []),
] {
var buffer: [UInt8] = Array(repeating: 0, count: bufferSize)
buffer.withUnsafeMutableBufferPointer {
generator.fill(buffer: $0)
}
let expected: [UInt8]
#if _endian(little)
expected = expectedBytes
#else
expected = Array(expectedBytes.reversed())
#endif
#expect(buffer == expected)
}
let expected: [UInt8]
#if _endian(little)
expected = expectedBytes
#else
expected = Array(expectedBytes.reversed())
#endif
XCTAssertEqual(buffer, expected)
}
}
}

#endif
Loading