1/*
2 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
13
14#include "list_wrapper.h"
15
16const int kNumberOfElements = 10;
17
18void FailTest(bool failed)
19{
20    if (failed)
21    {
22        printf("Test failed!\n");
23        printf("Press enter to continue:");
24        getchar();
25        exit(0);
26    }
27}
28
29int GetStoredIntegerValue(ListItem* list_item)
30{
31    void* list_item_pointer = list_item->GetItem();
32    if (list_item_pointer != NULL)
33    {
34        return *(reinterpret_cast<int*>(list_item_pointer));
35    }
36    return static_cast<int>(list_item->GetUnsignedItem());
37}
38
39void PrintList(ListWrapper& list)
40{
41    ListItem* list_item = list.First();
42    printf("List: ");
43    while (list_item != NULL)
44    {
45        int item_value = GetStoredIntegerValue(list_item);
46        FailTest(item_value < 0);
47        printf(" %d",item_value);
48        list_item = list.Next(list_item);
49    }
50    printf("\n");
51}
52
53// The list should always be in ascending order
54void ListSanity(ListWrapper& list)
55{
56    if(list.Empty())
57    {
58      return;
59    }
60    ListItem* item_iter = list.First();
61    // Fake a previous value for the first iteration
62    int previous_value = GetStoredIntegerValue(item_iter) - 1;
63    while (item_iter != NULL)
64    {
65        const int value = GetStoredIntegerValue(item_iter);
66        FailTest(value != previous_value + 1);
67        previous_value = value;
68        item_iter = list.Next(item_iter);
69    }
70}
71
72int main(int /*argc*/, char* /*argv*/[])
73{
74    printf("List Test:\n");
75    int element_array[kNumberOfElements];
76    for (int i = 0; i < kNumberOfElements; i++)
77    {
78        element_array[i] = i;
79    }
80    // Test PushBack 1
81    ListWrapper test_list;
82    for (int i = 2; i < kNumberOfElements - 2; i++)
83    {
84        FailTest(test_list.PushBack((void*)&element_array[i]) != 0);
85    }
86    // Test PushBack 2
87    FailTest(test_list.PushBack(element_array[kNumberOfElements - 2]) != 0);
88    FailTest(test_list.PushBack(element_array[kNumberOfElements - 1]) != 0);
89    // Test PushFront 2
90    FailTest(test_list.PushFront(element_array[1]) != 0);
91    // Test PushFront 1
92    FailTest(test_list.PushFront((void*)&element_array[0]) != 0);
93    // Test GetSize
94    FailTest(test_list.GetSize() != kNumberOfElements);
95    PrintList(test_list);
96    //Test PopFront
97    FailTest(test_list.PopFront() != 0);
98    //Test PopBack
99    FailTest(test_list.PopBack() != 0);
100    // Test GetSize
101    FailTest(test_list.GetSize() != kNumberOfElements - 2);
102    // Test Empty
103    FailTest(test_list.Empty());
104    // Test First
105    ListItem* first_item = test_list.First();
106    FailTest(first_item == NULL);
107    // Test Last
108    ListItem* last_item = test_list.Last();
109    FailTest(last_item == NULL);
110    // Test Next
111    ListItem* second_item = test_list.Next(first_item);
112    FailTest(second_item == NULL);
113    FailTest(test_list.Next(last_item) != NULL);
114    FailTest(test_list.Next(NULL) != NULL);
115    // Test Previous
116    ListItem* second_to_last_item = test_list.Previous(last_item);
117    FailTest(second_to_last_item == NULL);
118    FailTest(test_list.Previous(first_item) != NULL);
119    FailTest(test_list.Previous(NULL) != NULL);
120    // Test GetUnsignedItem
121    FailTest(last_item->GetUnsignedItem() !=
122             kNumberOfElements - 2);
123    FailTest(last_item->GetItem() !=
124             NULL);
125    // Test GetItem
126    FailTest(GetStoredIntegerValue(second_to_last_item) !=
127             kNumberOfElements - 3);
128    FailTest(second_to_last_item->GetUnsignedItem() != 0);
129    // Pop last and first since they are pushed as unsigned items.
130    FailTest(test_list.PopFront() != 0);
131    FailTest(test_list.PopBack() != 0);
132    // Test Insert. Please note that old iterators are no longer valid at
133    // this point.
134    ListItem* insert_item_last = new ListItem(reinterpret_cast<void*>(&element_array[kNumberOfElements - 2]));
135    FailTest(test_list.Insert(test_list.Last(),insert_item_last) != 0);
136    FailTest(test_list.Insert(NULL,insert_item_last) == 0);
137    ListItem* insert_item_last2 = new ListItem(reinterpret_cast<void*>(&element_array[kNumberOfElements - 2]));
138    FailTest(test_list.Insert(insert_item_last2,NULL) == 0);
139    // test InsertBefore
140    ListItem* insert_item_first = new ListItem(reinterpret_cast<void*>(&element_array[1]));
141    FailTest(test_list.InsertBefore(test_list.First(),insert_item_first) != 0);
142    FailTest(test_list.InsertBefore(NULL,insert_item_first) == 0);
143    ListItem* insert_item_first2 = new ListItem(reinterpret_cast<void*>(&element_array[1]));
144    FailTest(test_list.InsertBefore(insert_item_first2,NULL) == 0);
145    PrintList(test_list);
146    ListSanity(test_list);
147    // Erase the whole list
148    int counter = 0;
149    while (test_list.PopFront() == 0)
150    {
151        FailTest(counter++ > kNumberOfElements);
152    }
153    PrintList(test_list);
154    // Test APIs when list is empty
155    FailTest(test_list.GetSize() != 0);
156    FailTest(test_list.PopFront() != -1);
157    FailTest(test_list.PopBack() != -1);
158    FailTest(!test_list.Empty());
159    FailTest(test_list.First() != NULL);
160    FailTest(test_list.Last() != NULL);
161    FailTest(test_list.Next(NULL) != NULL);
162    FailTest(test_list.Previous(NULL) != NULL);
163    FailTest(test_list.Erase(NULL) != -1);
164    // Test Insert APIs when list is empty
165    ListItem* new_item = new ListItem(reinterpret_cast<void*>(&element_array[0]));
166    FailTest(test_list.Insert(NULL,new_item) != 0);
167    FailTest(test_list.Empty());
168    FailTest(test_list.PopFront() != 0);
169    ListItem* new_item2 = new ListItem(reinterpret_cast<void*>(&element_array[0]));
170    FailTest(test_list.InsertBefore(NULL,new_item2) != 0);
171    FailTest(test_list.Empty());
172
173    printf("Tests passed successfully!\n");
174}
175