File tree Expand file tree Collapse file tree 2 files changed +42
-3
lines changed
Sources/OctopusKit/Apple API Extensions/Foundation
Tests/OctopusKitTests/Apple API Extensions Tests Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Original file line number Diff line number Diff line change 1111import Foundation
1212
1313public extension FloatingPoint {
14-
14+
1515 // MARK: - Random Numbers
1616
1717 /// Shorthand for `Self(Int.random(in: range))`
@@ -41,4 +41,23 @@ public extension FloatingPoint {
4141 {
4242 Self ( Int . random ( in: range, using: & generator) )
4343 }
44+
45+ // MARK: - Percentages
46+
47+ /// Returns the specified percentage of `self`.
48+ @inlinable
49+ func percent( _ percent: Self ) -> Self {
50+ // TODO: Apply this extension to integers too?
51+ return self * ( percent / 100 )
52+ }
53+
54+ /// Returns `self` percent of the specified `value`.
55+ ///
56+ /// **Example:** `10.percent(of: 50)` returns `5`
57+ @inlinable
58+ func percent( of value: Self ) -> Self {
59+ // TODO: Apply this extension to integers too?
60+ return value * ( self / 100 )
61+ }
62+
4463}
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ class NumericsTests: XCTestCase {
4444
4545 // #2: Test floats
4646
47- XCTContext . runActivity ( named: " Test integers " ) { _ in
47+ XCTContext . runActivity ( named: " Test floats " ) { _ in
4848 var value : Float = 10.0
4949
5050 // #2A: Difference = 0.1
@@ -72,7 +72,27 @@ class NumericsTests: XCTestCase {
7272
7373 }
7474
75+ func testFloatingPoint( ) {
76+
77+ // TODO: #1: Test percent(_:)
78+
79+ /* NOTE: This test will fail because you're not supposed to directly compare floating point numbers.
80+
81+ XCTContext.runActivity(named: "Test percent") { _ in
82+
83+ // #1A: N% of 100 should be N
84+
85+ for percent in 0...100 {
86+ XCTAssertTrue(100.percent(Double(percent)).isEqual(to: Double(percent)))
87+ }
88+ }
89+ */
90+
91+ // TODO: #2: Test percent(of:)
92+ }
93+
7594 static var allTests = [
76- ( " Test AdditiveArithmetic extensions " , testAdditiveArithmetic)
95+ ( " Test AdditiveArithmetic extensions " , testAdditiveArithmetic) ,
96+ ( " Test FloatingPoint extensions " , testFloatingPoint)
7797 ]
7898}
You can’t perform that action at this time.
0 commit comments