@@ -844,72 +844,79 @@ TEST(remove,remove_node_on_pos){
844
844
}
845
845
EXPECT_EQ (actual,expectation);
846
846
}
847
- TEST (remove, stress_remove_various_cases){
848
- // Case 1: remove dari list kosong
849
- forward_lists<int > list_empty;
850
- EXPECT_EQ (list_empty.get_size (),0 );
851
- list_empty.remove (10 ); // should not crash
852
- EXPECT_EQ (list_empty.get_size (),0 );
853
-
854
- // Case 2: remove head (pos=0)
855
- forward_lists<int > list1 = {1 ,2 ,3 ,4 ,5 };
856
- list1.remove (0 );
857
- EXPECT_EQ (list1.get_size (),4 );
858
- std::vector<int > expected1 = {2 ,3 ,4 ,5 };
859
- std::vector<int > actual1;
860
- for (auto x: list1) actual1.push_back (x);
861
- EXPECT_EQ (actual1, expected1);
862
-
863
- // Case 3: remove tail by value
864
- forward_lists<int > list2 = {10 ,20 ,30 };
865
- list2.remove (30 );
866
- EXPECT_EQ (list2.get_size (),2 );
867
- EXPECT_EQ (list2.back (),20 );
868
-
869
- // Case 4: remove middle value
870
- forward_lists<int > list3 = {7 ,8 ,9 ,10 };
871
- list3.remove (9 );
872
- EXPECT_EQ (list3.get_size (),3 );
873
- std::vector<int > expected3 = {7 ,8 ,10 };
874
- std::vector<int > actual3;
875
- for (auto x: list3) actual3.push_back (x);
876
- EXPECT_EQ (actual3, expected3);
877
-
878
- // Case 5: remove semua elemen dengan loop (stress kecil)
879
- forward_lists<int > list4 = {1 ,2 ,3 ,4 ,5 };
880
- for (int i=0 ;i<5 ;i++){
881
- list4.remove (0 ); // selalu hapus head
882
- }
883
- EXPECT_EQ (list4.get_size (),0 );
884
- EXPECT_EQ (list4.begin (), list4.end ()); // list kosong
885
-
886
- // Case 6: hapus value yang tidak ada
887
- forward_lists<int > list5 = {1 ,2 ,3 };
888
- list5.remove (99 ); // nothing to remove
889
- EXPECT_EQ (list5.get_size (),3 );
890
- std::vector<int > expected5 = {1 ,2 ,3 };
891
- std::vector<int > actual5;
892
- for (auto x: list5) actual5.push_back (x);
893
- EXPECT_EQ (actual5, expected5);
894
-
895
- // Case 7: stress banyak elemen
896
- forward_lists<int > list6;
897
- for (int i=0 ;i<1000 ;i++){
898
- list6.push_back (i);
899
- }
900
- EXPECT_EQ (list6.get_size (),1000 );
901
-
902
- // remove beberapa posisi penting
903
- list6.remove (0 ); // hapus head
904
- list6.remove (500 ); // hapus tengah
905
- list6.remove (list6.get_size ()-1 ); // hapus tail
906
-
907
- EXPECT_EQ (list6.get_size (),997 );
908
-
909
- // sanity check: nilai 0 sudah terhapus
847
+ TEST (remove, remove_from_empty_list) {
848
+ forward_lists<int > list;
849
+ EXPECT_EQ (list.get_size (), 0 );
850
+ list.remove (10 ); // harusnya aman
851
+ EXPECT_EQ (list.get_size (), 0 );
852
+ }
853
+
854
+ TEST (remove, remove_head_by_pos) {
855
+ forward_lists<int > list = {1 ,2 ,3 ,4 ,5 };
856
+ list.remove (0 );
857
+ EXPECT_EQ (list.get_size (), 4 );
858
+
859
+ std::vector<int > expected = {2 ,3 ,4 ,5 };
860
+ std::vector<int > actual;
861
+ for (auto x: list) actual.push_back (x);
862
+ EXPECT_EQ (actual, expected);
863
+ }
864
+
865
+ TEST (remove, remove_tail_by_value) {
866
+ forward_lists<int > list = {10 ,20 ,30 };
867
+ list.remove (30 );
868
+ EXPECT_EQ (list.get_size (), 2 );
869
+ EXPECT_EQ (list.back (), 20 );
870
+ }
871
+
872
+ TEST (remove, remove_middle_by_value) {
873
+ forward_lists<int > list = {7 ,8 ,9 ,10 };
874
+ list.remove (9 );
875
+ EXPECT_EQ (list.get_size (), 3 );
876
+
877
+ std::vector<int > expected = {7 ,8 ,10 };
878
+ std::vector<int > actual;
879
+ for (auto x: list) actual.push_back (x);
880
+ EXPECT_EQ (actual, expected);
881
+ }
882
+
883
+ TEST (remove, remove_all_elements_by_pos) {
884
+ forward_lists<int > list = {1 ,2 ,3 ,4 ,5 };
885
+ for (int i=0 ; i<5 ; i++) {
886
+ list.remove (0 ); // selalu hapus head
887
+ }
888
+ EXPECT_EQ (list.get_size (), 0 );
889
+ EXPECT_EQ (list.begin (), list.end ()); // list kosong
890
+ }
891
+
892
+ TEST (remove, remove_non_existent_value) {
893
+ forward_lists<int > list = {1 ,2 ,3 };
894
+ list.remove (99 ); // tidak ada 99
895
+ EXPECT_EQ (list.get_size (), 3 );
896
+
897
+ std::vector<int > expected = {1 ,2 ,3 };
898
+ std::vector<int > actual;
899
+ for (auto x: list) actual.push_back (x);
900
+ EXPECT_EQ (actual, expected);
901
+ }
902
+
903
+ TEST (remove, stress_remove_many_elements) {
904
+ forward_lists<int > list;
905
+ for (int i=0 ; i<1000 ; i++) {
906
+ list.push_back (i);
907
+ }
908
+ EXPECT_EQ (list.get_size (), 1000 );
909
+
910
+ list.remove (0 ); // hapus head
911
+ list.remove (500 ); // hapus tengah
912
+ list.remove (list.get_size ()-1 ); // hapus tail
913
+
914
+ EXPECT_EQ (list.get_size (), 997 );
915
+
916
+ // pastikan nilai 0 sudah tidak ada
910
917
bool found0 = false ;
911
- for (auto x: list6) {
912
- if (x == 0 ) found0 = true ;
918
+ for (auto x: list) {
919
+ if (x == 0 ) { found0 = true ; break ; }
913
920
}
914
921
EXPECT_FALSE (found0);
915
922
}
0 commit comments