Skip to content

Commit 00c6256

Browse files
committed
add exp()
1 parent 11bf771 commit 00c6256

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

Firestore/Swift/Source/ExpressionImplementation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public extension Expression {
3333
return FunctionExpression("ceil", [self])
3434
}
3535

36+
func exp() -> FunctionExpression {
37+
return FunctionExpression("exp", [self])
38+
}
39+
3640
func add(_ value: Expression) -> FunctionExpression {
3741
return FunctionExpression("add", [self, value])
3842
}

Firestore/Swift/Source/SwiftAPI/Pipeline/Expressions/Expression.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ public protocol Expression: Sendable {
3636

3737
// --- Added Mathematical Operations ---
3838

39+
/// Creates an expression that returns e to the power of X.
40+
///
41+
/// Returns zero on underflow and nil on overflow.
42+
///
43+
/// ```swift
44+
/// // Get the exp of the "amount" field.
45+
/// Field("amount").exp()
46+
/// ```
47+
///
48+
/// - Returns: A new `FunctionExpression` representing the exp of the number.
49+
func exp() -> FunctionExpression
50+
3951
/// Creates an expression that returns the smallest numeric value that isn't less than the number.
4052
///
4153
/// ```swift

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,70 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
20522052
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
20532053
}
20542054

2055+
func testExpWorks() async throws {
2056+
let collRef = collectionRef(withDocuments: [
2057+
"doc1": ["value": 1],
2058+
"doc2": ["value": 0],
2059+
"doc3": ["value": -1],
2060+
])
2061+
let db = collRef.firestore
2062+
2063+
let pipeline = db.pipeline()
2064+
.collection(collRef.path)
2065+
.select([
2066+
Field("value").exp().as("expValue"),
2067+
])
2068+
.sort([Field("expValue").ascending()])
2069+
2070+
let snapshot = try await pipeline.execute()
2071+
2072+
let expectedResults: [[String: Sendable]] = [
2073+
["expValue": Foundation.exp(Double(-1))],
2074+
["expValue": Foundation.exp(Double(0))],
2075+
["expValue": Foundation.exp(Double(1))],
2076+
]
2077+
2078+
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
2079+
}
2080+
2081+
func testExpUnderflow() async throws {
2082+
let collRef = collectionRef(withDocuments: [
2083+
"doc1": ["value": -1000],
2084+
])
2085+
let db = collRef.firestore
2086+
2087+
let pipeline = db.pipeline()
2088+
.collection(collRef.path)
2089+
.select([
2090+
Field("value").exp().as("expValue"),
2091+
])
2092+
2093+
let snapshot = try await pipeline.execute()
2094+
2095+
let expectedResults: [[String: Sendable]] = [
2096+
["expValue": 0],
2097+
]
2098+
2099+
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
2100+
}
2101+
2102+
func testExpOverflow() async throws {
2103+
let collRef = collectionRef(withDocuments: [
2104+
"doc1": ["value": 1000],
2105+
])
2106+
let db = collRef.firestore
2107+
2108+
let pipeline = db.pipeline()
2109+
.collection(collRef.path)
2110+
.select([
2111+
Field("value").exp().as("expValue"),
2112+
])
2113+
2114+
let snapshot = try await pipeline.execute()
2115+
XCTAssertEqual(snapshot.results.count, 1)
2116+
XCTAssertNil(snapshot.results.first!.get("expValue"))
2117+
}
2118+
20552119
func testCollectionIdWorks() async throws {
20562120
let collRef = collectionRef()
20572121
let docRef = collRef.document("doc")

0 commit comments

Comments
 (0)