-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtypes_test.go
More file actions
94 lines (80 loc) · 2.37 KB
/
types_test.go
File metadata and controls
94 lines (80 loc) · 2.37 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
package virtual_device
import (
"math"
"testing"
"github.com/jbdemonte/virtual-device/linux"
)
func TestDenormalize_BiDirectional(t *testing.T) {
axis := AbsAxis{Axis: linux.ABS_X, Min: -32768, Max: 32767}
tests := []struct {
name string
input float32
want int32
}{
{"center", 0, 0}, // int32(-32768 + 1*65535/2) = int32(-0.5) = 0
{"full left", -1, -32768}, // int32(-32768 + 0*65535/2) = -32768
{"full right", 1, 32767}, // int32(-32768 + 2*65535/2) = 32767
{"half right", 0.5, 16383},
{"half left", -0.5, -16384},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := axis.Denormalize(tt.input)
if got != tt.want {
t.Errorf("Denormalize(%v) = %d, want %d", tt.input, got, tt.want)
}
})
}
}
func TestDenormalize_UniDirectional(t *testing.T) {
axis := AbsAxis{Axis: linux.ABS_Z, Min: 0, Max: 255, IsUnidirectional: true}
tests := []struct {
name string
input float32
want int32
}{
{"zero", 0, 0},
{"full", 1, 255},
{"half", 0.5, 127},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := axis.Denormalize(tt.input)
if got != tt.want {
t.Errorf("Denormalize(%v) = %d, want %d", tt.input, got, tt.want)
}
})
}
}
func TestDenormalize_Clamping(t *testing.T) {
biAxis := AbsAxis{Axis: linux.ABS_X, Min: -32768, Max: 32767}
// Bi-directional clamps to [-1, 1]
if got := biAxis.Denormalize(-2); got != -32768 {
t.Errorf("bi-dir clamp low: got %d, want -32768", got)
}
if got := biAxis.Denormalize(2); got != 32767 {
t.Errorf("bi-dir clamp high: got %d, want 32767", got)
}
uniAxis := AbsAxis{Axis: linux.ABS_Z, Min: 0, Max: 255, IsUnidirectional: true}
// Uni-directional clamps to [0, 1]
if got := uniAxis.Denormalize(-1); got != 0 {
t.Errorf("uni-dir clamp low: got %d, want 0", got)
}
if got := uniAxis.Denormalize(2); got != 255 {
t.Errorf("uni-dir clamp high: got %d, want 255", got)
}
}
func TestDenormalize_ZeroRange(t *testing.T) {
axis := AbsAxis{Axis: linux.ABS_X, Min: 0, Max: 0}
got := axis.Denormalize(0.5)
if got != 0 {
t.Errorf("zero range: got %d, want 0", got)
}
}
func TestDenormalize_NaN(t *testing.T) {
axis := AbsAxis{Axis: linux.ABS_X, Min: -100, Max: 100}
got := axis.Denormalize(float32(math.NaN()))
// NaN comparisons fail all branches, so value should not be clamped but will
// produce some int32 — just ensure no panic.
_ = got
}