forked from hyperledger-labs/go-perun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransaction_test.go
90 lines (79 loc) · 2.68 KB
/
transaction_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
// Copyright 2025 - See NOTICE file for copyright holders.
//
// 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 channel_test
import (
"testing"
"github.com/stretchr/testify/require"
"perun.network/go-perun/channel"
"perun.network/go-perun/channel/test"
peruniotest "perun.network/go-perun/wire/perunio/test"
pkgtest "polycry.pt/poly-go/test"
)
func TestTransactionSerialization(t *testing.T) {
rng := pkgtest.Prng(t)
lengths := []int{2, 5, 10, 40}
tests := [][]bool{}
for _, l := range lengths {
tests = append(tests,
newUniformBoolSlice(l, true), newUniformBoolSlice(l, false),
newStripedBoolSlice(l, true), newStripedBoolSlice(l, false))
for i := 0; i < l; i++ {
tests = append(tests, newAlmostUniformBoolSlice(i, l, true))
tests = append(tests, newAlmostUniformBoolSlice(i, l, false))
}
}
for _, tt := range tests {
tx := test.NewRandomTransaction(rng, tt)
peruniotest.GenericSerializerTest(t, tx)
}
tx := new(channel.Transaction)
peruniotest.GenericSerializerTest(t, tx)
}
// newUniformBoolSlice generates a slice long size with all the elements set to choice.
func newUniformBoolSlice(size int, choice bool) []bool {
uniform := make([]bool, size)
for i := range uniform {
uniform[i] = choice
}
return uniform
}
// newAlmostUniformBoolSlice creates []bool which has choice at indexChosen and all the others indexes are !choice.
func newAlmostUniformBoolSlice(indexChosen int, size int, choice bool) []bool {
almostUniform := make([]bool, size)
for i := range almostUniform {
if i == indexChosen {
almostUniform[i] = choice
} else {
almostUniform[i] = !choice
}
}
return almostUniform
}
// newStripedBoolSlice creates an array []bool of length == size in which all the even indexes are set to choice.
func newStripedBoolSlice(size int, choice bool) []bool {
striped := make([]bool, size)
for i := range striped {
striped[i] = choice
choice = !choice
}
return striped
}
func TestTransactionClone(t *testing.T) {
rng := pkgtest.Prng(t)
size := int(rng.Int31n(5)) + 2
testmask := newUniformBoolSlice(size, true)
tx := *test.NewRandomTransaction(rng, testmask)
clone := tx.Clone()
require.Equalf(t, tx, clone, "Clone() = %v, want %v", clone, tx)
}