Skip to content

Commit

Permalink
cover strict parameter in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zariiii9003 committed Aug 2, 2023
1 parent 9288998 commit c4cc6ba
Showing 1 changed file with 68 additions and 5 deletions.
73 changes: 68 additions & 5 deletions test/test_bit_timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def test_sja1000():
"""Test some values obtained using other bit timing calculators."""
timing = can.BitTiming(
f_clock=8_000_000, brp=4, tseg1=11, tseg2=4, sjw=2, nof_samples=3
f_clock=8_000_000, brp=4, tseg1=11, tseg2=4, sjw=2, nof_samples=3, strict=True
)
assert timing.f_clock == 8_000_000
assert timing.bitrate == 125_000
Expand All @@ -25,7 +25,9 @@ def test_sja1000():
assert timing.btr0 == 0x43
assert timing.btr1 == 0xBA

timing = can.BitTiming(f_clock=8_000_000, brp=1, tseg1=13, tseg2=2, sjw=1)
timing = can.BitTiming(
f_clock=8_000_000, brp=1, tseg1=13, tseg2=2, sjw=1, strict=True
)
assert timing.f_clock == 8_000_000
assert timing.bitrate == 500_000
assert timing.brp == 1
Expand All @@ -38,7 +40,9 @@ def test_sja1000():
assert timing.btr0 == 0x00
assert timing.btr1 == 0x1C

timing = can.BitTiming(f_clock=8_000_000, brp=1, tseg1=5, tseg2=2, sjw=1)
timing = can.BitTiming(
f_clock=8_000_000, brp=1, tseg1=5, tseg2=2, sjw=1, strict=True
)
assert timing.f_clock == 8_000_000
assert timing.bitrate == 1_000_000
assert timing.brp == 1
Expand Down Expand Up @@ -84,7 +88,7 @@ def test_from_bitrate_and_segments():
assert timing.btr1 == 0x1C

timing = can.BitTiming.from_bitrate_and_segments(
f_clock=8_000_000, bitrate=1_000_000, tseg1=5, tseg2=2, sjw=1
f_clock=8_000_000, bitrate=1_000_000, tseg1=5, tseg2=2, sjw=1, strict=True
)
assert timing.f_clock == 8_000_000
assert timing.bitrate == 1_000_000
Expand Down Expand Up @@ -127,8 +131,24 @@ def test_from_bitrate_and_segments():
assert timing.data_sjw == 10
assert timing.data_sample_point == 75

# test strict invalid
with pytest.raises(ValueError):
can.BitTimingFd.from_bitrate_and_segments(
f_clock=80_000_000,
nom_bitrate=500_000,
nom_tseg1=119,
nom_tseg2=40,
nom_sjw=40,
data_bitrate=2_000_000,
data_tseg1=29,
data_tseg2=10,
data_sjw=10,
strict=True,
)


def test_can_fd():
# test non-strict
timing = can.BitTimingFd(
f_clock=80_000_000,
nom_brp=1,
Expand All @@ -149,7 +169,6 @@ def test_can_fd():
assert timing.nom_tseg2 == 40
assert timing.nom_sjw == 40
assert timing.nom_sample_point == 75
assert timing.f_clock == 80_000_000
assert timing.data_bitrate == 2_000_000
assert timing.data_brp == 1
assert timing.dbt == 40
Expand All @@ -158,6 +177,50 @@ def test_can_fd():
assert timing.data_sjw == 10
assert timing.data_sample_point == 75

# test strict invalid
with pytest.raises(ValueError):
can.BitTimingFd(
f_clock=80_000_000,
nom_brp=1,
nom_tseg1=119,
nom_tseg2=40,
nom_sjw=40,
data_brp=1,
data_tseg1=29,
data_tseg2=10,
data_sjw=10,
strict=True,
)

# test strict valid
timing = can.BitTimingFd(
f_clock=80_000_000,
nom_brp=2,
nom_tseg1=59,
nom_tseg2=20,
nom_sjw=20,
data_brp=2,
data_tseg1=14,
data_tseg2=5,
data_sjw=5,
strict=True,
)
assert timing.f_clock == 80_000_000
assert timing.nom_bitrate == 500_000
assert timing.nom_brp == 2
assert timing.nbt == 80
assert timing.nom_tseg1 == 59
assert timing.nom_tseg2 == 20
assert timing.nom_sjw == 20
assert timing.nom_sample_point == 75
assert timing.data_bitrate == 2_000_000
assert timing.data_brp == 2
assert timing.dbt == 20
assert timing.data_tseg1 == 14
assert timing.data_tseg2 == 5
assert timing.data_sjw == 5
assert timing.data_sample_point == 75


def test_from_btr():
timing = can.BitTiming.from_registers(f_clock=8_000_000, btr0=0x00, btr1=0x14)
Expand Down

0 comments on commit c4cc6ba

Please sign in to comment.