@@ -937,4 +937,89 @@ mod tests {
937
937
938
938
TestResult :: from_bool ( double_complement == i)
939
939
}
940
+ #[ test]
941
+ fn test_basic_contains ( ) {
942
+ let outer = Interval :: Closed {
943
+ bound_pair : BoundPair :: new ( 0 , 10 ) . unwrap ( ) ,
944
+ } ;
945
+ let inner = Interval :: Closed {
946
+ bound_pair : BoundPair :: new ( 2 , 8 ) . unwrap ( ) ,
947
+ } ;
948
+ assert ! ( outer. contains( & inner) ) ;
949
+ assert ! ( !inner. contains( & outer) ) ;
950
+ }
951
+
952
+ #[ test]
953
+ fn test_empty_interval_contains ( ) {
954
+ let interval = Interval :: Closed {
955
+ bound_pair : BoundPair :: new ( 0 , 10 ) . unwrap ( ) ,
956
+ } ;
957
+ let empty = Interval :: Empty ;
958
+
959
+ // The empty interval is not contained by any interval
960
+ assert ! ( !interval. contains( & empty) ) ;
961
+ // Empty interval contains nothing, not even itself
962
+ assert ! ( !empty. contains( & empty) ) ;
963
+ assert ! ( !empty. contains( & interval) ) ;
964
+ }
965
+
966
+ #[ test]
967
+ fn test_unbounded_contains ( ) {
968
+ let unbounded = Interval :: Unbounded ;
969
+ let finite = Interval :: Closed {
970
+ bound_pair : BoundPair :: new ( 0 , 10 ) . unwrap ( ) ,
971
+ } ;
972
+
973
+ assert ! ( unbounded. contains( & finite) ) ;
974
+ assert ! ( !finite. contains( & unbounded) ) ;
975
+ }
976
+
977
+ #[ test]
978
+ fn test_mixed_bound_types ( ) {
979
+ let closed = Interval :: Closed {
980
+ bound_pair : BoundPair :: new ( 0 , 10 ) . unwrap ( ) ,
981
+ } ;
982
+ let open = Interval :: Open {
983
+ bound_pair : BoundPair :: new ( 0 , 10 ) . unwrap ( ) ,
984
+ } ;
985
+
986
+ // Closed interval contains its open counterpart
987
+ assert ! ( closed. contains( & open) ) ;
988
+ // Open interval does not contain its closed counterpart
989
+ assert ! ( !open. contains( & closed) ) ;
990
+ }
991
+
992
+ #[ test]
993
+ fn test_singleton_contains ( ) {
994
+ let singleton = Interval :: Singleton { at : 5 } ;
995
+ let containing = Interval :: Closed {
996
+ bound_pair : BoundPair :: new ( 0 , 10 ) . unwrap ( ) ,
997
+ } ;
998
+ let not_containing = Interval :: Open {
999
+ bound_pair : BoundPair :: new ( 0 , 5 ) . unwrap ( ) ,
1000
+ } ;
1001
+
1002
+ assert ! ( containing. contains( & singleton) ) ;
1003
+ // Open interval does not contain singleton on its bounds
1004
+ assert ! ( !not_containing. contains( & singleton) ) ;
1005
+ // Singleton only contains itself
1006
+ assert ! ( singleton. contains( & singleton) ) ;
1007
+ }
1008
+
1009
+ #[ quickcheck]
1010
+ fn prop_contains_transitive ( a : f64 , b : f64 , c : f64 ) -> TestResult {
1011
+ if let ( Some ( bp1) , Some ( bp2) , Some ( bp3) ) = (
1012
+ BoundPair :: new ( a, b) ,
1013
+ BoundPair :: new ( b, c) ,
1014
+ BoundPair :: new ( a, c) ,
1015
+ ) {
1016
+ let i1 = Interval :: Closed { bound_pair : bp1 } ;
1017
+ let i2 = Interval :: Closed { bound_pair : bp2 } ;
1018
+ let i3 = Interval :: Closed { bound_pair : bp3 } ;
1019
+
1020
+ TestResult :: from_bool ( !( i1. contains ( & i2) && i2. contains ( & i3) ) || i1. contains ( & i3) )
1021
+ } else {
1022
+ TestResult :: discard ( )
1023
+ }
1024
+ }
940
1025
}
0 commit comments