diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 232b632..8deeb3d 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -1,15 +1,28 @@ name: Swift -on: [push] +on: [push, pull_request] jobs: build: - - runs-on: macOS-latest - + strategy: + matrix: + os: [macOS-latest, ubuntu-latest] + swift: ["5.1"] + runs-on: ${{ matrix.os }} + env: + SWIFT_VERSION: ${{ matrix.swift }} + SWIFT_EXEC: .swiftenv/shims/swift steps: - uses: actions/checkout@v2 + - name: Install Swift + run: | + git clone https://github.com/kylef/swiftenv.git ~/.swiftenv + ~/.swiftenv/bin/swiftenv install $SWIFT_VERSION --skip-existing + ~/.swiftenv/bin/swiftenv rehash - name: Build - run: swift build -v - - name: Run Tests - run: swift test -v + run: | + ~/$SWIFT_EXEC --version + ~/$SWIFT_EXEC build -v + - name: Test + run: | + ~/$SWIFT_EXEC test -v diff --git a/.gitignore b/.gitignore index 692f4c0..cc4e93e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ -Complex.xcodeproj/project.xcworkspace +Complex.xcodeproj/project.xcworkspace/xcuserdata Complex.xcodeproj/xcuserdata .swiftpm Carthage gyb.pyc .build -Package.resolved +IDEWorkspaceChecks.plist diff --git a/.swiftlint.yml b/.swiftlint.yml index 71248be..ce56efe 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -58,7 +58,8 @@ opt_in_rules: reporter: "xcode" excluded: - - Carthage + - Tests/LinuxMain.swift + - Tests/ComplexTests/XCTestManifests.swift identifier_name: excluded: diff --git a/.travis.yml b/.travis.yml index 1d26935..d845acf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,14 +2,8 @@ os: osx language: swift osx_image: xcode11.3 xcode_project: Complex.xcodeproj -addons: - homebrew: - packages: - carthage script: - - carthage bootstrap - - set -o pipefail && travis_retry xcodebuild -scheme "Complex" -destination "platform=iOS Simulator,name=iPhone 11 Pro Max" -configuration Debug ONLY_ACTIVE_ARCH=YES -enableCodeCoverage YES test - set -o pipefail && travis_retry xcodebuild -scheme "Complex macOS" -destination "platform=macOS" -configuration Debug ONLY_ACTIVE_ARCH=YES -enableCodeCoverage YES test - set -o pipefail && travis_retry xcodebuild -scheme "Complex tvOS" -destination "platform=tvOS Simulator,name=Apple TV 4K" -configuration Debug ONLY_ACTIVE_ARCH=YES -enableCodeCoverage YES test diff --git a/Cartfile.private b/Cartfile.private deleted file mode 100644 index 2639593..0000000 --- a/Cartfile.private +++ /dev/null @@ -1 +0,0 @@ -github "SomeRandomiOSDev/Half" ~> 1.0 diff --git a/Cartfile.resolved b/Cartfile.resolved deleted file mode 100644 index e250439..0000000 --- a/Cartfile.resolved +++ /dev/null @@ -1 +0,0 @@ -github "SomeRandomiOSDev/Half" "1.0.0" diff --git a/Complex.podspec b/Complex.podspec index 9d1c0e6..0c3c8b4 100644 --- a/Complex.podspec +++ b/Complex.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "Complex" - s.version = "1.1.1" + s.version = "1.2.0" s.summary = "Swift Complex Number" s.description = <<-DESC A lightweight framework designed for representing and working with complex numbers for iOS, macOS, tvOS, and watchOS. @@ -18,8 +18,17 @@ Pod::Spec.new do |s| s.source = { :git => "https://github.com/SomeRandomiOSDev/Complex.git", :tag => s.version.to_s } s.source_files = 'Sources/**/*.swift' - s.frameworks = 'Foundation' - s.swift_versions = ['4.0', '4.2', '5.0'] + s.swift_versions = ['4.2', '5.0'] s.cocoapods_version = '>= 1.7.3' + + s.test_spec 'Tests' do |ts| + ts.ios.deployment_target = '8.0' + ts.macos.deployment_target = '10.10' + ts.tvos.deployment_target = '9.0' + + ts.dependency 'Half', '~> 1.2' + ts.source_files = 'Tests/ComplexTests/*Tests.swift', + 'Tests/ComplexTests/TestingBase.swift' + end end diff --git a/Complex.xcodeproj/project.pbxproj b/Complex.xcodeproj/project.pbxproj index 6f1050a..44d7af5 100644 --- a/Complex.xcodeproj/project.pbxproj +++ b/Complex.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 50; + objectVersion = 52; objects = { /* Begin PBXAggregateTarget section */ @@ -32,6 +32,9 @@ /* End PBXAggregateTarget section */ /* Begin PBXBuildFile section */ + DD0793442412F03000EF65B4 /* Half in Frameworks */ = {isa = PBXBuildFile; productRef = DD0793432412F03000EF65B4 /* Half */; }; + DD0793462412F03400EF65B4 /* Half in Frameworks */ = {isa = PBXBuildFile; productRef = DD0793452412F03400EF65B4 /* Half */; }; + DD0793482412F03700EF65B4 /* Half in Frameworks */ = {isa = PBXBuildFile; productRef = DD0793472412F03700EF65B4 /* Half */; }; DD6F08BE24001B9A00749359 /* ComplexOverflowingArithmetic.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6F08BD24001B9A00749359 /* ComplexOverflowingArithmetic.swift */; }; DD6F08BF24001B9A00749359 /* ComplexOverflowingArithmetic.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6F08BD24001B9A00749359 /* ComplexOverflowingArithmetic.swift */; }; DD6F08C024001B9A00749359 /* ComplexOverflowingArithmetic.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD6F08BD24001B9A00749359 /* ComplexOverflowingArithmetic.swift */; }; @@ -53,9 +56,6 @@ DDB8121123F5AEB10079FEB5 /* ComplexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDB8121023F5AEB10079FEB5 /* ComplexTests.swift */; }; DDB8121223F5AEB10079FEB5 /* ComplexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDB8121023F5AEB10079FEB5 /* ComplexTests.swift */; }; DDB8121323F5AEB10079FEB5 /* ComplexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDB8121023F5AEB10079FEB5 /* ComplexTests.swift */; }; - DDB8121D23F63B960079FEB5 /* Half.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DDB8121C23F63B900079FEB5 /* Half.framework */; }; - DDB8121E23F63BB60079FEB5 /* Half.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DDB8121B23F63B890079FEB5 /* Half.framework */; }; - DDB8121F23F63BBA0079FEB5 /* Half.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DDB8121A23F63B800079FEB5 /* Half.framework */; }; DDB8122123F656AC0079FEB5 /* FunctionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDB8122023F656AC0079FEB5 /* FunctionsTests.swift */; }; DDB8122223F656AC0079FEB5 /* FunctionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDB8122023F656AC0079FEB5 /* FunctionsTests.swift */; }; DDB8122323F656AC0079FEB5 /* FunctionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDB8122023F656AC0079FEB5 /* FunctionsTests.swift */; }; @@ -76,7 +76,7 @@ /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ - DDB8127723FA5C380079FEB5 /* PBXContainerItemProxy */ = { + DD07933E2412E54500EF65B4 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = DDFEEC2A23EF13900096015C /* Project object */; proxyType = 1; @@ -163,10 +163,6 @@ DDB8120623F59B760079FEB5 /* Complex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Complex.swift; sourceTree = ""; }; DDB8120B23F5A8E80079FEB5 /* Functions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Functions.swift; sourceTree = ""; }; DDB8121023F5AEB10079FEB5 /* ComplexTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComplexTests.swift; sourceTree = ""; }; - DDB8121423F634770079FEB5 /* Cartfile.private */ = {isa = PBXFileReference; lastKnownFileType = text; path = Cartfile.private; sourceTree = ""; }; - DDB8121A23F63B800079FEB5 /* Half.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Half.framework; path = Carthage/Build/tvOS/Half.framework; sourceTree = ""; }; - DDB8121B23F63B890079FEB5 /* Half.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Half.framework; path = Carthage/Build/Mac/Half.framework; sourceTree = ""; }; - DDB8121C23F63B900079FEB5 /* Half.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Half.framework; path = Carthage/Build/iOS/Half.framework; sourceTree = ""; }; DDB8122023F656AC0079FEB5 /* FunctionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FunctionsTests.swift; sourceTree = ""; }; DDB8125823F707EE0079FEB5 /* Operators.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = ""; }; DDB8126123F7B1C90079FEB5 /* ComplexArithmetic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComplexArithmetic.swift; sourceTree = ""; }; @@ -176,6 +172,7 @@ DDB8126F23FA2C210079FEB5 /* Functions.swift.gyb */ = {isa = PBXFileReference; explicitFileType = text.script.python; path = Functions.swift.gyb; sourceTree = ""; }; DDB8127123FA4DA50079FEB5 /* ATTRIBUTIONS */ = {isa = PBXFileReference; lastKnownFileType = text; path = ATTRIBUTIONS; sourceTree = ""; }; DDB8128523FB1A430079FEB5 /* FunctionsTests.swift.gyb */ = {isa = PBXFileReference; explicitFileType = text.script.python; path = FunctionsTests.swift.gyb; sourceTree = ""; }; + DDC7FF972412922500A5832A /* Package@swift-4.2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Package@swift-4.2.swift"; sourceTree = ""; }; DDFEEC3323EF13900096015C /* Complex.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Complex.framework; sourceTree = BUILT_PRODUCTS_DIR; }; DDFEEC3723EF13900096015C /* Complex-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "Complex-Info.plist"; path = "Info/Complex-Info.plist"; sourceTree = ""; }; DDFEEC3C23EF13910096015C /* ComplexTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ComplexTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -206,7 +203,7 @@ buildActionMask = 2147483647; files = ( DDFEEC3D23EF13910096015C /* Complex.framework in Frameworks */, - DDB8121D23F63B960079FEB5 /* Half.framework in Frameworks */, + DD0793442412F03000EF65B4 /* Half in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -222,7 +219,7 @@ buildActionMask = 2147483647; files = ( DDFEECC723F2001A0096015C /* Complex.framework in Frameworks */, - DDB8121E23F63BB60079FEB5 /* Half.framework in Frameworks */, + DD0793462412F03400EF65B4 /* Half in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -238,7 +235,7 @@ buildActionMask = 2147483647; files = ( DDFEECE323F2003E0096015C /* Complex.framework in Frameworks */, - DDB8121F23F63BBA0079FEB5 /* Half.framework in Frameworks */, + DD0793482412F03700EF65B4 /* Half in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -260,16 +257,6 @@ path = Tests; sourceTree = ""; }; - DDB8121523F63B2C0079FEB5 /* Frameworks */ = { - isa = PBXGroup; - children = ( - DDB8121C23F63B900079FEB5 /* Half.framework */, - DDB8121B23F63B890079FEB5 /* Half.framework */, - DDB8121A23F63B800079FEB5 /* Half.framework */, - ); - name = Frameworks; - sourceTree = ""; - }; DDB8126A23FA2B810079FEB5 /* Utils */ = { isa = PBXGroup; children = ( @@ -284,7 +271,6 @@ children = ( DDFEEC3523EF13900096015C /* Complex */, DDFEEC3423EF13900096015C /* Products */, - DDB8121523F63B2C0079FEB5 /* Frameworks */, ); sourceTree = ""; }; @@ -339,8 +325,8 @@ isa = PBXGroup; children = ( DDFEECAB23F1BA550096015C /* Complex.podspec */, - DDB8121423F634770079FEB5 /* Cartfile.private */, DDFEECAC23F1BA550096015C /* Package.swift */, + DDC7FF972412922500A5832A /* Package@swift-4.2.swift */, DD6F08D02400808300749359 /* codecov.yml */, DDFEECAE23F1BA5E0096015C /* .swiftlint.yml */, DDFEECAD23F1BA5E0096015C /* .travis.yml */, @@ -411,10 +397,12 @@ buildRules = ( ); dependencies = ( - DDB8127823FA5C380079FEB5 /* PBXTargetDependency */, + DD07933F2412E54500EF65B4 /* PBXTargetDependency */, DDFEECB323F1BB5B0096015C /* PBXTargetDependency */, ); name = Complex; + packageProductDependencies = ( + ); productName = Complex; productReference = DDFEEC3323EF13900096015C /* Complex.framework */; productType = "com.apple.product-type.framework"; @@ -430,9 +418,13 @@ buildRules = ( ); dependencies = ( + DDB1DF8E240C520C00C20FED /* PBXTargetDependency */, DDFEEC3F23EF13910096015C /* PBXTargetDependency */, ); name = ComplexTests; + packageProductDependencies = ( + DD0793432412F03000EF65B4 /* Half */, + ); productName = ComplexTests; productReference = DDFEEC3C23EF13910096015C /* ComplexTests.xctest */; productType = "com.apple.product-type.bundle.unit-test"; @@ -468,9 +460,13 @@ buildRules = ( ); dependencies = ( + DDB1DF90240C521000C20FED /* PBXTargetDependency */, DDFEECC923F2001A0096015C /* PBXTargetDependency */, ); name = "Complex macOS Tests"; + packageProductDependencies = ( + DD0793452412F03400EF65B4 /* Half */, + ); productName = "Complex macOSTests"; productReference = DDFEECC623F2001A0096015C /* ComplexTests.xctest */; productType = "com.apple.product-type.bundle.unit-test"; @@ -506,9 +502,13 @@ buildRules = ( ); dependencies = ( + DDB1DF92240C521500C20FED /* PBXTargetDependency */, DDFEECE523F2003E0096015C /* PBXTargetDependency */, ); name = "Complex tvOS Tests"; + packageProductDependencies = ( + DD0793472412F03700EF65B4 /* Half */, + ); productName = "Complex tvOSTests"; productReference = DDFEECE223F2003E0096015C /* ComplexTests.xctest */; productType = "com.apple.product-type.bundle.unit-test"; @@ -588,6 +588,9 @@ Base, ); mainGroup = DDFEEC2923EF13900096015C; + packageReferences = ( + DDB1DF8A240C51F900C20FED /* XCRemoteSwiftPackageReference "Half" */, + ); productRefGroup = DDFEEC3423EF13900096015C /* Products */; projectDirPath = ""; projectRoot = ""; @@ -675,6 +678,7 @@ outputPaths = ( "$(SRCROOT)/Sources/Complex/Functions.swift", "$(SRCROOT)/Tests/ComplexTests/FunctionsTests.swift", + "$(SRCROOT)/Sources/Complex/Complex.swift", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; @@ -788,10 +792,22 @@ /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ - DDB8127823FA5C380079FEB5 /* PBXTargetDependency */ = { + DD07933F2412E54500EF65B4 /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = DDB8127223FA5C0C0079FEB5 /* Generate Sources */; - targetProxy = DDB8127723FA5C380079FEB5 /* PBXContainerItemProxy */; + targetProxy = DD07933E2412E54500EF65B4 /* PBXContainerItemProxy */; + }; + DDB1DF8E240C520C00C20FED /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + productRef = DDB1DF8D240C520C00C20FED /* Half */; + }; + DDB1DF90240C521000C20FED /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + productRef = DDB1DF8F240C521000C20FED /* Half */; + }; + DDB1DF92240C521500C20FED /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + productRef = DDB1DF91240C521500C20FED /* Half */; }; DDB8127A23FA5C3E0079FEB5 /* PBXTargetDependency */ = { isa = PBXTargetDependency; @@ -1047,16 +1063,11 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PROJECT_DIR)/Carthage/Build/iOS", - ); INFOPLIST_FILE = "Info/ComplexTests-Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", "@loader_path/Frameworks", - "\"${PROJECT_DIR}/Carthage/Build/iOS\"", ); PRODUCT_BUNDLE_IDENTIFIER = com.somerandomiosdev.Complex.tests; PRODUCT_NAME = ComplexTests; @@ -1071,16 +1082,11 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PROJECT_DIR)/Carthage/Build/iOS", - ); INFOPLIST_FILE = "Info/ComplexTests-Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", "@loader_path/Frameworks", - "\"${PROJECT_DIR}/Carthage/Build/iOS\"", ); PRODUCT_BUNDLE_IDENTIFIER = com.somerandomiosdev.Complex.tests; PRODUCT_NAME = ComplexTests; @@ -1162,16 +1168,11 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PROJECT_DIR)/Carthage/Build/Mac", - ); INFOPLIST_FILE = "Info/ComplexTests-Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/../Frameworks", "@loader_path/../Frameworks", - "\"${PROJECT_DIR}/Carthage/Build/Mac\"", ); PRODUCT_BUNDLE_IDENTIFIER = com.somerandomiosdev.Complex.tests; PRODUCT_NAME = ComplexTests; @@ -1187,16 +1188,11 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PROJECT_DIR)/Carthage/Build/Mac", - ); INFOPLIST_FILE = "Info/ComplexTests-Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/../Frameworks", "@loader_path/../Frameworks", - "\"${PROJECT_DIR}/Carthage/Build/Mac\"", ); PRODUCT_BUNDLE_IDENTIFIER = com.somerandomiosdev.Complex.tests; PRODUCT_NAME = ComplexTests; @@ -1261,16 +1257,11 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PROJECT_DIR)/Carthage/Build/tvOS", - ); INFOPLIST_FILE = "Info/ComplexTests-Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", "@loader_path/Frameworks", - "\"${PROJECT_DIR}/Carthage/Build/tvOS\"", ); PRODUCT_BUNDLE_IDENTIFIER = com.somerandomiosdev.Complex.tests; PRODUCT_NAME = ComplexTests; @@ -1286,16 +1277,11 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; - FRAMEWORK_SEARCH_PATHS = ( - "$(inherited)", - "$(PROJECT_DIR)/Carthage/Build/tvOS", - ); INFOPLIST_FILE = "Info/ComplexTests-Info.plist"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", "@loader_path/Frameworks", - "\"${PROJECT_DIR}/Carthage/Build/tvOS\"", ); PRODUCT_BUNDLE_IDENTIFIER = com.somerandomiosdev.Complex.tests; PRODUCT_NAME = ComplexTests; @@ -1451,6 +1437,50 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ + +/* Begin XCRemoteSwiftPackageReference section */ + DDB1DF8A240C51F900C20FED /* XCRemoteSwiftPackageReference "Half" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/SomeRandomiOSDev/Half.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 1.2.0; + }; + }; +/* End XCRemoteSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + DD0793432412F03000EF65B4 /* Half */ = { + isa = XCSwiftPackageProductDependency; + package = DDB1DF8A240C51F900C20FED /* XCRemoteSwiftPackageReference "Half" */; + productName = Half; + }; + DD0793452412F03400EF65B4 /* Half */ = { + isa = XCSwiftPackageProductDependency; + package = DDB1DF8A240C51F900C20FED /* XCRemoteSwiftPackageReference "Half" */; + productName = Half; + }; + DD0793472412F03700EF65B4 /* Half */ = { + isa = XCSwiftPackageProductDependency; + package = DDB1DF8A240C51F900C20FED /* XCRemoteSwiftPackageReference "Half" */; + productName = Half; + }; + DDB1DF8D240C520C00C20FED /* Half */ = { + isa = XCSwiftPackageProductDependency; + package = DDB1DF8A240C51F900C20FED /* XCRemoteSwiftPackageReference "Half" */; + productName = Half; + }; + DDB1DF8F240C521000C20FED /* Half */ = { + isa = XCSwiftPackageProductDependency; + package = DDB1DF8A240C51F900C20FED /* XCRemoteSwiftPackageReference "Half" */; + productName = Half; + }; + DDB1DF91240C521500C20FED /* Half */ = { + isa = XCSwiftPackageProductDependency; + package = DDB1DF8A240C51F900C20FED /* XCRemoteSwiftPackageReference "Half" */; + productName = Half; + }; +/* End XCSwiftPackageProductDependency section */ }; rootObject = DDFEEC2A23EF13900096015C /* Project object */; } diff --git a/Complex.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Complex.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Complex.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Complex.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Complex.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 0000000..88144ae --- /dev/null +++ b/Complex.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -0,0 +1,16 @@ +{ + "object": { + "pins": [ + { + "package": "Half", + "repositoryURL": "https://github.com/SomeRandomiOSDev/Half.git", + "state": { + "branch": null, + "revision": "f40c243c767459426fdbbb5aad44f4a0d853542e", + "version": "1.2.0" + } + } + ] + }, + "version": 1 +} diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..40de30e --- /dev/null +++ b/Package.resolved @@ -0,0 +1,16 @@ +{ + "object": { + "pins": [ + { + "package": "Half", + "repositoryURL": "https://github.com/SomeRandomiOSDev/Half", + "state": { + "branch": null, + "revision": "f40c243c767459426fdbbb5aad44f4a0d853542e", + "version": "1.2.0" + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift index 96e3b8c..4174d73 100644 --- a/Package.swift +++ b/Package.swift @@ -12,11 +12,11 @@ let package = Package( ], products: [ - .library(name: "Complex", type: .dynamic, targets: ["Complex"]) + .library(name: "Complex", targets: ["Complex"]) ], dependencies: [ - .package(url: "https://github.com/SomeRandomiOSDev/Half", from: "1.0.1") + .package(url: "https://github.com/SomeRandomiOSDev/Half", from: "1.2.0") ], targets: [ @@ -24,5 +24,5 @@ let package = Package( .testTarget(name: "ComplexTests", dependencies: ["Complex", "Half"]) ], - swiftLanguageVersions: [.version("4"), .version("4.2"), .version("5")] + swiftLanguageVersions: [.version("4.2"), .version("5")] ) diff --git a/Package@swift-4.2.swift b/Package@swift-4.2.swift new file mode 100644 index 0000000..4f6f7cb --- /dev/null +++ b/Package@swift-4.2.swift @@ -0,0 +1,21 @@ +// swift-tools-version:4.2 +import PackageDescription + +let package = Package( + name: "Complex", + + products: [ + .library(name: "Complex", targets: ["Complex"]) + ], + + dependencies: [ + .package(url: "https://github.com/SomeRandomiOSDev/Half", from: "1.2.0") + ], + + targets: [ + .target(name: "Complex"), + .testTarget(name: "ComplexTests", dependencies: ["Complex", "Half"]) + ], + + swiftLanguageVersions: [.v4_2] +) diff --git a/README.md b/README.md index e408139..bc5573f 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Complex [![CocoaPods Compatible](https://img.shields.io/cocoapods/v/Complex.svg)](https://cocoapods.org/pods/Complex) [![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![Platform](https://img.shields.io/cocoapods/p/Complex.svg)](https://cocoapods.org/pods/Complex) +![Linux](https://img.shields.io/badge/platform-linux-lightgrey) [![Build](https://travis-ci.com/SomeRandomiOSDev/Complex.svg?branch=master)](https://travis-ci.com/SomeRandomiOSDev/Complex) ![Swift](https://github.com/SomeRandomiOSDev/Complex/workflows/Swift/badge.svg) [![Code Coverage](https://codecov.io/gh/SomeRandomiOSDev/Complex/branch/master/graph/badge.svg)](https://codecov.io/gh/SomeRandomiOSDev/Complex) @@ -73,8 +74,6 @@ Contributing If you have need for a specific feature or you encounter a bug, please open an issue. If you extend the functionality of **Complex** yourself or you feel like fixing a bug yourself, please submit a pull request. -Note: You'll need to run `carthage bootstrap` upon downloading to resolve and build **Complex**'s dependencies before being able to develop locally. Please look [here](https://github.com/Carthage/Carthage) for more info on installing Carthage on your local machine. - Author -------- diff --git a/Sources/Complex/Complex.swift b/Sources/Complex/Complex.swift index 45aba40..381002e 100644 --- a/Sources/Complex/Complex.swift +++ b/Sources/Complex/Complex.swift @@ -9,6 +9,7 @@ import Foundation // MARK: - Complex Definition +#if swift(>=5.1) @frozen public struct Complex { // MARK: Public Properties @@ -19,10 +20,30 @@ import Foundation // MARK: Initialization @_transparent - public init(real: Scalar = .zero, imaginary: Scalar = .zero) { + public init(real: Scalar = 0, imaginary: Scalar = 0) { self.real = real self.imaginary = imaginary } +} +#else +public struct Complex { + + // MARK: Public Properties + + public var real: Scalar + public var imaginary: Scalar + + // MARK: Initialization + + @_transparent + public init(real: Scalar = 0, imaginary: Scalar = 0) { + self.real = real + self.imaginary = imaginary + } +} +#endif // #if swift(>=5.1) + +extension Complex { @_transparent public init(_ other: Complex) { @@ -32,7 +53,7 @@ import Foundation @_transparent public init() { - self.init(real: .zero, imaginary: .zero) + self.init(real: 0, imaginary: 0) } } @@ -137,7 +158,7 @@ extension Complex where Scalar: FixedWidthInteger { // MARK: - ExpressibleByArrayLiteral Protocol Conformance -extension Complex: ExpressibleByArrayLiteral where Scalar: AdditiveArithmetic { +extension Complex: ExpressibleByArrayLiteral { @_transparent public init(arrayLiteral elements: Scalar...) { @@ -146,7 +167,7 @@ extension Complex: ExpressibleByArrayLiteral where Scalar: AdditiveArithmetic { if elements.isEmpty { self.init() } else if elements.count == 1 { - self.init(real: elements[0], imaginary: .zero) + self.init(real: elements[0], imaginary: 0) } else { self.init(real: elements[0], imaginary: elements[1]) } @@ -261,7 +282,7 @@ extension Complex where Scalar: FloatingPoint { @_transparent public func rounded() -> Complex { - rounded(.toNearestOrAwayFromZero) + return rounded(.toNearestOrAwayFromZero) } // @@ -354,7 +375,7 @@ extension Complex { @_transparent public static var i: Complex { - return Complex(real: .zero, imaginary: 1) + return Complex(real: 0, imaginary: 1) } } @@ -362,7 +383,7 @@ extension Complex { @_transparent public static var one: Complex { - return Complex(real: 1, imaginary: .zero) + return Complex(real: 1, imaginary: 0) } } diff --git a/Sources/Complex/ComplexArithmetic.swift b/Sources/Complex/ComplexArithmetic.swift index 9bf6c79..e604944 100644 --- a/Sources/Complex/ComplexArithmetic.swift +++ b/Sources/Complex/ComplexArithmetic.swift @@ -5,17 +5,19 @@ // Copyright © 2020 SomeRandomiOSDev. All rights reserved. // -import Foundation - //swiftlint:disable shorthand_operator // MARK: - AdditiveArithmetic Protocol Conformance -extension Complex: AdditiveArithmetic { +#if swift(>=5.0) +extension Complex: AdditiveArithmetic { } +#endif + +extension Complex { @_transparent public static var zero: Complex { - return Complex(real: .zero, imaginary: .zero) + return Complex(real: 0, imaginary: 0) } @_transparent @@ -163,7 +165,7 @@ extension Complex where Scalar: FixedWidthInteger { @_transparent public static func &- (lhs: Scalar, rhs: Complex) -> Complex { - return Complex(real: lhs &- rhs.real, imaginary: .zero &- rhs.imaginary) + return Complex(real: lhs &- rhs.real, imaginary: 0 &- rhs.imaginary) } @_transparent @@ -325,7 +327,7 @@ extension Complex where Scalar: SignedInteger { @_transparent public static func / (lhs: Scalar, rhs: Complex) -> Complex { - return Complex(real: lhs, imaginary: .zero) / rhs + return Complex(real: lhs, imaginary: 0) / rhs } } @@ -355,7 +357,7 @@ extension Complex where Scalar: FloatingPoint { @_transparent public static func / (lhs: Scalar, rhs: Complex) -> Complex { - return Complex(real: lhs, imaginary: .zero) / rhs + return Complex(real: lhs, imaginary: 0) / rhs } @_transparent diff --git a/Sources/Complex/ComplexOverflowingArithmetic.swift b/Sources/Complex/ComplexOverflowingArithmetic.swift index da3de8b..306e161 100644 --- a/Sources/Complex/ComplexOverflowingArithmetic.swift +++ b/Sources/Complex/ComplexOverflowingArithmetic.swift @@ -5,8 +5,6 @@ // Copyright © 2020 SomeRandomiOSDev. All rights reserved. // -import Foundation - // MARK: Overflowing Addition extension Complex where Scalar: FixedWidthInteger { @@ -127,7 +125,7 @@ extension Complex where Scalar: FixedWidthInteger { //swiftlint:disable identifier_name public func dividedReportingOverflow(by rhs: Complex) -> (partialValue: Complex, overflow: Bool) { // (a + bi) / (c + di) -> ((ac + bd) + (bc - ad)i) / (c^2 + d^2) - guard rhs.real != .zero || rhs.imaginary != .zero else { return (rhs, true) } + guard rhs.real != 0 || rhs.imaginary != 0 else { return (rhs, true) } let ac = self.real.multipliedFullWidth(by: rhs.real) let ad = self.real.multipliedFullWidth(by: rhs.imaginary) @@ -162,12 +160,12 @@ extension Complex where Scalar: FixedWidthInteger, Scalar: SignedInteger { let a = (high: (high: dividend.high.high.real, low: dividend.high.low.real), low: (high: dividend.low.high.real, low: dividend.low.low.real)) let b = (high: (high: dividend.high.high.imaginary, low: dividend.high.low.imaginary), low: (high: dividend.low.high.imaginary, low: dividend.low.low.imaginary)) - let ac = Complex.slowpathMultiply(real, a) - let ad = Complex.slowpathMultiply(imaginary, a) - let bc = Complex.slowpathMultiply(real, b) - let bd = Complex.slowpathMultiply(imaginary, b) - let cc = Complex.signExtend(real.multipliedFullWidth(by: real)) - let dd = Complex.signExtend(imaginary.multipliedFullWidth(by: imaginary)) + let ac = Complex.slowpathMultiply(self.real, a) + let ad = Complex.slowpathMultiply(self.imaginary, a) + let bc = Complex.slowpathMultiply(self.real, b) + let bd = Complex.slowpathMultiply(self.imaginary, b) + let cc = Complex.signExtend(self.real.multipliedFullWidth(by: self.real)) + let dd = Complex.signExtend(self.imaginary.multipliedFullWidth(by: self.imaginary)) let real = Complex.add(ac, bd) let imaginary = Complex.subtract(bc, ad) @@ -197,7 +195,7 @@ extension Complex where Scalar: FixedWidthInteger { // internal static func signExtend(_ value: Scalar) -> ExtendedScalar { - let highHigh = (Scalar.isSigned && value.leadingZeroBitCount == 0) ? ~Scalar.zero : Scalar.zero + let highHigh = (Scalar.isSigned && value.leadingZeroBitCount == 0) ? ~Scalar(0) : Scalar(0) let highLow = Scalar.Magnitude(truncatingIfNeeded: highHigh) let lowHigh = Scalar.Magnitude(truncatingIfNeeded: highLow) let lowLow = Scalar.Magnitude(truncatingIfNeeded: value) @@ -206,7 +204,7 @@ extension Complex where Scalar: FixedWidthInteger { } internal static func signExtend(_ value: (high: Scalar, low: Scalar.Magnitude)) -> ExtendedScalar { - let highHigh = (Scalar.isSigned && value.high.leadingZeroBitCount == 0) ? ~Scalar.zero : Scalar.zero + let highHigh = (Scalar.isSigned && value.high.leadingZeroBitCount == 0) ? ~Scalar(0) : Scalar(0) let highLow = Scalar.Magnitude(truncatingIfNeeded: highHigh) let lowHigh = Scalar.Magnitude(truncatingIfNeeded: value.high) let lowLow = value.low @@ -276,10 +274,10 @@ extension Complex where Scalar: FixedWidthInteger { internal static func slowpathMultiply(_ lhs: ExtendedScalar, _ rhs: ExtendedScalar) -> ExtendedScalar { var result: ExtendedScalar = ((0, 0), (0, 0)) - let lhsIsNegative = Scalar.isSigned && lhs.high.high < .zero + let lhsIsNegative = Scalar.isSigned && lhs.high.high < 0 let left = lhsIsNegative ? twosComplement(of: lhs) : lhs - let rhsIsNegative = Scalar.isSigned && rhs.high.high < .zero + let rhsIsNegative = Scalar.isSigned && rhs.high.high < 0 let right = rhsIsNegative ? twosComplement(of: rhs) : rhs let (leftIsPowerOfTwo, lhsShift) = isPowerOfTwo(left) @@ -295,7 +293,7 @@ extension Complex where Scalar: FixedWidthInteger { var shifted = right var bits = left - while bits.low.low != .zero || bits.low.high != .zero || bits.high.low != .zero || bits.high.high != .zero { + while bits.low.low != 0 || bits.low.high != 0 || bits.high.low != 0 || bits.high.high != 0 { if (bits.low.low & 1) == 1 { shifted = leftShift(shifted, by: shift - lastShift) result = add(result, shifted) @@ -316,7 +314,7 @@ extension Complex where Scalar: FixedWidthInteger { } internal static func slowpathMultiply(_ lhs: Scalar, _ rhs: ExtendedScalar) -> ExtendedScalar { - let lhsIsNegative = lhs < .zero + let lhsIsNegative = lhs < 0 let extended = signExtend(lhs) var result = slowpathMultiply(lhsIsNegative ? twosComplement(of: extended) : extended, rhs) @@ -329,10 +327,10 @@ extension Complex where Scalar: FixedWidthInteger { } internal static func slowpathDivide(_ lhs: ExtendedScalar, _ rhs: ExtendedScalar) -> (quotient: ExtendedScalar, remainder: ExtendedScalar) { - let dividendIsNegative = Scalar.isSigned && lhs.high.high < .zero + let dividendIsNegative = Scalar.isSigned && lhs.high.high < 0 var dividend = dividendIsNegative ? twosComplement(of: lhs) : lhs - let divisorIsNegative = Scalar.isSigned && rhs.high.high < .zero + let divisorIsNegative = Scalar.isSigned && rhs.high.high < 0 var divisor = divisorIsNegative ? twosComplement(of: rhs) : rhs guard isLessThanOrEqual(divisor, to: dividend) else { return (quotient: ((0, 0), (0, 0)), remainder: lhs) } @@ -469,7 +467,7 @@ extension Complex where Scalar: FixedWidthInteger { var bits = value var shift: Scalar.Magnitude = 0 - while bits.low.low > 1 || bits.low.high != .zero || bits.high.low != .zero || bits.high.high != .zero { + while bits.low.low > 1 || bits.low.high != 0 || bits.high.low != 0 || bits.high.high != 0 { bits = rightShift(bits, by: 1) shift += 1 } @@ -520,13 +518,13 @@ extension Complex where Scalar: FixedWidthInteger { // non-zero or if the most significant bit of the least significant word // is 1. - if value.high.high < .zero { + if value.high.high < 0 { if value.high.high.nonzeroBitCount != Scalar.bitWidth || value.high.low.nonzeroBitCount != Scalar.bitWidth || value.low.high.nonzeroBitCount != Scalar.bitWidth { overflow = true } else if value.low.low.leadingZeroBitCount > 0 { overflow = true } - } else if value.high.high != .zero || value.high.low != .zero || value.low.high != .zero { + } else if value.high.high != 0 || value.high.low != 0 || value.low.high != 0 { overflow = true } else if value.low.low.leadingZeroBitCount == 0 { overflow = true @@ -535,7 +533,7 @@ extension Complex where Scalar: FixedWidthInteger { // For unsigned integers overflow occurs when the any of the three most significant // words are non-zero. - overflow = (value.high.high > .zero) || (value.high.low > .zero) || (value.low.high > .zero) + overflow = (value.high.high > 0) || (value.high.low > 0) || (value.low.high > 0) } return (partialValue, overflow) diff --git a/Sources/Complex/Functions.swift b/Sources/Complex/Functions.swift index 4b7693a..2ffe307 100644 --- a/Sources/Complex/Functions.swift +++ b/Sources/Complex/Functions.swift @@ -5,15 +5,19 @@ // Copyright © 2020 SomeRandomiOSDev. All rights reserved. // -import Foundation +#if os(Linux) +import Glibc +#else +import Darwin +#endif @_transparent public func csqrt(_ value: F) -> Complex where F: FloatingPoint { let complex: Complex if value < 0 { - complex = Complex(real: .zero, imaginary: sqrt(-value)) + complex = Complex(real: 0, imaginary: sqrt(-value)) } else { - complex = Complex(real: sqrt(value), imaginary: .zero) + complex = Complex(real: sqrt(value), imaginary: 0) } return complex @@ -24,8 +28,8 @@ public func sqrt(_ value: Complex) -> Complex where F: FloatingPoint { //swiftlint:disable opening_brace statement_position let sign = { (value: F) -> F in let sign: F - if value < .zero { sign = -1 } - else { sign = 1 } + if value < 0 { sign = -1 } + else { sign = 1 } return sign } //swiftlint:enable opening_brace statement_position @@ -67,20 +71,32 @@ public func clamp(_ value: Complex, _ minimum: Complex, _ maximum: Comp @_transparent public func exp(_ value: Complex) -> Complex { +#if os(Linux) + let exp = Glibc.exp(value.real) +#else let exp = Darwin.exp(value.real) +#endif return Complex(real: exp * cos(value.imaginary), imaginary: exp * sin(value.imaginary)) } @_transparent public func exp(_ value: Complex) -> Complex { +#if os(Linux) + let exp = Glibc.exp(value.real) +#else let exp = Darwin.exp(value.real) +#endif return Complex(real: exp * cos(value.imaginary), imaginary: exp * sin(value.imaginary)) } #if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64)) @_transparent public func exp(_ value: Complex) -> Complex { +#if os(Linux) + let exp = Glibc.exp(value.real) +#else let exp = Darwin.exp(value.real) +#endif return Complex(real: exp * cos(value.imaginary), imaginary: exp * sin(value.imaginary)) } #endif diff --git a/Sources/Complex/Functions.swift.gyb b/Sources/Complex/Functions.swift.gyb index c0133da..9be3f65 100644 --- a/Sources/Complex/Functions.swift.gyb +++ b/Sources/Complex/Functions.swift.gyb @@ -5,15 +5,19 @@ // Copyright © 2020 SomeRandomiOSDev. All rights reserved. // -import Foundation +#if os(Linux) +import Glibc +#else +import Darwin +#endif @_transparent public func csqrt(_ value: F) -> Complex where F: FloatingPoint { let complex: Complex if value < 0 { - complex = Complex(real: .zero, imaginary: sqrt(-value)) + complex = Complex(real: 0, imaginary: sqrt(-value)) } else { - complex = Complex(real: sqrt(value), imaginary: .zero) + complex = Complex(real: sqrt(value), imaginary: 0) } return complex @@ -24,8 +28,8 @@ public func sqrt(_ value: Complex) -> Complex where F: FloatingPoint { //swiftlint:disable opening_brace statement_position let sign = { (value: F) -> F in let sign: F - if value < .zero { sign = -1 } - else { sign = 1 } + if value < 0 { sign = -1 } + else { sign = 1 } return sign } //swiftlint:enable opening_brace statement_position @@ -71,7 +75,11 @@ public func clamp(_ value: Complex, _ minimum: Complex, _ maximum: Comp % end @_transparent public func exp(_ value: Complex<${type}>) -> Complex<${type}> { +#if os(Linux) + let exp = Glibc.exp(value.real) +#else let exp = Darwin.exp(value.real) +#endif return Complex<${type}>(real: exp * cos(value.imaginary), imaginary: exp * sin(value.imaginary)) } % if type == "Float80": diff --git a/Sources/Complex/Operators.swift b/Sources/Complex/Operators.swift index 8cb684f..c50f11f 100644 --- a/Sources/Complex/Operators.swift +++ b/Sources/Complex/Operators.swift @@ -5,8 +5,6 @@ // Copyright © 2020 SomeRandomiOSDev. All rights reserved. // -import Foundation - /// Component-wise addition infix operator .+: AdditionPrecedence infix operator .+=: AssignmentPrecedence diff --git a/Tests/ComplexTests/ComplexArithmeticTests.swift b/Tests/ComplexTests/ComplexArithmeticTests.swift index 0d1775e..a21242f 100644 --- a/Tests/ComplexTests/ComplexArithmeticTests.swift +++ b/Tests/ComplexTests/ComplexArithmeticTests.swift @@ -27,7 +27,7 @@ class ComplexArithmeticTests: XCTestCase { testAdditionWithZero(Complex(real: Half(1.2), imaginary: Half(-7.4))) testAdditionWithZero(Complex(real: Float(-0.123), imaginary: Float(3.0))) testAdditionWithZero(Complex(real: Double(8.9), imaginary: Double(10.8))) - testAdditionWithZero(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + testAdditionWithZero(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testSubtractionWithZero() { @@ -44,7 +44,7 @@ class ComplexArithmeticTests: XCTestCase { testSubtractionWithZero(Complex(real: Half(1.2), imaginary: Half(-7.4))) testSubtractionWithZero(Complex(real: Float(-0.123), imaginary: Float(3.0))) testSubtractionWithZero(Complex(real: Double(8.9), imaginary: Double(10.8))) - testSubtractionWithZero(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + testSubtractionWithZero(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testMultiplicationWithZero() { @@ -61,7 +61,7 @@ class ComplexArithmeticTests: XCTestCase { testMultiplicationWithZero(Complex(real: Half(1.2), imaginary: Half(-7.4))) testMultiplicationWithZero(Complex(real: Float(-0.123), imaginary: Float(3.0))) testMultiplicationWithZero(Complex(real: Double(8.9), imaginary: Double(10.8))) - testMultiplicationWithZero(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + testMultiplicationWithZero(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testAddition() { @@ -78,7 +78,7 @@ class ComplexArithmeticTests: XCTestCase { testAddition(Complex(real: 1.0, imaginary: 2.0), Complex(real: 3.0, imaginary: 4.0), Complex(real: 4.0, imaginary: 6.0)) testAddition(Complex(real: 1.0, imaginary: 2.0), Complex(real: 3.0, imaginary: 4.0), Complex(real: 4.0, imaginary: 6.0)) testAddition(Complex(real: 1.0, imaginary: 2.0), Complex(real: 3.0, imaginary: 4.0), Complex(real: 4.0, imaginary: 6.0)) - testAddition(Complex(real: 1.0, imaginary: 2.0), Complex(real: 3.0, imaginary: 4.0), Complex(real: 4.0, imaginary: 6.0)) + testAddition(Complex(real: 1.0, imaginary: 2.0), Complex(real: 3.0, imaginary: 4.0), Complex(real: 4.0, imaginary: 6.0)) testAddition(Complex(real: 1, imaginary: 2), 3, Complex(real: 4, imaginary: 2)) testAddition(Complex(real: 1, imaginary: 2), 3, Complex(real: 4, imaginary: 2)) @@ -93,7 +93,7 @@ class ComplexArithmeticTests: XCTestCase { testAddition(Complex(real: 1.0, imaginary: 2.0), 3.0, Complex(real: 4.0, imaginary: 2.0)) testAddition(Complex(real: 1.0, imaginary: 2.0), 3.0, Complex(real: 4.0, imaginary: 2.0)) testAddition(Complex(real: 1.0, imaginary: 2.0), 3.0, Complex(real: 4.0, imaginary: 2.0)) - testAddition(Complex(real: 1.0, imaginary: 2.0), 3.0, Complex(real: 4.0, imaginary: 2.0)) + testAddition(Complex(real: 1.0, imaginary: 2.0), 3.0, Complex(real: 4.0, imaginary: 2.0)) } func testAdditionIgnoringOverflow() { @@ -123,7 +123,7 @@ class ComplexArithmeticTests: XCTestCase { testSubtraction(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: 2.0, imaginary: 2.0)) testSubtraction(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: 2.0, imaginary: 2.0)) testSubtraction(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: 2.0, imaginary: 2.0)) - testSubtraction(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: 2.0, imaginary: 2.0)) + testSubtraction(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: 2.0, imaginary: 2.0)) testSubtraction(Complex(real: 3, imaginary: 4), 1, Complex(real: 2, imaginary: 4)) testSubtraction(Complex(real: 3, imaginary: 4), 1, Complex(real: 2, imaginary: 4)) @@ -138,7 +138,7 @@ class ComplexArithmeticTests: XCTestCase { testSubtraction(Complex(real: 3.0, imaginary: 4.0), 1.0, Complex(real: 2.0, imaginary: 4.0)) testSubtraction(Complex(real: 3.0, imaginary: 4.0), 1.0, Complex(real: 2.0, imaginary: 4.0)) testSubtraction(Complex(real: 3.0, imaginary: 4.0), 1.0, Complex(real: 2.0, imaginary: 4.0)) - testSubtraction(Complex(real: 3.0, imaginary: 4.0), 1.0, Complex(real: 2.0, imaginary: 4.0)) + testSubtraction(Complex(real: 3.0, imaginary: 4.0), 1.0, Complex(real: 2.0, imaginary: 4.0)) } func testSubtractionIgnoringOverflow() { @@ -168,7 +168,7 @@ class ComplexArithmeticTests: XCTestCase { testMultiplication(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: -5.0, imaginary: 10.0)) testMultiplication(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: -5.0, imaginary: 10.0)) testMultiplication(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: -5.0, imaginary: 10.0)) - testMultiplication(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: -5.0, imaginary: 10.0)) + testMultiplication(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: -5.0, imaginary: 10.0)) testMultiplication(Complex(real: 3, imaginary: 4), 2) testMultiplication(Complex(real: 3, imaginary: 4), 2) @@ -183,7 +183,7 @@ class ComplexArithmeticTests: XCTestCase { testMultiplication(Complex(real: 3.0, imaginary: 4.0), 2.0) testMultiplication(Complex(real: 3.0, imaginary: 4.0), 2.0) testMultiplication(Complex(real: 3.0, imaginary: 4.0), 2.0) - testMultiplication(Complex(real: 3.0, imaginary: 4.0), 2.0) + testMultiplication(Complex(real: 3.0, imaginary: 4.0), 2.0) } func testMultiplicationIgnoringOverflow() { @@ -213,7 +213,7 @@ class ComplexArithmeticTests: XCTestCase { testComponentwiseMultiplication(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0)) testComponentwiseMultiplication(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0)) testComponentwiseMultiplication(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0)) - testComponentwiseMultiplication(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0)) + testComponentwiseMultiplication(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0)) } func testDivision() { @@ -230,7 +230,7 @@ class ComplexArithmeticTests: XCTestCase { testDivision(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: 11.0 / 5.0, imaginary: -0.4)) testDivision(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: 11.0 / 5.0, imaginary: -0.4)) testDivision(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: 11.0 / 5.0, imaginary: -0.4)) - testDivision(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: 11.0 / 5.0, imaginary: -0.4)) + testDivision(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0), Complex(real: 11.0 / 5.0, imaginary: -0.4)) testDivision(Complex(real: 3, imaginary: 4), 2) testDivision(Complex(real: 3, imaginary: 4), 2) @@ -245,7 +245,7 @@ class ComplexArithmeticTests: XCTestCase { testDivision(Complex(real: 3.0, imaginary: 4.0), 2.0) testDivision(Complex(real: 3.0, imaginary: 4.0), 2.0) testDivision(Complex(real: 3.0, imaginary: 4.0), 2.0) - testDivision(Complex(real: 3.0, imaginary: 4.0), 2.0) + testDivision(Complex(real: 3.0, imaginary: 4.0), 2.0) } func testComponentwiseDivision() { @@ -262,16 +262,16 @@ class ComplexArithmeticTests: XCTestCase { testComponentwiseDivision(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0)) testComponentwiseDivision(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0)) testComponentwiseDivision(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0)) - testComponentwiseDivision(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0)) + testComponentwiseDivision(Complex(real: 3.0, imaginary: 4.0), Complex(real: 1.0, imaginary: 2.0)) } // MARK: Private Methods - private func testAdditionWithZero(_ complex: Complex, file: StaticString = #file, line: UInt = #line) { + private func testAdditionWithZero(_ complex: Complex, file: StaticString = #file, line: UInt = #line) where Scalar: BinaryInteger { CTAssertEqual(complex, complex + .zero) CTAssertEqual(complex, complex .+ .zero) - CTAssertEqual(complex, complex + Scalar.zero) - CTAssertEqual(complex, Scalar.zero + complex) + CTAssertEqual(complex, complex + Scalar(0)) + CTAssertEqual(complex, Scalar(0) + complex) var result = complex result += .zero @@ -282,14 +282,33 @@ class ComplexArithmeticTests: XCTestCase { CTAssertEqual(result, complex) result = complex - result += Scalar.zero + result += Scalar(0) CTAssertEqual(result, complex) } - private func testSubtractionWithZero(_ complex: Complex, file: StaticString = #file, line: UInt = #line) { + private func testAdditionWithZero(_ complex: Complex, file: StaticString = #file, line: UInt = #line) where Scalar: FloatingPoint { + CTAssertEqual(complex, complex + .zero) + CTAssertEqual(complex, complex .+ .zero) + CTAssertEqual(complex, complex + Scalar(0)) + CTAssertEqual(complex, Scalar(0) + complex) + + var result = complex + result += .zero + CTAssertEqual(result, complex) + + result = complex + result .+= .zero + CTAssertEqual(result, complex) + + result = complex + result += Scalar(0) + CTAssertEqual(result, complex) + } + + private func testSubtractionWithZero(_ complex: Complex, file: StaticString = #file, line: UInt = #line) where Scalar: BinaryInteger { CTAssertEqual(complex, complex - .zero) CTAssertEqual(complex, complex .- .zero) - CTAssertEqual(complex, complex - Scalar.zero) + CTAssertEqual(complex, complex - Scalar(0)) var result = complex result -= .zero @@ -300,15 +319,52 @@ class ComplexArithmeticTests: XCTestCase { CTAssertEqual(result, complex) result = complex - result -= Scalar.zero + result -= Scalar(0) + CTAssertEqual(result, complex) + } + + private func testSubtractionWithZero(_ complex: Complex, file: StaticString = #file, line: UInt = #line) where Scalar: FloatingPoint { + CTAssertEqual(complex, complex - .zero) + CTAssertEqual(complex, complex .- .zero) + CTAssertEqual(complex, complex - Scalar(0)) + + var result = complex + result -= .zero CTAssertEqual(result, complex) + + result = complex + result .-= .zero + CTAssertEqual(result, complex) + + result = complex + result -= Scalar(0) + CTAssertEqual(result, complex) + } + + private func testMultiplicationWithZero(_ complex: Complex, file: StaticString = #file, line: UInt = #line) where Scalar: BinaryInteger { + CTAssertEqual(.zero, complex * .zero) + CTAssertEqual(.zero, complex .* .zero) + CTAssertEqual(.zero, complex * Scalar(0)) + CTAssertEqual(.zero, Scalar(0) * complex) + + var result = complex + result *= .zero + CTAssertEqual(result, .zero) + + result = complex + result .*= .zero + CTAssertEqual(result, .zero) + + result = complex + result *= Scalar(0) + CTAssertEqual(result, .zero) } - private func testMultiplicationWithZero(_ complex: Complex, file: StaticString = #file, line: UInt = #line) { + private func testMultiplicationWithZero(_ complex: Complex, file: StaticString = #file, line: UInt = #line) where Scalar: FloatingPoint { CTAssertEqual(.zero, complex * .zero) CTAssertEqual(.zero, complex .* .zero) - CTAssertEqual(.zero, complex * Scalar.zero) - CTAssertEqual(.zero, Scalar.zero * complex) + CTAssertEqual(.zero, complex * Scalar(0)) + CTAssertEqual(.zero, Scalar(0) * complex) var result = complex result *= .zero @@ -319,7 +375,7 @@ class ComplexArithmeticTests: XCTestCase { CTAssertEqual(result, .zero) result = complex - result *= Scalar.zero + result *= Scalar(0) CTAssertEqual(result, .zero) } diff --git a/Tests/ComplexTests/ComplexOverflowingArithmeticTests.swift b/Tests/ComplexTests/ComplexOverflowingArithmeticTests.swift index 008e6cb..a0b3c10 100644 --- a/Tests/ComplexTests/ComplexOverflowingArithmeticTests.swift +++ b/Tests/ComplexTests/ComplexOverflowingArithmeticTests.swift @@ -520,8 +520,8 @@ class ComplexOverflowingArithmeticTests: XCTestCase { var lhs = Complex(real: Scalar.max, imaginary: Scalar.max) var rhs = Complex(real: 8, imaginary: 4) var fullWidth = lhs.multipliedFullWidth(by: rhs) - var real = ((fullWidth.high.high.real, fullWidth.high.low.real), (fullWidth.low.high.real, fullWidth.low.low.real)) - var imaginary = ((fullWidth.high.high.imaginary, fullWidth.high.low.imaginary), (fullWidth.low.high.imaginary, fullWidth.low.low.imaginary)) + var real: Complex.ExtendedScalar = ((fullWidth.high.high.real, fullWidth.high.low.real), (fullWidth.low.high.real, fullWidth.low.low.real)) + var imaginary: Complex.ExtendedScalar = ((fullWidth.high.high.imaginary, fullWidth.high.low.imaginary), (fullWidth.low.high.imaginary, fullWidth.low.low.imaginary)) CTAssertEqual(real, ((0, 0), (1, Scalar.Magnitude.max &<< 2))) CTAssertEqual(imaginary, ((0, 0), (5, Scalar.max.multipliedFullWidth(by: 12).low))) @@ -630,7 +630,7 @@ class ComplexOverflowingArithmeticTests: XCTestCase { extended = Complex.signExtend((0, 0)) CTAssertEqual(extended, (high: (high: 0, low: 0), low: (high: 0, low: 0))) - extended = Complex.signExtend(Scalar.zero) + extended = Complex.signExtend(Scalar(0)) CTAssertEqual(extended, (high: (high: 0, low: 0), low: (high: 0, low: 0))) value = (Scalar.max, Scalar.Magnitude.max) @@ -709,7 +709,7 @@ class ComplexOverflowingArithmeticTests: XCTestCase { // Test overflow of `low.high` // nonextendedLHS = (high: 1, low: 0) - nonextendedRHS = (high: ~Scalar.zero, low: Scalar.Magnitude.max) + nonextendedRHS = (high: ~Scalar(0), low: Scalar.Magnitude.max) result = Complex.add(nonextendedLHS, nonextendedRHS) CTAssertEqual(result, (high: (high: 0, low: Scalar.isSigned ? 0 : 1), low: (high: 0, low: Scalar.Magnitude.max))) @@ -721,19 +721,19 @@ class ComplexOverflowingArithmeticTests: XCTestCase { // Test non-overflow of `low.high` // nonextendedLHS = (high: 1, low: 0) - nonextendedRHS = (high: ~Scalar.zero - 1, low: 0) + nonextendedRHS = (high: ~Scalar(0) - 1, low: 0) result = Complex.add(nonextendedLHS, nonextendedRHS) - CTAssertEqual(result, (high: (high: Scalar.isSigned ? ~Scalar.zero : 0, low: Scalar.isSigned ? ~Scalar.Magnitude.zero : 0), low: (high: ~Scalar.Magnitude.zero, low: 0))) + CTAssertEqual(result, (high: (high: Scalar.isSigned ? ~Scalar(0) : 0, low: Scalar.isSigned ? ~Scalar.Magnitude(0) : 0), low: (high: ~Scalar.Magnitude(0), low: 0))) extendedLHS = Complex.signExtend(nonextendedLHS) extendedRHS = Complex.signExtend(nonextendedRHS) result = Complex.add(extendedLHS, extendedRHS) - CTAssertEqual(result, (high: (high: Scalar.isSigned ? ~Scalar.zero : 0, low: Scalar.isSigned ? ~Scalar.Magnitude.zero : 0), low: (high: ~Scalar.Magnitude.zero, low: 0))) + CTAssertEqual(result, (high: (high: Scalar.isSigned ? ~Scalar(0) : 0, low: Scalar.isSigned ? ~Scalar.Magnitude(0) : 0), low: (high: ~Scalar.Magnitude(0), low: 0))) // Test overflow of `low.high` from overflow of `low.low` // nonextendedLHS = (high: 1, low: 1) - nonextendedRHS = (high: ~Scalar.zero - 1, low: Scalar.Magnitude.max) + nonextendedRHS = (high: ~Scalar(0) - 1, low: Scalar.Magnitude.max) result = Complex.add(nonextendedLHS, nonextendedRHS) CTAssertEqual(result, (high: (high: 0, low: Scalar.isSigned ? 0 : 1), low: (high: 0, low: 0))) @@ -773,35 +773,35 @@ class ComplexOverflowingArithmeticTests: XCTestCase { // Test overflow of `high.high` // extendedLHS = (high: (high: 1, low: 1), (high: 1, low: 1)) - extendedRHS = (high: (high: ~Scalar.zero, low: 0), (high: 0, low: 0)) + extendedRHS = (high: (high: ~Scalar(0), low: 0), (high: 0, low: 0)) result = Complex.add(extendedLHS, extendedRHS) CTAssertEqual(result, (high: (high: 0, low: 1), low: (high: 1, low: 1))) // Test non-overflow of `high.high` // extendedLHS = (high: (high: 1, low: 1), (high: 1, low: 1)) - extendedRHS = (high: (high: ~Scalar.zero - 1, low: 0), (high: 0, low: 0)) + extendedRHS = (high: (high: ~Scalar(0) - 1, low: 0), (high: 0, low: 0)) result = Complex.add(extendedLHS, extendedRHS) - CTAssertEqual(result, (high: (high: ~Scalar.zero, low: 1), low: (high: 1, low: 1))) + CTAssertEqual(result, (high: (high: ~Scalar(0), low: 1), low: (high: 1, low: 1))) // Test overflow of `high.high` from overflow of `high.low` // extendedLHS = (high: (high: 1, low: 1), (high: 1, low: 1)) - extendedRHS = (high: (high: ~Scalar.zero - 1, low: Scalar.Magnitude.max), (high: 0, low: 0)) + extendedRHS = (high: (high: ~Scalar(0) - 1, low: Scalar.Magnitude.max), (high: 0, low: 0)) result = Complex.add(extendedLHS, extendedRHS) CTAssertEqual(result, (high: (high: 0, low: 0), low: (high: 1, low: 1))) // Test overflow of `high.high` from overflow of `high.low` from overflow of `low.high` // extendedLHS = (high: (high: 1, low: 1), (high: 1, low: 1)) - extendedRHS = (high: (high: ~Scalar.zero - 1, low: Scalar.Magnitude.max - 1), (high: Scalar.Magnitude.max, low: 0)) + extendedRHS = (high: (high: ~Scalar(0) - 1, low: Scalar.Magnitude.max - 1), (high: Scalar.Magnitude.max, low: 0)) result = Complex.add(extendedLHS, extendedRHS) CTAssertEqual(result, (high: (high: 0, low: 0), low: (high: 0, low: 1))) // Test overflow of `high.high` from overflow of `high.low` from overflow of `low.high` from overflow of `low.low` // extendedLHS = (high: (high: 1, low: 1), (high: 1, low: 1)) - extendedRHS = (high: (high: ~Scalar.zero - 1, low: Scalar.Magnitude.max - 1), (high: Scalar.Magnitude.max - 1, low: Scalar.Magnitude.max)) + extendedRHS = (high: (high: ~Scalar(0) - 1, low: Scalar.Magnitude.max - 1), (high: Scalar.Magnitude.max - 1, low: Scalar.Magnitude.max)) result = Complex.add(extendedLHS, extendedRHS) CTAssertEqual(result, (high: (high: 0, low: 0), low: (high: 0, low: 0))) } @@ -845,31 +845,31 @@ class ComplexOverflowingArithmeticTests: XCTestCase { } private func testLeftShiftExtendedScalars(forType: Scalar.Type) where Scalar: FixedWidthInteger { - let upperHalf = (~Scalar.Magnitude.zero) &<< (Scalar.Magnitude.bitWidth / 2) - let lowerHalf = (~Scalar.Magnitude.zero) &>> (Scalar.Magnitude.bitWidth / 2) + let upperHalf = (~Scalar.Magnitude(0)) &<< (Scalar.Magnitude.bitWidth / 2) + let lowerHalf = (~Scalar.Magnitude(0)) &>> (Scalar.Magnitude.bitWidth / 2) let shiftedUpperHalf = (upperHalf &<< 1) + 1 - var value: Complex.ExtendedScalar = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + var value: Complex.ExtendedScalar = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) var result = Complex.leftShift(value, by: 1) - CTAssertEqual(result, (high: (high: ~Scalar.zero, low: Scalar.Magnitude.max), low: (high: Scalar.Magnitude.max, low: Scalar.Magnitude.max - 1))) + CTAssertEqual(result, (high: (high: ~Scalar(0), low: Scalar.Magnitude.max), low: (high: Scalar.Magnitude.max, low: Scalar.Magnitude.max - 1))) - value = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + value = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) result = Complex.leftShift(value, by: Scalar.Magnitude(Scalar.bitWidth)) - CTAssertEqual(result, (high: (high: ~Scalar.zero, low: Scalar.Magnitude.max), low: (high: Scalar.Magnitude.max, low: 0))) + CTAssertEqual(result, (high: (high: ~Scalar(0), low: Scalar.Magnitude.max), low: (high: Scalar.Magnitude.max, low: 0))) - value = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + value = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) result = Complex.leftShift(value, by: Scalar.Magnitude(Scalar.bitWidth / 2)) - CTAssertEqual(result, (high: (high: ~Scalar.zero, low: Scalar.Magnitude.max), low: (high: Scalar.Magnitude.max, low: upperHalf))) + CTAssertEqual(result, (high: (high: ~Scalar(0), low: Scalar.Magnitude.max), low: (high: Scalar.Magnitude.max, low: upperHalf))) - value = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + value = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) result = Complex.leftShift(value, by: Scalar.Magnitude(Scalar.bitWidth * 2)) - CTAssertEqual(result, (high: (high: ~Scalar.zero, low: Scalar.Magnitude.max), low: (high: 0, low: 0))) + CTAssertEqual(result, (high: (high: ~Scalar(0), low: Scalar.Magnitude.max), low: (high: 0, low: 0))) - value = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + value = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) result = Complex.leftShift(value, by: Scalar.Magnitude(Scalar.bitWidth * 3)) - CTAssertEqual(result, (high: (high: ~Scalar.zero, low: 0), low: (high: 0, low: 0))) + CTAssertEqual(result, (high: (high: ~Scalar(0), low: 0), low: (high: 0, low: 0))) - value = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + value = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) result = Complex.leftShift(value, by: Scalar.Magnitude(Scalar.bitWidth * 4)) CTAssertEqual(result, (high: (high: 0, low: 0), low: (high: 0, low: 0))) @@ -887,31 +887,31 @@ class ComplexOverflowingArithmeticTests: XCTestCase { } private func testRightShiftExtendedScalars(forType: Scalar.Type) where Scalar: FixedWidthInteger { - let upperHalf = (~Scalar.Magnitude.zero) &<< (Scalar.Magnitude.bitWidth / 2) - let lowerHalf = (~Scalar.Magnitude.zero) &>> (Scalar.Magnitude.bitWidth / 2) + let upperHalf = (~Scalar.Magnitude(0)) &<< (Scalar.Magnitude.bitWidth / 2) + let lowerHalf = (~Scalar.Magnitude(0)) &>> (Scalar.Magnitude.bitWidth / 2) let shiftedLowerHalf = (lowerHalf &>> 1) | upperHalf &<< ((Scalar.Magnitude.bitWidth / 2) - 1) - var value: Complex.ExtendedScalar = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + var value: Complex.ExtendedScalar = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) var result = Complex.rightShift(value, by: 1) CTAssertEqual(result, (high: (high: Scalar.max &>> (Scalar.isSigned ? 0 : 1), low: Scalar.Magnitude.max), low: (high: Scalar.Magnitude.max, low: Scalar.Magnitude.max))) - value = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + value = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) result = Complex.rightShift(value, by: Scalar.Magnitude(Scalar.bitWidth)) CTAssertEqual(result, (high: (high: 0, low: Scalar.Magnitude.max), low: (high: Scalar.Magnitude.max, low: Scalar.Magnitude.max))) - value = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + value = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) result = Complex.rightShift(value, by: Scalar.Magnitude(Scalar.bitWidth / 2)) CTAssertEqual(result, (high: (high: Scalar(truncatingIfNeeded: lowerHalf), low: Scalar.Magnitude.max), low: (high: Scalar.Magnitude.max, low: Scalar.Magnitude.max))) - value = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + value = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) result = Complex.rightShift(value, by: Scalar.Magnitude(Scalar.bitWidth * 2)) CTAssertEqual(result, (high: (high: 0, low: 0), low: (high: Scalar.Magnitude.max, low: Scalar.Magnitude.max))) - value = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + value = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) result = Complex.rightShift(value, by: Scalar.Magnitude(Scalar.bitWidth * 3)) CTAssertEqual(result, (high: (high: 0, low: 0), low: (high: 0, low: Scalar.Magnitude.max))) - value = ((~Scalar.zero, Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) + value = ((~Scalar(0), Scalar.Magnitude.max), (Scalar.Magnitude.max, Scalar.Magnitude.max)) result = Complex.rightShift(value, by: Scalar.Magnitude(Scalar.bitWidth * 4)) CTAssertEqual(result, (high: (high: 0, low: 0), low: (high: 0, low: 0))) @@ -1053,7 +1053,7 @@ class ComplexOverflowingArithmeticTests: XCTestCase { } if Scalar.isSigned { - value = randomValue() | (~Scalar.Magnitude.zero &<< (Scalar.bitWidth - 1)) // random value with the MSB set to 1 + value = randomValue() | (~Scalar.Magnitude(0) &<< (Scalar.bitWidth - 1)) // random value with the MSB set to 1 result = Complex.truncateAndReportOverflow(((0, 0), (0, value))) CTAssertEqual(result.partialValue, Scalar(truncatingIfNeeded: value)) CTAssertTrue(result.overflow) diff --git a/Tests/ComplexTests/ComplexTests.swift b/Tests/ComplexTests/ComplexTests.swift index 05bde7c..48ae7b3 100644 --- a/Tests/ComplexTests/ComplexTests.swift +++ b/Tests/ComplexTests/ComplexTests.swift @@ -25,7 +25,7 @@ class ComplexTests: XCTestCase { testInitialization(real: Half(1.2), imaginary: Half(-7.4)) testInitialization(real: Float(-0.123), imaginary: Float(3.0)) testInitialization(real: Double(8.9), imaginary: Double(10.8)) - testInitialization(real: Float80(11.1), imaginary: Float80(-0.9)) + testInitialization(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9)) } func testRandomFactoryMethods() { @@ -40,7 +40,7 @@ class ComplexTests: XCTestCase { testRandomFactoryMethods(lowerBound: Half(-1.2), upperBound: Half(7.4)) testRandomFactoryMethods(lowerBound: Float(-0.123), upperBound: Float(3.0)) testRandomFactoryMethods(lowerBound: Double(8.9), upperBound: Double(10.8)) - testRandomFactoryMethods(lowerBound: Float80(-11.1), upperBound: Float80(0.9)) + testRandomFactoryMethods(lowerBound: Complex.LargestFloatType(-11.1), upperBound: Complex.LargestFloatType(0.9)) } func testDescriptionMethods() throws { @@ -55,7 +55,7 @@ class ComplexTests: XCTestCase { try testDescriptionMethods(Complex(real: Half(1.2), imaginary: Half(-7.4))) try testDescriptionMethods(Complex(real: Float(-0.123), imaginary: Float(3.0))) try testDescriptionMethods(Complex(real: Double(8.9), imaginary: Double(10.8))) - try testDescriptionMethods(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + try testDescriptionMethods(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testHashing() { @@ -70,14 +70,14 @@ class ComplexTests: XCTestCase { testHashing(Complex(real: Half(1.2), imaginary: Half(-7.4))) testHashing(Complex(real: Float(-0.123), imaginary: Float(3.0))) testHashing(Complex(real: Double(8.9), imaginary: Double(10.8))) - testHashing(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + testHashing(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testRounding() { testRounding(Complex(real: 1.5, imaginary: -4.5)) testRounding(Complex(real: 1.5, imaginary: -4.5)) testRounding(Complex(real: 1.5, imaginary: -4.5)) - testRounding(Complex(real: 1.5, imaginary: -4.5)) + testRounding(Complex(real: 1.5, imaginary: -4.5)) } func testConjugateMethods() { @@ -88,7 +88,7 @@ class ComplexTests: XCTestCase { testConjugateMethods(Complex(real: Half(1.2), imaginary: Half(-7.4))) testConjugateMethods(Complex(real: Float(-0.123), imaginary: Float(3.0))) testConjugateMethods(Complex(real: Double(8.9), imaginary: Double(10.8))) - testConjugateMethods(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + testConjugateMethods(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testNegationMethods() { @@ -99,7 +99,7 @@ class ComplexTests: XCTestCase { testNegationMethods(Complex(real: Half(1.2), imaginary: Half(-7.4))) testNegationMethods(Complex(real: Float(-0.123), imaginary: Float(3.0))) testNegationMethods(Complex(real: Double(8.9), imaginary: Double(10.8))) - testNegationMethods(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + testNegationMethods(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testMultiplyByOne() { @@ -114,7 +114,7 @@ class ComplexTests: XCTestCase { testMultiplyByOne(Complex(real: Half(1.2), imaginary: Half(-7.4))) testMultiplyByOne(Complex(real: Float(-0.123), imaginary: Float(3.0))) testMultiplyByOne(Complex(real: Double(8.9), imaginary: Double(10.8))) - testMultiplyByOne(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + testMultiplyByOne(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testMultiplyByI() { @@ -125,7 +125,7 @@ class ComplexTests: XCTestCase { testMultiplyByI(Complex(real: Half(1.2), imaginary: Half(-7.4))) testMultiplyByI(Complex(real: Float(-0.123), imaginary: Float(3.0))) testMultiplyByI(Complex(real: Double(8.9), imaginary: Double(10.8))) - testMultiplyByI(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + testMultiplyByI(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testPlusPrefixOperator() { @@ -140,21 +140,21 @@ class ComplexTests: XCTestCase { testPlusPrefixOperator(Complex(real: Half(1.2), imaginary: Half(-7.4))) testPlusPrefixOperator(Complex(real: Float(-0.123), imaginary: Float(3.0))) testPlusPrefixOperator(Complex(real: Double(8.9), imaginary: Double(10.8))) - testPlusPrefixOperator(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + testPlusPrefixOperator(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testPolarComponents() { testPolarComponents(Complex(real: Half(1.2), imaginary: Half(-7.4))) testPolarComponents(Complex(real: Float(-0.123), imaginary: Float(3.0))) testPolarComponents(Complex(real: Double(8.9), imaginary: Double(10.8))) - testPolarComponents(Complex(real: Float80(11.1), imaginary: Float80(-0.9))) + testPolarComponents(Complex(real: Complex.LargestFloatType(11.1), imaginary: Complex.LargestFloatType(-0.9))) } func testAdditiveArithmeticProtocolRequirements() { CTAssertEqual(Complex.zero, Complex(real: 0.0, imaginary: 0.0)) CTAssertEqual(Complex.zero, Complex(real: 0.0, imaginary: 0.0)) CTAssertEqual(Complex.zero, Complex(real: 0.0, imaginary: 0.0)) - CTAssertEqual(Complex.zero, Complex(real: 0.0, imaginary: 0.0)) + CTAssertEqual(Complex.zero, Complex(real: 0.0, imaginary: 0.0)) do { let lhs = Complex(real: 1.0, imaginary: 2.0) @@ -208,8 +208,8 @@ class ComplexTests: XCTestCase { CTAssertEqual(value, lhs - rhs) } do { - let lhs = Complex(real: 1.0, imaginary: 2.0) - let rhs = Complex(real: 3.0, imaginary: 4.0) + let lhs = Complex(real: 1.0, imaginary: 2.0) + let rhs = Complex(real: 3.0, imaginary: 4.0) var value = lhs value += rhs @@ -249,13 +249,13 @@ class ComplexTests: XCTestCase { CTAssertEqual(complex4.real, Int64(real)) CTAssertEqual(complex4.imaginary, Int64(imaginary)) - let complex5 = Complex(real: real, imaginary: imaginary) - CTAssertEqual(complex5.real, Float80(real)) - CTAssertEqual(complex5.imaginary, Float80(imaginary)) + let complex5 = Complex(real: real, imaginary: imaginary) + CTAssertEqual(complex5.real, Complex.LargestFloatType(real)) + CTAssertEqual(complex5.imaginary, Complex.LargestFloatType(imaginary)) - let complex6 = Complex(complex1) - CTAssertEqual(complex6.real, Float80(real)) - CTAssertEqual(complex6.imaginary, Float80(imaginary)) + let complex6 = Complex(complex1) + CTAssertEqual(complex6.real, Complex.LargestFloatType(real)) + CTAssertEqual(complex6.imaginary, Complex.LargestFloatType(imaginary)) let complex7: Complex = [] CTAssertEqual(complex7.real, 0) @@ -291,13 +291,13 @@ class ComplexTests: XCTestCase { CTAssertEqual(complex4.real, Int64(real)) CTAssertEqual(complex4.imaginary, Int64(imaginary)) - let complex5 = Complex(real: real, imaginary: imaginary) - CTAssertEqual(complex5.real, Float80(real)) - CTAssertEqual(complex5.imaginary, Float80(imaginary)) + let complex5 = Complex(real: real, imaginary: imaginary) + CTAssertEqual(complex5.real, Complex.LargestFloatType(real)) + CTAssertEqual(complex5.imaginary, Complex.LargestFloatType(imaginary)) - let complex6 = Complex(complex1) - CTAssertEqual(complex6.real, Float80(real)) - CTAssertEqual(complex6.imaginary, Float80(imaginary)) + let complex6 = Complex(complex1) + CTAssertEqual(complex6.real, Complex.LargestFloatType(real)) + CTAssertEqual(complex6.imaginary, Complex.LargestFloatType(imaginary)) let complex7: Complex = [] CTAssertEqual(complex7.real, 0.0) @@ -480,6 +480,6 @@ class ComplexTests: XCTestCase { private func testPolarComponents(_ complex: Complex, file: StaticString = #file, line: UInt = #line) where Scalar: BinaryFloatingPoint { CTAssertEqual(complex.modulus, sqrt(complex.real * complex.real + complex.imaginary * complex.imaginary)) - CTAssertEqual(complex.angle, Scalar(atan2(Float80(complex.imaginary), Float80(complex.real)))) + CTAssertEqual(complex.angle, Scalar(atan2(Complex.LargestFloatType(complex.imaginary), Complex.LargestFloatType(complex.real)))) } } diff --git a/Tests/ComplexTests/XCTestManifests.swift b/Tests/ComplexTests/XCTestManifests.swift new file mode 100644 index 0000000..fa9ec7f --- /dev/null +++ b/Tests/ComplexTests/XCTestManifests.swift @@ -0,0 +1,108 @@ +#if !canImport(ObjectiveC) +import XCTest + +extension ComplexArithmeticTests { + // DO NOT MODIFY: This is autogenerated, use: + // `swift test --generate-linuxmain` + // to regenerate. + static let __allTests__ComplexArithmeticTests = [ + ("testAddition", testAddition), + ("testAdditionIgnoringOverflow", testAdditionIgnoringOverflow), + ("testAdditionWithZero", testAdditionWithZero), + ("testComponentwiseDivision", testComponentwiseDivision), + ("testComponentwiseMultiplication", testComponentwiseMultiplication), + ("testDivision", testDivision), + ("testMultiplication", testMultiplication), + ("testMultiplicationIgnoringOverflow", testMultiplicationIgnoringOverflow), + ("testMultiplicationWithZero", testMultiplicationWithZero), + ("testSubtraction", testSubtraction), + ("testSubtractionIgnoringOverflow", testSubtractionIgnoringOverflow), + ("testSubtractionWithZero", testSubtractionWithZero), + ] +} + +extension ComplexOverflowingArithmeticTests { + // DO NOT MODIFY: This is autogenerated, use: + // `swift test --generate-linuxmain` + // to regenerate. + static let __allTests__ComplexOverflowingArithmeticTests = [ + ("testAddExtendedScalars", testAddExtendedScalars), + ("testComponentwiseOverflowingDivision", testComponentwiseOverflowingDivision), + ("testComponentwiseOverflowingMultiplication", testComponentwiseOverflowingMultiplication), + ("testDivideFullWidth", testDivideFullWidth), + ("testDivideReportingOverflow", testDivideReportingOverflow), + ("testIsLessThan", testIsLessThan), + ("testIsLessThanOrEqual", testIsLessThanOrEqual), + ("testIsPowerOfTwo", testIsPowerOfTwo), + ("testLeadingZeroBitCount", testLeadingZeroBitCount), + ("testLeftShiftExtendedScalars", testLeftShiftExtendedScalars), + ("testMultiplyFullWidth", testMultiplyFullWidth), + ("testMultiplyReportingOverflow", testMultiplyReportingOverflow), + ("testOverflowingAddition", testOverflowingAddition), + ("testOverflowingSubtraction", testOverflowingSubtraction), + ("testRightShiftExtendedScalars", testRightShiftExtendedScalars), + ("testSignExtension", testSignExtension), + ("testSlowpathDivide", testSlowpathDivide), + ("testSlowpathMultiply", testSlowpathMultiply), + ("testSubtractExtendedScalars", testSubtractExtendedScalars), + ("testTruncateExtendedScalars", testTruncateExtendedScalars), + ("testTwosComplement", testTwosComplement), + ] +} + +extension ComplexTests { + // DO NOT MODIFY: This is autogenerated, use: + // `swift test --generate-linuxmain` + // to regenerate. + static let __allTests__ComplexTests = [ + ("testAdditiveArithmeticProtocolRequirements", testAdditiveArithmeticProtocolRequirements), + ("testConjugateMethods", testConjugateMethods), + ("testDescriptionMethods", testDescriptionMethods), + ("testHashing", testHashing), + ("testInitialization", testInitialization), + ("testMultiplyByI", testMultiplyByI), + ("testMultiplyByOne", testMultiplyByOne), + ("testNegationMethods", testNegationMethods), + ("testPlusPrefixOperator", testPlusPrefixOperator), + ("testPolarComponents", testPolarComponents), + ("testRandomFactoryMethods", testRandomFactoryMethods), + ("testRounding", testRounding), + ] +} + +extension FunctionsTests { + // DO NOT MODIFY: This is autogenerated, use: + // `swift test --generate-linuxmain` + // to regenerate. + static let __allTests__FunctionsTests = [ + ("test_abs", test_abs), + ("test_acos", test_acos), + ("test_asin", test_asin), + ("test_atan", test_atan), + ("test_clamp", test_clamp), + ("test_cos", test_cos), + ("test_cosh", test_cosh), + ("test_csqrt", test_csqrt), + ("test_exp", test_exp), + ("test_log", test_log), + ("test_log10", test_log10), + ("test_log2", test_log2), + ("test_max", test_max), + ("test_min", test_min), + ("test_sin", test_sin), + ("test_sinh", test_sinh), + ("test_sqrt", test_sqrt), + ("test_tan", test_tan), + ("test_tanh", test_tanh), + ] +} + +public func __allTests() -> [XCTestCaseEntry] { + return [ + testCase(ComplexArithmeticTests.__allTests__ComplexArithmeticTests), + testCase(ComplexOverflowingArithmeticTests.__allTests__ComplexOverflowingArithmeticTests), + testCase(ComplexTests.__allTests__ComplexTests), + testCase(FunctionsTests.__allTests__FunctionsTests), + ] +} +#endif diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift new file mode 100644 index 0000000..1e6fc04 --- /dev/null +++ b/Tests/LinuxMain.swift @@ -0,0 +1,8 @@ +import XCTest + +import ComplexTests + +var tests = [XCTestCaseEntry]() +tests += ComplexTests.__allTests() + +XCTMain(tests)