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 struct list_elem elem; /* List element. */
25 int value; /* Item value. */
28 static void shuffle (struct value[], size_t);
29 static bool value_less (const struct list_elem *, const struct list_elem *,
31 static void verify_list_fwd (struct list *, int size);
32 static void verify_list_bkwd (struct list *, int size);
34 /* Test the linked list implementation. */
40 printf ("testing various size lists:");
41 for (size = 0; size < MAX_SIZE; size++)
46 for (repeat = 0; repeat < 10; repeat++)
48 static struct value values[MAX_SIZE * 4];
53 /* Put values 0...SIZE in random order in VALUES. */
54 for (i = 0; i < size; i++)
56 shuffle (values, size);
60 for (i = 0; i < size; i++)
61 list_push_back (&list, &values[i].elem);
63 /* Verify correct minimum and maximum elements. */
64 e = list_min (&list, value_less, NULL);
65 ASSERT (size ? list_entry (e, struct value, elem)->value == 0
66 : e == list_begin (&list));
67 e = list_max (&list, value_less, NULL);
68 ASSERT (size ? list_entry (e, struct value, elem)->value == size - 1
69 : e == list_begin (&list));
71 /* Sort and verify list. */
72 list_sort (&list, value_less, NULL);
73 verify_list_fwd (&list, size);
75 /* Reverse and verify list. */
77 verify_list_bkwd (&list, size);
79 /* Shuffle, insert using list_insert_ordered(),
80 and verify ordering. */
81 shuffle (values, size);
83 for (i = 0; i < size; i++)
84 list_insert_ordered (&list, &values[i].elem,
86 verify_list_fwd (&list, size);
88 /* Duplicate some items, uniquify, and verify. */
90 for (e = list_begin (&list); e != list_end (&list);
93 struct value *v = list_entry (e, struct value, elem);
94 int copies = random_ulong () % 4;
97 values[ofs].value = v->value;
98 list_insert (e, &values[ofs++].elem);
101 ASSERT ((size_t) ofs < sizeof values / sizeof *values);
102 list_unique (&list, NULL, value_less, NULL);
103 verify_list_fwd (&list, size);
108 printf ("list: PASS\n");
111 /* Shuffles the CNT elements in ARRAY into random order. */
113 shuffle (struct value *array, size_t cnt)
117 for (i = 0; i < cnt; i++)
119 size_t j = i + random_ulong () % (cnt - i);
120 struct value t = array[j];
126 /* Returns true if value A is less than value B, false
129 value_less (const struct list_elem *a_, const struct list_elem *b_,
132 const struct value *a = list_entry (a_, struct value, elem);
133 const struct value *b = list_entry (b_, struct value, elem);
135 return a->value < b->value;
138 /* Verifies that LIST contains the values 0...SIZE when traversed
141 verify_list_fwd (struct list *list, int size)
146 for (i = 0, e = list_begin (list);
147 i < size && e != list_end (list);
148 i++, e = list_next (e))
150 struct value *v = list_entry (e, struct value, elem);
151 ASSERT (i == v->value);
154 ASSERT (e == list_end (list));
157 /* Verifies that LIST contains the values 0...SIZE when traversed
160 verify_list_bkwd (struct list *list, int size)
165 for (i = 0, e = list_rbegin (list);
166 i < size && e != list_rend (list);
167 i++, e = list_prev (e))
169 struct value *v = list_entry (e, struct value, elem);
170 ASSERT (i == v->value);
173 ASSERT (e == list_rend (list));