// reverse []byte.
func reverse(b []byte) []byte {
- for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
- b[i], b[j] = b[j], b[i]
+ r := make([]byte, len(b))
+ copy(r, b)
+ for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
+ r[i], r[j] = r[j], r[i]
}
-
- return b
+ return r
}
import (
"bytes"
"math/big"
+ "reflect"
"testing"
"github.com/holiman/uint256"
}
}
}
+
+func Test_reverse(t *testing.T) {
+ type args struct {
+ b []byte
+ }
+ type wants struct {
+ origin []byte
+ want []byte
+ }
+ tests := []struct {
+ name string
+ args args
+ wants wants
+ }{
+ {
+ name: "test reverse",
+ args: args{
+ b: []byte{0x00, 0x00, 0x00, 0x00, 0x01},
+ },
+ wants: wants{
+ origin: []byte{0x00, 0x00, 0x00, 0x00, 0x01},
+ want: []byte{0x01, 0x00, 0x00, 0x00, 0x00},
+ },
+ },
+ {
+ name: "test reverse 1",
+ args: args{
+ b: []byte{0x01, 0x02, 0x20, 0x03, 0x01},
+ },
+ wants: wants{
+ origin: []byte{0x01, 0x02, 0x20, 0x03, 0x01},
+ want: []byte{0x01, 0x03, 0x20, 0x02, 0x01},
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := reverse(tt.args.b); !reflect.DeepEqual(got, tt.wants.want) {
+ t.Errorf("reverse() = %v, want %v", got, tt.wants.want)
+ }
+ if !reflect.DeepEqual(tt.args.b, tt.wants.origin) {
+ t.Errorf("after reverse args = %v, origin %v", tt.args.b, tt.wants.origin)
+ }
+ })
+ }
+}
import (
"crypto/ed25519"
"encoding/hex"
+ "reflect"
"testing"
"github.com/bytom/bytom/errors"
if err != nil {
t.Fatal(err)
}
-
+ originProgram := make([]byte, len(program))
+ copy(originProgram, program)
gotHeight := GetIssuanceProgramRestrictHeight(program)
if gotHeight != test.wantHeight {
t.Errorf("TestGetIssuanceProgramRestrictHeight #%d failed: got %d want %d", i, gotHeight, test.wantHeight)
+ return
+ }
+
+ if !reflect.DeepEqual(originProgram, program) {
+ t.Errorf("TestGetIssuanceProgramRestrictHeight #%d failed: after got %v before %v", i, program, originProgram)
}
}
}