aboutsummaryrefslogtreecommitdiff
path: root/src/system_wrappers/test/list/list.cc
blob: 5c4f0b96ecee67eea53cce1db7de72a6b067774a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <stdio.h>
#include <stdlib.h>

#include "list_wrapper.h"

const int kNumberOfElements = 10;

void FailTest(bool failed)
{
    if (failed)
    {
        printf("Test failed!\n");
        printf("Press enter to continue:");
        getchar();
        exit(0);
    }
}

int GetStoredIntegerValue(ListItem* list_item)
{
    void* list_item_pointer = list_item->GetItem();
    if (list_item_pointer != NULL)
    {
        return *(reinterpret_cast<int*>(list_item_pointer));
    }
    return static_cast<int>(list_item->GetUnsignedItem());
}

void PrintList(ListWrapper& list)
{
    ListItem* list_item = list.First();
    printf("List: ");
    while (list_item != NULL)
    {
        int item_value = GetStoredIntegerValue(list_item);
        FailTest(item_value < 0);
        printf(" %d",item_value);
        list_item = list.Next(list_item);
    }
    printf("\n");
}

// The list should always be in ascending order
void ListSanity(ListWrapper& list)
{
    if(list.Empty())
    {
      return;
    }
    ListItem* item_iter = list.First();
    // Fake a previous value for the first iteration
    int previous_value = GetStoredIntegerValue(item_iter) - 1;
    while (item_iter != NULL)
    {
        const int value = GetStoredIntegerValue(item_iter);
        FailTest(value != previous_value + 1);
        previous_value = value;
        item_iter = list.Next(item_iter);
    }
}

int main(int /*argc*/, char* /*argv*/[])
{
    printf("List Test:\n");
    int element_array[kNumberOfElements];
    for (int i = 0; i < kNumberOfElements; i++)
    {
        element_array[i] = i;
    }
    // Test PushBack 1
    ListWrapper test_list;
    for (int i = 2; i < kNumberOfElements - 2; i++)
    {
        FailTest(test_list.PushBack((void*)&element_array[i]) != 0);
    }
    // Test PushBack 2
    FailTest(test_list.PushBack(element_array[kNumberOfElements - 2]) != 0);
    FailTest(test_list.PushBack(element_array[kNumberOfElements - 1]) != 0);
    // Test PushFront 2
    FailTest(test_list.PushFront(element_array[1]) != 0);
    // Test PushFront 1
    FailTest(test_list.PushFront((void*)&element_array[0]) != 0);
    // Test GetSize
    FailTest(test_list.GetSize() != kNumberOfElements);
    PrintList(test_list);
    //Test PopFront
    FailTest(test_list.PopFront() != 0);
    //Test PopBack
    FailTest(test_list.PopBack() != 0);
    // Test GetSize
    FailTest(test_list.GetSize() != kNumberOfElements - 2);
    // Test Empty
    FailTest(test_list.Empty());
    // Test First
    ListItem* first_item = test_list.First();
    FailTest(first_item == NULL);
    // Test Last
    ListItem* last_item = test_list.Last();
    FailTest(last_item == NULL);
    // Test Next
    ListItem* second_item = test_list.Next(first_item);
    FailTest(second_item == NULL);
    FailTest(test_list.Next(last_item) != NULL);
    FailTest(test_list.Next(NULL) != NULL);
    // Test Previous
    ListItem* second_to_last_item = test_list.Previous(last_item);
    FailTest(second_to_last_item == NULL);
    FailTest(test_list.Previous(first_item) != NULL);
    FailTest(test_list.Previous(NULL) != NULL);
    // Test GetUnsignedItem
    FailTest(last_item->GetUnsignedItem() !=
             kNumberOfElements - 2);
    FailTest(last_item->GetItem() !=
             NULL);
    // Test GetItem
    FailTest(GetStoredIntegerValue(second_to_last_item) !=
             kNumberOfElements - 3);
    FailTest(second_to_last_item->GetUnsignedItem() != 0);
    // Pop last and first since they are pushed as unsigned items.
    FailTest(test_list.PopFront() != 0);
    FailTest(test_list.PopBack() != 0);
    // Test Insert. Please note that old iterators are no longer valid at
    // this point.
    ListItem* insert_item_last = new ListItem(reinterpret_cast<void*>(&element_array[kNumberOfElements - 2]));
    FailTest(test_list.Insert(test_list.Last(),insert_item_last) != 0);
    FailTest(test_list.Insert(NULL,insert_item_last) == 0);
    ListItem* insert_item_last2 = new ListItem(reinterpret_cast<void*>(&element_array[kNumberOfElements - 2]));
    FailTest(test_list.Insert(insert_item_last2,NULL) == 0);
    // test InsertBefore
    ListItem* insert_item_first = new ListItem(reinterpret_cast<void*>(&element_array[1]));
    FailTest(test_list.InsertBefore(test_list.First(),insert_item_first) != 0);
    FailTest(test_list.InsertBefore(NULL,insert_item_first) == 0);
    ListItem* insert_item_first2 = new ListItem(reinterpret_cast<void*>(&element_array[1]));
    FailTest(test_list.InsertBefore(insert_item_first2,NULL) == 0);
    PrintList(test_list);
    ListSanity(test_list);
    // Erase the whole list
    int counter = 0;
    while (test_list.PopFront() == 0)
    {
        FailTest(counter++ > kNumberOfElements);
    }
    PrintList(test_list);
    // Test APIs when list is empty
    FailTest(test_list.GetSize() != 0);
    FailTest(test_list.PopFront() != -1);
    FailTest(test_list.PopBack() != -1);
    FailTest(!test_list.Empty());
    FailTest(test_list.First() != NULL);
    FailTest(test_list.Last() != NULL);
    FailTest(test_list.Next(NULL) != NULL);
    FailTest(test_list.Previous(NULL) != NULL);
    FailTest(test_list.Erase(NULL) != -1);
    // Test Insert APIs when list is empty
    ListItem* new_item = new ListItem(reinterpret_cast<void*>(&element_array[0]));
    FailTest(test_list.Insert(NULL,new_item) != 0);
    FailTest(test_list.Empty());
    FailTest(test_list.PopFront() != 0);
    ListItem* new_item2 = new ListItem(reinterpret_cast<void*>(&element_array[0]));
    FailTest(test_list.InsertBefore(NULL,new_item2) != 0);
    FailTest(test_list.Empty());

    printf("Tests passed successfully!\n");
}