linux/lib/list-test.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * KUnit test for the Kernel Linked-list structures.
   4 *
   5 * Copyright (C) 2019, Google LLC.
   6 * Author: David Gow <davidgow@google.com>
   7 */
   8#include <kunit/test.h>
   9
  10#include <linux/list.h>
  11
  12struct list_test_struct {
  13        int data;
  14        struct list_head list;
  15};
  16
  17static void list_test_list_init(struct kunit *test)
  18{
  19        /* Test the different ways of initialising a list. */
  20        struct list_head list1 = LIST_HEAD_INIT(list1);
  21        struct list_head list2;
  22        LIST_HEAD(list3);
  23        struct list_head *list4;
  24        struct list_head *list5;
  25
  26        INIT_LIST_HEAD(&list2);
  27
  28        list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
  29        INIT_LIST_HEAD(list4);
  30
  31        list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
  32        memset(list5, 0xFF, sizeof(*list5));
  33        INIT_LIST_HEAD(list5);
  34
  35        /* list_empty_careful() checks both next and prev. */
  36        KUNIT_EXPECT_TRUE(test, list_empty_careful(&list1));
  37        KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
  38        KUNIT_EXPECT_TRUE(test, list_empty_careful(&list3));
  39        KUNIT_EXPECT_TRUE(test, list_empty_careful(list4));
  40        KUNIT_EXPECT_TRUE(test, list_empty_careful(list5));
  41
  42        kfree(list4);
  43        kfree(list5);
  44}
  45
  46static void list_test_list_add(struct kunit *test)
  47{
  48        struct list_head a, b;
  49        LIST_HEAD(list);
  50
  51        list_add(&a, &list);
  52        list_add(&b, &list);
  53
  54        /* should be [list] -> b -> a */
  55        KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
  56        KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
  57        KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
  58}
  59
  60static void list_test_list_add_tail(struct kunit *test)
  61{
  62        struct list_head a, b;
  63        LIST_HEAD(list);
  64
  65        list_add_tail(&a, &list);
  66        list_add_tail(&b, &list);
  67
  68        /* should be [list] -> a -> b */
  69        KUNIT_EXPECT_PTR_EQ(test, list.next, &a);
  70        KUNIT_EXPECT_PTR_EQ(test, a.prev, &list);
  71        KUNIT_EXPECT_PTR_EQ(test, a.next, &b);
  72}
  73
  74static void list_test_list_del(struct kunit *test)
  75{
  76        struct list_head a, b;
  77        LIST_HEAD(list);
  78
  79        list_add_tail(&a, &list);
  80        list_add_tail(&b, &list);
  81
  82        /* before: [list] -> a -> b */
  83        list_del(&a);
  84
  85        /* now: [list] -> b */
  86        KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
  87        KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
  88}
  89
  90static void list_test_list_replace(struct kunit *test)
  91{
  92        struct list_head a_old, a_new, b;
  93        LIST_HEAD(list);
  94
  95        list_add_tail(&a_old, &list);
  96        list_add_tail(&b, &list);
  97
  98        /* before: [list] -> a_old -> b */
  99        list_replace(&a_old, &a_new);
 100
 101        /* now: [list] -> a_new -> b */
 102        KUNIT_EXPECT_PTR_EQ(test, list.next, &a_new);
 103        KUNIT_EXPECT_PTR_EQ(test, b.prev, &a_new);
 104}
 105
 106static void list_test_list_replace_init(struct kunit *test)
 107{
 108        struct list_head a_old, a_new, b;
 109        LIST_HEAD(list);
 110
 111        list_add_tail(&a_old, &list);
 112        list_add_tail(&b, &list);
 113
 114        /* before: [list] -> a_old -> b */
 115        list_replace_init(&a_old, &a_new);
 116
 117        /* now: [list] -> a_new -> b */
 118        KUNIT_EXPECT_PTR_EQ(test, list.next, &a_new);
 119        KUNIT_EXPECT_PTR_EQ(test, b.prev, &a_new);
 120
 121        /* check a_old is empty (initialized) */
 122        KUNIT_EXPECT_TRUE(test, list_empty_careful(&a_old));
 123}
 124
 125static void list_test_list_swap(struct kunit *test)
 126{
 127        struct list_head a, b;
 128        LIST_HEAD(list);
 129
 130        list_add_tail(&a, &list);
 131        list_add_tail(&b, &list);
 132
 133        /* before: [list] -> a -> b */
 134        list_swap(&a, &b);
 135
 136        /* after: [list] -> b -> a */
 137        KUNIT_EXPECT_PTR_EQ(test, &b, list.next);
 138        KUNIT_EXPECT_PTR_EQ(test, &a, list.prev);
 139
 140        KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
 141        KUNIT_EXPECT_PTR_EQ(test, &list, b.prev);
 142
 143        KUNIT_EXPECT_PTR_EQ(test, &list, a.next);
 144        KUNIT_EXPECT_PTR_EQ(test, &b, a.prev);
 145}
 146
 147static void list_test_list_del_init(struct kunit *test)
 148{
 149        struct list_head a, b;
 150        LIST_HEAD(list);
 151
 152        list_add_tail(&a, &list);
 153        list_add_tail(&b, &list);
 154
 155        /* before: [list] -> a -> b */
 156        list_del_init(&a);
 157        /* after: [list] -> b, a initialised */
 158
 159        KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
 160        KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
 161        KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
 162}
 163
 164static void list_test_list_move(struct kunit *test)
 165{
 166        struct list_head a, b;
 167        LIST_HEAD(list1);
 168        LIST_HEAD(list2);
 169
 170        list_add_tail(&a, &list1);
 171        list_add_tail(&b, &list2);
 172
 173        /* before: [list1] -> a, [list2] -> b */
 174        list_move(&a, &list2);
 175        /* after: [list1] empty, [list2] -> a -> b */
 176
 177        KUNIT_EXPECT_TRUE(test, list_empty(&list1));
 178
 179        KUNIT_EXPECT_PTR_EQ(test, &a, list2.next);
 180        KUNIT_EXPECT_PTR_EQ(test, &b, a.next);
 181}
 182
 183static void list_test_list_move_tail(struct kunit *test)
 184{
 185        struct list_head a, b;
 186        LIST_HEAD(list1);
 187        LIST_HEAD(list2);
 188
 189        list_add_tail(&a, &list1);
 190        list_add_tail(&b, &list2);
 191
 192        /* before: [list1] -> a, [list2] -> b */
 193        list_move_tail(&a, &list2);
 194        /* after: [list1] empty, [list2] -> b -> a */
 195
 196        KUNIT_EXPECT_TRUE(test, list_empty(&list1));
 197
 198        KUNIT_EXPECT_PTR_EQ(test, &b, list2.next);
 199        KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
 200}
 201
 202static void list_test_list_bulk_move_tail(struct kunit *test)
 203{
 204        struct list_head a, b, c, d, x, y;
 205        struct list_head *list1_values[] = { &x, &b, &c, &y };
 206        struct list_head *list2_values[] = { &a, &d };
 207        struct list_head *ptr;
 208        LIST_HEAD(list1);
 209        LIST_HEAD(list2);
 210        int i = 0;
 211
 212        list_add_tail(&x, &list1);
 213        list_add_tail(&y, &list1);
 214
 215        list_add_tail(&a, &list2);
 216        list_add_tail(&b, &list2);
 217        list_add_tail(&c, &list2);
 218        list_add_tail(&d, &list2);
 219
 220        /* before: [list1] -> x -> y, [list2] -> a -> b -> c -> d */
 221        list_bulk_move_tail(&y, &b, &c);
 222        /* after: [list1] -> x -> b -> c -> y, [list2] -> a -> d */
 223
 224        list_for_each(ptr, &list1) {
 225                KUNIT_EXPECT_PTR_EQ(test, ptr, list1_values[i]);
 226                i++;
 227        }
 228        KUNIT_EXPECT_EQ(test, i, 4);
 229        i = 0;
 230        list_for_each(ptr, &list2) {
 231                KUNIT_EXPECT_PTR_EQ(test, ptr, list2_values[i]);
 232                i++;
 233        }
 234        KUNIT_EXPECT_EQ(test, i, 2);
 235}
 236
 237static void list_test_list_is_first(struct kunit *test)
 238{
 239        struct list_head a, b;
 240        LIST_HEAD(list);
 241
 242        list_add_tail(&a, &list);
 243        list_add_tail(&b, &list);
 244
 245        KUNIT_EXPECT_TRUE(test, list_is_first(&a, &list));
 246        KUNIT_EXPECT_FALSE(test, list_is_first(&b, &list));
 247}
 248
 249static void list_test_list_is_last(struct kunit *test)
 250{
 251        struct list_head a, b;
 252        LIST_HEAD(list);
 253
 254        list_add_tail(&a, &list);
 255        list_add_tail(&b, &list);
 256
 257        KUNIT_EXPECT_FALSE(test, list_is_last(&a, &list));
 258        KUNIT_EXPECT_TRUE(test, list_is_last(&b, &list));
 259}
 260
 261static void list_test_list_empty(struct kunit *test)
 262{
 263        struct list_head a;
 264        LIST_HEAD(list1);
 265        LIST_HEAD(list2);
 266
 267        list_add_tail(&a, &list1);
 268
 269        KUNIT_EXPECT_FALSE(test, list_empty(&list1));
 270        KUNIT_EXPECT_TRUE(test, list_empty(&list2));
 271}
 272
 273static void list_test_list_empty_careful(struct kunit *test)
 274{
 275        /* This test doesn't check correctness under concurrent access */
 276        struct list_head a;
 277        LIST_HEAD(list1);
 278        LIST_HEAD(list2);
 279
 280        list_add_tail(&a, &list1);
 281
 282        KUNIT_EXPECT_FALSE(test, list_empty_careful(&list1));
 283        KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
 284}
 285
 286static void list_test_list_rotate_left(struct kunit *test)
 287{
 288        struct list_head a, b;
 289        LIST_HEAD(list);
 290
 291        list_add_tail(&a, &list);
 292        list_add_tail(&b, &list);
 293
 294        /* before: [list] -> a -> b */
 295        list_rotate_left(&list);
 296        /* after: [list] -> b -> a */
 297
 298        KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
 299        KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
 300        KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
 301}
 302
 303static void list_test_list_rotate_to_front(struct kunit *test)
 304{
 305        struct list_head a, b, c, d;
 306        struct list_head *list_values[] = { &c, &d, &a, &b };
 307        struct list_head *ptr;
 308        LIST_HEAD(list);
 309        int i = 0;
 310
 311        list_add_tail(&a, &list);
 312        list_add_tail(&b, &list);
 313        list_add_tail(&c, &list);
 314        list_add_tail(&d, &list);
 315
 316        /* before: [list] -> a -> b -> c -> d */
 317        list_rotate_to_front(&c, &list);
 318        /* after: [list] -> c -> d -> a -> b */
 319
 320        list_for_each(ptr, &list) {
 321                KUNIT_EXPECT_PTR_EQ(test, ptr, list_values[i]);
 322                i++;
 323        }
 324        KUNIT_EXPECT_EQ(test, i, 4);
 325}
 326
 327static void list_test_list_is_singular(struct kunit *test)
 328{
 329        struct list_head a, b;
 330        LIST_HEAD(list);
 331
 332        /* [list] empty */
 333        KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
 334
 335        list_add_tail(&a, &list);
 336
 337        /* [list] -> a */
 338        KUNIT_EXPECT_TRUE(test, list_is_singular(&list));
 339
 340        list_add_tail(&b, &list);
 341
 342        /* [list] -> a -> b */
 343        KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
 344}
 345
 346static void list_test_list_cut_position(struct kunit *test)
 347{
 348        struct list_head entries[3], *cur;
 349        LIST_HEAD(list1);
 350        LIST_HEAD(list2);
 351        int i = 0;
 352
 353        list_add_tail(&entries[0], &list1);
 354        list_add_tail(&entries[1], &list1);
 355        list_add_tail(&entries[2], &list1);
 356
 357        /* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
 358        list_cut_position(&list2, &list1, &entries[1]);
 359        /* after: [list2] -> entries[0] -> entries[1], [list1] -> entries[2] */
 360
 361        list_for_each(cur, &list2) {
 362                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 363                i++;
 364        }
 365
 366        KUNIT_EXPECT_EQ(test, i, 2);
 367
 368        list_for_each(cur, &list1) {
 369                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 370                i++;
 371        }
 372}
 373
 374static void list_test_list_cut_before(struct kunit *test)
 375{
 376        struct list_head entries[3], *cur;
 377        LIST_HEAD(list1);
 378        LIST_HEAD(list2);
 379        int i = 0;
 380
 381        list_add_tail(&entries[0], &list1);
 382        list_add_tail(&entries[1], &list1);
 383        list_add_tail(&entries[2], &list1);
 384
 385        /* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
 386        list_cut_before(&list2, &list1, &entries[1]);
 387        /* after: [list2] -> entries[0], [list1] -> entries[1] -> entries[2] */
 388
 389        list_for_each(cur, &list2) {
 390                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 391                i++;
 392        }
 393
 394        KUNIT_EXPECT_EQ(test, i, 1);
 395
 396        list_for_each(cur, &list1) {
 397                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 398                i++;
 399        }
 400}
 401
 402static void list_test_list_splice(struct kunit *test)
 403{
 404        struct list_head entries[5], *cur;
 405        LIST_HEAD(list1);
 406        LIST_HEAD(list2);
 407        int i = 0;
 408
 409        list_add_tail(&entries[0], &list1);
 410        list_add_tail(&entries[1], &list1);
 411        list_add_tail(&entries[2], &list2);
 412        list_add_tail(&entries[3], &list2);
 413        list_add_tail(&entries[4], &list1);
 414
 415        /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
 416        list_splice(&list2, &entries[1]);
 417        /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
 418
 419        list_for_each(cur, &list1) {
 420                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 421                i++;
 422        }
 423
 424        KUNIT_EXPECT_EQ(test, i, 5);
 425}
 426
 427static void list_test_list_splice_tail(struct kunit *test)
 428{
 429        struct list_head entries[5], *cur;
 430        LIST_HEAD(list1);
 431        LIST_HEAD(list2);
 432        int i = 0;
 433
 434        list_add_tail(&entries[0], &list1);
 435        list_add_tail(&entries[1], &list1);
 436        list_add_tail(&entries[2], &list2);
 437        list_add_tail(&entries[3], &list2);
 438        list_add_tail(&entries[4], &list1);
 439
 440        /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
 441        list_splice_tail(&list2, &entries[4]);
 442        /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
 443
 444        list_for_each(cur, &list1) {
 445                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 446                i++;
 447        }
 448
 449        KUNIT_EXPECT_EQ(test, i, 5);
 450}
 451
 452static void list_test_list_splice_init(struct kunit *test)
 453{
 454        struct list_head entries[5], *cur;
 455        LIST_HEAD(list1);
 456        LIST_HEAD(list2);
 457        int i = 0;
 458
 459        list_add_tail(&entries[0], &list1);
 460        list_add_tail(&entries[1], &list1);
 461        list_add_tail(&entries[2], &list2);
 462        list_add_tail(&entries[3], &list2);
 463        list_add_tail(&entries[4], &list1);
 464
 465        /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
 466        list_splice_init(&list2, &entries[1]);
 467        /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
 468
 469        list_for_each(cur, &list1) {
 470                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 471                i++;
 472        }
 473
 474        KUNIT_EXPECT_EQ(test, i, 5);
 475
 476        KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
 477}
 478
 479static void list_test_list_splice_tail_init(struct kunit *test)
 480{
 481        struct list_head entries[5], *cur;
 482        LIST_HEAD(list1);
 483        LIST_HEAD(list2);
 484        int i = 0;
 485
 486        list_add_tail(&entries[0], &list1);
 487        list_add_tail(&entries[1], &list1);
 488        list_add_tail(&entries[2], &list2);
 489        list_add_tail(&entries[3], &list2);
 490        list_add_tail(&entries[4], &list1);
 491
 492        /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
 493        list_splice_tail_init(&list2, &entries[4]);
 494        /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
 495
 496        list_for_each(cur, &list1) {
 497                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 498                i++;
 499        }
 500
 501        KUNIT_EXPECT_EQ(test, i, 5);
 502
 503        KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
 504}
 505
 506static void list_test_list_entry(struct kunit *test)
 507{
 508        struct list_test_struct test_struct;
 509
 510        KUNIT_EXPECT_PTR_EQ(test, &test_struct, list_entry(&(test_struct.list),
 511                                struct list_test_struct, list));
 512}
 513
 514static void list_test_list_first_entry(struct kunit *test)
 515{
 516        struct list_test_struct test_struct1, test_struct2;
 517        LIST_HEAD(list);
 518
 519        list_add_tail(&test_struct1.list, &list);
 520        list_add_tail(&test_struct2.list, &list);
 521
 522
 523        KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_first_entry(&list,
 524                                struct list_test_struct, list));
 525}
 526
 527static void list_test_list_last_entry(struct kunit *test)
 528{
 529        struct list_test_struct test_struct1, test_struct2;
 530        LIST_HEAD(list);
 531
 532        list_add_tail(&test_struct1.list, &list);
 533        list_add_tail(&test_struct2.list, &list);
 534
 535
 536        KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_last_entry(&list,
 537                                struct list_test_struct, list));
 538}
 539
 540static void list_test_list_first_entry_or_null(struct kunit *test)
 541{
 542        struct list_test_struct test_struct1, test_struct2;
 543        LIST_HEAD(list);
 544
 545        KUNIT_EXPECT_FALSE(test, list_first_entry_or_null(&list,
 546                                struct list_test_struct, list));
 547
 548        list_add_tail(&test_struct1.list, &list);
 549        list_add_tail(&test_struct2.list, &list);
 550
 551        KUNIT_EXPECT_PTR_EQ(test, &test_struct1,
 552                        list_first_entry_or_null(&list,
 553                                struct list_test_struct, list));
 554}
 555
 556static void list_test_list_next_entry(struct kunit *test)
 557{
 558        struct list_test_struct test_struct1, test_struct2;
 559        LIST_HEAD(list);
 560
 561        list_add_tail(&test_struct1.list, &list);
 562        list_add_tail(&test_struct2.list, &list);
 563
 564
 565        KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_next_entry(&test_struct1,
 566                                list));
 567}
 568
 569static void list_test_list_prev_entry(struct kunit *test)
 570{
 571        struct list_test_struct test_struct1, test_struct2;
 572        LIST_HEAD(list);
 573
 574        list_add_tail(&test_struct1.list, &list);
 575        list_add_tail(&test_struct2.list, &list);
 576
 577
 578        KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_prev_entry(&test_struct2,
 579                                list));
 580}
 581
 582static void list_test_list_for_each(struct kunit *test)
 583{
 584        struct list_head entries[3], *cur;
 585        LIST_HEAD(list);
 586        int i = 0;
 587
 588        list_add_tail(&entries[0], &list);
 589        list_add_tail(&entries[1], &list);
 590        list_add_tail(&entries[2], &list);
 591
 592        list_for_each(cur, &list) {
 593                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 594                i++;
 595        }
 596
 597        KUNIT_EXPECT_EQ(test, i, 3);
 598}
 599
 600static void list_test_list_for_each_prev(struct kunit *test)
 601{
 602        struct list_head entries[3], *cur;
 603        LIST_HEAD(list);
 604        int i = 2;
 605
 606        list_add_tail(&entries[0], &list);
 607        list_add_tail(&entries[1], &list);
 608        list_add_tail(&entries[2], &list);
 609
 610        list_for_each_prev(cur, &list) {
 611                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 612                i--;
 613        }
 614
 615        KUNIT_EXPECT_EQ(test, i, -1);
 616}
 617
 618static void list_test_list_for_each_safe(struct kunit *test)
 619{
 620        struct list_head entries[3], *cur, *n;
 621        LIST_HEAD(list);
 622        int i = 0;
 623
 624
 625        list_add_tail(&entries[0], &list);
 626        list_add_tail(&entries[1], &list);
 627        list_add_tail(&entries[2], &list);
 628
 629        list_for_each_safe(cur, n, &list) {
 630                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 631                list_del(&entries[i]);
 632                i++;
 633        }
 634
 635        KUNIT_EXPECT_EQ(test, i, 3);
 636        KUNIT_EXPECT_TRUE(test, list_empty(&list));
 637}
 638
 639static void list_test_list_for_each_prev_safe(struct kunit *test)
 640{
 641        struct list_head entries[3], *cur, *n;
 642        LIST_HEAD(list);
 643        int i = 2;
 644
 645        list_add_tail(&entries[0], &list);
 646        list_add_tail(&entries[1], &list);
 647        list_add_tail(&entries[2], &list);
 648
 649        list_for_each_prev_safe(cur, n, &list) {
 650                KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
 651                list_del(&entries[i]);
 652                i--;
 653        }
 654
 655        KUNIT_EXPECT_EQ(test, i, -1);
 656        KUNIT_EXPECT_TRUE(test, list_empty(&list));
 657}
 658
 659static void list_test_list_for_each_entry(struct kunit *test)
 660{
 661        struct list_test_struct entries[5], *cur;
 662        LIST_HEAD(list);
 663        int i = 0;
 664
 665        for (i = 0; i < 5; ++i) {
 666                entries[i].data = i;
 667                list_add_tail(&entries[i].list, &list);
 668        }
 669
 670        i = 0;
 671
 672        list_for_each_entry(cur, &list, list) {
 673                KUNIT_EXPECT_EQ(test, cur->data, i);
 674                i++;
 675        }
 676
 677        KUNIT_EXPECT_EQ(test, i, 5);
 678}
 679
 680static void list_test_list_for_each_entry_reverse(struct kunit *test)
 681{
 682        struct list_test_struct entries[5], *cur;
 683        LIST_HEAD(list);
 684        int i = 0;
 685
 686        for (i = 0; i < 5; ++i) {
 687                entries[i].data = i;
 688                list_add_tail(&entries[i].list, &list);
 689        }
 690
 691        i = 4;
 692
 693        list_for_each_entry_reverse(cur, &list, list) {
 694                KUNIT_EXPECT_EQ(test, cur->data, i);
 695                i--;
 696        }
 697
 698        KUNIT_EXPECT_EQ(test, i, -1);
 699}
 700
 701static struct kunit_case list_test_cases[] = {
 702        KUNIT_CASE(list_test_list_init),
 703        KUNIT_CASE(list_test_list_add),
 704        KUNIT_CASE(list_test_list_add_tail),
 705        KUNIT_CASE(list_test_list_del),
 706        KUNIT_CASE(list_test_list_replace),
 707        KUNIT_CASE(list_test_list_replace_init),
 708        KUNIT_CASE(list_test_list_swap),
 709        KUNIT_CASE(list_test_list_del_init),
 710        KUNIT_CASE(list_test_list_move),
 711        KUNIT_CASE(list_test_list_move_tail),
 712        KUNIT_CASE(list_test_list_bulk_move_tail),
 713        KUNIT_CASE(list_test_list_is_first),
 714        KUNIT_CASE(list_test_list_is_last),
 715        KUNIT_CASE(list_test_list_empty),
 716        KUNIT_CASE(list_test_list_empty_careful),
 717        KUNIT_CASE(list_test_list_rotate_left),
 718        KUNIT_CASE(list_test_list_rotate_to_front),
 719        KUNIT_CASE(list_test_list_is_singular),
 720        KUNIT_CASE(list_test_list_cut_position),
 721        KUNIT_CASE(list_test_list_cut_before),
 722        KUNIT_CASE(list_test_list_splice),
 723        KUNIT_CASE(list_test_list_splice_tail),
 724        KUNIT_CASE(list_test_list_splice_init),
 725        KUNIT_CASE(list_test_list_splice_tail_init),
 726        KUNIT_CASE(list_test_list_entry),
 727        KUNIT_CASE(list_test_list_first_entry),
 728        KUNIT_CASE(list_test_list_last_entry),
 729        KUNIT_CASE(list_test_list_first_entry_or_null),
 730        KUNIT_CASE(list_test_list_next_entry),
 731        KUNIT_CASE(list_test_list_prev_entry),
 732        KUNIT_CASE(list_test_list_for_each),
 733        KUNIT_CASE(list_test_list_for_each_prev),
 734        KUNIT_CASE(list_test_list_for_each_safe),
 735        KUNIT_CASE(list_test_list_for_each_prev_safe),
 736        KUNIT_CASE(list_test_list_for_each_entry),
 737        KUNIT_CASE(list_test_list_for_each_entry_reverse),
 738        {},
 739};
 740
 741static struct kunit_suite list_test_module = {
 742        .name = "list-kunit-test",
 743        .test_cases = list_test_cases,
 744};
 745
 746kunit_test_suites(&list_test_module);
 747
 748MODULE_LICENSE("GPL v2");
 749