summaryrefslogblamecommitdiff
path: root/lib/list-test.c
blob: d374cf5d1a5790ee7be762630434acbcb1eca31a (plain) (tree)
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202


































































































































































                                                                   



















                                                                        








































































                                                                    

















                                                          




















































































































































































































































































                                                                               



















                                                                              



















































































































































                                                                               
                        



















                                                                     
                        

























                                                       
                                                    


                                                  
                                           













                                                    
                                                 


















                                                          











































































































































































































































































































































































































                                                                       

                         
// SPDX-License-Identifier: GPL-2.0
/*
 * KUnit test for the Kernel Linked-list structures.
 *
 * Copyright (C) 2019, Google LLC.
 * Author: David Gow <davidgow@google.com>
 */
#include <kunit/test.h>

#include <linux/list.h>

struct list_test_struct {
	int data;
	struct list_head list;
};

static void list_test_list_init(struct kunit *test)
{
	/* Test the different ways of initialising a list. */
	struct list_head list1 = LIST_HEAD_INIT(list1);
	struct list_head list2;
	LIST_HEAD(list3);
	struct list_head *list4;
	struct list_head *list5;

	INIT_LIST_HEAD(&list2);

	list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
	INIT_LIST_HEAD(list4);

	list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
	memset(list5, 0xFF, sizeof(*list5));
	INIT_LIST_HEAD(list5);

	/* list_empty_careful() checks both next and prev. */
	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list1));
	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list3));
	KUNIT_EXPECT_TRUE(test, list_empty_careful(list4));
	KUNIT_EXPECT_TRUE(test, list_empty_careful(list5));

	kfree(list4);
	kfree(list5);
}

static void list_test_list_add(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list);

	list_add(&a, &list);
	list_add(&b, &list);

	/* should be [list] -> b -> a */
	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
	KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
}

static void list_test_list_add_tail(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list);

	list_add_tail(&a, &list);
	list_add_tail(&b, &list);

	/* should be [list] -> a -> b */
	KUNIT_EXPECT_PTR_EQ(test, list.next, &a);
	KUNIT_EXPECT_PTR_EQ(test, a.prev, &list);
	KUNIT_EXPECT_PTR_EQ(test, a.next, &b);
}

static void list_test_list_del(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list);

	list_add_tail(&a, &list);
	list_add_tail(&b, &list);

	/* before: [list] -> a -> b */
	list_del(&a);

	/* now: [list] -> b */
	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
}

static void list_test_list_replace(struct kunit *test)
{
	struct list_head a_old, a_new, b;
	LIST_HEAD(list);

	list_add_tail(&a_old, &list);
	list_add_tail(&b, &list);

	/* before: [list] -> a_old -> b */
	list_replace(&a_old, &a_new);

	/* now: [list] -> a_new -> b */
	KUNIT_EXPECT_PTR_EQ(test, list.next, &a_new);
	KUNIT_EXPECT_PTR_EQ(test, b.prev, &a_new);
}

static void list_test_list_replace_init(struct kunit *test)
{
	struct list_head a_old, a_new, b;
	LIST_HEAD(list);

	list_add_tail(&a_old, &list);
	list_add_tail(&b, &list);

	/* before: [list] -> a_old -> b */
	list_replace_init(&a_old, &a_new);

	/* now: [list] -> a_new -> b */
	KUNIT_EXPECT_PTR_EQ(test, list.next, &a_new);
	KUNIT_EXPECT_PTR_EQ(test, b.prev, &a_new);

	/* check a_old is empty (initialized) */
	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a_old));
}

static void list_test_list_swap(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list);

	list_add_tail(&a, &list);
	list_add_tail(&b, &list);

	/* before: [list] -> a -> b */
	list_swap(&a, &b);

	/* after: [list] -> b -> a */
	KUNIT_EXPECT_PTR_EQ(test, &b, list.next);
	KUNIT_EXPECT_PTR_EQ(test, &a, list.prev);

	KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
	KUNIT_EXPECT_PTR_EQ(test, &list, b.prev);

	KUNIT_EXPECT_PTR_EQ(test, &list, a.next);
	KUNIT_EXPECT_PTR_EQ(test, &b, a.prev);
}

static void list_test_list_del_init(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list);

	list_add_tail(&a, &list);
	list_add_tail(&b, &list);

	/* before: [list] -> a -> b */
	list_del_init(&a);
	/* after: [list] -> b, a initialised */

	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
}

static void list_test_list_del_init_careful(struct kunit *test)
{
	/* NOTE: This test only checks the behaviour of this function in
	 * isolation. It does not verify memory model guarantees.
	 */
	struct list_head a, b;
	LIST_HEAD(list);

	list_add_tail(&a, &list);
	list_add_tail(&b, &list);

	/* before: [list] -> a -> b */
	list_del_init_careful(&a);
	/* after: [list] -> b, a initialised */

	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
}

static void list_test_list_move(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list1);
	LIST_HEAD(list2);

	list_add_tail(&a, &list1);
	list_add_tail(&b, &list2);

	/* before: [list1] -> a, [list2] -> b */
	list_move(&a, &list2);
	/* after: [list1] empty, [list2] -> a -> b */

	KUNIT_EXPECT_TRUE(test, list_empty(&list1));

	KUNIT_EXPECT_PTR_EQ(test, &a, list2.next);
	KUNIT_EXPECT_PTR_EQ(test, &b, a.next);
}

static void list_test_list_move_tail(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list1);
	LIST_HEAD(list2);

	list_add_tail(&a, &list1);
	list_add_tail(&b, &list2);

	/* before: [list1] -> a, [list2] -> b */
	list_move_tail(&a, &list2);
	/* after: [list1] empty, [list2] -> b -> a */

	KUNIT_EXPECT_TRUE(test, list_empty(&list1));

	KUNIT_EXPECT_PTR_EQ(test, &b, list2.next);
	KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
}

static void list_test_list_bulk_move_tail(struct kunit *test)
{
	struct list_head a, b, c, d, x, y;
	struct list_head *list1_values[] = { &x, &b, &c, &y };
	struct list_head *list2_values[] = { &a, &d };
	struct list_head *ptr;
	LIST_HEAD(list1);
	LIST_HEAD(list2);
	int i = 0;

	list_add_tail(&x, &list1);
	list_add_tail(&y, &list1);

	list_add_tail(&a, &list2);
	list_add_tail(&b, &list2);
	list_add_tail(&c, &list2);
	list_add_tail(&d, &list2);

	/* before: [list1] -> x -> y, [list2] -> a -> b -> c -> d */
	list_bulk_move_tail(&y, &b, &c);
	/* after: [list1] -> x -> b -> c -> y, [list2] -> a -> d */

	list_for_each(ptr, &list1) {
		KUNIT_EXPECT_PTR_EQ(test, ptr, list1_values[i]);
		i++;
	}
	KUNIT_EXPECT_EQ(test, i, 4);
	i = 0;
	list_for_each(ptr, &list2) {
		KUNIT_EXPECT_PTR_EQ(test, ptr, list2_values[i]);
		i++;
	}
	KUNIT_EXPECT_EQ(test, i, 2);
}

static void list_test_list_is_head(struct kunit *test)
{
	struct list_head a, b, c;

	/* Two lists: [a] -> b, [c] */
	INIT_LIST_HEAD(&a);
	INIT_LIST_HEAD(&c);
	list_add_tail(&b, &a);

	KUNIT_EXPECT_TRUE_MSG(test, list_is_head(&a, &a),
		"Head element of same list");
	KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &b),
		"Non-head element of same list");
	KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &c),
		"Head element of different list");
}


static void list_test_list_is_first(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list);

	list_add_tail(&a, &list);
	list_add_tail(&b, &list);

	KUNIT_EXPECT_TRUE(test, list_is_first(&a, &list));
	KUNIT_EXPECT_FALSE(test, list_is_first(&b, &list));
}

static void list_test_list_is_last(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list);

	list_add_tail(&a, &list);
	list_add_tail(&b, &list);

	KUNIT_EXPECT_FALSE(test, list_is_last(&a, &list));
	KUNIT_EXPECT_TRUE(test, list_is_last(&b, &list));
}

static void list_test_list_empty(struct kunit *test)
{
	struct list_head a;
	LIST_HEAD(list1);
	LIST_HEAD(list2);

	list_add_tail(&a, &list1);

	KUNIT_EXPECT_FALSE(test, list_empty(&list1));
	KUNIT_EXPECT_TRUE(test, list_empty(&list2));
}

static void list_test_list_empty_careful(struct kunit *test)
{
	/* This test doesn't check correctness under concurrent access */
	struct list_head a;
	LIST_HEAD(list1);
	LIST_HEAD(list2);

	list_add_tail(&a, &list1);

	KUNIT_EXPECT_FALSE(test, list_empty_careful(&list1));
	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
}

static void list_test_list_rotate_left(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list);

	list_add_tail(&a, &list);
	list_add_tail(&b, &list);

	/* before: [list] -> a -> b */
	list_rotate_left(&list);
	/* after: [list] -> b -> a */

	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
	KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
}

static void list_test_list_rotate_to_front(struct kunit *test)
{
	struct list_head a, b, c, d;
	struct list_head *list_values[] = { &c, &d, &a, &b };
	struct list_head *ptr;
	LIST_HEAD(list);
	int i = 0;

	list_add_tail(&a, &list);
	list_add_tail(&b, &list);
	list_add_tail(&c, &list);
	list_add_tail(&d, &list);

	/* before: [list] -> a -> b -> c -> d */
	list_rotate_to_front(&c, &list);
	/* after: [list] -> c -> d -> a -> b */

	list_for_each(ptr, &list) {
		KUNIT_EXPECT_PTR_EQ(test, ptr, list_values[i]);
		i++;
	}
	KUNIT_EXPECT_EQ(test, i, 4);
}

static void list_test_list_is_singular(struct kunit *test)
{
	struct list_head a, b;
	LIST_HEAD(list);

	/* [list] empty */
	KUNIT_EXPECT_FALSE(test, list_is_singular(&list));

	list_add_tail(&a, &list);

	/* [list] -> a */
	KUNIT_EXPECT_TRUE(test, list_is_singular(&list));

	list_add_tail(&b, &list);

	/* [list] -> a -> b */
	KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
}

static void list_test_list_cut_position(struct kunit *test)
{
	struct list_head entries[3], *cur;
	LIST_HEAD(list1);
	LIST_HEAD(list2);
	int i = 0;

	list_add_tail(&entries[0], &list1);
	list_add_tail(&entries[1], &list1);
	list_add_tail(&entries[2], &list1);

	/* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
	list_cut_position(&list2, &list1, &entries[1]);
	/* after: [list2] -> entries[0] -> entries[1], [list1] -> entries[2] */

	list_for_each(cur, &list2) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 2);

	list_for_each(cur, &list1) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i++;
	}
}

static void list_test_list_cut_before(struct kunit *test)
{
	struct list_head entries[3], *cur;
	LIST_HEAD(list1);
	LIST_HEAD(list2);
	int i = 0;

	list_add_tail(&entries[0], &list1);
	list_add_tail(&entries[1], &list1);
	list_add_tail(&entries[2], &list1);

	/* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
	list_cut_before(&list2, &list1, &entries[1]);
	/* after: [list2] -> entries[0], [list1] -> entries[1] -> entries[2] */

	list_for_each(cur, &list2) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 1);

	list_for_each(cur, &list1) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i++;
	}
}

static void list_test_list_splice(struct kunit *test)
{
	struct list_head entries[5], *cur;
	LIST_HEAD(list1);
	LIST_HEAD(list2);
	int i = 0;

	list_add_tail(&entries[0], &list1);
	list_add_tail(&entries[1], &list1);
	list_add_tail(&entries[2], &list2);
	list_add_tail(&entries[3], &list2);
	list_add_tail(&entries[4], &list1);

	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
	list_splice(&list2, &entries[1]);
	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */

	list_for_each(cur, &list1) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 5);
}

static void list_test_list_splice_tail(struct kunit *test)
{
	struct list_head entries[5], *cur;
	LIST_HEAD(list1);
	LIST_HEAD(list2);
	int i = 0;

	list_add_tail(&entries[0], &list1);
	list_add_tail(&entries[1], &list1);
	list_add_tail(&entries[2], &list2);
	list_add_tail(&entries[3], &list2);
	list_add_tail(&entries[4], &list1);

	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
	list_splice_tail(&list2, &entries[4]);
	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */

	list_for_each(cur, &list1) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 5);
}

static void list_test_list_splice_init(struct kunit *test)
{
	struct list_head entries[5], *cur;
	LIST_HEAD(list1);
	LIST_HEAD(list2);
	int i = 0;

	list_add_tail(&entries[0], &list1);
	list_add_tail(&entries[1], &list1);
	list_add_tail(&entries[2], &list2);
	list_add_tail(&entries[3], &list2);
	list_add_tail(&entries[4], &list1);

	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
	list_splice_init(&list2, &entries[1]);
	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */

	list_for_each(cur, &list1) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 5);

	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
}

static void list_test_list_splice_tail_init(struct kunit *test)
{
	struct list_head entries[5], *cur;
	LIST_HEAD(list1);
	LIST_HEAD(list2);
	int i = 0;

	list_add_tail(&entries[0], &list1);
	list_add_tail(&entries[1], &list1);
	list_add_tail(&entries[2], &list2);
	list_add_tail(&entries[3], &list2);
	list_add_tail(&entries[4], &list1);

	/* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
	list_splice_tail_init(&list2, &entries[4]);
	/* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */

	list_for_each(cur, &list1) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 5);

	KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
}

static void list_test_list_entry(struct kunit *test)
{
	struct list_test_struct test_struct;

	KUNIT_EXPECT_PTR_EQ(test, &test_struct, list_entry(&(test_struct.list),
				struct list_test_struct, list));
}

static void list_test_list_entry_is_head(struct kunit *test)
{
	struct list_test_struct test_struct1, test_struct2, test_struct3;

	INIT_LIST_HEAD(&test_struct1.list);
	INIT_LIST_HEAD(&test_struct3.list);

	list_add_tail(&test_struct2.list, &test_struct1.list);

	KUNIT_EXPECT_TRUE_MSG(test,
		list_entry_is_head((&test_struct1), &test_struct1.list, list),
		"Head element of same list");
	KUNIT_EXPECT_FALSE_MSG(test,
		list_entry_is_head((&test_struct2), &test_struct1.list, list),
		"Non-head element of same list");
	KUNIT_EXPECT_FALSE_MSG(test,
		list_entry_is_head((&test_struct3), &test_struct1.list, list),
		"Head element of different list");
}

static void list_test_list_first_entry(struct kunit *test)
{
	struct list_test_struct test_struct1, test_struct2;
	LIST_HEAD(list);

	list_add_tail(&test_struct1.list, &list);
	list_add_tail(&test_struct2.list, &list);


	KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_first_entry(&list,
				struct list_test_struct, list));
}

static void list_test_list_last_entry(struct kunit *test)
{
	struct list_test_struct test_struct1, test_struct2;
	LIST_HEAD(list);

	list_add_tail(&test_struct1.list, &list);
	list_add_tail(&test_struct2.list, &list);


	KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_last_entry(&list,
				struct list_test_struct, list));
}

static void list_test_list_first_entry_or_null(struct kunit *test)
{
	struct list_test_struct test_struct1, test_struct2;
	LIST_HEAD(list);

	KUNIT_EXPECT_FALSE(test, list_first_entry_or_null(&list,
				struct list_test_struct, list));

	list_add_tail(&test_struct1.list, &list);
	list_add_tail(&test_struct2.list, &list);

	KUNIT_EXPECT_PTR_EQ(test, &test_struct1,
			list_first_entry_or_null(&list,
				struct list_test_struct, list));
}

static void list_test_list_next_entry(struct kunit *test)
{
	struct list_test_struct test_struct1, test_struct2;
	LIST_HEAD(list);

	list_add_tail(&test_struct1.list, &list);
	list_add_tail(&test_struct2.list, &list);


	KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_next_entry(&test_struct1,
				list));
}

static void list_test_list_prev_entry(struct kunit *test)
{
	struct list_test_struct test_struct1, test_struct2;
	LIST_HEAD(list);

	list_add_tail(&test_struct1.list, &list);
	list_add_tail(&test_struct2.list, &list);


	KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_prev_entry(&test_struct2,
				list));
}

static void list_test_list_for_each(struct kunit *test)
{
	struct list_head entries[3], *cur;
	LIST_HEAD(list);
	int i = 0;

	list_add_tail(&entries[0], &list);
	list_add_tail(&entries[1], &list);
	list_add_tail(&entries[2], &list);

	list_for_each(cur, &list) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 3);
}

static void list_test_list_for_each_prev(struct kunit *test)
{
	struct list_head entries[3], *cur;
	LIST_HEAD(list);
	int i = 2;

	list_add_tail(&entries[0], &list);
	list_add_tail(&entries[1], &list);
	list_add_tail(&entries[2], &list);

	list_for_each_prev(cur, &list) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i--;
	}

	KUNIT_EXPECT_EQ(test, i, -1);
}

static void list_test_list_for_each_safe(struct kunit *test)
{
	struct list_head entries[3], *cur, *n;
	LIST_HEAD(list);
	int i = 0;


	list_add_tail(&entries[0], &list);
	list_add_tail(&entries[1], &list);
	list_add_tail(&entries[2], &list);

	list_for_each_safe(cur, n, &list) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		list_del(&entries[i]);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 3);
	KUNIT_EXPECT_TRUE(test, list_empty(&list));
}

static void list_test_list_for_each_prev_safe(struct kunit *test)
{
	struct list_head entries[3], *cur, *n;
	LIST_HEAD(list);
	int i = 2;

	list_add_tail(&entries[0], &list);
	list_add_tail(&entries[1], &list);
	list_add_tail(&entries[2], &list);

	list_for_each_prev_safe(cur, n, &list) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		list_del(&entries[i]);
		i--;
	}

	KUNIT_EXPECT_EQ(test, i, -1);
	KUNIT_EXPECT_TRUE(test, list_empty(&list));
}

static void list_test_list_for_each_entry(struct kunit *test)
{
	struct list_test_struct entries[5], *cur;
	LIST_HEAD(list);
	int i = 0;

	for (i = 0; i < 5; ++i) {
		entries[i].data = i;
		list_add_tail(&entries[i].list, &list);
	}

	i = 0;

	list_for_each_entry(cur, &list, list) {
		KUNIT_EXPECT_EQ(test, cur->data, i);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 5);
}

static void list_test_list_for_each_entry_reverse(struct kunit *test)
{
	struct list_test_struct entries[5], *cur;
	LIST_HEAD(list);
	int i = 0;

	for (i = 0; i < 5; ++i) {
		entries[i].data = i;
		list_add_tail(&entries[i].list, &list);
	}

	i = 4;

	list_for_each_entry_reverse(cur, &list, list) {
		KUNIT_EXPECT_EQ(test, cur->data, i);
		i--;
	}

	KUNIT_EXPECT_EQ(test, i, -1);
}

static struct kunit_case list_test_cases[] = {
	KUNIT_CASE(list_test_list_init),
	KUNIT_CASE(list_test_list_add),
	KUNIT_CASE(list_test_list_add_tail),
	KUNIT_CASE(list_test_list_del),
	KUNIT_CASE(list_test_list_replace),
	KUNIT_CASE(list_test_list_replace_init),
	KUNIT_CASE(list_test_list_swap),
	KUNIT_CASE(list_test_list_del_init),
	KUNIT_CASE(list_test_list_del_init_careful),
	KUNIT_CASE(list_test_list_move),
	KUNIT_CASE(list_test_list_move_tail),
	KUNIT_CASE(list_test_list_bulk_move_tail),
	KUNIT_CASE(list_test_list_is_head),
	KUNIT_CASE(list_test_list_is_first),
	KUNIT_CASE(list_test_list_is_last),
	KUNIT_CASE(list_test_list_empty),
	KUNIT_CASE(list_test_list_empty_careful),
	KUNIT_CASE(list_test_list_rotate_left),
	KUNIT_CASE(list_test_list_rotate_to_front),
	KUNIT_CASE(list_test_list_is_singular),
	KUNIT_CASE(list_test_list_cut_position),
	KUNIT_CASE(list_test_list_cut_before),
	KUNIT_CASE(list_test_list_splice),
	KUNIT_CASE(list_test_list_splice_tail),
	KUNIT_CASE(list_test_list_splice_init),
	KUNIT_CASE(list_test_list_splice_tail_init),
	KUNIT_CASE(list_test_list_entry),
	KUNIT_CASE(list_test_list_entry_is_head),
	KUNIT_CASE(list_test_list_first_entry),
	KUNIT_CASE(list_test_list_last_entry),
	KUNIT_CASE(list_test_list_first_entry_or_null),
	KUNIT_CASE(list_test_list_next_entry),
	KUNIT_CASE(list_test_list_prev_entry),
	KUNIT_CASE(list_test_list_for_each),
	KUNIT_CASE(list_test_list_for_each_prev),
	KUNIT_CASE(list_test_list_for_each_safe),
	KUNIT_CASE(list_test_list_for_each_prev_safe),
	KUNIT_CASE(list_test_list_for_each_entry),
	KUNIT_CASE(list_test_list_for_each_entry_reverse),
	{},
};

static struct kunit_suite list_test_module = {
	.name = "list-kunit-test",
	.test_cases = list_test_cases,
};

struct hlist_test_struct {
	int data;
	struct hlist_node list;
};

static void hlist_test_init(struct kunit *test)
{
	/* Test the different ways of initialising a list. */
	struct hlist_head list1 = HLIST_HEAD_INIT;
	struct hlist_head list2;
	HLIST_HEAD(list3);
	struct hlist_head *list4;
	struct hlist_head *list5;

	INIT_HLIST_HEAD(&list2);

	list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
	INIT_HLIST_HEAD(list4);

	list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
	memset(list5, 0xFF, sizeof(*list5));
	INIT_HLIST_HEAD(list5);

	KUNIT_EXPECT_TRUE(test, hlist_empty(&list1));
	KUNIT_EXPECT_TRUE(test, hlist_empty(&list2));
	KUNIT_EXPECT_TRUE(test, hlist_empty(&list3));
	KUNIT_EXPECT_TRUE(test, hlist_empty(list4));
	KUNIT_EXPECT_TRUE(test, hlist_empty(list5));

	kfree(list4);
	kfree(list5);
}

static void hlist_test_unhashed(struct kunit *test)
{
	struct hlist_node a;
	HLIST_HEAD(list);

	INIT_HLIST_NODE(&a);

	/* is unhashed by default */
	KUNIT_EXPECT_TRUE(test, hlist_unhashed(&a));

	hlist_add_head(&a, &list);

	/* is hashed once added to list */
	KUNIT_EXPECT_FALSE(test, hlist_unhashed(&a));

	hlist_del_init(&a);

	/* is again unhashed after del_init */
	KUNIT_EXPECT_TRUE(test, hlist_unhashed(&a));
}

/* Doesn't test concurrency guarantees */
static void hlist_test_unhashed_lockless(struct kunit *test)
{
	struct hlist_node a;
	HLIST_HEAD(list);

	INIT_HLIST_NODE(&a);

	/* is unhashed by default */
	KUNIT_EXPECT_TRUE(test, hlist_unhashed_lockless(&a));

	hlist_add_head(&a, &list);

	/* is hashed once added to list */
	KUNIT_EXPECT_FALSE(test, hlist_unhashed_lockless(&a));

	hlist_del_init(&a);

	/* is again unhashed after del_init */
	KUNIT_EXPECT_TRUE(test, hlist_unhashed_lockless(&a));
}

static void hlist_test_del(struct kunit *test)
{
	struct hlist_node a, b;
	HLIST_HEAD(list);

	hlist_add_head(&a, &list);
	hlist_add_behind(&b, &a);

	/* before: [list] -> a -> b */
	hlist_del(&a);

	/* now: [list] -> b */
	KUNIT_EXPECT_PTR_EQ(test, list.first, &b);
	KUNIT_EXPECT_PTR_EQ(test, b.pprev, &list.first);
}

static void hlist_test_del_init(struct kunit *test)
{
	struct hlist_node a, b;
	HLIST_HEAD(list);

	hlist_add_head(&a, &list);
	hlist_add_behind(&b, &a);

	/* before: [list] -> a -> b */
	hlist_del_init(&a);

	/* now: [list] -> b */
	KUNIT_EXPECT_PTR_EQ(test, list.first, &b);
	KUNIT_EXPECT_PTR_EQ(test, b.pprev, &list.first);

	/* a is now initialised */
	KUNIT_EXPECT_PTR_EQ(test, a.next, NULL);
	KUNIT_EXPECT_PTR_EQ(test, a.pprev, NULL);
}

/* Tests all three hlist_add_* functions */
static void hlist_test_add(struct kunit *test)
{
	struct hlist_node a, b, c, d;
	HLIST_HEAD(list);

	hlist_add_head(&a, &list);
	hlist_add_head(&b, &list);
	hlist_add_before(&c, &a);
	hlist_add_behind(&d, &a);

	/* should be [list] -> b -> c -> a -> d */
	KUNIT_EXPECT_PTR_EQ(test, list.first, &b);

	KUNIT_EXPECT_PTR_EQ(test, c.pprev, &(b.next));
	KUNIT_EXPECT_PTR_EQ(test, b.next, &c);

	KUNIT_EXPECT_PTR_EQ(test, a.pprev, &(c.next));
	KUNIT_EXPECT_PTR_EQ(test, c.next, &a);

	KUNIT_EXPECT_PTR_EQ(test, d.pprev, &(a.next));
	KUNIT_EXPECT_PTR_EQ(test, a.next, &d);
}

/* Tests both hlist_fake() and hlist_add_fake() */
static void hlist_test_fake(struct kunit *test)
{
	struct hlist_node a;

	INIT_HLIST_NODE(&a);

	/* not fake after init */
	KUNIT_EXPECT_FALSE(test, hlist_fake(&a));

	hlist_add_fake(&a);

	/* is now fake */
	KUNIT_EXPECT_TRUE(test, hlist_fake(&a));
}

static void hlist_test_is_singular_node(struct kunit *test)
{
	struct hlist_node a, b;
	HLIST_HEAD(list);

	INIT_HLIST_NODE(&a);
	KUNIT_EXPECT_FALSE(test, hlist_is_singular_node(&a, &list));

	hlist_add_head(&a, &list);
	KUNIT_EXPECT_TRUE(test, hlist_is_singular_node(&a, &list));

	hlist_add_head(&b, &list);
	KUNIT_EXPECT_FALSE(test, hlist_is_singular_node(&a, &list));
	KUNIT_EXPECT_FALSE(test, hlist_is_singular_node(&b, &list));
}

static void hlist_test_empty(struct kunit *test)
{
	struct hlist_node a;
	HLIST_HEAD(list);

	/* list starts off empty */
	KUNIT_EXPECT_TRUE(test, hlist_empty(&list));

	hlist_add_head(&a, &list);

	/* list is no longer empty */
	KUNIT_EXPECT_FALSE(test, hlist_empty(&list));
}

static void hlist_test_move_list(struct kunit *test)
{
	struct hlist_node a;
	HLIST_HEAD(list1);
	HLIST_HEAD(list2);

	hlist_add_head(&a, &list1);

	KUNIT_EXPECT_FALSE(test, hlist_empty(&list1));
	KUNIT_EXPECT_TRUE(test, hlist_empty(&list2));
	hlist_move_list(&list1, &list2);
	KUNIT_EXPECT_TRUE(test, hlist_empty(&list1));
	KUNIT_EXPECT_FALSE(test, hlist_empty(&list2));

}

static void hlist_test_entry(struct kunit *test)
{
	struct hlist_test_struct test_struct;

	KUNIT_EXPECT_PTR_EQ(test, &test_struct,
			    hlist_entry(&(test_struct.list),
				struct hlist_test_struct, list));
}

static void hlist_test_entry_safe(struct kunit *test)
{
	struct hlist_test_struct test_struct;

	KUNIT_EXPECT_PTR_EQ(test, &test_struct,
			    hlist_entry_safe(&(test_struct.list),
				struct hlist_test_struct, list));

	KUNIT_EXPECT_PTR_EQ(test, NULL,
			    hlist_entry_safe((struct hlist_node *)NULL,
				struct hlist_test_struct, list));
}

static void hlist_test_for_each(struct kunit *test)
{
	struct hlist_node entries[3], *cur;
	HLIST_HEAD(list);
	int i = 0;

	hlist_add_head(&entries[0], &list);
	hlist_add_behind(&entries[1], &entries[0]);
	hlist_add_behind(&entries[2], &entries[1]);

	hlist_for_each(cur, &list) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 3);
}


static void hlist_test_for_each_safe(struct kunit *test)
{
	struct hlist_node entries[3], *cur, *n;
	HLIST_HEAD(list);
	int i = 0;

	hlist_add_head(&entries[0], &list);
	hlist_add_behind(&entries[1], &entries[0]);
	hlist_add_behind(&entries[2], &entries[1]);

	hlist_for_each_safe(cur, n, &list) {
		KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
		hlist_del(&entries[i]);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 3);
	KUNIT_EXPECT_TRUE(test, hlist_empty(&list));
}

static void hlist_test_for_each_entry(struct kunit *test)
{
	struct hlist_test_struct entries[5], *cur;
	HLIST_HEAD(list);
	int i = 0;

	entries[0].data = 0;
	hlist_add_head(&entries[0].list, &list);
	for (i = 1; i < 5; ++i) {
		entries[i].data = i;
		hlist_add_behind(&entries[i].list, &entries[i-1].list);
	}

	i = 0;

	hlist_for_each_entry(cur, &list, list) {
		KUNIT_EXPECT_EQ(test, cur->data, i);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 5);
}

static void hlist_test_for_each_entry_continue(struct kunit *test)
{
	struct hlist_test_struct entries[5], *cur;
	HLIST_HEAD(list);
	int i = 0;

	entries[0].data = 0;
	hlist_add_head(&entries[0].list, &list);
	for (i = 1; i < 5; ++i) {
		entries[i].data = i;
		hlist_add_behind(&entries[i].list, &entries[i-1].list);
	}

	/* We skip the first (zero-th) entry. */
	i = 1;

	cur = &entries[0];
	hlist_for_each_entry_continue(cur, list) {
		KUNIT_EXPECT_EQ(test, cur->data, i);
		/* Stamp over the entry. */
		cur->data = 42;
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 5);
	/* The first entry was not visited. */
	KUNIT_EXPECT_EQ(test, entries[0].data, 0);
	/* The second (and presumably others), were. */
	KUNIT_EXPECT_EQ(test, entries[1].data, 42);
}

static void hlist_test_for_each_entry_from(struct kunit *test)
{
	struct hlist_test_struct entries[5], *cur;
	HLIST_HEAD(list);
	int i = 0;

	entries[0].data = 0;
	hlist_add_head(&entries[0].list, &list);
	for (i = 1; i < 5; ++i) {
		entries[i].data = i;
		hlist_add_behind(&entries[i].list, &entries[i-1].list);
	}

	i = 0;

	cur = &entries[0];
	hlist_for_each_entry_from(cur, list) {
		KUNIT_EXPECT_EQ(test, cur->data, i);
		/* Stamp over the entry. */
		cur->data = 42;
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 5);
	/* The first entry was visited. */
	KUNIT_EXPECT_EQ(test, entries[0].data, 42);
}

static void hlist_test_for_each_entry_safe(struct kunit *test)
{
	struct hlist_test_struct entries[5], *cur;
	struct hlist_node *tmp_node;
	HLIST_HEAD(list);
	int i = 0;

	entries[0].data = 0;
	hlist_add_head(&entries[0].list, &list);
	for (i = 1; i < 5; ++i) {
		entries[i].data = i;
		hlist_add_behind(&entries[i].list, &entries[i-1].list);
	}

	i = 0;

	hlist_for_each_entry_safe(cur, tmp_node, &list, list) {
		KUNIT_EXPECT_EQ(test, cur->data, i);
		hlist_del(&cur->list);
		i++;
	}

	KUNIT_EXPECT_EQ(test, i, 5);
	KUNIT_EXPECT_TRUE(test, hlist_empty(&list));
}


static struct kunit_case hlist_test_cases[] = {
	KUNIT_CASE(hlist_test_init),
	KUNIT_CASE(hlist_test_unhashed),
	KUNIT_CASE(hlist_test_unhashed_lockless),
	KUNIT_CASE(hlist_test_del),
	KUNIT_CASE(hlist_test_del_init),
	KUNIT_CASE(hlist_test_add),
	KUNIT_CASE(hlist_test_fake),
	KUNIT_CASE(hlist_test_is_singular_node),
	KUNIT_CASE(hlist_test_empty),
	KUNIT_CASE(hlist_test_move_list),
	KUNIT_CASE(hlist_test_entry),
	KUNIT_CASE(hlist_test_entry_safe),
	KUNIT_CASE(hlist_test_for_each),
	KUNIT_CASE(hlist_test_for_each_safe),
	KUNIT_CASE(hlist_test_for_each_entry),
	KUNIT_CASE(hlist_test_for_each_entry_continue),
	KUNIT_CASE(hlist_test_for_each_entry_from),
	KUNIT_CASE(hlist_test_for_each_entry_safe),
	{},
};

static struct kunit_suite hlist_test_module = {
	.name = "hlist",
	.test_cases = hlist_test_cases,
};

kunit_test_suites(&list_test_module, &hlist_test_module);

MODULE_LICENSE("GPL v2");