1/*
2 * Copyright (c) 2014 ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Universitaetsstrasse 4, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#include <iostream>
11#include <list>
12#include <string>
13
14#include "cxxtest.hpp"
15
16using namespace std;
17
18class AAA
19{
20    friend ostream &operator<<(ostream &,
21                               const AAA &);
22
23public:
24    int x;
25    int y;
26    float z;
27
28    AAA();
29    AAA(const AAA &);
30    ~AAA()
31    {
32    }
33    ;
34    AAA &operator=(const AAA &rhs);
35    int operator==(const AAA &rhs) const;
36    int operator<(const AAA &rhs) const;
37};
38
39AAA::AAA()   // Constructor
40{
41    x = 0;
42    y = 0;
43    z = 0;
44}
45
46AAA::AAA(const AAA &copyin)   // Copy constructor to handle pass by value.
47{
48    x = copyin.x;
49    y = copyin.y;
50    z = copyin.z;
51}
52
53ostream &operator<<(ostream &output,
54                    const AAA &aaa)
55{
56    output << aaa.x << ' ' << aaa.y << ' ' << aaa.z << endl;
57    return output;
58}
59
60AAA& AAA::operator=(const AAA &rhs)
61{
62    this->x = rhs.x;
63    this->y = rhs.y;
64    this->z = rhs.z;
65    return *this;
66}
67
68int AAA::operator==(const AAA &rhs) const
69{
70    if (this->x != rhs.x)
71        return 0;
72    if (this->y != rhs.y)
73        return 0;
74    if (this->z != rhs.z)
75        return 0;
76    return 1;
77}
78
79// This function is required for built-in STL list functions like sort
80int AAA::operator<(const AAA &rhs) const
81{
82    if (this->x == rhs.x && this->y == rhs.y && this->z < rhs.z)
83        return 1;
84    if (this->x == rhs.x && this->y < rhs.y)
85        return 1;
86    if (this->x < rhs.x)
87        return 1;
88    return 0;
89}
90
91static void stl_list_test_double(void)
92{
93    std::cout << "STL List: stl_list_test_double" << std::endl;
94
95    list<AAA> L;
96    AAA Ablob;
97
98    Ablob.x = 7;
99    Ablob.y = 2;
100    Ablob.z = 4.2355;
101    L.push_back(Ablob);  // Insert a new element at the end
102
103    Ablob.x = 5;
104    L.push_back(Ablob);  // Object passed by value. Uses default member-wise
105                         // copy constructor
106    Ablob.z = 3.2355;
107    L.push_back(Ablob);
108
109    Ablob.x = 3;
110    Ablob.y = 7;
111    Ablob.z = 7.2355;
112    L.push_back(Ablob);
113
114    list<AAA>::iterator i;
115
116    for (i = L.begin(); i != L.end(); ++i)
117        cout << (*i).x << " ";  // print member
118    cout << endl;
119
120    for (i = L.begin(); i != L.end(); ++i)
121        cout << *i << " ";  // print with overloaded operator
122    cout << endl;
123
124    cout << "Sorted: " << endl;
125    L.sort();
126    for (i = L.begin(); i != L.end(); ++i)
127        cout << *i << " ";  // print with overloaded operator
128    cout << endl;
129}
130
131static void stl_list_test_simple(void)
132{
133    std::cout << "STL List: stl_list_test_simple" << std::endl;
134    list<int> L;
135    L.push_back(0);              // Insert a new element at the end
136    L.push_front(0);             // Insert a new element at the beginning
137    L.insert(++L.begin(), 2);     // Insert "2" before position of first argument
138                                  // (Place before second argument)
139    L.push_back(5);
140    L.push_back(6);
141
142    list<int>::iterator i;
143
144    for (i = L.begin(); i != L.end(); ++i)
145        cout << *i << " ";
146    cout << endl;
147}
148
149void stl_list_test(void)
150{
151    std::cout << "STL List tests" << std::endl;
152    stl_list_test_simple();
153    stl_list_test_double();
154}
155