<commit_before>from udsoncan import DataFormatIdentifier, AddressAndLengthFormatIdentifier,MemoryLocation, CommunicationType, Baudrate, IOMasks, IOValues, Dtc, DidCodec, AsciiCodec
from test.UdsTest import UdsTest
import struct

class TestAddressAndLengthFormatIdentifier(UdsTest):
	def test_ali_1(self):
		alfid = AddressAndLengthFormatIdentifier(memorysize_format=8, address_format=8)
		self.assertEqual(alfid.get_byte(),b'\x11')

	def test_ali_2(self):
		alfid = AddressAndLengthFormatIdentifier(memorysize_format=16, address_format=8)
		self.assertEqual(alfid.get_byte(),b'\x21')

	def test_ali_oob_values(self):	# Out Of Bounds Value
		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=1, address_format=1)
		
		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=0, address_format=8)

		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=8, address_format=0)

		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=40, address_format=0)

		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=8, address_format=48)

		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format='8', address_format=8)

		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=8, address_format='8')

	def test_str_repr(self):
		alfid = AddressAndLengthFormatIdentifier(memorysize_format=8, address_format=8)
		str(alfid)
		alfid.__repr__()

class TestDataFormatIdentifier(UdsTest):
	def test_dfi(self):
		dfi = DataFormatIdentifier(compression=1, encryption=2)
		self.assertEqual(dfi.get_byte(),b'\x12')

	def test_dfi2(self):
		dfi = DataFormatIdentifier(compression=15, encryption=15)
		self.assertEqual(dfi.get_byte(),b'\xFF')

	def test_str_repr(self):
		dfi = DataFormatIdentifier(compression=1, encryption=2)
		str(dfi)
		dfi.__repr__()

	def test_ali_oob_values(self):
		with self.assertRaises(ValueError):
			DataFormatIdentifier(compression=-1, encryption=1)
		
		with self.assertRaises(ValueError):
			DataFormatIdentifier(compression=1, encryption=-1)

		with self.assertRaises(ValueError):
			DataFormatIdentifier(compression=16, encryption=1)

		with self.assertRaises(ValueError):
			DataFormatIdentifier(compression=1, encryption=16)

class TestMemoryLocation(UdsTest):
	def test_memloc1(self):
		memloc = MemoryLocation(address=0x1234, memorysize=0x78, address_format=16, memorysize_format=8)
		self.assertEqual(memloc.get_address_bytes(), b'\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x78')

	def test_memloc_autosize1(self):
		memloc = MemoryLocation(address=0x1234, memorysize=0x78)
		self.assertEqual(memloc.get_address_bytes(), b'\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x78')

	def test_memloc_autosize2(self):
		memloc = MemoryLocation(address=0x1234567, memorysize=0x789abb)
		self.assertEqual(memloc.get_address_bytes(), b'\x01\x23\x45\x67')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x78\x9a\xbb')

	def test_memloc_str_repr(self):
		memloc = MemoryLocation(address=0x1234, memorysize=0x78, address_format=16, memorysize_format=8)
		str(memloc)
		memloc.__repr__()

	def test_memloc_override(self):
		memloc = MemoryLocation(address=0x1234, memorysize=0x78)
		self.assertEqual(memloc.get_address_bytes(), b'\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x78')
		memloc.set_format_if_none(address_format=32)
		self.assertEqual(memloc.get_address_bytes(), b'\x00\x00\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x78')
		memloc.set_format_if_none(memorysize_format=24)
		self.assertEqual(memloc.get_address_bytes(), b'\x00\x00\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x00\x00\x78')

	def test_memloc_from_bytes(self):
		memloc = MemoryLocation.from_bytes(address_bytes=b'\x12\x34', memorysize_bytes=b'\xFF')
		self.assertEqual(memloc.address, 0x1234)
		self.assertEqual(memloc.memorysize, 0xFF)
		self.assertEqual(memloc.address_format, 16)
		self.assertEqual(memloc.memorysize_format, 8)
<commit_msg>Added unit test to verify #11<commit_after>from udsoncan import DataFormatIdentifier, AddressAndLengthFormatIdentifier,MemoryLocation, CommunicationType, Baudrate, IOMasks, IOValues, Dtc, DidCodec, AsciiCodec
from test.UdsTest import UdsTest
import struct

class TestAddressAndLengthFormatIdentifier(UdsTest):
	def test_ali_1(self):
		alfid = AddressAndLengthFormatIdentifier(memorysize_format=8, address_format=8)
		self.assertEqual(alfid.get_byte(),b'\x11')

	def test_ali_2(self):
		alfid = AddressAndLengthFormatIdentifier(memorysize_format=16, address_format=8)
		self.assertEqual(alfid.get_byte(),b'\x21')

	def test_ali_oob_values(self):	# Out Of Bounds Value
		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=1, address_format=1)
		
		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=0, address_format=8)

		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=8, address_format=0)

		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=40, address_format=0)

		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=8, address_format=48)

		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format='8', address_format=8)

		with self.assertRaises(ValueError):
			AddressAndLengthFormatIdentifier(memorysize_format=8, address_format='8')

	def test_str_repr(self):
		alfid = AddressAndLengthFormatIdentifier(memorysize_format=8, address_format=8)
		str(alfid)
		alfid.__repr__()

class TestDataFormatIdentifier(UdsTest):
	def test_dfi(self):
		dfi = DataFormatIdentifier(compression=1, encryption=2)
		self.assertEqual(dfi.get_byte(),b'\x12')

	def test_dfi2(self):
		dfi = DataFormatIdentifier(compression=15, encryption=15)
		self.assertEqual(dfi.get_byte(),b'\xFF')

	def test_str_repr(self):
		dfi = DataFormatIdentifier(compression=1, encryption=2)
		str(dfi)
		dfi.__repr__()

	def test_ali_oob_values(self):
		with self.assertRaises(ValueError):
			DataFormatIdentifier(compression=-1, encryption=1)
		
		with self.assertRaises(ValueError):
			DataFormatIdentifier(compression=1, encryption=-1)

		with self.assertRaises(ValueError):
			DataFormatIdentifier(compression=16, encryption=1)

		with self.assertRaises(ValueError):
			DataFormatIdentifier(compression=1, encryption=16)

class TestMemoryLocation(UdsTest):
	def test_memloc1(self):
		memloc = MemoryLocation(address=0x1234, memorysize=0x78, address_format=16, memorysize_format=8)
		self.assertEqual(memloc.get_address_bytes(), b'\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x78')

	def test_memloc_autosize1(self):
		memloc = MemoryLocation(address=0x1234, memorysize=0x78)
		self.assertEqual(memloc.get_address_bytes(), b'\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x78')

	def test_memloc_autosize2(self):
		memloc = MemoryLocation(address=0x1234567, memorysize=0x789abb)
		self.assertEqual(memloc.get_address_bytes(), b'\x01\x23\x45\x67')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x78\x9a\xbb')

	def test_memloc_str_repr(self):
		memloc = MemoryLocation(address=0x1234, memorysize=0x78, address_format=16, memorysize_format=8)
		str(memloc)
		memloc.__repr__()

	def test_memloc_override(self):
		memloc = MemoryLocation(address=0x1234, memorysize=0x78)
		self.assertEqual(memloc.get_address_bytes(), b'\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x78')
		memloc.set_format_if_none(address_format=32)
		self.assertEqual(memloc.get_address_bytes(), b'\x00\x00\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x78')
		memloc.set_format_if_none(memorysize_format=24)
		self.assertEqual(memloc.get_address_bytes(), b'\x00\x00\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x00\x00\x78')

		memloc = MemoryLocation(address=0x1234, memorysize=0x78)
		memloc.set_format_if_none(address_format=32, memorysize_format=24)	# Both at same time.
		self.assertEqual(memloc.get_address_bytes(), b'\x00\x00\x12\x34')
		self.assertEqual(memloc.get_memorysize_bytes(), b'\x00\x00\x78')


	def test_memloc_from_bytes(self):
		memloc = MemoryLocation.from_bytes(address_bytes=b'\x12\x34', memorysize_bytes=b'\xFF')
		self.assertEqual(memloc.address, 0x1234)
		self.assertEqual(memloc.memorysize, 0xFF)
		self.assertEqual(memloc.address_format, 16)
		self.assertEqual(memloc.memorysize_format, 8)

{'fraction_non_alphanumeric': 0.11478060046189377, 'fraction_numerical': 0.043187066974595845, 'mean_word_length': 7.837755102040816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '485275', 'n_tokens_mistral': 3341, 'n_tokens_neox': 2982, 'n_words': 386}