-
Notifications
You must be signed in to change notification settings - Fork 15
/
rune_test.go
278 lines (230 loc) · 8.6 KB
/
rune_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2024 The BxELab studyzy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package runestone
import (
"math"
"strings"
"testing"
"github.com/btcsuite/btcd/wire"
"github.com/stretchr/testify/assert"
"lukechampine.com/uint128"
)
func TestRoundTrip(t *testing.T) {
testCase := func(n uint128.Uint128, s string) {
r := Rune{Value: n}
if r.String() != s {
t.Errorf("Rune(%v).String() = %v, want %v", n, r.String(), s)
}
parsedRune, err := RuneFromString(s)
if err != nil {
t.Errorf("RuneFromString(%v) returned error: %v", s, err)
}
if *parsedRune != r {
t.Errorf("RuneFromString(%v) = %v, want %v", s, parsedRune, r)
}
}
testCase(uint128.From64(0), "A")
testCase(uint128.From64(1), "B")
testCase(uint128.From64(2), "C")
testCase(uint128.From64(3), "D")
testCase(uint128.From64(4), "E")
testCase(uint128.From64(5), "F")
testCase(uint128.From64(6), "G")
testCase(uint128.From64(7), "H")
testCase(uint128.From64(8), "I")
testCase(uint128.From64(9), "J")
testCase(uint128.From64(10), "K")
testCase(uint128.From64(11), "L")
testCase(uint128.From64(12), "M")
testCase(uint128.From64(13), "N")
testCase(uint128.From64(14), "O")
testCase(uint128.From64(15), "P")
testCase(uint128.From64(16), "Q")
testCase(uint128.From64(17), "R")
testCase(uint128.From64(18), "S")
testCase(uint128.From64(19), "T")
testCase(uint128.From64(20), "U")
testCase(uint128.From64(21), "V")
testCase(uint128.From64(22), "W")
testCase(uint128.From64(23), "X")
testCase(uint128.From64(24), "Y")
testCase(uint128.From64(25), "Z")
testCase(uint128.From64(26), "AA")
testCase(uint128.From64(27), "AB")
testCase(uint128.From64(51), "AZ")
testCase(uint128.From64(52), "BA")
testCase(uint128.Max.Sub64(2), "BCGDENLQRQWDSLRUGSNLBTMFIJAT")
testCase(uint128.Max.Sub64(1), "BCGDENLQRQWDSLRUGSNLBTMFIJAU")
testCase(uint128.Max, "BCGDENLQRQWDSLRUGSNLBTMFIJAV")
}
func TestFromStrError(t *testing.T) {
_, err := RuneFromString("BCGDENLQRQWDSLRUGSNLBTMFIJAW")
assert.Error(t, err)
_, err = RuneFromString("BCGDENLQRQWDSLRUGSNLBTMFIJAVX")
assert.Error(t, err)
_, err = RuneFromString("x")
assert.Error(t, err)
}
func TestMainnetMinimumAtHeight(t *testing.T) {
testCase := func(height uint32, expected string) {
t.Helper()
r := MinimumAtHeight(wire.MainNet, uint64(height))
if r.String() != expected {
t.Errorf("minimumAtHeight(%d) = %s, want %s", height, r.String(), expected)
}
}
start := SUBSIDY_HALVING_INTERVAL * 4
end := start + SUBSIDY_HALVING_INTERVAL
interval := SUBSIDY_HALVING_INTERVAL / 12
testCase(0, "AAAAAAAAAAAAA")
testCase(start/2, "AAAAAAAAAAAAA")
testCase(start, "ZZYZXBRKWXVA")
testCase(start+1, "ZZXZUDIVTVQA")
testCase(end-1, "A")
testCase(end, "A")
testCase(end+1, "A")
testCase(^uint32(0), "A")
testCase(start+interval*0-1, "AAAAAAAAAAAAA")
testCase(start+interval*0+0, "ZZYZXBRKWXVA")
testCase(start+interval*0+1, "ZZXZUDIVTVQA")
testCase(start+interval*1-1, "AAAAAAAAAAAA")
testCase(start+interval*1+0, "ZZYZXBRKWXV")
testCase(start+interval*1+1, "ZZXZUDIVTVQ")
testCase(start+interval*2-1, "AAAAAAAAAAA")
testCase(start+interval*2+0, "ZZYZXBRKWY")
testCase(start+interval*2+1, "ZZXZUDIVTW")
testCase(start+interval*3-1, "AAAAAAAAAA")
testCase(start+interval*3+0, "ZZYZXBRKX")
testCase(start+interval*3+1, "ZZXZUDIVU")
testCase(start+interval*4-1, "AAAAAAAAA")
testCase(start+interval*4+0, "ZZYZXBRL")
testCase(start+interval*4+1, "ZZXZUDIW")
testCase(start+interval*5-1, "AAAAAAAA")
testCase(start+interval*5+0, "ZZYZXBS")
testCase(start+interval*5+1, "ZZXZUDJ")
testCase(start+interval*6-1, "AAAAAAA")
testCase(start+interval*6+0, "ZZYZXC")
testCase(start+interval*6+1, "ZZXZUE")
testCase(start+interval*7-1, "AAAAAA")
testCase(start+interval*7+0, "ZZYZY")
testCase(start+interval*7+1, "ZZXZV")
testCase(start+interval*8-1, "AAAAA")
testCase(start+interval*8+0, "ZZZA")
testCase(start+interval*8+1, "ZZYA")
testCase(start+interval*9-1, "AAAA")
testCase(start+interval*9+0, "ZZZ")
testCase(start+interval*9+1, "ZZY")
testCase(start+interval*10-2, "AAC")
testCase(start+interval*10-1, "AAA")
testCase(start+interval*10+0, "AAA")
testCase(start+interval*10+1, "AAA")
testCase(start+interval*10+interval/2, "NA")
testCase(start+interval*11-2, "AB")
testCase(start+interval*11-1, "AA")
testCase(start+interval*11+0, "AA")
testCase(start+interval*11+1, "AA")
testCase(start+interval*11+interval/2, "N")
testCase(start+interval*12-2, "B")
testCase(start+interval*12-1, "A")
testCase(start+interval*12+0, "A")
testCase(start+interval*12+1, "A")
}
func TestMinimumAtHeight(t *testing.T) {
testCase := func(network wire.BitcoinNet, height uint32, minimum string) {
t.Helper()
result := MinimumAtHeight(network, uint64(height)).String()
if result != minimum {
t.Errorf("MinimumAtHeight(%v, %v) = %v, want %v", network, height, result, minimum)
}
}
testCase(wire.TestNet3, 0, "AAAAAAAAAAAAA")
testCase(wire.TestNet3, SUBSIDY_HALVING_INTERVAL*12-1, "AAAAAAAAAAAAA")
testCase(wire.TestNet3, SUBSIDY_HALVING_INTERVAL*12, "ZZYZXBRKWXVA")
testCase(wire.TestNet3, SUBSIDY_HALVING_INTERVAL*12+1, "ZZXZUDIVTVQA")
// Assuming Signet and Regtest are defined as custom Bitcoin networks in your code
testCase(wire.SimNet, 0, "ZZYZXBRKWXVA")
testCase(wire.SimNet, 1, "ZZXZUDIVTVQA")
testCase(wire.TestNet, 0, "ZZYZXBRKWXVA")
testCase(wire.TestNet, 1, "ZZXZUDIVTVQA")
}
//TODO:serde
func TestReserved(t *testing.T) {
assert := assert.New(t)
// Assuming you have defined Rune::RESERVED as a constant
const A = "AAAAAAAAAAAAAAAAAAAAAAAAAAA"
r, err := RuneFromString(A)
assert.NoError(err)
assertJsonEqual(t, r, Rune{RESERVED})
assert.Equal(Reserved(0, 0), Rune{RESERVED}, "Rune.Reserved(0, 0) should return RESERVED")
assert.Equal(Reserved(0, 1), Rune{RESERVED.Add64(1)}, "Rune.Reserved(0, 1) should return RESERVED + 1")
assert.Equal(Reserved(1, 0), Rune{RESERVED.Add64(1 << 32)}, "Rune.Reserved(1, 0) should return RESERVED + (1 << 32)")
assert.Equal(Reserved(1, 1), Rune{RESERVED.Add64(1 << 32).Add64(1)}, "Rune.Reserved(1, 1) should return RESERVED + (1 << 32) + 1")
// Rune::reserved(u64::MAX, u32::MAX),
r1 := Reserved(math.MaxUint64, math.MaxUint32)
// Rune(Rune::RESERVED + (u128::from(u64::MAX) << 32 | u128::from(u32::MAX))),
r2 := Rune{RESERVED.Add(uint128.From64(math.MaxUint64).Lsh(32).Or(uint128.From64(math.MaxUint32)))}
assertJsonEqual(t, r1, r2)
}
func TestIsReserved(t *testing.T) {
assert := assert.New(t)
r, _ := RuneFromString("A")
assert.Equal(r.IsReserved(), false, "Rune 'A' should not be reserved")
r, _ = RuneFromString("ZZZZZZZZZZZZZZZZZZZZZZZZZZ")
assert.Equal(r.IsReserved(), false, "Rune 'ZZZZZZZZZZZZZZZZZZZZZZZZZZ' should not be reserved")
r, _ = RuneFromString("AAAAAAAAAAAAAAAAAAAAAAAAAAA")
assert.Equal(r.IsReserved(), true, "Rune 'AAAAAAAAAAAAAAAAAAAAAAAAAAA' should be reserved")
r, _ = RuneFromString("AAAAAAAAAAAAAAAAAAAAAAAAAAB")
assert.Equal(r.IsReserved(), true, "Rune 'AAAAAAAAAAAAAAAAAAAAAAAAAAB' should be reserved")
r, _ = RuneFromString("BCGDENLQRQWDSLRUGSNLBTMFIJAV")
assert.Equal(r.IsReserved(), true, "Rune 'BCGDENLQRQWDSLRUGSNLBTMFIJAV' should be reserved")
}
func TestSteps(t *testing.T) {
assert := assert.New(t)
// Assuming you have defined Rune::STEPS as a constant array
for i := 0; ; i++ {
repeatedA := strings.Repeat("A", i+1)
r, err := RuneFromString(repeatedA)
if err != nil {
assert.Equal(len(STEPS), i, "Rune::STEPS length should be equal to i")
break
} else {
assertJsonEqual(t, Rune{STEPS[i]}, r)
}
}
}
func TestCommitment(t *testing.T) {
assert := assert.New(t)
// Anonymous function testCase
testCase := func(r Rune, expected []byte) {
assert.Equal(r.Commitment(), expected, "Rune commitment should be equal to expected")
}
r := Rune{uint128.From64(0)}
testCase(r, []byte{})
r = Rune{uint128.From64(1)}
testCase(r, []byte{1})
r = Rune{uint128.From64(255)}
testCase(r, []byte{255})
r = Rune{uint128.From64(256)}
testCase(r, []byte{0, 1})
r = Rune{uint128.From64(65535)}
testCase(r, []byte{255, 255})
r = Rune{uint128.From64(65536)}
testCase(r, []byte{0, 0, 1})
r = Rune{uint128.Max}
expected := make([]byte, 16)
for i := range expected {
expected[i] = 255
}
testCase(r, expected)
}