Cysharp / Cysharp/ObservableCollections

`ObservableStack<T>.CreateView` reverses the order of a non-empty source, then desyncs on `Pop`

Open
#126 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
1k
Forks
73
Avg merge
5d 4h
Merged PRs (30d)
2

Description

Summary

When CreateView is called on an ObservableStack<T> that already contains elements, the view is built in the reverse order of the source.

Creating the view on an empty stack and populating it afterwards works correctly, which is why the existing tests do not catch this.

Repro

var stack = new ObservableStack<int>(new[] { 1, 2, 3 });
var view = stack.CreateView(x => x);
var notify = stack.ToNotifyCollectionChanged();

var removed1 = -1;
stack.CollectionChanged += (in args) => { removed1 = args.OldItem; };

var removed2 = -1;
notify.CollectionChanged += (sender, args) => { removed2 = (int)args.OldItems[args.OldStartingIndex]; };

stack.ToArray();                   // 3, 2, 1
view.ToArray();                    // 1, 2, 3   <-- reversed

var popped = stack.Pop();          // returns 3
stack.ToArray();                   // 2, 1
view.ToArray();                    // 2, 3      <-- 1 was removed instead of 3

Assert.Equal(3, popped);           // pass
Assert.Equal(3, removed1);         // pass
Assert.Equal(3, removed2);         // fail. removed2 is actually 1

Expected:

view enumerates in the same order as stack (top to bottom), and the two stay in sync across Push/`P

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from ObservableStack.CreateView and the existing tests for creating a view, then reproduce the non-empty-stack case from the issue with ToArray, Pop, and collection-change notifications. Add regression coverage showing that view enumeration matches the stack and that Pop removes the same item from both; run the ObservableStack test suite to verify Push and Pop synchronization.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.