-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymboltoken_test.go
105 lines (101 loc) · 3.26 KB
/
symboltoken_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
package main
import "testing"
func TestSeparateFloat(t *testing.T) {
type args struct {
s string
base int
}
tests := []struct {
name string
args args
wantMl int
wantHasExp bool
wantManSign bool
wantExpSign bool
}{
{"all three", args{"31.574e30", 10}, 6, true, false, false},
{"no exponent", args{"684.6516", 10}, 8, false, false, false},
{"no fractional component", args{"468e12", 10}, 3, true, false, false},
{"integer", args{"4525", 10}, 4, false, false, false},
{"no integer component", args{".5467e34", 10}, 5, true, false, false},
{"just fractional", args{".574654", 10}, 7, false, false, false},
{"negative mantissa", args{"-643.765e3", 10}, 7, true, true, false},
{"negative explicit", args{"-54.548", 10}, 6, false, true, false},
{"negative exponent", args{"6e-5", 10}, 1, true, false, true},
{"very positive", args{"+654.3e+345", 10}, 5, true, true, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotIml, gotHasExp, gotManSign, gotExpSign := separateFloat(tt.args.s, tt.args.base)
if gotIml != tt.wantMl {
t.Errorf("separateFloat() gotIml = %v, want %v", gotIml, tt.wantMl)
}
if gotHasExp != tt.wantHasExp {
t.Errorf("separateFloat() gotHasExp = %v, want %v", gotHasExp, tt.wantHasExp)
}
if gotManSign != tt.wantManSign {
t.Errorf("separateFloat() gotManSign = %v, want %v", gotManSign, tt.wantManSign)
}
if gotExpSign != tt.wantExpSign {
t.Errorf("separateFloat() gotExpSign = %v, want %v", gotExpSign, tt.wantExpSign)
}
})
}
}
func Test_squareBase(t *testing.T) {
type args struct {
s string
base int
}
tests := []struct {
name string
args args
want string
}{
{"binary", args{"111100100", 2}, "13210"},
{"binary signed", args{"+11010100101", 2}, "+122211"},
{"ternary", args{"10210111220212200", 3}, "123456780"},
{"quaternary", args{"-12321311132130", 4}, "-6E7579C"},
{"fractional component 1", args{"3.14152535", 6}, "3.ABHN"},
{"fractional component 2", args{"1313131.3131313", 4}, "1DDD.DDDC"},
{"fractional component 3", args{"131313.13131313", 4}, "777.7777"},
{"empty fractional component", args{"010101.", 2}, "111."},
{"no int component", args{".3412013", 5}, ".J71F"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := squareBase(tt.args.s, tt.args.base); got != tt.want {
t.Errorf("squareBase() = %v, want %v", got, tt.want)
}
})
}
}
func TestParseFloat(t *testing.T) {
type args struct {
s string
base int
}
tests := []struct {
name string
args args
want float64
wantErr bool
}{
{"call original library base 10", args{"3456.645136e12", 10}, 3456.645136e12, false},
{"call original library base 16", args{"2C67.3948Ap11", 16}, 0x2C67.3948Ap11, false},
{"try base 4", args{"1001.3212301132123e12", 4}, 0x41.E6C5E6Cp6, false},
{"try base 4 power not base 10", args{"1001.3212301132123e22", 4}, 0x41.E6C5E6Cp22, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseFloat(tt.args.s, tt.args.base)
if (err != nil) != tt.wantErr {
t.Errorf("ParseFloat() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseFloat() got = %v, want %v", got, tt.want)
}
})
}
}