Skip to content

updateValue in InsertionOrderedDictionary fails invariant assertion #141

@vadimtrifonov

Description

@vadimtrifonov

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions