00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #undef NDEBUG
00011 #include <debug.h>
00012 #include <list.h>
00013 #include <random.h>
00014 #include <stdio.h>
00015 #include "threads/test.h"
00016
00017
00018
00019 #define MAX_SIZE 64
00020
00021
00022 struct value
00023 {
00024 struct list_elem elem;
00025 int value;
00026 };
00027
00028 static void shuffle (struct value[], size_t);
00029 static bool value_less (const struct list_elem *, const struct list_elem *,
00030 void *);
00031 static void verify_list_fwd (struct list *, int size);
00032 static void verify_list_bkwd (struct list *, int size);
00033
00034
00035 void
00036 test (void)
00037 {
00038 int size;
00039
00040 printf ("testing various size lists:");
00041 for (size = 0; size < MAX_SIZE; size++)
00042 {
00043 int repeat;
00044
00045 printf (" %d", size);
00046 for (repeat = 0; repeat < 10; repeat++)
00047 {
00048 static struct value values[MAX_SIZE * 4];
00049 struct list list;
00050 struct list_elem *e;
00051 int i, ofs;
00052
00053
00054 for (i = 0; i < size; i++)
00055 values[i].value = i;
00056 shuffle (values, size);
00057
00058
00059 list_init (&list);
00060 for (i = 0; i < size; i++)
00061 list_push_back (&list, &values[i].elem);
00062
00063
00064 e = list_min (&list, value_less, NULL);
00065 ASSERT (size ? list_entry (e, struct value, elem)->value == 0
00066 : e == list_begin (&list));
00067 e = list_max (&list, value_less, NULL);
00068 ASSERT (size ? list_entry (e, struct value, elem)->value == size - 1
00069 : e == list_begin (&list));
00070
00071
00072 list_sort (&list, value_less, NULL);
00073 verify_list_fwd (&list, size);
00074
00075
00076 list_reverse (&list);
00077 verify_list_bkwd (&list, size);
00078
00079
00080
00081 shuffle (values, size);
00082 list_init (&list);
00083 for (i = 0; i < size; i++)
00084 list_insert_ordered (&list, &values[i].elem,
00085 value_less, NULL);
00086 verify_list_fwd (&list, size);
00087
00088
00089 ofs = size;
00090 for (e = list_begin (&list); e != list_end (&list);
00091 e = list_next (e))
00092 {
00093 struct value *v = list_entry (e, struct value, elem);
00094 int copies = random_ulong () % 4;
00095 while (copies-- > 0)
00096 {
00097 values[ofs].value = v->value;
00098 list_insert (e, &values[ofs++].elem);
00099 }
00100 }
00101 ASSERT ((size_t) ofs < sizeof values / sizeof *values);
00102 list_unique (&list, NULL, value_less, NULL);
00103 verify_list_fwd (&list, size);
00104 }
00105 }
00106
00107 printf (" done\n");
00108 printf ("list: PASS\n");
00109 }
00110
00111
00112 static void
00113 shuffle (struct value *array, size_t cnt)
00114 {
00115 size_t i;
00116
00117 for (i = 0; i < cnt; i++)
00118 {
00119 size_t j = i + random_ulong () % (cnt - i);
00120 struct value t = array[j];
00121 array[j] = array[i];
00122 array[i] = t;
00123 }
00124 }
00125
00126
00127
00128 static bool
00129 value_less (const struct list_elem *a_, const struct list_elem *b_,
00130 void *aux UNUSED)
00131 {
00132 const struct value *a = list_entry (a_, struct value, elem);
00133 const struct value *b = list_entry (b_, struct value, elem);
00134
00135 return a->value < b->value;
00136 }
00137
00138
00139
00140 static void
00141 verify_list_fwd (struct list *list, int size)
00142 {
00143 struct list_elem *e;
00144 int i;
00145
00146 for (i = 0, e = list_begin (list);
00147 i < size && e != list_end (list);
00148 i++, e = list_next (e))
00149 {
00150 struct value *v = list_entry (e, struct value, elem);
00151 ASSERT (i == v->value);
00152 }
00153 ASSERT (i == size);
00154 ASSERT (e == list_end (list));
00155 }
00156
00157
00158
00159 static void
00160 verify_list_bkwd (struct list *list, int size)
00161 {
00162 struct list_elem *e;
00163 int i;
00164
00165 for (i = 0, e = list_rbegin (list);
00166 i < size && e != list_rend (list);
00167 i++, e = list_prev (e))
00168 {
00169 struct value *v = list_entry (e, struct value, elem);
00170 ASSERT (i == v->value);
00171 }
00172 ASSERT (i == size);
00173 ASSERT (e == list_rend (list));
00174 }