Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sejongk committed Aug 23, 2023
1 parent 1deb19d commit e100c79
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions pkg/document/crdt/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,21 @@ func TestTree(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, `<root><p color="red" weight="bold">ab</p><p>cd</p></root>`, tree.ToXML())

// style attributes with the whole
err = tree.StyleByIndex(0, 4, map[string]string{"size": "small"}, helper.IssueTime(ctx))
assert.NoError(t, err)
assert.Equal(t, `<root><p color="red" size="small" weight="bold">ab</p><p>cd</p></root>`, tree.ToXML())

// 02. style attributes to elements.
err = tree.StyleByIndex(0, 5, map[string]string{"style": "italic"}, helper.IssueTime(ctx))
assert.NoError(t, err)
assert.Equal(t, `<root><p color="red" style="italic" weight="bold">ab</p>`+
assert.Equal(t, `<root><p color="red" size="small" style="italic" weight="bold">ab</p>`+
`<p style="italic">cd</p></root>`, tree.ToXML())

// 03. Ignore styling attributes to text nodes.
err = tree.StyleByIndex(1, 3, map[string]string{"bold": "true"}, helper.IssueTime(ctx))
assert.NoError(t, err)
assert.Equal(t, `<root><p color="red" style="italic" weight="bold">ab</p>`+
assert.Equal(t, `<root><p color="red" size="small" style="italic" weight="bold">ab</p>`+
`<p style="italic">cd</p></root>`, tree.ToXML())
})

Expand Down Expand Up @@ -352,4 +357,38 @@ func TestTree(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, idx)
})

t.Run("delete nodes in a multi-level range test", func(t *testing.T) {
ctx := helper.TextChangeContext(helper.TestRoot())
tree := crdt.NewTree(crdt.NewTreeNode(helper.IssuePos(ctx), "root", nil), helper.IssueTime(ctx))
_, err := tree.EditByIndex(0, 0, nil, []*crdt.TreeNode{crdt.NewTreeNode(helper.IssuePos(ctx),
"p", nil)}, helper.IssueTime(ctx))
assert.NoError(t, err)
_, err = tree.EditByIndex(1, 1, nil, []*crdt.TreeNode{crdt.NewTreeNode(helper.IssuePos(ctx),
"text", nil, "ab")}, helper.IssueTime(ctx))
assert.NoError(t, err)
_, err = tree.EditByIndex(4, 4, nil, []*crdt.TreeNode{crdt.NewTreeNode(helper.IssuePos(ctx),
"p", nil)}, helper.IssueTime(ctx))
assert.NoError(t, err)
_, err = tree.EditByIndex(5, 5, nil, []*crdt.TreeNode{crdt.NewTreeNode(helper.IssuePos(ctx),
"p", nil)}, helper.IssueTime(ctx))
assert.NoError(t, err)
_, err = tree.EditByIndex(6, 6, nil, []*crdt.TreeNode{crdt.NewTreeNode(helper.IssuePos(ctx),
"text", nil, "cd")}, helper.IssueTime(ctx))
assert.NoError(t, err)
_, err = tree.EditByIndex(10, 10, nil, []*crdt.TreeNode{crdt.NewTreeNode(helper.IssuePos(ctx),
"p", nil)}, helper.IssueTime(ctx))
assert.NoError(t, err)
_, err = tree.EditByIndex(11, 11, nil, []*crdt.TreeNode{crdt.NewTreeNode(helper.IssuePos(ctx),
"text", nil, "ef")}, helper.IssueTime(ctx))
assert.NoError(t, err)
assert.Equal(t, "<root><p>ab</p><p><p>cd</p></p><p>ef</p></root>", tree.ToXML())

_, err = tree.EditByIndex(2, 12, nil, nil, helper.IssueTime(ctx))
assert.NoError(t, err)
assert.Equal(t, "<root><p>a</p><p>f</p></root>", tree.ToXML())

// TODO(sejongk): Use the below assertion after implementing Tree.Move.
// assert.Equal(t, "<root><p>af</p></root>", tree.ToXML())
})
}

1 comment on commit e100c79

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go Benchmark

Benchmark suite Current: e100c79 Previous: 1deb19d Ratio
BenchmarkDocument/constructor_test - ns/op 1681 ns/op 1532 ns/op 1.10
BenchmarkDocument/constructor_test - B/op 984 B/op 984 B/op 1
BenchmarkDocument/constructor_test - allocs/op 16 allocs/op 16 allocs/op 1
BenchmarkDocument/status_test - ns/op 989.3 ns/op 869.4 ns/op 1.14
BenchmarkDocument/status_test - B/op 952 B/op 952 B/op 1
BenchmarkDocument/status_test - allocs/op 14 allocs/op 14 allocs/op 1
BenchmarkDocument/equals_test - ns/op 9663 ns/op 10937 ns/op 0.88
BenchmarkDocument/equals_test - B/op 6192 B/op 6192 B/op 1
BenchmarkDocument/equals_test - allocs/op 106 allocs/op 106 allocs/op 1
BenchmarkDocument/nested_update_test - ns/op 24142 ns/op 22029 ns/op 1.10
BenchmarkDocument/nested_update_test - B/op 11689 B/op 11689 B/op 1
BenchmarkDocument/nested_update_test - allocs/op 248 allocs/op 248 allocs/op 1
BenchmarkDocument/delete_test - ns/op 33876 ns/op 30122 ns/op 1.12
BenchmarkDocument/delete_test - B/op 14915 B/op 14915 B/op 1
BenchmarkDocument/delete_test - allocs/op 327 allocs/op 327 allocs/op 1
BenchmarkDocument/object_test - ns/op 12194 ns/op 11045 ns/op 1.10
BenchmarkDocument/object_test - B/op 6448 B/op 6448 B/op 1
BenchmarkDocument/object_test - allocs/op 110 allocs/op 110 allocs/op 1
BenchmarkDocument/array_test - ns/op 50039 ns/op 36927 ns/op 1.36
BenchmarkDocument/array_test - B/op 11545 B/op 11545 B/op 1
BenchmarkDocument/array_test - allocs/op 264 allocs/op 264 allocs/op 1
BenchmarkDocument/text_test - ns/op 41587 ns/op 39488 ns/op 1.05
BenchmarkDocument/text_test - B/op 14618 B/op 14617 B/op 1.00
BenchmarkDocument/text_test - allocs/op 470 allocs/op 470 allocs/op 1
BenchmarkDocument/text_composition_test - ns/op 39495 ns/op 39537 ns/op 1.00
BenchmarkDocument/text_composition_test - B/op 18002 B/op 18002 B/op 1
BenchmarkDocument/text_composition_test - allocs/op 471 allocs/op 471 allocs/op 1
BenchmarkDocument/rich_text_test - ns/op 106841 ns/op 115729 ns/op 0.92
BenchmarkDocument/rich_text_test - B/op 36806 B/op 36816 B/op 1.00
BenchmarkDocument/rich_text_test - allocs/op 1131 allocs/op 1131 allocs/op 1
BenchmarkDocument/counter_test - ns/op 23381 ns/op 22176 ns/op 1.05
BenchmarkDocument/counter_test - B/op 9971 B/op 9969 B/op 1.00
BenchmarkDocument/counter_test - allocs/op 235 allocs/op 235 allocs/op 1
BenchmarkDocument/text_edit_gc_100 - ns/op 4398592 ns/op 4135564 ns/op 1.06
BenchmarkDocument/text_edit_gc_100 - B/op 1553028 B/op 1553233 B/op 1.00
BenchmarkDocument/text_edit_gc_100 - allocs/op 17162 allocs/op 17162 allocs/op 1
BenchmarkDocument/text_edit_gc_1000 - ns/op 356510972 ns/op 324838368 ns/op 1.10
BenchmarkDocument/text_edit_gc_1000 - B/op 136656514 B/op 136659268 B/op 1.00
BenchmarkDocument/text_edit_gc_1000 - allocs/op 210810 allocs/op 210835 allocs/op 1.00
BenchmarkDocument/text_split_gc_100 - ns/op 5202560 ns/op 4743081 ns/op 1.10
BenchmarkDocument/text_split_gc_100 - B/op 2217717 B/op 2217902 B/op 1.00
BenchmarkDocument/text_split_gc_100 - allocs/op 16589 allocs/op 16590 allocs/op 1.00
BenchmarkDocument/text_split_gc_1000 - ns/op 406938756 ns/op 383612355 ns/op 1.06
BenchmarkDocument/text_split_gc_1000 - B/op 214889160 B/op 214858946 B/op 1.00
BenchmarkDocument/text_split_gc_1000 - allocs/op 211585 allocs/op 211421 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 - ns/op 20604063 ns/op 17286967 ns/op 1.19
BenchmarkDocument/text_delete_all_10000 - B/op 5903839 B/op 5904278 B/op 1.00
BenchmarkDocument/text_delete_all_10000 - allocs/op 41124 allocs/op 41126 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 - ns/op 264131521 ns/op 228813907 ns/op 1.15
BenchmarkDocument/text_delete_all_100000 - B/op 53849780 B/op 53849148 B/op 1.00
BenchmarkDocument/text_delete_all_100000 - allocs/op 416011 allocs/op 416029 allocs/op 1.00
BenchmarkDocument/text_100 - ns/op 358726 ns/op 315329 ns/op 1.14
BenchmarkDocument/text_100 - B/op 118211 B/op 118211 B/op 1
BenchmarkDocument/text_100 - allocs/op 5074 allocs/op 5074 allocs/op 1
BenchmarkDocument/text_1000 - ns/op 3926687 ns/op 3418509 ns/op 1.15
BenchmarkDocument/text_1000 - B/op 1152829 B/op 1152816 B/op 1.00
BenchmarkDocument/text_1000 - allocs/op 50078 allocs/op 50078 allocs/op 1
BenchmarkDocument/array_1000 - ns/op 1886069 ns/op 1762911 ns/op 1.07
BenchmarkDocument/array_1000 - B/op 1102701 B/op 1102668 B/op 1.00
BenchmarkDocument/array_1000 - allocs/op 11867 allocs/op 11867 allocs/op 1
BenchmarkDocument/array_10000 - ns/op 21773984 ns/op 19001813 ns/op 1.15
BenchmarkDocument/array_10000 - B/op 9907871 B/op 9908731 B/op 1.00
BenchmarkDocument/array_10000 - allocs/op 120722 allocs/op 120727 allocs/op 1.00
BenchmarkDocument/array_gc_100 - ns/op 203388 ns/op 183829 ns/op 1.11
BenchmarkDocument/array_gc_100 - B/op 98165 B/op 98163 B/op 1.00
BenchmarkDocument/array_gc_100 - allocs/op 1243 allocs/op 1243 allocs/op 1
BenchmarkDocument/array_gc_1000 - ns/op 2241369 ns/op 2010637 ns/op 1.11
BenchmarkDocument/array_gc_1000 - B/op 1170292 B/op 1170535 B/op 1.00
BenchmarkDocument/array_gc_1000 - allocs/op 12906 allocs/op 12907 allocs/op 1.00
BenchmarkDocument/counter_1000 - ns/op 344294 ns/op 284469 ns/op 1.21
BenchmarkDocument/counter_1000 - B/op 198533 B/op 198532 B/op 1.00
BenchmarkDocument/counter_1000 - allocs/op 6503 allocs/op 6503 allocs/op 1
BenchmarkDocument/counter_10000 - ns/op 3740598 ns/op 3017581 ns/op 1.24
BenchmarkDocument/counter_10000 - B/op 2165470 B/op 2165458 B/op 1.00
BenchmarkDocument/counter_10000 - allocs/op 69510 allocs/op 69510 allocs/op 1
BenchmarkDocument/object_1000 - ns/op 2170349 ns/op 1951429 ns/op 1.11
BenchmarkDocument/object_1000 - B/op 1451224 B/op 1451310 B/op 1.00
BenchmarkDocument/object_1000 - allocs/op 9915 allocs/op 9915 allocs/op 1
BenchmarkDocument/object_10000 - ns/op 25525560 ns/op 22760781 ns/op 1.12
BenchmarkDocument/object_10000 - B/op 12370475 B/op 12372457 B/op 1.00
BenchmarkDocument/object_10000 - allocs/op 101222 allocs/op 101229 allocs/op 1.00
BenchmarkRPC/client_to_server - ns/op 444469661 ns/op 389731391 ns/op 1.14
BenchmarkRPC/client_to_server - B/op 12260976 B/op 12242672 B/op 1.00
BenchmarkRPC/client_to_server - allocs/op 177169 allocs/op 177036 allocs/op 1.00
BenchmarkRPC/client_to_client_via_server - ns/op 728810080 ns/op 642045922 ns/op 1.14
BenchmarkRPC/client_to_client_via_server - B/op 22560856 B/op 22658348 B/op 1.00
BenchmarkRPC/client_to_client_via_server - allocs/op 330243 allocs/op 331639 allocs/op 1.00
BenchmarkRPC/attach_large_document - ns/op 1309677608 ns/op 1527324807 ns/op 0.86
BenchmarkRPC/attach_large_document - B/op 1809607608 B/op 1799383144 B/op 1.01
BenchmarkRPC/attach_large_document - allocs/op 9456 allocs/op 9624 allocs/op 0.98
BenchmarkRPC/adminCli_to_server - ns/op 633827602 ns/op 523263078 ns/op 1.21
BenchmarkRPC/adminCli_to_server - B/op 20389768 B/op 20394360 B/op 1.00
BenchmarkRPC/adminCli_to_server - allocs/op 321621 allocs/op 321620 allocs/op 1.00
BenchmarkLocker - ns/op 139.2 ns/op 117.5 ns/op 1.18
BenchmarkLocker - B/op 16 B/op 16 B/op 1
BenchmarkLocker - allocs/op 1 allocs/op 1 allocs/op 1
BenchmarkLockerParallel - ns/op 180.3 ns/op 114.5 ns/op 1.57
BenchmarkLockerParallel - B/op 0 B/op 0 B/op NaN
BenchmarkLockerParallel - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkLockerMoreKeys - ns/op 353.1 ns/op 317.6 ns/op 1.11
BenchmarkLockerMoreKeys - B/op 14 B/op 14 B/op 1
BenchmarkLockerMoreKeys - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkSync/memory_sync_10_test - ns/op 8456 ns/op 7248 ns/op 1.17
BenchmarkSync/memory_sync_10_test - B/op 1284 B/op 1286 B/op 1.00
BenchmarkSync/memory_sync_10_test - allocs/op 38 allocs/op 38 allocs/op 1
BenchmarkSync/memory_sync_100_test - ns/op 74892 ns/op 61335 ns/op 1.22
BenchmarkSync/memory_sync_100_test - B/op 8846 B/op 9086 B/op 0.97
BenchmarkSync/memory_sync_100_test - allocs/op 286 allocs/op 301 allocs/op 0.95
BenchmarkSync/memory_sync_1000_test - ns/op 779253 ns/op 645301 ns/op 1.21
BenchmarkSync/memory_sync_1000_test - B/op 82218 B/op 84099 B/op 0.98
BenchmarkSync/memory_sync_1000_test - allocs/op 2604 allocs/op 2731 allocs/op 0.95
BenchmarkSync/memory_sync_10000_test - ns/op 8220920 ns/op 7312902 ns/op 1.12
BenchmarkSync/memory_sync_10000_test - B/op 854647 B/op 867228 B/op 0.99
BenchmarkSync/memory_sync_10000_test - allocs/op 27301 allocs/op 27780 allocs/op 0.98
BenchmarkTextEditing - ns/op 29405041376 ns/op 25876054639 ns/op 1.14
BenchmarkTextEditing - B/op 8456938592 B/op 8456962672 B/op 1.00
BenchmarkTextEditing - allocs/op 20614279 allocs/op 20614639 allocs/op 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.