Skip to content

Commit

Permalink
v2: add several new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
edwingeng committed Aug 26, 2022
1 parent dbc607e commit 5d4cdd2
Show file tree
Hide file tree
Showing 4 changed files with 1,211 additions and 202 deletions.
35 changes: 35 additions & 0 deletions coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

[[ "$TRACE" ]] && set -x
pushd `dirname "$0"` > /dev/null
trap __EXIT EXIT

colorful=false
tput setaf 7 > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
colorful=true
fi

function __EXIT() {
popd > /dev/null
}

function printError() {
$colorful && tput setaf 1
>&2 echo "Error: $@"
$colorful && tput setaf 7
}

function printImportantMessage() {
$colorful && tput setaf 3
>&2 echo "$@"
$colorful && tput setaf 7
}

function printUsage() {
$colorful && tput setaf 3
>&2 echo "$@"
$colorful && tput setaf 7
}

go test -cover -coverprofile=c.out -v "$@" && go tool cover -html=c.out
59 changes: 58 additions & 1 deletion deque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ func TestDeque_Dump(t *testing.T) {
}
}

func rec() {
_ = recover()
}

func TestDeque_Replace(t *testing.T) {
for _, n := range []int{1, 100, chunkSize, chunkSize + 1, chunkSize * 2, 1000} {
a := make([]int, n)
Expand All @@ -524,7 +528,29 @@ func TestDeque_Replace(t *testing.T) {
a[i] = v
dq.PushBack(v)
}
for i := 0; i < 100; i++ {

func() {
defer rec()
dq.Peek(-1)
t.Fatal("impossible")
}()
func() {
defer rec()
dq.Peek(n + 10)
t.Fatal("impossible")
}()
func() {
defer rec()
dq.Replace(-1, math.MaxInt)
t.Fatal("impossible")
}()
func() {
defer rec()
dq.Replace(n+10, math.MaxInt)
t.Fatal("impossible")
}()

for i := 0; i < 2000; i++ {
idx := rand.Intn(n)
if dq.Peek(idx).(int) != a[idx] {
t.Fatal("dq.Peek(idx).(int) != a[idx]")
Expand All @@ -543,3 +569,34 @@ func TestDeque_Replace(t *testing.T) {
}
}
}

func TestDeque_Range(t *testing.T) {
dq := NewDeque()
dq.Range(func(i int, v Elem) bool {
panic("impossible")
})

for i := 0; i < 10; i++ {
dq.PushFront(1)
}
for i := 0; i < 10; i++ {
dq.PushBack(i)
}

if len(dq.(*deque).chunks) != 2 {
t.Fatal(`len(dq.(*deque).chunks) != 2`)
}

var count int
dq.Range(func(i int, v Elem) bool {
if i < 15 {
count++
return true
} else {
return false
}
})
if count != 15 {
t.Fatal(`count != 15`, count)
}
}
Loading

0 comments on commit 5d4cdd2

Please sign in to comment.