@@ -196,3 +196,114 @@ def test_string_conversion():
196196 # Skip device field tests which are causing issues
197197 # They're already tested in test_create_device_struct
198198 print ("Skipping device field tests - already covered in other tests" )
199+
200+
201+ def test_smpt_device_struct_size_compatibility ():
202+ """Test that Smpt_device struct can be created without size mismatches.
203+
204+ This test specifically addresses the issue where CFFI would fail with:
205+ "ffi.error: Smpt_device: wrong size for field 'packet' (cdef says X, but C
206+ compiler says Y)"
207+
208+ The fix uses flexible struct syntax (...;) to handle platform differences.
209+ """
210+ from sciencemode import sciencemode
211+
212+ print ("Testing Smpt_device struct size compatibility..." )
213+
214+ # Test 1: Basic struct allocation should not fail
215+ try :
216+ device = sciencemode .ffi .new ("Smpt_device*" )
217+ assert device is not None , "Smpt_device* allocation succeeded"
218+ print ("✓ Basic Smpt_device* allocation works" )
219+ except Exception as e :
220+ pytest .fail (f"Failed to allocate Smpt_device*: { e } " )
221+
222+ # Test 2: Test accessing known fields that should be available
223+ try :
224+ # These fields should be accessible based on the struct definition
225+ device .packet_length = 0
226+ device .current_packet_number = 1
227+
228+ # Verify the values were set
229+ assert device .packet_length == 0 , "packet_length field accessible"
230+ assert (
231+ device .current_packet_number == 1
232+ ), "current_packet_number field accessible"
233+ print ("✓ Core struct fields are accessible" )
234+ except Exception as e :
235+ pytest .fail (f"Failed to access Smpt_device fields: { e } " )
236+
237+ # Test 3: Test array field access (the problematic 'packet' field)
238+ try :
239+ # The packet field was the source of the size mismatch error
240+ # With flexible struct (...;), this should work
241+ device .packet [0 ] = 42 # Try to write to first element
242+ assert device .packet [0 ] == 42 , "packet array field accessible"
243+ print ("✓ Packet array field is accessible (size mismatch fixed)" )
244+ except Exception as e :
245+ pytest .fail (f"Failed to access packet array field: { e } " )
246+
247+ # Test 4: Test string field access
248+ try :
249+ # Test serial port name field
250+ test_name = b"test_port"
251+ sciencemode .ffi .memmove (device .serial_port_name , test_name , len (test_name ))
252+
253+ # Read back the first few bytes
254+ read_back = sciencemode .ffi .string (device .serial_port_name , len (test_name ))
255+ assert read_back == test_name , "serial_port_name field accessible"
256+ print ("✓ String fields are accessible" )
257+ except Exception as e :
258+ pytest .fail (f"Failed to access string fields: { e } " )
259+
260+ # Test 5: Test struct size calculation
261+ try :
262+ # This should not throw an error anymore with flexible struct
263+ struct_size = sciencemode .ffi .sizeof ("Smpt_device" )
264+ assert struct_size > 0 , "Struct size calculation succeeds"
265+ print (f"✓ Smpt_device struct size: { struct_size } bytes" )
266+ except Exception as e :
267+ pytest .fail (f"Failed to calculate struct size: { e } " )
268+
269+ print ("All Smpt_device struct compatibility tests passed!" )
270+
271+
272+ def test_smpt_device_flexible_struct_behavior ():
273+ """Test that the flexible struct (...;) behaves correctly with different
274+ operations."""
275+ from sciencemode import sciencemode
276+
277+ print ("Testing flexible struct behavior..." )
278+
279+ # Test multiple device allocations
280+ devices = []
281+ try :
282+ for i in range (3 ):
283+ device = sciencemode .ffi .new ("Smpt_device*" )
284+ device .current_packet_number = i
285+ devices .append (device )
286+
287+ # Verify all devices are independent
288+ for i , device in enumerate (devices ):
289+ assert (
290+ device .current_packet_number == i
291+ ), f"Device { i } has correct packet number"
292+
293+ print ("✓ Multiple device allocations work independently" )
294+ except Exception as e :
295+ pytest .fail (f"Failed multiple device allocation test: { e } " )
296+
297+ # Test that we can still detect the struct properly
298+ try :
299+ device = sciencemode .ffi .new ("Smpt_device*" )
300+
301+ # The struct should have the expected type
302+ assert sciencemode .ffi .typeof (device ) == sciencemode .ffi .typeof (
303+ "Smpt_device*"
304+ ), "Device has correct type"
305+ print ("✓ Device type detection works correctly" )
306+ except Exception as e :
307+ pytest .fail (f"Failed device type detection: { e } " )
308+
309+ print ("All flexible struct behavior tests passed!" )
0 commit comments