Skip to content

Commit

Permalink
Fix reverseChunked(by:) Method Implementation (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahan-wu committed Mar 8, 2024
1 parent 43929b0 commit 6f0fc05
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
10 changes: 2 additions & 8 deletions Sources/PostgresNIO/Data/PostgresData+Numeric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,10 @@ private extension Collection {
// splits the collection into chunks of the supplied size
// if the collection is not evenly divisible, the first chunk will be smaller
func reverseChunked(by maxSize: Int) -> [SubSequence] {
var lastDistance = 0
var chunkStartIndex = self.startIndex
return stride(from: 0, to: self.count, by: maxSize).reversed().map { current in
let distance = (self.count - current) - lastDistance
lastDistance = distance
let chunkEndOffset = Swift.min(
self.distance(from: chunkStartIndex, to: self.endIndex),
distance
)
let chunkEndIndex = self.index(chunkStartIndex, offsetBy: chunkEndOffset)
let distance = self.count - current
let chunkEndIndex = self.index(self.startIndex, offsetBy: distance)
defer { chunkStartIndex = chunkEndIndex }
return self[chunkStartIndex..<chunkEndIndex]
}
Expand Down
8 changes: 6 additions & 2 deletions Tests/IntegrationTests/PostgresNIOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -617,21 +617,25 @@ final class PostgresNIOTests: XCTestCase {
let a = PostgresNumeric(string: "123456.789123")!
let b = PostgresNumeric(string: "-123456.789123")!
let c = PostgresNumeric(string: "3.14159265358979")!
let d = PostgresNumeric(string: "1234567898765")!
var rows: PostgresQueryResult?
XCTAssertNoThrow(rows = try conn?.query("""
select
$1::numeric as a,
$2::numeric as b,
$3::numeric as c
$3::numeric as c,
$4::numeric as d
""", [
.init(numeric: a),
.init(numeric: b),
.init(numeric: c)
.init(numeric: c),
.init(numeric: d)
]).wait())
let row = rows?.first?.makeRandomAccess()
XCTAssertEqual(row?[data: "a"].decimal, Decimal(string: "123456.789123")!)
XCTAssertEqual(row?[data: "b"].decimal, Decimal(string: "-123456.789123")!)
XCTAssertEqual(row?[data: "c"].decimal, Decimal(string: "3.14159265358979")!)
XCTAssertEqual(row?[data: "d"].decimal, Decimal(string: "1234567898765")!)
}

func testDecimalStringSerialization() {
Expand Down

0 comments on commit 6f0fc05

Please sign in to comment.