1 /* Test program for lib/kernel/list.c.
3 Attempts to test the list functionality that is not
4 sufficiently tested elsewhere in Pintos.
6 This is not a test we will run on your submitted projects.
7 It is here for completeness.
15 #include "threads/test.h"
17 /* Maximum number of elements in a linked list that we will
21 /* A linked list element. */
24 list_elem elem; /* List element. */
25 int value; /* Item value. */
28 static void shuffle (struct value[], size_t);
29 static bool value_less (const list_elem *, const list_elem *, void *);
30 static void verify_list_fwd (struct list *, int size);
31 static void verify_list_bkwd (struct list *, int size);
33 /* Test the linked list implementation. */
39 printf ("testing various size lists:");
40 for (size = 0; size < MAX_SIZE; size++)
44 printf (" %zu", size);
45 for (repeat = 0; repeat < 10; repeat++)
47 static struct value values[MAX_SIZE * 4];
52 /* Put values 0...SIZE in random order in VALUES. */
53 for (i = 0; i < size; i++)
55 shuffle (values, size);
59 for (i = 0; i < size; i++)
60 list_push_back (&list, &values[i].elem);
62 /* Verify correct minimum and maximum elements. */
63 e = list_min (&list, value_less, NULL);
64 ASSERT (size ? list_entry (e, struct value, elem)->value == 0
65 : e == list_begin (&list));
66 e = list_max (&list, value_less, NULL);
67 ASSERT (size ? list_entry (e, struct value, elem)->value == size - 1
68 : e == list_begin (&list));
70 /* Sort and verify list. */
71 list_sort (&list, value_less, NULL);
72 verify_list_fwd (&list, size);
74 /* Reverse and verify list. */
76 verify_list_bkwd (&list, size);
78 /* Shuffle, insert using list_insert_ordered(),
79 and verify ordering. */
80 shuffle (values, size);
82 for (i = 0; i < size; i++)
83 list_insert_ordered (&list, &values[i].elem,
85 verify_list_fwd (&list, size);
87 /* Duplicate some items, uniquify, and verify. */
89 for (e = list_begin (&list); e != list_end (&list);
92 struct value *v = list_entry (e, struct value, elem);
93 int copies = random_ulong () % 4;
96 values[ofs].value = v->value;
97 list_insert (e, &values[ofs++].elem);
100 ASSERT ((size_t) ofs < sizeof values / sizeof *values);
101 list_unique (&list, NULL, value_less, NULL);
102 verify_list_fwd (&list, size);
107 printf ("list: PASS\n");
110 /* Shuffles the CNT elements in ARRAY into random order. */
112 shuffle (struct value *array, size_t cnt)
116 for (i = 0; i < cnt; i++)
118 size_t j = i + random_ulong () % (cnt - i);
119 struct value t = array[j];
125 /* Returns true if value A is less than value B, false
128 value_less (const list_elem *a_, const list_elem *b_, void *aux UNUSED)
130 const struct value *a = list_entry (a_, struct value, elem);
131 const struct value *b = list_entry (b_, struct value, elem);
133 return a->value < b->value;
136 /* Verifies that LIST contains the values 0...SIZE when traversed
139 verify_list_fwd (struct list *list, int size)
144 for (i = 0, e = list_begin (list);
145 i < size && e != list_end (list);
146 i++, e = list_next (e))
148 struct value *v = list_entry (e, struct value, elem);
149 ASSERT (i == v->value);
152 ASSERT (e == list_end (list));
155 /* Verifies that LIST contains the values 0...SIZE when traversed
158 verify_list_bkwd (struct list *list, int size)
163 for (i = 0, e = list_rbegin (list);
164 i < size && e != list_rend (list);
165 i++, e = list_prev (e))
167 struct value *v = list_entry (e, struct value, elem);
168 ASSERT (i == v->value);
171 ASSERT (e == list_rend (list));