diff --git a/exercises/practice/binary-search-tree/.meta/Sources/BinarySearchTree/BinarySearchTreeExample.swift b/exercises/practice/binary-search-tree/.meta/Sources/BinarySearchTree/BinarySearchTreeExample.swift index 3c696d1a4..45a695561 100644 --- a/exercises/practice/binary-search-tree/.meta/Sources/BinarySearchTree/BinarySearchTreeExample.swift +++ b/exercises/practice/binary-search-tree/.meta/Sources/BinarySearchTree/BinarySearchTreeExample.swift @@ -1,66 +1,72 @@ indirect enum BinarySearchTree { - var data: Element? { - guard case let .node(_, value, _) = self else { return nil } - return value + var data: Element? { + guard case let .node(_, value, _) = self else { return nil } + return value + } + + var left: BinarySearchTree? { + guard case let .node(left, _, _) = self else { return nil } + return left + } + + var right: BinarySearchTree? { + guard case let .node(_, _, right) = self else { return nil } + return right + } + + case empty + case node(BinarySearchTree, Element, BinarySearchTree) + + init( + left: BinarySearchTree = .empty, + _ value: Element, + right: BinarySearchTree = .empty + ) { + self = .node(left, value, right) + } + init() { + self = .empty + } + private func insert(_ into: BinarySearchTree, _ newValue: Element) -> BinarySearchTree< + Element + > { + let bailOut = BinarySearchTree(left: .empty, newValue, right: .empty) + + guard case let .node(left, value, right) = into else { return bailOut } + + if newValue <= value { + return + BinarySearchTree(left: insert(left, newValue), value, right: right) } - var left: BinarySearchTree? { - guard case let .node(left, _, _) = self else { return nil } - return left + if value <= newValue { + return + BinarySearchTree(left: left, value, right: insert(right, newValue)) } - var right: BinarySearchTree? { - guard case let .node(_, _, right) = self else { return nil } - return right - } + return .node(left, value, right) + } + mutating func insert(_ newValue: Element) { - case empty - case node(BinarySearchTree, Element, BinarySearchTree) + guard case let .node(left, value, right) = insert(self, newValue) else { return } - init(left: BinarySearchTree = .empty, - _ value: Element, - right: BinarySearchTree = .empty ) { - self = .node(left, value, right) - } - init() { - self = .empty - } - private func insert ( _ into: BinarySearchTree, _ newValue: Element ) -> BinarySearchTree { - let bailOut = BinarySearchTree(left: .empty, newValue, right: .empty) + self = .node(left, value, right) + } - guard case let .node(left, value, right) = into else { return bailOut } + private func getAllData() -> [Element] { - if newValue <= value { return - BinarySearchTree(left: insert(left, newValue), value, right: right) - } + guard case let .node(left, value, right) = self else { return [] } + var result = [value] - if value <= newValue { return - BinarySearchTree(left: left, value, right: insert(right, newValue)) - } + result += left.getAllData() + result += right.getAllData() - return .node(left, value, right) - } - mutating func insert( _ newValue: Element ) { - - guard case let .node(left, value, right) = insert(self, newValue) else { return } - - self = .node(left, value, right) - } + return result - private func getAllData() -> [Element] { + } - guard case let .node( left, value, right) = self else { return [] } - var result = [value] - - result += left.getAllData() - result += right.getAllData() - - return result - - } - - func allData() -> [Element] { - return getAllData().sorted(by: <) - } + func allData() -> [Element] { + return getAllData().sorted(by: <) + } } diff --git a/exercises/practice/binary-search-tree/Package.swift b/exercises/practice/binary-search-tree/Package.swift index 290cf0dbf..b02ca1b25 100644 --- a/exercises/practice/binary-search-tree/Package.swift +++ b/exercises/practice/binary-search-tree/Package.swift @@ -1,21 +1,21 @@ -// swift-tools-version:5.3 +// swift-tools-version:6.0 import PackageDescription let package = Package( - name: "BinarySearchTree", - products: [ - .library( - name: "BinarySearchTree", - targets: ["BinarySearchTree"]), - ], - dependencies: [], - targets: [ - .target( - name: "BinarySearchTree", - dependencies: []), - .testTarget( - name: "BinarySearchTreeTests", - dependencies: ["BinarySearchTree"]), - ] + name: "BinarySearchTree", + products: [ + .library( + name: "BinarySearchTree", + targets: ["BinarySearchTree"]) + ], + dependencies: [], + targets: [ + .target( + name: "BinarySearchTree", + dependencies: []), + .testTarget( + name: "BinarySearchTreeTests", + dependencies: ["BinarySearchTree"]), + ] ) diff --git a/exercises/practice/binary-search-tree/Tests/BinarySearchTreeTests/BinarySearchTreeTests.swift b/exercises/practice/binary-search-tree/Tests/BinarySearchTreeTests/BinarySearchTreeTests.swift index c83b98439..a39c3c9a7 100644 --- a/exercises/practice/binary-search-tree/Tests/BinarySearchTreeTests/BinarySearchTreeTests.swift +++ b/exercises/practice/binary-search-tree/Tests/BinarySearchTreeTests/BinarySearchTreeTests.swift @@ -1,74 +1,87 @@ -import XCTest +import Foundation +import Testing + @testable import BinarySearchTree -class BinarySearchTreeTests: XCTestCase { +let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL"] ?? "fa,se") ?? false + +@Suite struct BinarySearchTreeTests { - func testDataIsRetained() { - XCTAssertEqual(4, BinarySearchTree(4).data) - } + @Test("Data is retained") + func testDataIsRetained() { + #expect(4 == BinarySearchTree(4).data) + } - func testInsertingLess() { - var four = BinarySearchTree(4) - four.insert(2) - XCTAssertEqual(4, four.data) - XCTAssertEqual(2, four.left?.data) - } + @Test("Inserting less", .enabled(if: RUNALL)) + func testInsertingLess() { + var four = BinarySearchTree(4) + four.insert(2) + #expect(4 == four.data) + #expect(2 == four.left?.data) + } - func testInsertingSame() { - var four = BinarySearchTree(4) - four.insert(4) - XCTAssertEqual(4, four.data) - XCTAssertEqual(4, four.left?.data) - } + @Test("Inserting same", .enabled(if: RUNALL)) + func testInsertingSame() { + var four = BinarySearchTree(4) + four.insert(4) + #expect(4 == four.data) + #expect(4 == four.left?.data) + } - func testInsertingRight() { - var four = BinarySearchTree(4) - four.insert(5) - XCTAssertEqual(4, four.data) - XCTAssertEqual(5, four.right?.data) - } + @Test("Inserting right", .enabled(if: RUNALL)) + func testInsertingRight() { + var four = BinarySearchTree(4) + four.insert(5) + #expect(4 == four.data) + #expect(5 == four.right?.data) + } - func testComplexTree() { - var four = BinarySearchTree(4) - four.insert(2) - four.insert(6) - four.insert(1) - four.insert(3) - four.insert(7) - four.insert(5) - XCTAssertEqual(4, four.data) - XCTAssertEqual(2, four.left?.data) - XCTAssertEqual(1, four.left?.left?.data) - XCTAssertEqual(3, four.left?.right?.data) - XCTAssertEqual(6, four.right?.data) - XCTAssertEqual(5, four.right?.left?.data) - XCTAssertEqual(7, four.right?.right?.data) - } + @Test("Complex tree", .enabled(if: RUNALL)) + func testComplexTree() { + var four = BinarySearchTree(4) + four.insert(2) + four.insert(6) + four.insert(1) + four.insert(3) + four.insert(7) + four.insert(5) + #expect(4 == four.data) + #expect(2 == four.left?.data) + #expect(1 == four.left?.left?.data) + #expect(3 == four.left?.right?.data) + #expect(6 == four.right?.data) + #expect(5 == four.right?.left?.data) + #expect(7 == four.right?.right?.data) + } - func testAllDataForOneElement() { - XCTAssertEqual([4], BinarySearchTree(4).allData()) - } + @Test("All data for one element", .enabled(if: RUNALL)) + func testAllDataForOneElement() { + #expect([4] == BinarySearchTree(4).allData()) + } - func testAllDataForSmallerElement() { - var four = BinarySearchTree(4) - four.insert(2) - XCTAssertEqual([2, 4], four.allData()) - } + @Test("All data for smaller element", .enabled(if: RUNALL)) + func testAllDataForSmallerElement() { + var four = BinarySearchTree(4) + four.insert(2) + #expect([2, 4] == four.allData()) + } - func testAllDataForLargerElement() { - var four = BinarySearchTree(4) - four.insert(5) - XCTAssertEqual([4, 5], four.allData()) - } + @Test("All data for larger element", .enabled(if: RUNALL)) + func testAllDataForLargerElement() { + var four = BinarySearchTree(4) + four.insert(5) + #expect([4, 5] == four.allData()) + } - func testAllDataForComplexTree() { - var four = BinarySearchTree(4) - four.insert(2) - four.insert(1) - four.insert(3) - four.insert(6) - four.insert(7) - four.insert(5) - XCTAssertEqual([1, 2, 3, 4, 5, 6, 7], four.allData()) - } + @Test("All data for complex tree", .enabled(if: RUNALL)) + func testAllDataForComplexTree() { + var four = BinarySearchTree(4) + four.insert(2) + four.insert(1) + four.insert(3) + four.insert(6) + four.insert(7) + four.insert(5) + #expect([1, 2, 3, 4, 5, 6, 7] == four.allData()) + } } diff --git a/exercises/practice/simple-cipher/.meta/Sources/SimpleCipher/SimpleCipherExample.swift b/exercises/practice/simple-cipher/.meta/Sources/SimpleCipher/SimpleCipherExample.swift index ec6a5fa23..e6a0b382c 100644 --- a/exercises/practice/simple-cipher/.meta/Sources/SimpleCipher/SimpleCipherExample.swift +++ b/exercises/practice/simple-cipher/.meta/Sources/SimpleCipher/SimpleCipherExample.swift @@ -1,95 +1,96 @@ import Foundation #if os(Linux) - import Glibc + import Glibc #elseif os(OSX) - import Darwin + import Darwin #endif func arc4random_uniform(_ input: Int) -> Int { - #if os(Linux) - return random() % input - #elseif os(OSX) - let temp = UInt32(input) - return Int(arc4random_uniform(temp)) - #endif + #if os(Linux) + return random() % input + #elseif os(OSX) + let temp = UInt32(input) + return Int(arc4random_uniform(temp)) + #endif } public struct Cipher { - private let abc = "abcdefghijklmnopqrstuvwxyz" - private var alphabet: [Character] { return Array(abc) } - private(set) var key: String = "" - private var keyArray: [Character] { return Array(key) } - private func randomKeySet() -> String { - var tempKey = "" - for _ in (0..<100).enumerated() { - tempKey.append(alphabet[arc4random_uniform(alphabet.count)]) - } - return tempKey + private let abc = "abcdefghijklmnopqrstuvwxyz" + private var alphabet: [Character] { return Array(abc) } + private(set) var key: String = "" + private var keyArray: [Character] { return Array(key) } + private func randomKeySet() -> String { + var tempKey = "" + for _ in (0..<100).enumerated() { + tempKey.append(alphabet[arc4random_uniform(alphabet.count)]) } - init() { - key = randomKeySet() - } - init?(key: String) { - if isLowerCaseAlfabet(key) { - self.key = key + return tempKey + } + init() { + key = randomKeySet() + } + init?(key: String) { + if isLowerCaseAlfabet(key) { + self.key = key - if key.isEmpty { - return nil - } - } else { return nil - //self.key = randomKeySet() // Alternative non Optional faiulure - } + if key.isEmpty { + return nil + } + } else { + return nil + //self.key = randomKeySet() // Alternative non Optional faiulure } - func isLowerCaseAlfabet(_ inkey: String) -> Bool { - var valid = true - inkey.forEach { + } + func isLowerCaseAlfabet(_ inkey: String) -> Bool { + var valid = true + inkey.forEach { - if "abcdefghijklmnopqrstuvwxyz".contains(String($0)) == false { - valid = false - } - } - return valid + if "abcdefghijklmnopqrstuvwxyz".contains(String($0)) == false { + valid = false + } } - func encode(_ plaintext: String) -> String { - let plainTextArray = Array(plaintext) + return valid + } + func encode(_ plaintext: String) -> String { + let plainTextArray = Array(plaintext) - func encodeCharacter(_ plaintext: String, idx: Int) -> Character { - //let plainTextArray = Array(plaintext) // hack for subscript support for Strings - var alphabetIdx: Int = - (alphabet.index(of: plainTextArray[idx]) ?? 0) + - (alphabet.index(of: keyArray[idx]) ?? 0) - if alphabetIdx >= alphabet.count { - alphabetIdx -= alphabet.count - } - return alphabet[alphabetIdx] - } + func encodeCharacter(_ plaintext: String, idx: Int) -> Character { + //let plainTextArray = Array(plaintext) // hack for subscript support for Strings + var alphabetIdx: Int = + (alphabet.firstIndex(of: plainTextArray[idx]) ?? 0) + + (alphabet.firstIndex(of: keyArray[idx % keyArray.count]) ?? 0) + if alphabetIdx >= alphabet.count { + alphabetIdx -= alphabet.count + } + return alphabet[alphabetIdx] + } - var ciphertext = "" - for i in 0 ..< min(plainTextArray.count, keyArray.count) { - ciphertext.append(encodeCharacter(plaintext, idx: i)) - } - return ciphertext + var ciphertext = "" + for i in 0.. String { - let cipherTextArray = Array(ciphertext) + func decode(_ ciphertext: String) -> String { + let cipherTextArray = Array(ciphertext) - func decodeCharacter(_ ciphertext: String, idx: Int) -> Character { - //let cipherTextArray = Array(ciphertext) // no native subscript for String - var alphabetIdx: Int = - (alphabet.index(of: cipherTextArray[idx]) ?? 0) - - (alphabet.index(of: keyArray[idx]) ?? 0) - if alphabetIdx < 0 { - alphabetIdx += alphabet.count - } - return alphabet[alphabetIdx] - } - var plaintext = "" + func decodeCharacter(_ ciphertext: String, idx: Int) -> Character { + //let cipherTextArray = Array(ciphertext) // no native subscript for String + var alphabetIdx: Int = + (alphabet.firstIndex(of: cipherTextArray[idx]) ?? 0) + - (alphabet.firstIndex(of: keyArray[idx % keyArray.count]) ?? 0) + if alphabetIdx < 0 { + alphabetIdx += alphabet.count + } + return alphabet[alphabetIdx] + } + var plaintext = "" - for i in 0 ..< cipherTextArray.count { - plaintext.append(decodeCharacter(ciphertext, idx: i)) - } - return plaintext + for i in 0..