X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/7b6d0a81e0f3c8e33b1b76d5ecbc5bdb25a87249..80e348724041a694002fc38f57e8868b9b9a2be9:/view_test.go?ds=sidebyside diff --git a/view_test.go b/view_test.go index 7184f8c..f771eb2 100644 --- a/view_test.go +++ b/view_test.go @@ -10,8 +10,11 @@ import ( func TestViewMergesInTheOrderOfTheRollbackPush(t *testing.T) { tree := ViewBuiltTree{} + view := newView(0, 0) + var states *ViewPushEntry + for tick := range 20001 { states = viewPush(tick, states) starts := states.starts() @@ -31,11 +34,13 @@ func TestViewMergesInTheOrderOfTheRollbackPush(t *testing.T) { func TestViewMergesInOneBatchFromHighToLow(t *testing.T) { tree := ViewBuiltTree{} + view := newView(10, 5) sizes := make([]int, 0, 20) for range 20 { _, err := view.Append(tree) require.NoError(t, err) + sizes = append(sizes, view.Size()) } @@ -44,7 +49,7 @@ func TestViewMergesInOneBatchFromHighToLow(t *testing.T) { func TestViewMergesOnlyBuiltParents(t *testing.T) { tree := ViewPartTree{ - sizes: map[nodeKey]int{ + sizes: map[NodeKey]int{ {0, 0}: 1, {0, 1}: 1, {0, 2}: 1, @@ -60,27 +65,27 @@ func TestViewMergesOnlyBuiltParents(t *testing.T) { require.NoError(t, err) } - require.Equal(t, []nodeKey{{0, 0}, {0, 1}, {0, 2}, {0, 3}}, view.lines) + require.Equal(t, []NodeKey{{0, 0}, {0, 1}, {0, 2}, {0, 3}}, view.lines) require.True(t, view.merging) - tree.sizes[nodeKey{1, 0}] = 1 + tree.sizes[NodeKey{1, 0}] = 1 _, err := view.Append(tree) require.NoError(t, err) - require.Equal(t, []nodeKey{{1, 0}, {0, 2}, {0, 3}, {0, 4}}, view.lines) + require.Equal(t, []NodeKey{{1, 0}, {0, 2}, {0, 3}, {0, 4}}, view.lines) require.True(t, view.merging) - tree.sizes[nodeKey{1, 1}] = 1 - tree.sizes[nodeKey{1, 2}] = 1 - tree.sizes[nodeKey{2, 0}] = 1 + tree.sizes[NodeKey{1, 1}] = 1 + tree.sizes[NodeKey{1, 2}] = 1 + tree.sizes[NodeKey{2, 0}] = 1 _, err = view.Append(tree) require.NoError(t, err) - require.Equal(t, []nodeKey{{2, 0}, {1, 2}}, view.lines) + require.Equal(t, []NodeKey{{2, 0}, {1, 2}}, view.lines) require.False(t, view.merging) } func TestViewRendersPendingLinesWithoutTheirMessage(t *testing.T) { tree := ViewPartTree{ - sizes: map[nodeKey]int{ + sizes: map[NodeKey]int{ {0, 0}: 3, {0, 1}: 3, {1, 0}: 7, @@ -107,17 +112,17 @@ func TestViewCoarsenTakesTheCoarserLines(t *testing.T) { require.NoError(t, err) require.NoError(t, view.Coarsen(other, ViewBuiltTree{})) - require.Equal(t, []nodeKey{{1, 0}, {1, 1}, {0, 4}, {0, 5}}, view.lines) + require.Equal(t, []NodeKey{{1, 0}, {1, 1}, {0, 4}, {0, 5}}, view.lines) view.low = 2 require.NoError(t, view.Coarsen(other, ViewBuiltTree{})) - require.Equal(t, []nodeKey{{2, 0}, {1, 2}}, view.lines) + require.Equal(t, []NodeKey{{2, 0}, {1, 2}}, view.lines) require.False(t, view.merging) } func TestViewContextStopsAtTheFirstPendingLine(t *testing.T) { tree := ViewPartTree{ - sizes: map[nodeKey]int{ + sizes: map[NodeKey]int{ {0, 0}: 1, {0, 1}: 1, {1, 0}: 1, @@ -144,14 +149,6 @@ func TestRestoreViewRejectsAGap(t *testing.T) { // ViewBuiltTree is a tree with each node built, with 1 byte of text. type ViewBuiltTree struct{} -func (ViewBuiltTree) Built(level, index int) (bool, error) { - return true, nil -} - -func (ViewBuiltTree) Node(level, index int) (Node, bool, error) { - return testNode(level, index, "a"), true, nil -} - // ViewPushEntry is one entry of the rollback state list of the reference // design, newest first. Under push alone, life stays 0, so the entry leaves it // out. @@ -161,9 +158,23 @@ type ViewPushEntry struct { older *ViewPushEntry } +// ViewPartTree is a tree with the built nodes of its sizes. +type ViewPartTree struct { + sizes map[NodeKey]int +} + +func (v ViewBuiltTree) Built(_, _ int) (bool, error) { + return true, nil +} + +func (ViewBuiltTree) Node(level, index int) (Node, bool, error) { + return treeTestNode(level, index, "a"), true, nil +} + // starts returns the states of the list, oldest first. func (e *ViewPushEntry) starts() []int { var starts []int + for entry := e; entry != nil; entry = entry.older { starts = append(starts, entry.state) } @@ -173,18 +184,13 @@ func (e *ViewPushEntry) starts() []int { return starts } -// ViewPartTree is a tree with the built nodes of its sizes. -type ViewPartTree struct { - sizes map[nodeKey]int -} - func (p ViewPartTree) Built(level, index int) (bool, error) { - _, built := p.sizes[nodeKey{level, index}] + _, built := p.sizes[NodeKey{level, index}] return built, nil } func (p ViewPartTree) Node(level, index int) (Node, bool, error) { - size, built := p.sizes[nodeKey{level, index}] + size, built := p.sizes[NodeKey{level, index}] if !built { return Node{}, false, nil } @@ -194,10 +200,14 @@ func (p ViewPartTree) Node(level, index int) (Node, bool, error) { text = "aaa\naaa" } - return testNode(level, index, text), true, nil + return treeTestNode(level, index, text), true, nil } -// viewPush is the push of rollback_state_list.js. +// viewPush adds state to the newest end of states and returns the new list. An +// empty list gets one entry. When the newest entry is not kept, it becomes kept +// with its own state, and state is dropped. When it is kept, state becomes a +// new newest entry, and the state of the old newest entry goes to the older +// entries by the same push. func viewPush(state int, states *ViewPushEntry) *ViewPushEntry { if states == nil { return &ViewPushEntry{