Skip to content

Commit

Permalink
Added unit tests to test Decode() and DecodePayload()
Browse files Browse the repository at this point in the history
  • Loading branch information
Naveen Venkatesan committed Mar 23, 2021
1 parent 592beb4 commit fd0c28c
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/descriptor/signal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package descriptor

import (
"encoding/hex"
"math"
"testing"

Expand All @@ -21,10 +22,17 @@ func TestSignal_Decode_UnsignedBigEndian(t *testing.T) {
Max: 1,
}
const value uint64 = 180

// Testing can.Data
var data can.Data
data.SetUnsignedBitsBigEndian(uint8(s.Start), uint8(s.Length), value)
actual := s.Decode(data)
assert.DeepEqual(t, s.Offset+float64(value)*s.Scale, actual)

// Testing payload
p, _ := can.PayloadFromHex(hex.EncodeToString(data[:]))
actual = s.DecodePayload(&p)
assert.DeepEqual(t, s.Offset+float64(value)*s.Scale, actual)
}

func TestSignal_Decode_SignedBigEndian(t *testing.T) {
Expand All @@ -40,10 +48,17 @@ func TestSignal_Decode_SignedBigEndian(t *testing.T) {
Max: 1,
}
const value int64 = -180

// Testing can.Data
var data can.Data
data.SetSignedBitsBigEndian(uint8(s.Start), uint8(s.Length), value)
actual := s.Decode(data)
assert.DeepEqual(t, s.Offset+float64(value)*s.Scale, actual)

// Testing payload
p, _ := can.PayloadFromHex(hex.EncodeToString(data[:]))
actual = s.DecodePayload(&p)
assert.DeepEqual(t, s.Offset+float64(value)*s.Scale, actual)
}

func TestSignal_Decode_UnsignedLittleEndian(t *testing.T) {
Expand All @@ -59,10 +74,17 @@ func TestSignal_Decode_UnsignedLittleEndian(t *testing.T) {
Max: 1,
}
const value uint64 = 180

// Testing can.Data
var data can.Data
data.SetUnsignedBitsLittleEndian(uint8(s.Start), uint8(s.Length), value)
actual := s.Decode(data)
assert.DeepEqual(t, s.Offset+float64(value)*s.Scale, actual)

// Testing payload
p, _ := can.PayloadFromHex(hex.EncodeToString(data[:]))
actual = s.DecodePayload(&p)
assert.DeepEqual(t, s.Offset+float64(value)*s.Scale, actual)
}

func TestSignal_Decode_SignedLittleEndian(t *testing.T) {
Expand All @@ -78,10 +100,17 @@ func TestSignal_Decode_SignedLittleEndian(t *testing.T) {
Max: 1,
}
const value int64 = -180

// Testing can.Data
var data can.Data
data.SetSignedBitsLittleEndian(uint8(s.Start), uint8(s.Length), value)
actual := s.Decode(data)
assert.DeepEqual(t, s.Offset+float64(value)*s.Scale, actual)

// Testing payload
p, _ := can.PayloadFromHex(hex.EncodeToString(data[:]))
actual = s.DecodePayload(&p)
assert.DeepEqual(t, s.Offset+float64(value)*s.Scale, actual)
}

func TestSignal_FromPhysical_SaturatedCast(t *testing.T) {
Expand Down

0 comments on commit fd0c28c

Please sign in to comment.