Skip to content

Commit 5b11022

Browse files
committed
#15 Test integer division matches CoreData
1 parent cdf0a80 commit 5b11022

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

‎Tests/CoreModelTests/CoreDataModelTests.swift‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,33 @@ import Testing
173173
#expect(convertedResults.map { $0.attributes["name"] } == [.string("Alice")])
174174
}
175175

176+
@available(macOS 14.0, iOS 17.0, tvOS 17.0, watchOS 10.0, *)
177+
@Test func integerDivisionMatchesCoreData() throws {
178+
179+
// `divide:by:` truncates for integer operands, so CoreModel's in-memory
180+
// evaluator has to agree with CoreData rather than divide as floating-point
181+
let context = try Self.makeContext()
182+
try context.insert(Person(name: "Seven", age: 7).encode())
183+
184+
// age / 2 == 3 holds under integer division, not under floating-point
185+
let predicate = FetchRequest.Predicate.Expression
186+
.arithmetic(.init(function: .divide, left: .keyPath("age"), right: .attribute(.int64(2))))
187+
.compare(.equalTo, .attribute(.int64(3)))
188+
let request = FetchRequest(entity: Person.entityName, predicate: predicate)
189+
let coreData = try context.fetch(request)
190+
#expect(coreData.map { $0.attributes["name"] } == [.string("Seven")])
191+
192+
let objects = try context.fetch(FetchRequest(entity: Person.entityName))
193+
#expect(objects.filtered(by: predicate).map { $0.attributes["name"] } == [.string("Seven")])
194+
195+
// and the floating-point quotient matches neither engine
196+
let floating = FetchRequest.Predicate.Expression
197+
.arithmetic(.init(function: .divide, left: .keyPath("age"), right: .attribute(.int64(2))))
198+
.compare(.equalTo, .attribute(.double(3.5)))
199+
#expect(try context.fetch(FetchRequest(entity: Person.entityName, predicate: floating)).isEmpty)
200+
#expect(objects.filtered(by: floating).isEmpty)
201+
}
202+
176203
@available(macOS 14.0, iOS 17.0, tvOS 17.0, watchOS 10.0, *)
177204
@Test func modifierPredicateFetch() throws {
178205

0 commit comments

Comments
 (0)