@@ -204,3 +204,148 @@ impl FromStr for PciBdf {
204204 Ok ( PciBdf :: new ( segment, bus, device, function) )
205205 }
206206}
207+
208+ #[ cfg( test) ]
209+ mod tests {
210+ use super :: * ;
211+
212+ #[ test]
213+ fn test_pci_bdf_new ( ) {
214+ let bdf = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x7 ) ;
215+ assert_eq ! ( bdf. segment( ) , 0x1234 ) ;
216+ assert_eq ! ( bdf. bus( ) , 0x56 ) ;
217+ assert_eq ! ( bdf. device( ) , 0x1f ) ;
218+ assert_eq ! ( bdf. function( ) , 0x7 ) ;
219+ }
220+
221+ #[ test]
222+ fn test_pci_bdf_from_u32 ( ) {
223+ let bdf = PciBdf :: from ( 0x12345678 ) ;
224+ assert_eq ! ( bdf. segment( ) , 0x1234 ) ;
225+ assert_eq ! ( bdf. bus( ) , 0x56 ) ;
226+ assert_eq ! ( bdf. device( ) , 0x0f ) ;
227+ assert_eq ! ( bdf. function( ) , 0x0 ) ;
228+ }
229+
230+ #[ test]
231+ fn test_pci_bdf_to_u32 ( ) {
232+ let bdf = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x7 ) ;
233+ let val: u32 = bdf. into ( ) ;
234+ assert_eq ! ( val, 0x123456ff ) ;
235+ }
236+
237+ #[ test]
238+ fn test_pci_bdf_to_u16 ( ) {
239+ let bdf = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x7 ) ;
240+ let val: u16 = bdf. into ( ) ;
241+ assert_eq ! ( val, 0x56ff ) ;
242+ }
243+
244+ #[ test]
245+ fn test_pci_bdf_from_str_valid ( ) {
246+ let bdf = PciBdf :: from_str ( "1234:56:1f.7" ) . unwrap ( ) ;
247+ assert_eq ! ( bdf. segment( ) , 0x1234 ) ;
248+ assert_eq ! ( bdf. bus( ) , 0x56 ) ;
249+ assert_eq ! ( bdf. device( ) , 0x1f ) ;
250+ assert_eq ! ( bdf. function( ) , 0x7 ) ;
251+ }
252+
253+ #[ test]
254+ fn test_pci_bdf_from_str_zero ( ) {
255+ let bdf = PciBdf :: from_str ( "0000:00:00.0" ) . unwrap ( ) ;
256+ assert_eq ! ( bdf. segment( ) , 0 ) ;
257+ assert_eq ! ( bdf. bus( ) , 0 ) ;
258+ assert_eq ! ( bdf. device( ) , 0 ) ;
259+ assert_eq ! ( bdf. function( ) , 0 ) ;
260+ }
261+
262+ #[ test]
263+ fn test_pci_bdf_from_str_invalid_format ( ) {
264+ assert ! ( matches!(
265+ PciBdf :: from_str( "invalid" ) ,
266+ Err ( PciBdfParseError :: InvalidFormat ( _) )
267+ ) ) ;
268+ assert ! ( matches!(
269+ PciBdf :: from_str( "1234:56" ) ,
270+ Err ( PciBdfParseError :: InvalidFormat ( _) )
271+ ) ) ;
272+ assert ! ( matches!(
273+ PciBdf :: from_str( "1234:56:78:9a.b" ) ,
274+ Err ( PciBdfParseError :: InvalidFormat ( _) )
275+ ) ) ;
276+ }
277+
278+ #[ test]
279+ fn test_pci_bdf_from_str_invalid_hex ( ) {
280+ assert ! ( matches!(
281+ PciBdf :: from_str( "xxxx:00:00.0" ) ,
282+ Err ( PciBdfParseError :: InvalidHex ( _) )
283+ ) ) ;
284+ assert ! ( matches!(
285+ PciBdf :: from_str( "0000:xx:00.0" ) ,
286+ Err ( PciBdfParseError :: InvalidHex ( _) )
287+ ) ) ;
288+ assert ! ( matches!(
289+ PciBdf :: from_str( "0000:00:xx.0" ) ,
290+ Err ( PciBdfParseError :: InvalidHex ( _) )
291+ ) ) ;
292+ assert ! ( matches!(
293+ PciBdf :: from_str( "0000:00:00.x" ) ,
294+ Err ( PciBdfParseError :: InvalidHex ( _) )
295+ ) ) ;
296+ }
297+
298+ #[ test]
299+ fn test_pci_bdf_display ( ) {
300+ let bdf = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x7 ) ;
301+ assert_eq ! ( format!( "{}" , bdf) , "1234:56:1f.7" ) ;
302+ }
303+
304+ #[ test]
305+ fn test_pci_bdf_debug ( ) {
306+ let bdf = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x7 ) ;
307+ assert_eq ! ( format!( "{:?}" , bdf) , "1234:56:1f.7" ) ;
308+ }
309+
310+ #[ test]
311+ fn test_pci_bdf_partial_eq ( ) {
312+ let bdf1 = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x7 ) ;
313+ let bdf2 = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x7 ) ;
314+ let bdf3 = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x6 ) ;
315+ assert_eq ! ( bdf1, bdf2) ;
316+ assert_ne ! ( bdf1, bdf3) ;
317+ }
318+
319+ #[ test]
320+ fn test_pci_bdf_partial_ord ( ) {
321+ let bdf1 = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x6 ) ;
322+ let bdf2 = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x7 ) ;
323+ assert ! ( bdf1 < bdf2) ;
324+ }
325+
326+ #[ test]
327+ fn test_pci_bdf_deserialize_ok ( ) {
328+ // Test deserializer
329+ let visitor = PciBdfVisitor ;
330+ let result = visitor
331+ . visit_str :: < serde:: de:: value:: Error > ( "1234:56:1f.7" )
332+ . unwrap ( ) ;
333+ assert_eq ! ( result, PciBdf :: new( 0x1234 , 0x56 , 0x1f , 0x7 ) ) ;
334+ }
335+
336+ #[ test]
337+ fn test_pci_bdf_deserialize_invalid ( ) {
338+ // Test deserializer with invalid input returns error
339+ let visitor = PciBdfVisitor ;
340+ assert ! ( visitor
341+ . visit_str:: <serde:: de:: value:: Error >( "invalid" )
342+ . is_err( ) ) ;
343+ }
344+
345+ #[ test]
346+ fn test_pci_bdf_serialize ( ) {
347+ // Test serializer using serde_test
348+ let bdf = PciBdf :: new ( 0x1234 , 0x56 , 0x1f , 0x7 ) ;
349+ serde_test:: assert_tokens ( & bdf, & [ serde_test:: Token :: Str ( "1234:56:1f.7" ) ] ) ;
350+ }
351+ }
0 commit comments