New testcase, distilled from ed-0.2
[oota-llvm.git] / test / CFrontend / 2002-07-14-MiscListTests.c
1 // Test list stuff
2
3 void *malloc(unsigned);
4
5 // Test opaque structure support.  the list type is defined later
6 struct list;
7
8 struct list *PassThroughList(struct list *L) {
9   return L;
10 }
11
12
13 // Recursive data structure tests...
14
15 typedef struct list {
16   int Data;
17   struct list *Next;
18 } list;
19
20 list *Data;
21
22 void foo() {
23   static int Foo = 0;            // Test static local variable
24   Foo += 1;                      // Increment static variable
25
26   Data = (list*)malloc(12);      // This is not a proper list allocation
27 }
28
29 extern list ListNode1;
30 list ListNode3 = { 4, 0          };
31 list ListNode2 = { 3, &ListNode3 };
32 list ListNode0 = { 1, &ListNode1 };
33 list ListNode1 = { 2, &ListNode2 };
34
35
36 list ListArray[10];
37
38 // Iterative insert fn
39 void InsertIntoListTail(list **L, int Data) {
40   while (*L)
41     L = &(*L)->Next;
42   *L = (list*)malloc(sizeof(list));
43   (*L)->Data = Data;
44   (*L)->Next = 0;
45 }
46
47 // Recursive list search fn
48 list *FindData(list *L, int Data) {
49   if (L == 0) return 0;
50   if (L->Data == Data) return L;
51   return FindData(L->Next, Data);
52 }
53
54 void foundIt(void);
55
56 // Driver fn...
57 void DoListStuff() {
58   list *MyList = 0;
59   InsertIntoListTail(&MyList, 100);
60   InsertIntoListTail(&MyList, 12);
61   InsertIntoListTail(&MyList, 42);
62   InsertIntoListTail(&MyList, 1123);
63   InsertIntoListTail(&MyList, 1213);
64
65   if (FindData(MyList, 75)) foundIt();
66   if (FindData(MyList, 42)) foundIt();
67   if (FindData(MyList, 700)) foundIt();
68 }
69