@@ -853,7 +853,11 @@ pub fn call_successors_fn(
853
853
854
854
#[ cfg( test) ]
855
855
mod tests {
856
- use std:: { collections:: HashSet , sync:: Arc } ;
856
+ use std:: {
857
+ collections:: HashSet ,
858
+ sync:: Arc ,
859
+ time:: { Duration , Instant } ,
860
+ } ;
857
861
858
862
use azalea_core:: position:: { BlockPos , ChunkPos , Vec3 } ;
859
863
use azalea_world:: { Chunk , ChunkStorage , PartialChunkStorage } ;
@@ -904,6 +908,22 @@ mod tests {
904
908
simulation
905
909
}
906
910
911
+ pub fn assert_simulation_reaches ( simulation : & mut Simulation , ticks : usize , end_pos : BlockPos ) {
912
+ // wait until the bot starts moving
913
+ let start_pos = simulation. position ( ) ;
914
+ let start_time = Instant :: now ( ) ;
915
+ while simulation. position ( ) == start_pos
916
+ && start_time. elapsed ( ) < Duration :: from_millis ( 500 )
917
+ {
918
+ simulation. tick ( ) ;
919
+ std:: thread:: yield_now ( ) ;
920
+ }
921
+ for _ in 0 ..ticks {
922
+ simulation. tick ( ) ;
923
+ }
924
+ assert_eq ! ( BlockPos :: from( simulation. position( ) ) , end_pos, ) ;
925
+ }
926
+
907
927
#[ test]
908
928
fn test_simple_forward ( ) {
909
929
let mut partial_chunks = PartialChunkStorage :: default ( ) ;
@@ -913,13 +933,7 @@ mod tests {
913
933
BlockPos :: new ( 0 , 71 , 1 ) ,
914
934
vec ! [ BlockPos :: new( 0 , 70 , 0 ) , BlockPos :: new( 0 , 70 , 1 ) ] ,
915
935
) ;
916
- for _ in 0 ..20 {
917
- simulation. tick ( ) ;
918
- }
919
- assert_eq ! (
920
- BlockPos :: from( simulation. position( ) ) ,
921
- BlockPos :: new( 0 , 71 , 1 )
922
- ) ;
936
+ assert_simulation_reaches ( & mut simulation, 20 , BlockPos :: new ( 0 , 71 , 1 ) ) ;
923
937
}
924
938
925
939
#[ test]
@@ -937,13 +951,7 @@ mod tests {
937
951
BlockPos :: new( 2 , 72 , 1 ) ,
938
952
] ,
939
953
) ;
940
- for _ in 0 ..30 {
941
- simulation. tick ( ) ;
942
- }
943
- assert_eq ! (
944
- BlockPos :: from( simulation. position( ) ) ,
945
- BlockPos :: new( 2 , 71 , 2 )
946
- ) ;
954
+ assert_simulation_reaches ( & mut simulation, 30 , BlockPos :: new ( 2 , 71 , 2 ) ) ;
947
955
}
948
956
949
957
#[ test]
@@ -965,13 +973,7 @@ mod tests {
965
973
BlockPos :: new( 5 , 75 , 0 ) ,
966
974
] ,
967
975
) ;
968
- for _ in 0 ..120 {
969
- simulation. tick ( ) ;
970
- }
971
- assert_eq ! (
972
- BlockPos :: from( simulation. position( ) ) ,
973
- BlockPos :: new( 5 , 76 , 0 )
974
- ) ;
976
+ assert_simulation_reaches ( & mut simulation, 120 , BlockPos :: new ( 5 , 76 , 0 ) ) ;
975
977
}
976
978
977
979
#[ test]
@@ -983,13 +985,7 @@ mod tests {
983
985
BlockPos :: new ( 0 , 71 , 3 ) ,
984
986
vec ! [ BlockPos :: new( 0 , 70 , 0 ) , BlockPos :: new( 0 , 70 , 3 ) ] ,
985
987
) ;
986
- for _ in 0 ..40 {
987
- simulation. tick ( ) ;
988
- }
989
- assert_eq ! (
990
- BlockPos :: from( simulation. position( ) ) ,
991
- BlockPos :: new( 0 , 71 , 3 )
992
- ) ;
988
+ assert_simulation_reaches ( & mut simulation, 40 , BlockPos :: new ( 0 , 71 , 3 ) ) ;
993
989
}
994
990
995
991
#[ test]
@@ -1008,13 +1004,7 @@ mod tests {
1008
1004
BlockPos :: new( 3 , 66 , 4 ) ,
1009
1005
] ,
1010
1006
) ;
1011
- for _ in 0 ..100 {
1012
- simulation. tick ( ) ;
1013
- }
1014
- assert_eq ! (
1015
- BlockPos :: from( simulation. position( ) ) ,
1016
- BlockPos :: new( 3 , 67 , 4 )
1017
- ) ;
1007
+ assert_simulation_reaches ( & mut simulation, 100 , BlockPos :: new ( 3 , 67 , 4 ) ) ;
1018
1008
}
1019
1009
1020
1010
#[ test]
@@ -1031,13 +1021,7 @@ mod tests {
1031
1021
BlockPos :: new( 0 , 69 , 5 ) ,
1032
1022
] ,
1033
1023
) ;
1034
- for _ in 0 ..40 {
1035
- simulation. tick ( ) ;
1036
- }
1037
- assert_eq ! (
1038
- BlockPos :: from( simulation. position( ) ) ,
1039
- BlockPos :: new( 0 , 70 , 5 )
1040
- ) ;
1024
+ assert_simulation_reaches ( & mut simulation, 40 , BlockPos :: new ( 0 , 70 , 5 ) ) ;
1041
1025
}
1042
1026
1043
1027
#[ test]
@@ -1054,13 +1038,7 @@ mod tests {
1054
1038
BlockPos :: new( 0 , 67 , 3 ) ,
1055
1039
] ,
1056
1040
) ;
1057
- for _ in 0 ..60 {
1058
- simulation. tick ( ) ;
1059
- }
1060
- assert_eq ! (
1061
- BlockPos :: from( simulation. position( ) ) ,
1062
- BlockPos :: new( 0 , 68 , 3 )
1063
- ) ;
1041
+ assert_simulation_reaches ( & mut simulation, 60 , BlockPos :: new ( 0 , 68 , 3 ) ) ;
1064
1042
}
1065
1043
1066
1044
#[ test]
@@ -1077,13 +1055,7 @@ mod tests {
1077
1055
BlockPos :: new( 3 , 73 , 0 ) ,
1078
1056
] ,
1079
1057
) ;
1080
- for _ in 0 ..60 {
1081
- simulation. tick ( ) ;
1082
- }
1083
- assert_eq ! (
1084
- BlockPos :: from( simulation. position( ) ) ,
1085
- BlockPos :: new( 3 , 74 , 0 )
1086
- ) ;
1058
+ assert_simulation_reaches ( & mut simulation, 60 , BlockPos :: new ( 3 , 74 , 0 ) ) ;
1087
1059
}
1088
1060
1089
1061
#[ test]
@@ -1101,12 +1073,6 @@ mod tests {
1101
1073
BlockPos :: new( 4 , 70 , 12 ) ,
1102
1074
] ,
1103
1075
) ;
1104
- for _ in 0 ..80 {
1105
- simulation. tick ( ) ;
1106
- }
1107
- assert_eq ! (
1108
- BlockPos :: from( simulation. position( ) ) ,
1109
- BlockPos :: new( 4 , 71 , 12 )
1110
- ) ;
1076
+ assert_simulation_reaches ( & mut simulation, 80 , BlockPos :: new ( 4 , 71 , 12 ) ) ;
1111
1077
}
1112
1078
}
0 commit comments