Skip to content

Commit

Permalink
Add complementary Unit Test for signed Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
rviollette committed Mar 23, 2021
1 parent f19e689 commit ea8b269
Showing 1 changed file with 50 additions and 8 deletions.
58 changes: 50 additions & 8 deletions test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ def test_enum_default_size_unsigned(self):
self.assertEqual(self.namespace.MAX, 0xFFFFFFFF)
self.assertTrue(self.namespace.MAX > 0)

def test_enum_signedness(self):
def test_enum_unsigned_signedness(self):
"""
Test enum signedness, based on the size and sign of the values it contains.
Test with/without setting compiler flag '-fshort-enums'
Test with/without setting compiler flag '-fshort-enums'.
Test for unsigned enum (contain only positive or null values).
"""
flags = [None, ['-fshort-enums']]
enum_sizes = [1, 2, 4, 8]
Expand All @@ -202,7 +203,7 @@ def test_enum_signedness(self):

for flag in flags:
for (enum_size, enum_type, enum_value) in zip(enum_sizes, enum_types, enum_values):
with self.subTest(flags=flag, enum_size=enum_size, enum_value=enum_value):
with self.subTest(flag=flag, enum_size=enum_size, enum_value=enum_value):

# Declare an enum, holding one entry of value arbitrary set to enum_value//2.
self.convert(f'enum myEnum {{ FOO = {enum_value//2:#x}U}};', flag)
Expand All @@ -220,13 +221,54 @@ def test_enum_signedness(self):
'Test value used to define the enum size (arbitrary set to enum_value//2).')

my_enum = self.namespace.myEnum()
# Set a 1-byte value with the most significant bit set (MSb).
# Set a value with the most significant bit set (positive unsigned integer).
my_enum.value = enum_value
# We expect that enum is interpreted as an positive unsigned byte
# and NOT as a negative signed byte.

self.assertTrue(my_enum.value > 0, msg=
'We expect that the enum is interpreted as an positive unsigned integer '
'and NOT as a negative signed integer.')
'We expect that the enum is interpreted as an positive unsigned integer.')

def test_enum_signed_signedness(self):
"""
Test enum signedness, based on the size and sign of the values it contains.
Test with/without setting compiler flag '-fshort-enums'.
Test for signed enum (contain at least one negative value)
"""
flags = [None, ['-fshort-enums']]
enum_sizes = [1, 2, 4, 8]
enum_types = [ctypes.c_int8, ctypes.c_int16, ctypes.c_int32, ctypes.c_int64]
enum_values = [0x7F, 0x7FFF, 0x7FFFFFFF, 0x7FFFFFFFFFFFFFFF]

for flag in flags:
for (enum_size, enum_type, enum_value) in zip(enum_sizes, enum_types, enum_values):
with self.subTest(flag=flag, enum_size=enum_size, enum_value=enum_value):

# Declare an enum, holding one entry of value arbitrary set to -enum_value//2.
self.convert(f'enum myEnum {{ FOO = {-enum_value//2:#x}}};', flag)

if flag is None:
# Without compiler flag '-fshort-enums'
self.assertEqual(ctypes.sizeof(self.namespace.myEnum), max(4, enum_size))
self.assertEqual(self.namespace.myEnum, ctypes.c_int32 if enum_size <= 4 else enum_type)
else:
# With compiler flag '-fshort-enums'
self.assertEqual(ctypes.sizeof(self.namespace.myEnum), enum_size)
self.assertEqual(self.namespace.myEnum, enum_type)

self.assertEqual(self.namespace.FOO, -enum_value//2, msg=
'Test value used to define the enum size (arbitrary set to -enum_value//2).')

my_enum = self.namespace.myEnum()
# Set a value with the most significant bit cleared (positive signed integer).
my_enum.value = enum_value

self.assertTrue(my_enum.value > 0, msg=
'We expect that the enum is interpreted as an positive unsigned integer.')

# Set a value with the most significant cleared (negative signed integer).
my_enum.value = (-enum_value - 1)

self.assertTrue(my_enum.value < 0, msg=
'We expect that the enum is interpreted as an negative unsigned integer.')


if __name__ == "__main__":
Expand Down

0 comments on commit ea8b269

Please sign in to comment.