Skip to content

Commit

Permalink
rangeReplace bug + test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Dec 22, 2023
1 parent b8b1cf9 commit 20fbdb0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 50 deletions.
11 changes: 7 additions & 4 deletions Sources/RelativeCollections/RelativeArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,20 @@ extension RelativeArray {
dependency = configuration.add(element.weight, dependency)
}

let originalDependency = findDependency(for: range.upperBound)

storage.replaceSubrange(range, with: newRecords)

if range.upperBound >= storage.endIndex {
let affectedIndex = range.lowerBound + newRecords.count
if affectedIndex >= storage.endIndex {
return
}

// and now, we have to adjust everything past the insertion
let upperDependency = storage[range.upperBound].dependency
let delta = configuration.subtract(upperDependency, dependency)

applyDelta(delta, after: range.upperBound)
let delta = configuration.subtract(dependency, originalDependency)

applyDelta(delta, startingAt: affectedIndex)
}
}

Expand Down
90 changes: 44 additions & 46 deletions Tests/RelativeCollectionsTests/RelativeArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ import XCTest

import RelativeCollections

extension Array where Element == Int {
var relativeRanges: [Range<Int>] {
var start = 0

var ranges = [Range<Int>]()

for element in self {
let end = start + element

ranges.append(start..<end)

start = end
}

return ranges
}
}

final class RelativeArrayTests: XCTestCase {
typealias TestArray = RelativeArray<Int, Int>

Expand All @@ -27,57 +45,30 @@ final class RelativeArrayTests: XCTestCase {
func testUpdateFirst() throws {
var array = TestArray()

let ranges = [
0..<1,
1..<3,
3..<6,
6..<10
]

// insert lengths
for range in ranges {
array.append(.init(value: range.count, weight: range.count))
for length in [1, 2, 3, 4] {
array.append(.init(weight: length))
}

array.replace(.init(value: 5, weight: 5), at: 0)

let rebuilt = array.map { $0.dependency..<($0.dependency + $0.value) }

let expected = [
0..<5,
5..<7,
7..<10,
10..<14
]
let expected = [5, 2, 3, 4].relativeRanges

XCTAssertEqual(rebuilt, expected)
}

func testUpdateLast() throws {
var array = TestArray()

let ranges = [
0..<1,
1..<3,
3..<6,
6..<10
]

// insert lengths
for range in ranges {
array.append(.init(value: range.count, weight: range.count))
for length in [1, 2, 3, 4] {
array.append(.init(weight: length))
}

array.replace(.init(value: 5, weight: 5), at: 3)
array.replace(.init(weight: 5), at: 3)

let rebuilt = array.map { $0.dependency..<($0.dependency + $0.value) }

let expected = [
0..<1,
1..<3,
3..<6,
6..<11
]
let expected = [1, 2, 3, 5].relativeRanges

XCTAssertEqual(rebuilt, expected)
}
Expand All @@ -91,9 +82,10 @@ final class RelativeArrayTests: XCTestCase {

array.remove(at: 0)

let expected = [2, 3, 4]
let rebuilt = array.map { $0.dependency..<($0.dependency + $0.value) }
let expected = [2, 3, 4].relativeRanges

XCTAssertEqual(array.map { $0.value }, expected)
XCTAssertEqual(rebuilt, expected)
}

func testInsertWithPredicate() {
Expand All @@ -107,9 +99,10 @@ final class RelativeArrayTests: XCTestCase {
return idx >= 1
}

let expected = [1, 5, 2, 3, 4]
let rebuilt = array.map { $0.dependency..<($0.dependency + $0.value) }
let expected = [1, 5, 2, 3, 4].relativeRanges

XCTAssertEqual(array.map { $0.value }, expected)
XCTAssertEqual(rebuilt, expected)
}

func testInsertAtBeginning() {
Expand All @@ -121,9 +114,11 @@ final class RelativeArrayTests: XCTestCase {

array.insert(.init(value: 5, weight: 5), at: 0)

let expected = [5, 1, 2, 3, 4]
let expected = [5, 1, 2, 3, 4].relativeRanges

XCTAssertEqual(array.map { $0.value }, expected)
let rebuilt = array.map { $0.dependency..<($0.dependency + $0.value) }

XCTAssertEqual(rebuilt, expected)
}

func testInsertWithPredicateAtFirst() {
Expand All @@ -137,9 +132,10 @@ final class RelativeArrayTests: XCTestCase {
return idx >= 0
}

let expected = [5, 1, 2, 3, 4]
let rebuilt = array.map { $0.dependency..<($0.dependency + $0.value) }
let expected = [5, 1, 2, 3, 4].relativeRanges

XCTAssertEqual(array.map { $0.value }, expected)
XCTAssertEqual(rebuilt, expected)
}

func testSubscriptAndRecordAreTheSame() {
Expand Down Expand Up @@ -179,9 +175,10 @@ extension RelativeArrayTests {
TestArray.WeightedValue(weight: 9)
])

let expected = [1, 8, 9, 4]
let rebuilt = array.map { $0.dependency..<($0.dependency + $0.value) }
let expected = [1, 8, 9, 4].relativeRanges

XCTAssertEqual(array.map { $0.value }, expected)
XCTAssertEqual(rebuilt, expected)
}

func testRangeReplacementEntireArray() throws {
Expand All @@ -196,8 +193,9 @@ extension RelativeArrayTests {
TestArray.WeightedValue(weight: 9)
])

let expected = [8, 9]
let rebuilt = array.map { $0.dependency..<($0.dependency + $0.value) }
let expected = [8, 9].relativeRanges

XCTAssertEqual(array.map { $0.value }, expected)
XCTAssertEqual(rebuilt, expected)
}
}

0 comments on commit 20fbdb0

Please sign in to comment.