Debian Codegraph @

queue_test.go

raw
1 // mgo - MongoDB driver for Go
2 //
3 // Copyright (c) 2010-2012 - Gustavo Niemeyer <[email protected]>
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 // 1. Redistributions of source code must retain the above copyright notice, this
11 // list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright notice,
13 // this list of conditions and the following disclaimer in the documentation
14 // and/or other materials provided with the distribution.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 package mgo
28
29 import (
30 . "gopkg.in/check.v1"
31 )
32
33 type QS struct{}
34
35 var _ = Suite(&QS{})
36
37 func (s *QS) TestSequentialGrowth(c *C) {
38 q := queue{}
39 n := 2048
40 for i := 0; i != n; i++ {
41 q.Push(i)
42 }
43 for i := 0; i != n; i++ {
44 c.Assert(q.Pop(), Equals, i)
45 }
46 }
47
48 var queueTestLists = [][]int{
49 // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
50 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
51
52 // {8, 9, 10, 11, ... 2, 3, 4, 5, 6, 7}
53 {0, 1, 2, 3, 4, 5, 6, 7, -1, -1, 8, 9, 10, 11},
54
55 // {8, 9, 10, 11, ... 2, 3, 4, 5, 6, 7}
56 {0, 1, 2, 3, -1, -1, 4, 5, 6, 7, 8, 9, 10, 11},
57
58 // {0, 1, 2, 3, 4, 5, 6, 7, 8}
59 {0, 1, 2, 3, 4, 5, 6, 7, 8,
60 -1, -1, -1, -1, -1, -1, -1, -1, -1,
61 0, 1, 2, 3, 4, 5, 6, 7, 8},
62 }
63
64 func (s *QS) TestQueueTestLists(c *C) {
65 test := []int{}
66 testi := 0
67 reset := func() {
68 test = test[0:0]
69 testi = 0
70 }
71 push := func(i int) {
72 test = append(test, i)
73 }
74 pop := func() (i int) {
75 if testi == len(test) {
76 return -1
77 }
78 i = test[testi]
79 testi++
80 return
81 }
82
83 for _, list := range queueTestLists {
84 reset()
85 q := queue{}
86 for _, n := range list {
87 if n == -1 {
88 c.Assert(q.Pop(), Equals, pop(), Commentf("With list %#v", list))
89 } else {
90 q.Push(n)
91 push(n)
92 }
93 }
94
95 for n := pop(); n != -1; n = pop() {
96 c.Assert(q.Pop(), Equals, n, Commentf("With list %#v", list))
97 }
98
99 c.Assert(q.Pop(), Equals, nil, Commentf("With list %#v", list))
100 }
101 }