Skip to content

Commit

Permalink
#1 3.1 Three in One: test for Peek method
Browse files Browse the repository at this point in the history
  • Loading branch information
krlv committed Mar 26, 2019
1 parent 5afe876 commit d5f8e7d
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion ch03/01_threesome_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,52 @@ func TestNthPush(t *testing.T) {
}

func TestNthPeek(t *testing.T) {
// TBD
n, m := 3, 2
nth := new(NthStack)

nth.n = n
nth.storage = make([]int, n*(m+1))
nth.start = make([]int, n)
nth.len = make([]int, n)
nth.cap = make([]int, n)

values := [][]int{
{10, 1},
{20, 2},
{30, 3},
}

for i := 0; i < n; i++ {
start := i * (m + 1)
stop := start + m

nth.start[i] = start
nth.len[i] = 0
nth.cap[i] = 1

for j := start; j < stop; j++ {
nth.storage[j] = values[i][j-start]
nth.len[i]++
nth.cap[i]++
}
}

for i := 0; i < nth.n; i++ {
for j := 0; nth.len[i] > 0; j++ {
val := values[i][j]

num, err := nth.Peek(i)
if err != nil {
t.Errorf("%dth stack, %dth Peek() expected to be valid, got error %s\n", (i + 1), (j + 1), err)
}

if num != val {
t.Errorf("%dth stack, %dth Peek() expected to be %d, got %d\n", (i + 1), (j + 1), val, num)
}

nth.len[i]--
}
}
}

func TestNthIsEmpty(t *testing.T) {
Expand Down

0 comments on commit d5f8e7d

Please sign in to comment.