1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 func fromMyNode(node *MyNode) *LFNode {
20 return (*LFNode)(unsafe.Pointer(node))
23 func toMyNode(node *LFNode) *MyNode {
24 return (*MyNode)(unsafe.Pointer(node))
27 func TestLFStack(t *testing.T) {
29 // Need to keep additional referenfces to nodes, the stack is not all that type-safe.
32 // Check the stack is initially empty.
33 if LFStackPop(stack) != nil {
34 t.Fatalf("stack is not empty")
38 node := &MyNode{data: 42}
39 nodes = append(nodes, node)
40 LFStackPush(stack, fromMyNode(node))
43 node = &MyNode{data: 43}
44 nodes = append(nodes, node)
45 LFStackPush(stack, fromMyNode(node))
48 node = toMyNode(LFStackPop(stack))
50 t.Fatalf("stack is empty")
57 node = toMyNode(LFStackPop(stack))
59 t.Fatalf("stack is empty")
65 // Check the stack is empty again.
66 if LFStackPop(stack) != nil {
67 t.Fatalf("stack is not empty")
70 t.Fatalf("stack is not empty")
74 func TestLFStackStress(t *testing.T) {
76 P := 4 * GOMAXPROCS(-1)
82 stacks := [2]*uint64{new(uint64), new(uint64)}
83 // Need to keep additional referenfces to nodes, the stack is not all that type-safe.
85 // Push K elements randomly onto the stacks.
87 for i := 0; i < K; i++ {
89 node := &MyNode{data: i}
90 nodes = append(nodes, node)
91 LFStackPush(stacks[i%2], fromMyNode(node))
93 c := make(chan bool, P)
94 for p := 0; p < P; p++ {
96 r := rand.New(rand.NewSource(rand.Int63()))
97 // Pop a node from a random stack, then push it onto a random stack.
98 for i := 0; i < N; i++ {
99 node := toMyNode(LFStackPop(stacks[r.Intn(2)]))
101 LFStackPush(stacks[r.Intn(2)], fromMyNode(node))
107 for i := 0; i < P; i++ {
110 // Pop all elements from both stacks, and verify that nothing lost.
113 for i := 0; i < 2; i++ {
115 node := toMyNode(LFStackPop(stacks[i]))
125 t.Fatalf("Wrong number of nodes %d/%d", cnt, K)
128 t.Fatalf("Wrong sum %d/%d", sum2, sum)