aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/gopkg.in/karalabe/cookiejar.v2/collections/prque/sstack_test.go
blob: bcb5b830bdbff320deb763a45b63b5d6b0e4dfe3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// CookieJar - A contestant's algorithm toolbox
// Copyright (c) 2013 Peter Szilagyi. All rights reserved.
//
// CookieJar is dual licensed: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// The toolbox is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
// more details.
//
// Alternatively, the CookieJar toolbox may be used in accordance with the terms
// and conditions contained in a signed written agreement between you and the
// author(s).

package prque

import (
    "math/rand"
    "sort"
    "testing"
)

func TestSstack(t *testing.T) {
    // Create some initial data
    size := 16 * blockSize
    data := make([]*item, size)
    for i := 0; i < size; i++ {
        data[i] = &item{rand.Int(), rand.Float32()}
    }
    stack := newSstack()
    for rep := 0; rep < 2; rep++ {
        // Push all the data into the stack, pop out every second
        secs := []*item{}
        for i := 0; i < size; i++ {
            stack.Push(data[i])
            if i%2 == 0 {
                secs = append(secs, stack.Pop().(*item))
            }
        }
        rest := []*item{}
        for stack.Len() > 0 {
            rest = append(rest, stack.Pop().(*item))
        }
        // Make sure the contents of the resulting slices are ok
        for i := 0; i < size; i++ {
            if i%2 == 0 && data[i] != secs[i/2] {
                t.Errorf("push/pop mismatch: have %v, want %v.", secs[i/2], data[i])
            }
            if i%2 == 1 && data[i] != rest[len(rest)-i/2-1] {
                t.Errorf("push/pop mismatch: have %v, want %v.", rest[len(rest)-i/2-1], data[i])
            }
        }
    }
}

func TestSstackSort(t *testing.T) {
    // Create some initial data
    size := 16 * blockSize
    data := make([]*item, size)
    for i := 0; i < size; i++ {
        data[i] = &item{rand.Int(), float32(i)}
    }
    // Push all the data into the stack
    stack := newSstack()
    for _, val := range data {
        stack.Push(val)
    }
    // Sort and pop the stack contents (should reverse the order)
    sort.Sort(stack)
    for _, val := range data {
        out := stack.Pop()
        if out != val {
            t.Errorf("push/pop mismatch after sort: have %v, want %v.", out, val)
        }
    }
}

func TestSstackReset(t *testing.T) {
    // Create some initial data
    size := 16 * blockSize
    data := make([]*item, size)
    for i := 0; i < size; i++ {
        data[i] = &item{rand.Int(), rand.Float32()}
    }
    stack := newSstack()
    for rep := 0; rep < 2; rep++ {
        // Push all the data into the stack, pop out every second
        secs := []*item{}
        for i := 0; i < size; i++ {
            stack.Push(data[i])
            if i%2 == 0 {
                secs = append(secs, stack.Pop().(*item))
            }
        }
        // Reset and verify both pulled and stack contents
        stack.Reset()
        if stack.Len() != 0 {
            t.Errorf("stack not empty after reset: %v", stack)
        }
        for i := 0; i < size; i++ {
            if i%2 == 0 && data[i] != secs[i/2] {
                t.Errorf("push/pop mismatch: have %v, want %v.", secs[i/2], data[i])
            }
        }
    }
}