@@ -451,3 +451,269 @@ macro_rules! register_bitfields {
451
451
) *
452
452
}
453
453
}
454
+
455
+ #[ cfg( not( feature = "no_std_unit_tests" ) ) ]
456
+ #[ cfg( test) ]
457
+ mod tests {
458
+ #[ derive( Debug , PartialEq , Eq ) ]
459
+ enum Foo {
460
+ Foo0 ,
461
+ Foo1 ,
462
+ Foo2 ,
463
+ Foo3 ,
464
+ Foo4 ,
465
+ Foo5 ,
466
+ Foo6 ,
467
+ Foo7 ,
468
+ }
469
+
470
+ impl crate :: fields:: TryFromValue < u16 > for Foo {
471
+ type EnumType = Foo ;
472
+
473
+ fn try_from ( v : u16 ) -> Option < Self :: EnumType > {
474
+ Self :: try_from ( v as u32 )
475
+ }
476
+ }
477
+ impl crate :: fields:: TryFromValue < u32 > for Foo {
478
+ type EnumType = Foo ;
479
+
480
+ fn try_from ( v : u32 ) -> Option < Self :: EnumType > {
481
+ match v {
482
+ 0 => Some ( Foo :: Foo0 ) ,
483
+ 1 => Some ( Foo :: Foo1 ) ,
484
+ 2 => Some ( Foo :: Foo2 ) ,
485
+ 3 => Some ( Foo :: Foo3 ) ,
486
+ 4 => Some ( Foo :: Foo4 ) ,
487
+ 5 => Some ( Foo :: Foo5 ) ,
488
+ 6 => Some ( Foo :: Foo6 ) ,
489
+ 7 => Some ( Foo :: Foo7 ) ,
490
+ _ => None ,
491
+ }
492
+ }
493
+ }
494
+
495
+ mod field {
496
+ use super :: Foo ;
497
+ use crate :: fields:: { Field , TryFromValue } ;
498
+
499
+ #[ test]
500
+ fn test_new ( ) {
501
+ let field8 = Field :: < u8 , ( ) > :: new ( 0x12 , 3 ) ;
502
+ assert_eq ! ( field8. mask, 0x12_u8 ) ;
503
+ assert_eq ! ( field8. shift, 3 ) ;
504
+ let field16 = Field :: < u16 , ( ) > :: new ( 0x1234 , 5 ) ;
505
+ assert_eq ! ( field16. mask, 0x1234_u16 ) ;
506
+ assert_eq ! ( field16. shift, 5 ) ;
507
+ let field32 = Field :: < u32 , ( ) > :: new ( 0x12345678 , 9 ) ;
508
+ assert_eq ! ( field32. mask, 0x12345678_u32 ) ;
509
+ assert_eq ! ( field32. shift, 9 ) ;
510
+ let field64 = Field :: < u64 , ( ) > :: new ( 0x12345678_9abcdef0 , 1 ) ;
511
+ assert_eq ! ( field64. mask, 0x12345678_9abcdef0_u64 ) ;
512
+ assert_eq ! ( field64. shift, 1 ) ;
513
+ let field128 = Field :: < u128 , ( ) > :: new ( 0x12345678_9abcdef0_0fedcba9_87654321 , 1 ) ;
514
+ assert_eq ! ( field128. mask, 0x12345678_9abcdef0_0fedcba9_87654321_u128 ) ;
515
+ assert_eq ! ( field128. shift, 1 ) ;
516
+ }
517
+
518
+ #[ test]
519
+ fn test_read ( ) {
520
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , 4 ) ;
521
+ assert_eq ! ( field. read( 0x123 ) , 0x12 ) ;
522
+ let field = Field :: < u32 , ( ) > :: new ( 0xF0F , 4 ) ;
523
+ assert_eq ! ( field. read( 0x1234 ) , 0x103 ) ;
524
+ }
525
+
526
+ #[ test]
527
+ fn test_is_set ( ) {
528
+ let field = Field :: < u16 , ( ) > :: new ( 0xFF , 4 ) ;
529
+ assert_eq ! ( field. is_set( 0 ) , false ) ;
530
+ assert_eq ! ( field. is_set( 0xFFFF ) , true ) ;
531
+ assert_eq ! ( field. is_set( 0x0FF0 ) , true ) ;
532
+ assert_eq ! ( field. is_set( 0x1000 ) , false ) ;
533
+ assert_eq ! ( field. is_set( 0x0100 ) , true ) ;
534
+ assert_eq ! ( field. is_set( 0x0010 ) , true ) ;
535
+ assert_eq ! ( field. is_set( 0x0001 ) , false ) ;
536
+
537
+ for shift in 0 ..24 {
538
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , shift) ;
539
+ for x in 1 ..=0xFF {
540
+ assert_eq ! ( field. is_set( x << shift) , true ) ;
541
+ }
542
+ assert_eq ! ( field. is_set( !( 0xFF << shift) ) , false ) ;
543
+ }
544
+ }
545
+
546
+ #[ test]
547
+ fn test_read_as_enum ( ) {
548
+ let field = Field :: < u16 , ( ) > :: new ( 0x7 , 4 ) ;
549
+ assert_eq ! ( field. read_as_enum( 0x1234 ) , Some ( Foo :: Foo3 ) ) ;
550
+ assert_eq ! ( field. read_as_enum( 0x5678 ) , Some ( Foo :: Foo7 ) ) ;
551
+ assert_eq ! ( field. read_as_enum( 0xFFFF ) , Some ( Foo :: Foo7 ) ) ;
552
+ assert_eq ! ( field. read_as_enum( 0x0000 ) , Some ( Foo :: Foo0 ) ) ;
553
+ assert_eq ! ( field. read_as_enum( 0x0010 ) , Some ( Foo :: Foo1 ) ) ;
554
+ assert_eq ! ( field. read_as_enum( 0x1204 ) , Some ( Foo :: Foo0 ) ) ;
555
+
556
+ for shift in 0 ..29 {
557
+ let field = Field :: < u32 , ( ) > :: new ( 0x7 , shift) ;
558
+ for x in 0 ..8 {
559
+ assert_eq ! ( field. read_as_enum( x << shift) , Foo :: try_from( x) ) ;
560
+ }
561
+ }
562
+ }
563
+ }
564
+
565
+ mod field_value {
566
+ use crate :: fields:: Field ;
567
+
568
+ #[ test]
569
+ fn test_from ( ) {
570
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , 4 ) ;
571
+ assert_eq ! ( u32 :: from( field. val( 0 ) ) , 0 ) ;
572
+ assert_eq ! ( u32 :: from( field. val( 0xFFFFFFFF ) ) , 0xFF0 ) ;
573
+ assert_eq ! ( u32 :: from( field. val( 0x12 ) ) , 0x120 ) ;
574
+ assert_eq ! ( u32 :: from( field. val( 0x123 ) ) , 0x230 ) ;
575
+
576
+ for shift in 0 ..32 {
577
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , shift) ;
578
+ for x in 0 ..=0xFF {
579
+ assert_eq ! ( u32 :: from( field. val( x) ) , x << shift) ;
580
+ }
581
+ }
582
+ }
583
+
584
+ #[ test]
585
+ fn test_read_same_field ( ) {
586
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , 4 ) ;
587
+ assert_eq ! ( field. val( 0 ) . read( field) , 0 ) ;
588
+ assert_eq ! ( field. val( 0xFFFFFFFF ) . read( field) , 0xFF ) ;
589
+ assert_eq ! ( field. val( 0x12 ) . read( field) , 0x12 ) ;
590
+ assert_eq ! ( field. val( 0x123 ) . read( field) , 0x23 ) ;
591
+
592
+ for shift in 0 ..24 {
593
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , shift) ;
594
+ for x in 0 ..=0xFF {
595
+ assert_eq ! ( field. val( x) . read( field) , x) ;
596
+ }
597
+ }
598
+ }
599
+
600
+ #[ test]
601
+ fn test_read_disjoint_fields ( ) {
602
+ for shift in 0 ..24 {
603
+ let field1 = Field :: < u32 , ( ) > :: new ( 0xF0 , shift) ;
604
+ let field2 = Field :: < u32 , ( ) > :: new ( 0x0F , shift) ;
605
+ for x in 0 ..=0xFF {
606
+ assert_eq ! ( field1. val( x) . read( field2) , 0 ) ;
607
+ assert_eq ! ( field2. val( x) . read( field1) , 0 ) ;
608
+ }
609
+ }
610
+ for shift in 0 ..24 {
611
+ let field1 = Field :: < u32 , ( ) > :: new ( 0xF , shift) ;
612
+ let field2 = Field :: < u32 , ( ) > :: new ( 0xF , shift + 4 ) ;
613
+ for x in 0 ..=0xFF {
614
+ assert_eq ! ( field1. val( x) . read( field2) , 0 ) ;
615
+ assert_eq ! ( field2. val( x) . read( field1) , 0 ) ;
616
+ }
617
+ }
618
+ }
619
+
620
+ #[ test]
621
+ fn test_modify ( ) {
622
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , 4 ) ;
623
+ assert_eq ! ( field. val( 0x23 ) . modify( 0x0000 ) , 0x0230 ) ;
624
+ assert_eq ! ( field. val( 0x23 ) . modify( 0xFFFF ) , 0xF23F ) ;
625
+ assert_eq ! ( field. val( 0x23 ) . modify( 0x1234 ) , 0x1234 ) ;
626
+ assert_eq ! ( field. val( 0x23 ) . modify( 0x5678 ) , 0x5238 ) ;
627
+ }
628
+
629
+ #[ test]
630
+ fn test_matches_any ( ) {
631
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , 4 ) ;
632
+ assert_eq ! ( field. val( 0x23 ) . matches_any( 0x1234 ) , true ) ;
633
+ assert_eq ! ( field. val( 0x23 ) . matches_any( 0x5678 ) , true ) ;
634
+ assert_eq ! ( field. val( 0x23 ) . matches_any( 0x5008 ) , false ) ;
635
+
636
+ for shift in 0 ..24 {
637
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , shift) ;
638
+ for x in 0 ..=0xFF {
639
+ let field_value = field. val ( x) ;
640
+ for y in 1 ..=0xFF {
641
+ assert_eq ! ( field_value. matches_any( y << shift) , true ) ;
642
+ }
643
+ assert_eq ! ( field_value. matches_any( 0 ) , false ) ;
644
+ assert_eq ! ( field_value. matches_any( !( 0xFF << shift) ) , false ) ;
645
+ }
646
+ }
647
+ }
648
+
649
+ #[ test]
650
+ fn test_matches_all ( ) {
651
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , 4 ) ;
652
+ assert_eq ! ( field. val( 0x23 ) . matches_all( 0x1234 ) , true ) ;
653
+ assert_eq ! ( field. val( 0x23 ) . matches_all( 0x5678 ) , false ) ;
654
+
655
+ for shift in 0 ..24 {
656
+ let field = Field :: < u32 , ( ) > :: new ( 0xFF , shift) ;
657
+ for x in 0 ..=0xFF {
658
+ assert_eq ! ( field. val( x) . matches_all( x << shift) , true ) ;
659
+ assert_eq ! ( field. val( x + 1 ) . matches_all( x << shift) , false ) ;
660
+ }
661
+ }
662
+ }
663
+
664
+ #[ test]
665
+ fn test_add_disjoint_fields ( ) {
666
+ let field1 = Field :: < u32 , ( ) > :: new ( 0xFF , 24 ) ;
667
+ let field2 = Field :: < u32 , ( ) > :: new ( 0xFF , 16 ) ;
668
+ let field3 = Field :: < u32 , ( ) > :: new ( 0xFF , 8 ) ;
669
+ let field4 = Field :: < u32 , ( ) > :: new ( 0xFF , 0 ) ;
670
+ assert_eq ! (
671
+ u32 :: from(
672
+ field1. val( 0x12 ) + field2. val( 0x34 ) + field3. val( 0x56 ) + field4. val( 0x78 )
673
+ ) ,
674
+ 0x12345678
675
+ ) ;
676
+
677
+ for shift in 0 ..24 {
678
+ let field1 = Field :: < u32 , ( ) > :: new ( 0xF , shift) ;
679
+ let field2 = Field :: < u32 , ( ) > :: new ( 0xF , shift + 4 ) ;
680
+ for x in 0 ..=0xF {
681
+ for y in 0 ..=0xF {
682
+ assert_eq ! (
683
+ u32 :: from( field1. val( x) + field2. val( y) ) ,
684
+ ( x | ( y << 4 ) ) << shift
685
+ ) ;
686
+ }
687
+ }
688
+ }
689
+ }
690
+
691
+ #[ test]
692
+ fn test_add_assign_disjoint_fields ( ) {
693
+ let field1 = Field :: < u32 , ( ) > :: new ( 0xFF , 24 ) ;
694
+ let field2 = Field :: < u32 , ( ) > :: new ( 0xFF , 16 ) ;
695
+ let field3 = Field :: < u32 , ( ) > :: new ( 0xFF , 8 ) ;
696
+ let field4 = Field :: < u32 , ( ) > :: new ( 0xFF , 0 ) ;
697
+
698
+ let mut value = field1. val ( 0x12 ) ;
699
+ value += field2. val ( 0x34 ) ;
700
+ value += field3. val ( 0x56 ) ;
701
+ value += field4. val ( 0x78 ) ;
702
+ assert_eq ! ( u32 :: from( value) , 0x12345678 ) ;
703
+
704
+ for shift in 0 ..24 {
705
+ let field1 = Field :: < u32 , ( ) > :: new ( 0xF , shift) ;
706
+ let field2 = Field :: < u32 , ( ) > :: new ( 0xF , shift + 4 ) ;
707
+ for x in 0 ..=0xF {
708
+ for y in 0 ..=0xF {
709
+ let mut value = field1. val ( x) ;
710
+ value += field2. val ( y) ;
711
+ assert_eq ! ( u32 :: from( value) , ( x | ( y << 4 ) ) << shift) ;
712
+ }
713
+ }
714
+ }
715
+ }
716
+ }
717
+
718
+ // TODO: More unit tests here.
719
+ }
0 commit comments