-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_test.go
More file actions
207 lines (192 loc) · 5.97 KB
/
Copy pathtypes_test.go
File metadata and controls
207 lines (192 loc) · 5.97 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package ulogo
import "testing"
func TestValueTypedAccessors(t *testing.T) {
tests := []struct {
name string
value Value
wantInt int64
intOK bool
wantUint uint64
uintOK bool
wantFloat float64
floatOK bool
wantBool bool
boolOK bool
wantText string
textOK bool
}{
{
name: "signed integer",
value: Value{Type: "int32_t", Any: int32(-42)},
wantInt: -42,
intOK: true,
wantFloat: -42,
floatOK: true,
},
{
name: "unsigned integer",
value: Value{Type: "uint32_t", Any: uint32(42)},
wantInt: 42,
intOK: true,
wantUint: 42,
uintOK: true,
wantFloat: 42,
floatOK: true,
},
{
name: "negative integer has no uint",
value: Value{Type: "int16_t", Any: int16(-7)},
wantInt: -7,
intOK: true,
wantFloat: -7,
floatOK: true,
},
{
name: "float",
value: Value{Type: "float", Any: float32(1.5)},
wantFloat: 1.5,
floatOK: true,
},
{
name: "bool",
value: Value{Type: "bool", Any: true},
wantBool: true,
boolOK: true,
},
{
name: "string",
value: Value{Type: "char[3]", Any: "PX4"},
wantText: "PX4",
textOK: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, ok := tt.value.Int64(); got != tt.wantInt || ok != tt.intOK {
t.Fatalf("Int64() = %d, %v; want %d, %v", got, ok, tt.wantInt, tt.intOK)
}
if got, ok := tt.value.Uint64(); got != tt.wantUint || ok != tt.uintOK {
t.Fatalf("Uint64() = %d, %v; want %d, %v", got, ok, tt.wantUint, tt.uintOK)
}
if got, ok := tt.value.Float64(); got != tt.wantFloat || ok != tt.floatOK {
t.Fatalf("Float64() = %v, %v; want %v, %v", got, ok, tt.wantFloat, tt.floatOK)
}
if got, ok := tt.value.Bool(); got != tt.wantBool || ok != tt.boolOK {
t.Fatalf("Bool() = %v, %v; want %v, %v", got, ok, tt.wantBool, tt.boolOK)
}
if got, ok := tt.value.StringValue(); got != tt.wantText || ok != tt.textOK {
t.Fatalf("StringValue() = %q, %v; want %q, %v", got, ok, tt.wantText, tt.textOK)
}
})
}
}
func TestValueTypedAccessorBranches(t *testing.T) {
integerValues := []Value{
{Type: "int8_t", Any: int8(-8)},
{Type: "int16_t", Any: int16(-16)},
{Type: "int64_t", Any: int64(-64)},
{Type: "int", Any: int(-32)},
{Type: "uint8_t", Any: uint8(8)},
{Type: "uint16_t", Any: uint16(16)},
{Type: "uint64_t", Any: uint64(64)},
}
for _, value := range integerValues {
if _, ok := value.Int64(); !ok {
t.Fatalf("%s Int64() ok = false", value.Type)
}
if _, ok := value.Float64(); !ok {
t.Fatalf("%s Float64() ok = false", value.Type)
}
}
if _, ok := (Value{Type: "uint64_t", Any: ^uint64(0)}).Int64(); ok {
t.Fatal("large uint64 Int64() ok = true, want false")
}
if _, ok := (Value{Type: "float", Any: float32(1)}).Int64(); ok {
t.Fatal("float Int64() ok = true, want false")
}
if _, ok := (Value{Type: "double", Any: float64(1)}).Uint64(); ok {
t.Fatal("double Uint64() ok = true, want false")
}
if got, ok := (Value{Type: "double", Any: float64(2.5)}).Float64(); !ok || got != 2.5 {
t.Fatalf("double Float64() = %v, %v; want 2.5, true", got, ok)
}
if _, ok := (Value{Type: "char[3]", Any: "PX4"}).Float64(); ok {
t.Fatal("string Float64() ok = true, want false")
}
}
func TestValueStringCoversScalarTypes(t *testing.T) {
tests := []struct {
value Value
want string
}{
{value: Value{}, want: ""},
{value: Value{Raw: []byte{0x0a, 0xff}}, want: "0aff"},
{value: Value{Type: "bool", Any: true}, want: "true"},
{value: Value{Type: "float", Any: float32(1.25)}, want: "1.25"},
{value: Value{Type: "double", Any: float64(1.25)}, want: "1.25"},
{value: Value{Type: "int8_t", Any: int8(-8)}, want: "-8"},
{value: Value{Type: "int16_t", Any: int16(-16)}, want: "-16"},
{value: Value{Type: "int32_t", Any: int32(-32)}, want: "-32"},
{value: Value{Type: "int64_t", Any: int64(-64)}, want: "-64"},
{value: Value{Type: "int", Any: int(-1)}, want: "-1"},
{value: Value{Type: "uint8_t", Any: uint8(8)}, want: "8"},
{value: Value{Type: "uint16_t", Any: uint16(16)}, want: "16"},
{value: Value{Type: "uint32_t", Any: uint32(32)}, want: "32"},
{value: Value{Type: "uint64_t", Any: uint64(64)}, want: "64"},
}
for _, tt := range tests {
if got := tt.value.String(); got != tt.want {
t.Fatalf("%#v.String() = %q, want %q", tt.value, got, tt.want)
}
}
}
func TestValueBytesReturnsCopy(t *testing.T) {
value := Value{Type: "uint8_t[]", Raw: []byte{1, 2, 3}}
got := value.Bytes()
got[0] = 99
if value.Raw[0] != 1 {
t.Fatalf("Bytes returned internal slice; Raw[0] = %d", value.Raw[0])
}
}
func TestValueBytesEmpty(t *testing.T) {
if got := (Value{}).Bytes(); got != nil {
t.Fatalf("empty Bytes() = %#v, want nil", got)
}
}
func TestULogAccessorsAndLogLevelStrings(t *testing.T) {
var nilLog *ULog
if nilLog.HasDataAppended() {
t.Fatal("nil HasDataAppended() = true")
}
if nilLog.HasDefaultParameters() {
t.Fatal("nil HasDefaultParameters() = true")
}
if got := nilLog.DefaultParametersByType(0); got != nil {
t.Fatalf("nil DefaultParametersByType() = %#v, want nil", got)
}
log, err := ReadFile("testdata/pyulog/sample_logging_tagged_and_default_params.ulg")
if err != nil {
t.Fatal(err)
}
if !log.HasDefaultParameters() {
t.Fatal("expected default parameters flag")
}
if len(log.DefaultParametersByType(0)) == 0 {
t.Fatal("expected system default parameters")
}
keys := log.SortedInfoKeys()
for i := 1; i < len(keys); i++ {
if keys[i-1] > keys[i] {
t.Fatalf("info keys not sorted at %d: %q > %q", i, keys[i-1], keys[i])
}
}
if got := (LoggingMessage{LogLevel: '6'}).LogLevelString(); got != "INFO" {
t.Fatalf("logging level = %q, want INFO", got)
}
if got := (TaggedLoggingMessage{LogLevel: '4'}).LogLevelString(); got != "WARNING" {
t.Fatalf("tagged logging level = %q, want WARNING", got)
}
if got := (LoggingMessage{LogLevel: '?'}).LogLevelString(); got != "UNKNOWN" {
t.Fatalf("unknown logging level = %q, want UNKNOWN", got)
}
}