This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfigure4_test.go
131 lines (100 loc) · 3.63 KB
/
figure4_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
// Copyright 2018 The godag Authors
// This file is part of the godag library.
//
// The godag library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The godag library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the godag library. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
."github.com/garyyu/go-dag/godag"
."github.com/garyyu/go-dag/utils"
"testing"
"bytes"
)
func chainFig4Initialize() map[string]*Block{
//initial an empty chain
chain := make(map[string]*Block)
//add blocks
ChainAddBlock("Genesis", []string{}, chain)
ChainAddBlock("B", []string{"Genesis"}, chain)
ChainAddBlock("C", []string{"Genesis"}, chain)
ChainAddBlock("D", []string{"Genesis"}, chain)
ChainAddBlock("E", []string{"Genesis"}, chain)
ChainAddBlock("F", []string{"B","C"}, chain)
ChainAddBlock("I", []string{"C","D"}, chain)
ChainAddBlock("H", []string{"E"}, chain)
ChainAddBlock("J", []string{"F","D"}, chain)
ChainAddBlock("L", []string{"F"}, chain)
ChainAddBlock("K", []string{"J","I","E"}, chain)
ChainAddBlock("N", []string{"D","H"}, chain)
ChainAddBlock("M", []string{"L","K"}, chain)
ChainAddBlock("O", []string{"K"}, chain)
ChainAddBlock("P", []string{"K"}, chain)
ChainAddBlock("Q", []string{"N"}, chain)
ChainAddBlock("R", []string{"O","P","N"}, chain)
ChainAddBlock("S", []string{"Q"}, chain)
ChainAddBlock("T", []string{"S"}, chain)
ChainAddBlock("U", []string{"T"}, chain)
tips := FindTips(chain)
tipsName := LTPQ(tips, true) // LTPQ is not relevant here, I just use it to get Tips name.
ChainAddBlock("Virtual", tipsName, chain)
return chain
}
// Tests Algorithm 1 Selection of a blue set, with the example on paper page 16 Fig.4
//
func TestFig4(t *testing.T) {
var actual bytes.Buffer
var expected string
fmt.Println("\n- BlockDAG Algorithms Simulation - Algorithm 1: Selection of a blue set. -")
fmt.Println("- The example on Fig.4. -")
chain := chainFig4Initialize()
fmt.Println("chainInitialize(): done. blocks=", len(chain)-1)
//CalcBlue(chain, 3, chain["Virtual"])
orderedList := Order(chain, 3)
// print the result of blue sets
ltpq := LTPQ(chain, true)
fmt.Print("blue set selection done. blue blocks = ")
nBlueBlocks := 0
actual.Reset()
for _, name := range ltpq {
block := chain[name]
if IsBlueBlock(block)==true {
if name=="Genesis" || name=="Virtual" {
actual.WriteString(fmt.Sprintf("(%s).",name[:1]))
}else {
actual.WriteString(name+".")
}
nBlueBlocks++
}
}
fmt.Println(actual.String(), " total blue:", nBlueBlocks)
expected = "(G).B.C.D.F.I.J.K.O.P.M.R.(V)."
if actual.String() != expected {
t.Errorf("blue selection test not matched. expected=%s", expected)
}
// print the result of ordered blocks of this chain
fmt.Print("ordered chain blocks = ")
actual.Reset()
for _, name := range orderedList {
if name=="Genesis" || name=="Virtual" {
actual.WriteString(fmt.Sprintf("(%s).",name[:1]))
} else {
actual.WriteString(name+".")
}
}
fmt.Println(actual.String())
expected = "(G).B.C.D.F.I.J.E.K.O.P.L.M.H.N.R.Q.S.T.U.(V)."
if actual.String() != expected {
t.Errorf("block ordering test not matched. expected=%s", expected)
}
}