github.com/blueinnovationsgroup/can-go@v0.0.0-20230518195432-d0567cda0028/pkg/descriptor/signal_test.go (about) 1 package descriptor 2 3 import ( 4 "math" 5 "testing" 6 7 "github.com/blueinnovationsgroup/can-go" 8 "gotest.tools/v3/assert" 9 ) 10 11 func TestSignal_FromPhysical_SaturatedCast(t *testing.T) { 12 s := &Signal{ 13 Name: "TestSignal", 14 Offset: -1, 15 Scale: 3.0517578125e-05, 16 Min: -1, 17 Max: 1, 18 Length: 16, 19 } 20 // without a saturated cast, the result would be math.MaxUint16 + 1, which would wrap around to 0 21 assert.Equal(t, uint16(math.MaxUint16), uint16(s.FromPhysical(180))) 22 } 23 24 func TestSignal_SaturatedCastSigned(t *testing.T) { 25 s := &Signal{ 26 Name: "TestSignal", 27 IsSigned: true, 28 Length: 6, 29 } 30 assert.Equal(t, int64(31), s.SaturatedCastSigned(254)) 31 assert.Equal(t, int64(-32), s.SaturatedCastSigned(-255)) 32 } 33 34 func TestSignal_SaturatedCastUnsigned(t *testing.T) { 35 s := &Signal{ 36 Name: "TestSignal", 37 Length: 6, 38 } 39 assert.Equal(t, uint64(63), s.SaturatedCastUnsigned(255)) 40 } 41 42 func TestSignal_UnmarshalSigned_BigEndian(t *testing.T) { 43 s := &Signal{ 44 Name: "TestSignal", 45 IsSigned: true, 46 IsBigEndian: true, 47 Length: 8, 48 Start: 32, 49 } 50 const value int64 = -8 51 var data can.Data 52 data.SetSignedBitsBigEndian(s.Start, s.Length, value) 53 assert.Equal(t, value, s.UnmarshalSigned(data)) 54 } 55 56 func TestSignal_MarshalUnsigned_BigEndian(t *testing.T) { 57 s := &Signal{ 58 Name: "TestSignal", 59 IsBigEndian: true, 60 Length: 8, 61 Start: 32, 62 } 63 const value uint64 = 8 64 var expected can.Data 65 expected.SetUnsignedBitsBigEndian(s.Start, s.Length, value) 66 var actual can.Data 67 s.MarshalUnsigned(&actual, value) 68 assert.DeepEqual(t, expected, actual) 69 } 70 71 func TestSignal_MarshalSigned_BigEndian(t *testing.T) { 72 s := &Signal{ 73 Name: "TestSignal", 74 IsSigned: true, 75 IsBigEndian: true, 76 Length: 8, 77 Start: 32, 78 } 79 const value int64 = -8 80 var expected can.Data 81 expected.SetSignedBitsBigEndian(s.Start, s.Length, value) 82 var actual can.Data 83 s.MarshalSigned(&actual, value) 84 assert.DeepEqual(t, expected, actual) 85 }