Skip to content

Commit

Permalink
Added unit tests for UnmarshalPhysical() and UnmarshalPhysicalPayload()
Browse files Browse the repository at this point in the history
  • Loading branch information
Naveen Venkatesan committed Mar 24, 2021
1 parent fd0c28c commit f845a4a
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions pkg/descriptor/signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,110 @@ func TestSignal_Decode_SignedLittleEndian(t *testing.T) {
assert.DeepEqual(t, s.Offset+float64(value)*s.Scale, actual)
}

func TestSignal_UnmarshalPhysical_UnsignedBigEndian(t *testing.T) {
s := &Signal{
Name: "TestSignal",
IsSigned: false,
IsBigEndian: true,
Offset: -1,
Scale: 0.5,
Length: 10,
Start: 32,
Min: 0,
Max: 50,
}
const value uint64 = 180

// Testing can.Data
var data can.Data
data.SetUnsignedBitsBigEndian(uint8(s.Start), uint8(s.Length), value)
actual := s.UnmarshalPhysical(data)
assert.DeepEqual(t, s.Max, actual)

// Testing payload
p, _ := can.PayloadFromHex(hex.EncodeToString(data[:]))
actual = s.UnmarshalPhysicalPayload(&p)
assert.DeepEqual(t, s.Max, actual)
}

func TestSignal_UnmarshalPhysical_SignedBigEndian(t *testing.T) {
s := &Signal{
Name: "TestSignal",
IsSigned: true,
IsBigEndian: true,
Offset: -1,
Scale: 0.5,
Length: 10,
Start: 32,
Min: -50,
Max: 0,
}
const value int64 = -180

// Testing can.Data
var data can.Data
data.SetSignedBitsBigEndian(uint8(s.Start), uint8(s.Length), value)
actual := s.UnmarshalPhysical(data)
assert.DeepEqual(t, s.Min, actual)

// Testing payload
p, _ := can.PayloadFromHex(hex.EncodeToString(data[:]))
actual = s.UnmarshalPhysicalPayload(&p)
assert.DeepEqual(t, s.Min, actual)
}

func TestSignal_UnmarshalPhysical_UnsignedLittleEndian(t *testing.T) {
s := &Signal{
Name: "TestSignal",
IsSigned: false,
IsBigEndian: false,
Offset: -1,
Scale: 0.5,
Length: 10,
Start: 32,
Min: 0,
Max: 50,
}
const value uint64 = 180

// Testing can.Data
var data can.Data
data.SetUnsignedBitsLittleEndian(uint8(s.Start), uint8(s.Length), value)
actual := s.UnmarshalPhysical(data)
assert.DeepEqual(t, s.Max, actual)

// Testing payload
p, _ := can.PayloadFromHex(hex.EncodeToString(data[:]))
actual = s.UnmarshalPhysicalPayload(&p)
assert.DeepEqual(t, s.Max, actual)
}

func TestSignal_UnmarshalPhysical_SignedLittleEndian(t *testing.T) {
s := &Signal{
Name: "TestSignal",
IsSigned: true,
IsBigEndian: false,
Offset: -1,
Scale: 0.5,
Length: 10,
Start: 32,
Min: -50,
Max: 0,
}
const value int64 = -180

// Testing can.Data
var data can.Data
data.SetSignedBitsLittleEndian(uint8(s.Start), uint8(s.Length), value)
actual := s.UnmarshalPhysical(data)
assert.DeepEqual(t, s.Min, actual)

// Testing payload
p, _ := can.PayloadFromHex(hex.EncodeToString(data[:]))
actual = s.UnmarshalPhysicalPayload(&p)
assert.DeepEqual(t, s.Min, actual)
}

func TestSignal_FromPhysical_SaturatedCast(t *testing.T) {
s := &Signal{
Name: "TestSignal",
Expand Down

0 comments on commit f845a4a

Please sign in to comment.