@@ -768,5 +768,126 @@ public static void ReplaceWithJsonElement(string json)
768
768
769
769
Assert . Equal ( $ "[{ json } ]", array . ToJsonString ( ) ) ;
770
770
}
771
+
772
+ [ Fact ]
773
+ public static void RemoveAll_InvalidValuesAndEmptyArray ( )
774
+ {
775
+ JsonArray testArray = new ( ) ;
776
+ Assert . Throws < ArgumentNullException > ( ( ) => testArray . RemoveAll ( null ! ) ) ;
777
+ testArray . RemoveAll ( whatever => true ) ; // RemoveAll on empty array always succeeds.
778
+ }
779
+
780
+ [ Fact ]
781
+ public static void RemoveAll_FreshlyDeserialized ( )
782
+ {
783
+ JsonArray testArray = JsonSerializer . Deserialize < JsonArray > ( "[1,2,3,4,5]" ) ;
784
+ Assert . Equal ( 3 , testArray . RemoveAll ( val => val . GetValue < int > ( ) % 2 == 1 ) ) ;
785
+ JsonNodeTests . AssertDeepEqual ( new JsonArray { 2 , 4 } , testArray ) ;
786
+ }
787
+
788
+ [ Theory ]
789
+ [ InlineData ( new int [ ] { 1 , 1 , 1 , 1 , 1 } , new int [ ] { 1 , 1 , 1 , 1 , 1 } , 0 ) ]
790
+ [ InlineData ( new int [ ] { 2 , 1 , 1 , 1 , 1 } , new int [ ] { 1 , 1 , 1 , 1 } , 1 ) ]
791
+ [ InlineData ( new int [ ] { 1 , 1 , 1 , 1 , 2 } , new int [ ] { 1 , 1 , 1 , 1 } , 1 ) ]
792
+ [ InlineData ( new int [ ] { 1 , 1 , 1 , 2 , 1 } , new int [ ] { 1 , 1 , 1 , 1 } , 1 ) ]
793
+ public static void RemoveAll_SpecialValues ( int [ ] original , int [ ] expected , int removed )
794
+ {
795
+ // This test is to ensure the O(n) scan algorithm of JsonArray.RemoveAll is correct.
796
+
797
+ JsonArray testArray = new ( ToNodes ( original ) ) ;
798
+ Assert . Equal ( removed , testArray . RemoveAll ( Filter ) ) ;
799
+ JsonNodeTests . AssertDeepEqual ( new JsonArray ( ToNodes ( expected ) ) , testArray ) ;
800
+
801
+ bool Filter ( JsonNode ? v ) => v . GetValue < int > ( ) > 1 ;
802
+ JsonNode ? [ ] ToNodes ( int [ ] values ) => values . Select ( v => JsonValue . Create ( v ) ) . ToArray ( ) ;
803
+ }
804
+
805
+ [ Fact ]
806
+ public static void RemoveAll ( )
807
+ {
808
+ JsonArray testArray = [ 1 , 2 , 3 , 4 , 5 , 7 , 8 , 8 , 8 ] ;
809
+ JsonNode [ ] nodes = testArray . ToArray ( ) ;
810
+
811
+ // Correct amount of nodes are removed / remaining
812
+ Assert . Equal ( 5 , testArray . RemoveAll ( Filter ) ) ;
813
+ Assert . Equal ( 4 , testArray . Count ) ;
814
+
815
+ // The order is preserved
816
+ JsonNodeTests . AssertDeepEqual ( new JsonArray { 1 , 3 , 5 , 7 } , testArray ) ;
817
+
818
+ // Node parents are correctly handled
819
+ foreach ( JsonNode node in nodes )
820
+ {
821
+ if ( Filter ( node ) )
822
+ {
823
+ Assert . Null ( node . Parent ) ;
824
+ }
825
+ else
826
+ {
827
+ Assert . Same ( testArray , node . Parent ) ;
828
+ }
829
+ }
830
+
831
+ static bool Filter ( JsonNode node ) => node . GetValue < int > ( ) % 2 == 0 ;
832
+ }
833
+
834
+ [ Fact ]
835
+ public static void RemoveRange_InvalidAndSpecialValues ( )
836
+ {
837
+ JsonArray emptyArray = new ( ) ;
838
+ emptyArray . RemoveRange ( 0 , 0 ) ;
839
+ Assert . Throws < ArgumentException > ( ( ) => emptyArray . RemoveRange ( 0 , 1 ) ) ;
840
+ Assert . Throws < ArgumentException > ( ( ) => emptyArray . RemoveRange ( 1 , 0 ) ) ;
841
+
842
+ JsonArray testArray = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ;
843
+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => testArray . RemoveRange ( - 1 , 1 ) ) ;
844
+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => testArray . RemoveRange ( 1 , - 1 ) ) ;
845
+ Assert . Throws < ArgumentException > ( ( ) => testArray . RemoveRange ( 10 , 1 ) ) ;
846
+ Assert . Throws < ArgumentException > ( ( ) => testArray . RemoveRange ( 1 , 10 ) ) ;
847
+ }
848
+
849
+ [ Fact ]
850
+ public static void RemoveRange ( )
851
+ {
852
+ JsonArray testArray = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ;
853
+ JsonNode [ ] nodes = testArray . ToArray ( ) ;
854
+
855
+ const int RemoveStartIndex = 3 ;
856
+ const int RemoveLength = 3 ;
857
+
858
+ testArray . RemoveRange ( RemoveStartIndex , RemoveLength ) ;
859
+ Assert . Equal ( 8 - RemoveLength , testArray . Count ) ;
860
+ JsonNodeTests . AssertDeepEqual ( new JsonArray { 1 , 2 , 3 , 7 , 8 } , testArray ) ;
861
+
862
+ for ( int i = 0 ; i < nodes . Length ; i ++ )
863
+ {
864
+ if ( IsIndexRemoved ( i ) )
865
+ {
866
+ Assert . Null ( nodes [ i ] . Parent ) ;
867
+ }
868
+ else
869
+ {
870
+ Assert . Same ( testArray , nodes [ i ] . Parent ) ;
871
+ }
872
+ }
873
+
874
+ static bool IsIndexRemoved ( int originalIndex ) => originalIndex >= RemoveStartIndex &&
875
+ originalIndex < RemoveStartIndex + RemoveLength ;
876
+ }
877
+
878
+ [ Fact ]
879
+ public static void RemoveRange_FreshlyDeserialized ( )
880
+ {
881
+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => PrepareData ( ) . RemoveRange ( - 1 , 1 ) ) ;
882
+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => PrepareData ( ) . RemoveRange ( 1 , - 1 ) ) ;
883
+ Assert . Throws < ArgumentException > ( ( ) => PrepareData ( ) . RemoveRange ( 10 , 1 ) ) ;
884
+ Assert . Throws < ArgumentException > ( ( ) => PrepareData ( ) . RemoveRange ( 1 , 10 ) ) ;
885
+
886
+ JsonArray testArray = PrepareData ( ) ;
887
+ testArray . RemoveRange ( 2 , 2 ) ;
888
+ JsonNodeTests . AssertDeepEqual ( new JsonArray { 1 , 2 , 5 } , testArray ) ;
889
+
890
+ static JsonArray PrepareData ( ) => JsonSerializer . Deserialize < JsonArray > ( "[1,2,3,4,5]" ) ;
891
+ }
771
892
}
772
893
}
0 commit comments