From b91267330eede6c105d29317b14598ba0cb23f72 Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Sun, 28 Aug 2016 11:03:18 -0400 Subject: [PATCH] add tests and benchmark for appendWithoutDuplicates optimization --- .gitignore | 11 ++++++++++- bench_expand_test.go | 24 ++++++++++++++++++++++++ expand_test.go | 18 ++++++++++++++++++ traversal_test.go | 10 ++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index bf2ff73..970381c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,16 @@ +# editor temporary files *.sublime-* .DS_Store *.swp #*.*# tags -goquery.test + +# direnv config +.env* + +# test binaries +*.test + +# coverage and profilte outputs +*.out diff --git a/bench_expand_test.go b/bench_expand_test.go index d0eb2b3..61f1947 100644 --- a/bench_expand_test.go +++ b/bench_expand_test.go @@ -61,6 +61,30 @@ func BenchmarkAddNodes(b *testing.B) { } } +func BenchmarkAddNodesBig(b *testing.B) { + var n int + + doc := DocW() + sel := doc.Find("li") + // make nodes > 1000 + nodes := sel.Nodes + nodes = append(nodes, nodes...) + nodes = append(nodes, nodes...) + sel = doc.Find("xyz") + b.ResetTimer() + + for i := 0; i < b.N; i++ { + if n == 0 { + n = sel.AddNodes(nodes...).Length() + } else { + sel.AddNodes(nodes...) + } + } + if n != 373 { + b.Fatalf("want 373, got %d", n) + } +} + func BenchmarkAndSelf(b *testing.B) { var n int diff --git a/expand_test.go b/expand_test.go index e53b83f..4557025 100644 --- a/expand_test.go +++ b/expand_test.go @@ -66,6 +66,24 @@ func TestAddNodesRollback(t *testing.T) { assertEqual(t, sel, sel2) } +func TestAddNodesBig(t *testing.T) { + doc := DocW() + sel := doc.Find("li") + assertLength(t, sel.Nodes, 373) + sel2 := doc.Find("xyz") + assertLength(t, sel2.Nodes, 0) + + nodes := sel.Nodes + sel2 = sel2.AddNodes(nodes...) + assertLength(t, sel2.Nodes, 373) + nodes2 := append(nodes, nodes...) + sel2 = sel2.End().AddNodes(nodes2...) + assertLength(t, sel2.Nodes, 373) + nodes3 := append(nodes2, nodes...) + sel2 = sel2.End().AddNodes(nodes3...) + assertLength(t, sel2.Nodes, 373) +} + func TestAndSelf(t *testing.T) { sel := Doc().Find(".span12").Last().AndSelf() assertLength(t, sel.Nodes, 2) diff --git a/traversal_test.go b/traversal_test.go index 856e71d..04383a4 100644 --- a/traversal_test.go +++ b/traversal_test.go @@ -26,6 +26,16 @@ func TestFindInvalid(t *testing.T) { assertLength(t, sel.Nodes, 0) } +func TestFindBig(t *testing.T) { + doc := DocW() + sel := doc.Find("li") + assertLength(t, sel.Nodes, 373) + sel2 := doc.Find("span") + assertLength(t, sel2.Nodes, 448) + sel3 := sel.FindSelection(sel2) + assertLength(t, sel3.Nodes, 248) +} + func TestChainedFind(t *testing.T) { sel := Doc().Find("div.hero-unit").Find(".row-fluid") assertLength(t, sel.Nodes, 4)