-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathnumber.go
149 lines (131 loc) · 3.28 KB
/
number.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
package faker
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"unsafe"
)
type FakeNumber interface {
Number(digits int) string // => "43202"
NumberInt(digits int) int // => 213
NumberInt32(digits int) int32 // => 92938
NumberInt64(digits int) int64 // => 1689541633257139096
Decimal(precision, scale int) string // => "879420.60"
Digit() string // => "7"
Hexadecimal(digits int) string // => "e7f3"
Between(min, max int) string // => "-47"
Positive(max int) string // => "3"
Negative(min int) string // => "-16"
}
type fakeNumber struct{}
func Number() FakeNumber {
return fakeNumber{}
}
func (n fakeNumber) Number(digits int) string {
if digits <= 0 {
panic("invalid digits value")
}
dd := make([]string, digits, digits)
for i := range dd {
dd[i] = n.Digit()
}
return strings.Join(dd, "")
}
const (
maxDigitsInt32 = 10
maxDigitsInt64 = 19
)
func nonZeroDigit() string {
return fmt.Sprintf("%d", localRand.Int31n(9)+1)
}
func numberPattern(digits, maxDigits int) string {
switch digits {
case 1:
return nonZeroDigit()
case maxDigits:
return fmt.Sprintf(`1\d{%d}`, digits-1)
default:
return fmt.Sprintf(`%s\d{%d}`, nonZeroDigit(), digits-1)
}
}
func (n fakeNumber) NumberInt32(digits int) int32 {
if digits <= 0 || digits > maxDigitsInt32 {
panic("invalid digits value")
}
pat := numberPattern(digits, maxDigitsInt32)
num, err := Regexify(pat)
if err != nil {
panic(fmt.Sprintf("error regexifying %v: %v", pat, err))
}
res, err := strconv.ParseInt(num, 10, 32)
if err != nil {
panic(fmt.Sprintf("error parsing %v as int32: %v", num, err))
}
return int32(res)
}
func (n fakeNumber) NumberInt64(digits int) int64 {
if digits <= 0 || digits > maxDigitsInt64 {
panic("invalid digits value")
}
pat := numberPattern(digits, maxDigitsInt64)
num, err := Regexify(pat)
if err != nil {
panic(fmt.Sprintf("error regexifying %v: %v", pat, err))
}
res, err := strconv.ParseInt(num, 10, 64)
if err != nil {
panic(fmt.Sprintf("error parsing %v as int64: %v", num, err))
}
return int64(res)
}
func (n fakeNumber) NumberInt(digits int) int {
if unsafe.Sizeof(int(0)) == unsafe.Sizeof(int64(0)) {
return int(n.NumberInt64(digits))
}
return int(n.NumberInt32(digits))
}
func (n fakeNumber) Decimal(precision, scale int) string {
if precision <= 0 || scale < 0 || precision < scale {
panic("invalid precision or scale values")
}
s := n.Number(precision)
ii := s[:precision-scale]
if len(ii) == 0 {
ii = "0"
}
ff := s[precision-scale:]
if len(ff) == 0 {
ff = "0"
}
return ii + "." + ff
}
func (n fakeNumber) Digit() string {
return fmt.Sprintf("%d", localRand.Int31n(10))
}
func (n fakeNumber) Hexadecimal(digits int) string {
if digits <= 0 {
panic("invalid digits value")
}
bytes := make([]byte, (digits+1)/2)
localRand.Read(bytes)
return hex.EncodeToString(bytes)[:digits]
}
func (n fakeNumber) Between(min, max int) string {
if min > max {
panic("invalid range")
}
return fmt.Sprintf("%d", RandomInt(min, max))
}
func (n fakeNumber) Positive(max int) string {
if max < 0 {
panic("invalid max value")
}
return n.Between(0, max)
}
func (n fakeNumber) Negative(min int) string {
if min > 0 {
panic("invalid max value")
}
return n.Between(min, 0)
}