-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag_test.go
160 lines (125 loc) · 3.24 KB
/
dag_test.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package dag
import (
"container/list"
"fmt"
"log"
"math/rand"
"sync"
"testing"
"time"
)
func TestNewDAG(t *testing.T) {
MAX := 100000000
dagNew := NewDAG()
root := NewVertex("genesis", "000000", "the genesis block")
dagNew.AddVertex(root)
for i := 0; i < MAX; i++ {
v := NewVertex("TX", fmt.Sprintf("%d", i), "transaction")
dagNew.AddVertex(v)
}
for i := 0; i < MAX; i++ {
from := rand.Intn(MAX)
var to int
for {
to = rand.Intn(MAX)
if to != from {
break
}
}
t.Log(from, to)
dagNew.AddEdge(
NewVertex("TX", fmt.Sprintf("%d", from), "transaction"), // {Hash: fmt.Sprintf("%d", from), Type: "TX", Value: "transaction"},
NewVertex("TX", fmt.Sprintf("%d", to), "transaction"),
)
}
time.Sleep(10 * time.Second)
}
func TestDAG_IsEqual(t *testing.T) {
MAX := 50
dagNew := NewDAG()
root := NewVertex("genesis", "000000", "the genesis block")
dagNew.AddVertex(root)
for i := 0; i < MAX; i++ {
v := NewVertex("TX", fmt.Sprintf("%d", i), "transaction")
dagNew.AddVertex(v)
}
for from := 0; from < MAX-1; from++ {
for to := 1; to < MAX; to++ {
dagNew.AddEdge(
NewVertex("TX", fmt.Sprintf("%d", from), "transaction"), // {Hash: fmt.Sprintf("%d", from), Type: "TX", Value: "transaction"},
NewVertex("TX", fmt.Sprintf("%d", to), "transaction"),
)
}
}
dagNew.AddEdge(
NewVertex("TX", fmt.Sprintf("%d", 10), "transaction"), // {Hash: fmt.Sprintf("%d", from), Type: "TX", Value: "transaction"},
NewVertex("TX", fmt.Sprintf("%d", 40), "transaction"),
)
}
func TestNewSortedVertexes(t *testing.T) {
MAX := 5
dagNew := NewDAG()
root := NewVertex("genesis", "000000", "the genesis block")
dagNew.AddVertex(root)
for i := 0; i < MAX; i++ {
if i == 0 {
continue
}
v := NewVertex("TX", fmt.Sprintf("%d", i), "transaction")
dagNew.AddVertex(v)
}
for from := 0; from < MAX-1; from++ {
fromHash := fmt.Sprintf("%d", from)
if from == 0 {
fromHash = "000000"
}
for to := 1; to < MAX; to++ {
dagNew.AddEdge(
NewVertex("TX", fromHash, "transaction"), // {Hash: fmt.Sprintf("%d", from), Type: "TX", Value: "transaction"},
NewVertex("TX", fmt.Sprintf("%d", to), "transaction"),
)
}
}
t.Log(dagNew.AddEdge(
NewVertex("TX", fmt.Sprintf("%d", 2), "transaction"), // {Hash: fmt.Sprintf("%d", from), Type: "TX", Value: "transaction"},
NewVertex("TX", fmt.Sprintf("%d", 4), "transaction"),
))
t.Log(dagNew.Print("000000"))
}
func Test_MapListPointer(t *testing.T) {
type MyMap struct {
Items *sync.Map
}
type MyList struct {
Items *list.List
}
type Item struct {
Name string
Type string
}
myMap := &MyMap{
Items: &sync.Map{},
}
myList := &MyList{
Items: list.New(),
}
original := &Item{Type: "TEST"}
myMap.Items.Store("test", original)
myList.Items.PushBack(original)
log.Println(fmt.Sprintf("%p", original))
v, ok := myMap.Items.Load("test")
if ok {
log.Println(fmt.Sprintf("%p", v))
log.Println(fmt.Sprintf("%p", v.(*Item)))
}
myMap.Items.Range(func(key, v interface{}) bool {
log.Println(key)
log.Println(fmt.Sprintf("%p", v))
log.Println(fmt.Sprintf("%p", v.(*Item)))
return true
})
for e := myList.Items.Front(); e != nil; e = e.Next() {
log.Println(fmt.Sprintf("%p", e.Value))
log.Println(fmt.Sprintf("%p", e.Value.(*Item)))
}
}