penguin icon indicating copy to clipboard operation
penguin copied to clipboard

`updateValue` in `InsertionOrderedDictionary` fails invariant assertion

Open vadimtrifonov opened this issue 4 years ago • 0 comments

When updating the existing key with updateValue method the original index is not set back after acquiring it with indexForKey.updateValue(endIndex, forKey: key).

I believe this should be done after the guard statement:

guard let index = indexForKey.updateValue(endIndex, forKey: key) else {
    elements.append(.init(key: key, value: newValue))
    return nil
}
indexForKey[key] = index

At the moment of writing InsertionOrderedDictionaryTests don't have tests for updateValue.

The following test triggers the assertion:

func test_updateValue() {
    var d0 = p0
    for (key, value) in p1 {
        XCTAssertEqual(d0[key], value)
        let replaced = d0.updateValue("*", forKey: key)
        XCTAssertEqual(d0[key], "*")
        XCTAssertEqual(replaced, value)
    }
    XCTAssertEqual(d0[99], nil)
}

vadimtrifonov avatar Jan 29 '21 02:01 vadimtrifonov