Skip to content

Commit

Permalink
Merge pull request #4 from LucianoPAlmeida/lucianopa/swift-testing
Browse files Browse the repository at this point in the history
[Test] Migrate to Swift testing for learnig
  • Loading branch information
LucianoPAlmeida authored Sep 22, 2024
2 parents 56ea226 + 00a3bfc commit cfbda4c
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 100 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ jobs:
Darwin:

runs-on: macos-latest

env:
DEVELOPER_DIR: /Applications/Xcode_16.app/Contents/Developer
steps:
- uses: actions/checkout@v2
- name: Build
Expand All @@ -19,8 +20,8 @@ jobs:
run: swift test --parallel

Linux:
runs-on: [ubuntu-18.04]
container: swift:5.3.3
runs-on: [ubuntu-latest]
container: swift:6.0.0
steps:
- uses: actions/checkout@v1
- name: Linux
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
"revision" : "8163295f6fe82356b0bcf8e1ab991645de17d096",
"version" : "0.1.2"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics",
"state" : {
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
}
],
"version" : 2
Expand Down
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/google/swift-benchmark", from: "0.1.2")
.package(url: "https://github.com/google/swift-benchmark", from: "0.1.2"),
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.2")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -30,6 +31,6 @@ let package = Package(
dependencies: ["strings", .product(name: "Benchmark", package: "swift-benchmark")]),
.testTarget(
name: "stringsTests",
dependencies: ["strings"])
dependencies: ["strings", .product(name: "Numerics", package: "swift-numerics")])
]
)
10 changes: 8 additions & 2 deletions Sources/strings/FixedBitArray.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import Darwin
//
// FixedBitArray.swift
//
//
// Created by Luciano Almeida on 16/03/21.
//
// ===----------------------------------------------------------------------===//

@usableFromInline
internal struct FixedBitArray {
Expand Down Expand Up @@ -29,7 +35,7 @@ internal struct FixedBitArray {
get {
assert(0..<count ~= idx, "Out of bounds index!")
let (pos, posIdx) = idx.quotientAndRemainder(dividingBy: FixedBitArray._bitsPerWord)
return _words.withUnsafeBufferPointer{ $0[pos] } & (1 << posIdx) != 0
return _words.withUnsafeBufferPointer { $0[pos] } & (1 << posIdx) != 0
}
set {
assert(0..<count ~= idx, "Out of bounds index!")
Expand Down
7 changes: 0 additions & 7 deletions Tests/LinuxMain.swift

This file was deleted.

12 changes: 12 additions & 0 deletions Tests/stringsTests/DistanceExpectation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// DistanceExpectation.swift
// strings
//
// Created by Luciano Almeida on 22/09/24.
//

struct DistanceExpectation<DistanceValue: Numeric> {
let source: String
let target: String
let expectedValue: DistanceValue
}
26 changes: 10 additions & 16 deletions Tests/stringsTests/FixedBitArrayTests.swift
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
import XCTest
import Testing
@testable import strings

final class FixedBitArrayTests: XCTestCase {
struct FixedBitArrayTests {

func testDefaultValues() {
@Test func testDefaultValues() {
let size = 10
let storage = FixedBitArray(count: size)
XCTAssertEqual(size, storage.count)
#expect(size == storage.count)

for i in 0..<size {
XCTAssertFalse(storage[i])
#expect(!storage[i])
}
}

func testModifyValues() {
_modifyValuesTest(size: 10)
_modifyValuesTest(size: 64)
_modifyValuesTest(size: 1024)
_modifyValuesTest(size: 1030)
}

func _modifyValuesTest(size: Int) {
@Test(arguments: [10, 64, 1024, 1030])
func testModifyValues(size: Int) {
var storage = FixedBitArray(count: size)
for i in 0..<size where i % 2 == 0 {
storage[i] = true
}

for i in 0..<size {
if i % 2 == 0 {
XCTAssertTrue(storage[i])
#expect(storage[i])
} else {
XCTAssertFalse(storage[i])
#expect(!storage[i])
}
}

Expand All @@ -39,7 +33,7 @@ final class FixedBitArrayTests: XCTestCase {
}

for i in 0..<size {
XCTAssertFalse(storage[i])
#expect(!storage[i])
}
}
}
22 changes: 11 additions & 11 deletions Tests/stringsTests/HammingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
// Created by Luciano Almeida on 16/03/21.
//

import XCTest
import Testing
import strings

final class HammingTests: XCTestCase {
struct HammingTests {
static let values: [DistanceExpectation<Int>] = [
DistanceExpectation(source: "friend", target: "friend", expectedValue: 0),
DistanceExpectation(source: "friend", target: "frinnd", expectedValue: 1),
DistanceExpectation(source: "test", target: "team", expectedValue: 2),
DistanceExpectation(source: "", target: "", expectedValue: 0)
]

func testHammingDistances() {
XCTAssertEqual("friend".hammingDistance(to: "friend"), 0)
XCTAssertEqual("friend".hammingDistance(to: "frinnd"), 1)
XCTAssertEqual("test".hammingDistance(to: "team"), 2)
XCTAssertEqual("".hammingDistance(to: ""), 0)
@Test(arguments: values)
func testHammingDistances(expectation: DistanceExpectation<Int>) {
#expect(expectation.source.hammingDistance(to: expectation.target) == expectation.expectedValue)
}

static var allTests = [
("testHammingDistances", testHammingDistances)
]
}
71 changes: 41 additions & 30 deletions Tests/stringsTests/JaroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,52 @@
// Created by Luciano Almeida on 12/02/21.
//

import XCTest
import Testing
import strings
import Numerics

final class JaroTests: XCTestCase {
struct JaroTests {
static let jaroValues: [DistanceExpectation<Double>] = [
DistanceExpectation(source: "friend", target: "fresh", expectedValue: 0.70),
DistanceExpectation(source: "friend", target: "friend", expectedValue: 1),
DistanceExpectation(source: "friend", target: "fried", expectedValue: 0.9444),
DistanceExpectation(source: "rick", target: "rcik", expectedValue: 0.9166),
DistanceExpectation(source: "rick", target: "irkc", expectedValue: 0.8333),
DistanceExpectation(source: "", target: "team", expectedValue: 0),
DistanceExpectation(source: "team", target: "", expectedValue: 0),
DistanceExpectation(source: "adlsajdlsa", target: "asv", expectedValue: 0.6222),
DistanceExpectation(source: "DwAyNE", target: "DuANE", expectedValue: 0.822222)
]

func testJaroDistances() {
XCTAssertEqual(0.70, "friend".jaroDistance(to: "fresh"), accuracy: 0.0001)
XCTAssertEqual(1, "friend".jaroDistance(to: "friend"), accuracy: 0.0001)
XCTAssertEqual(0.9444, "friend".jaroDistance(to: "fried"), accuracy: 0.0001)
XCTAssertEqual(0.9166, "rick".jaroDistance(to: "rcik"), accuracy: 0.0001)
XCTAssertEqual(0.8333, "rick".jaroDistance(to: "irkc"), accuracy: 0.0001)
XCTAssertEqual(0, "".jaroDistance(to: "team"), accuracy: 0.0001)
XCTAssertEqual(0, "team".jaroDistance(to: ""), accuracy: 0.0001)
XCTAssertEqual(0.6222, "adlsajdlsa".jaroDistance(to: "asv"), accuracy: 0.0001)
XCTAssertEqual(0.822222, "DwAyNE".jaroDistance(to: "DuANE"), accuracy: 0.0001)
}
static let jaroWinklerValues: [DistanceExpectation<Double>] = [
DistanceExpectation(source: "friend", target: "fresh", expectedValue: 0.760),
DistanceExpectation(source: "friend", target: "friend", expectedValue: 1),
DistanceExpectation(source: "friend", target: "fried", expectedValue: 0.9666),
DistanceExpectation(source: "rick", target: "rcik", expectedValue: 0.9249),
DistanceExpectation(source: "rick", target: "irkc", expectedValue: 0.8333),
DistanceExpectation(source: "", target: "team", expectedValue: 0),
DistanceExpectation(source: "team", target: "", expectedValue: 0),
DistanceExpectation(source: "adlsajdlsa", target: "asv", expectedValue: 0.6222),
DistanceExpectation(source: "DwAyNE", target: "DuANE", expectedValue: 0.840),
DistanceExpectation(source: "TRATE", target: "TRACE", expectedValue: 0.906667)

func testJaroWinklerDistances() {
XCTAssertEqual(0.760, "friend".jaroWinklerDistance(to: "fresh"), accuracy: 0.0001)
XCTAssertEqual(1, "friend".jaroWinklerDistance(to: "friend"), accuracy: 0.0001)
XCTAssertEqual(0.9666, "friend".jaroWinklerDistance(to: "fried"), accuracy: 0.0001)
XCTAssertEqual(0.9249, "rick".jaroWinklerDistance(to: "rcik"), accuracy: 0.0001)
XCTAssertEqual(0.8333, "rick".jaroWinklerDistance(to: "irkc"), accuracy: 0.0001)
XCTAssertEqual(0, "".jaroWinklerDistance(to: "team"), accuracy: 0.0001)
XCTAssertEqual(0, "team".jaroWinklerDistance(to: ""), accuracy: 0.0001)
XCTAssertEqual(0.6222, "adlsajdlsa".jaroWinklerDistance(to: "asv"), accuracy: 0.0001)
XCTAssertEqual(0.840, "DwAyNE".jaroWinklerDistance(to: "DuANE"), accuracy: 0.0001)
XCTAssertEqual(0.906667, "TRATE".jaroWinklerDistance(to: "TRACE"), accuracy: 0.0001)
]

// Scaling
XCTAssertEqual(0.9466, "TRATE".jaroWinklerDistance(to: "TRACE", scaling: 0.2), accuracy: 0.0001)
@Test(arguments: jaroValues)
func testJaroDistances(expectation: DistanceExpectation<Double>) {
#expect(expectation.source.jaroDistance(to: expectation.target)
.isApproximatelyEqual(to: expectation.expectedValue, absoluteTolerance: 0.0001))
}

static var allTests = [
("testJaroDistances", testJaroDistances),
("testJaroWinklerDistances", testJaroWinklerDistances)
]
@Test(arguments: jaroWinklerValues)
func testJaroWinklerDistances(expectation: DistanceExpectation<Double>) {
#expect(expectation.source.jaroWinklerDistance(to: expectation.target)
.isApproximatelyEqual(to: expectation.expectedValue, absoluteTolerance: 0.0001))
}

@Test
func testJaroWinlerDistanceWithScaling() {
#expect("TRATE".jaroWinklerDistance(to: "TRACE", scaling: 0.2)
.isApproximatelyEqual(to: 0.9466, absoluteTolerance: 0.0001))
}
}
36 changes: 18 additions & 18 deletions Tests/stringsTests/LevenshteinTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
//
// Created by Luciano Almeida on 15/12/20.
//
import XCTest
import Testing
import strings

final class LevenshteinTests: XCTestCase {
struct LevenshteinTests {
static let values: [DistanceExpectation<Int>] = [
DistanceExpectation(source: "friend", target: "", expectedValue: 6),
DistanceExpectation(source: "friend", target: "fresh", expectedValue: 3),
DistanceExpectation(source: "friend", target: "friend", expectedValue: 0),
DistanceExpectation(source: "friend", target: "fried", expectedValue: 1),
DistanceExpectation(source: "rick", target: "rcik", expectedValue: 2),
DistanceExpectation(source: "rick", target: "irkc", expectedValue: 3),
DistanceExpectation(source: "irkc", target: "rcik", expectedValue: 4),
DistanceExpectation(source: "test", target: "team", expectedValue: 2),
DistanceExpectation(source: "", target: "team", expectedValue: 4),
DistanceExpectation(source: "test", target: "", expectedValue: 4),
DistanceExpectation(source: "adlsajdlsa", target: "asv", expectedValue: 8)
]

func testLevenshteinDistances() {
XCTAssertEqual("friend".levenshteinDistance(to: ""), 6)
XCTAssertEqual("friend".levenshteinDistance(to: "fresh"), 3)
XCTAssertEqual("friend".levenshteinDistance(to: "friend"), 0)
XCTAssertEqual("friend".levenshteinDistance(to: "fried"), 1)
XCTAssertEqual("rick".levenshteinDistance(to: "rcik"), 2)
XCTAssertEqual("rick".levenshteinDistance(to: "irkc"), 3)
XCTAssertEqual("irkc".levenshteinDistance(to: "rcik"), 4)
XCTAssertEqual("test".levenshteinDistance(to: "team"), 2)
XCTAssertEqual("".levenshteinDistance(to: "team"), 4)
XCTAssertEqual("test".levenshteinDistance(to: ""), 4)
XCTAssertEqual("adlsajdlsa".levenshteinDistance(to: "asv"), 8)
@Test(arguments: values)
func testLevenshteinDistances(expectation: DistanceExpectation<Int>) {
#expect(expectation.source.levenshteinDistance(to: expectation.target) == expectation.expectedValue)
}

static var allTests = [
("testLevenshteinDistances", testLevenshteinDistances)
]
}
11 changes: 0 additions & 11 deletions Tests/stringsTests/XCTestManifests.swift

This file was deleted.

0 comments on commit cfbda4c

Please sign in to comment.