forked from joshlf/gopack
/
util_test.go
92 lines (81 loc) · 2.53 KB
/
util_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
// Copyright 2014 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gopack
import (
"fmt"
"math/rand"
"reflect"
"unsafe"
)
func randUint64() uint64 {
return uint64(rand.Uint32()) | (uint64(rand.Uint32()) << 32)
}
func randUint64Bits(bits uint8) uint64 {
return randUint64() >> (64 - bits)
}
func randInt64() int64 {
u := randUint64()
return *(*int64)(unsafe.Pointer(&u))
}
func randInt64Bits(bits uint8) int64 {
u := randUint64Bits(bits)
return (*(*int64)(unsafe.Pointer(&u)) << (64 - bits)) >> (64 - bits)
}
func randBool() bool {
return rand.Int()%2 == 0
}
func randWidthLSBPair() (uint8, uint8) {
width := uint8(1 + (rand.Uint32() & 0x3F)) // Restrict to range [1, 64]
lsb := uint8(rand.Uint32() & 0x3F) // Restrict to range [0, 63]
for width+lsb > 64 {
width = uint8(1 + (rand.Uint32() & 0x3F)) // Restrict to range [1, 64]
lsb = uint8(rand.Uint32() & 0x3F) // Restrict to range [0, 63]
}
return width, lsb
}
func nOnes(n int) uint64 {
var u uint64
for i := 0; i < n; i++ {
u = (u << 1) | 1
}
return u
}
// func randInstance(typ reflect.Type) reflect.Value {
// val := reflect.New(typ).Elem()
// for i := 0; i < typ.NumField(); i++ {
// switch typ.Field(i).Type.Kind() {
// case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// n, _ := getFieldWidth(typ.Field(i))
// val.Field(i).SetInt(randInt64Bits(uint8(n)))
// case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// n, _ := getFieldWidth(typ.Field(i))
// val.Field(i).SetUint(randUint64Bits(uint8(n)))
// case reflect.Bool:
// val.Field(i).SetBool(randBool())
// default:
// panic(fmt.Sprint("Cannot generate type:", typ.Field(i).Type))
// }
// }
// return val
// }
func randInstance(typ reflect.Type) reflect.Value {
val := reflect.New(typ).Elem()
for i := 0; i < typ.NumField(); i++ {
switch typ.Field(i).Type.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, _ := getFieldWidth(typ.Field(i))
val.Field(i).SetInt(randInt64Bits(uint8(n)))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n, _ := getFieldWidth(typ.Field(i))
val.Field(i).SetUint(randUint64Bits(uint8(n)))
case reflect.Bool:
val.Field(i).SetBool(randBool())
case reflect.Struct:
val.Field(i).Set(randInstance(typ.Field(i).Type))
default:
panic(fmt.Sprint("Cannot generate type:", typ.Field(i).Type))
}
}
return val
}