aboutsummaryrefslogtreecommitdiff
path: root/src/unit/list.unit
blob: db1095cca1702c26598dea14073576bc1b0c3474 (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
// vim: syntax=cpp
/*
 * Copyright (C) 2007-2019 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/string.h"
#include "bu/list.h"

typedef Bu::List<int> IntList;

suite List
{
    test append
    {
        IntList lst;
        for( int j = 0; j < 50; j++ )
        {
            lst.append( j );
        }
        int j = 0;
        for( IntList::iterator i = lst.begin(); i; i++, j++ )
        {
            unitTest( *i == j );
        }
    }

    test prepend
    {
        IntList lst;
        for( int j = 0; j < 50; j++ )
        {
            lst.prepend( j );
        }
        int j = 49;
        for( IntList::iterator i = lst.begin(); i; i++, j-- )
        {
            unitTest( *i == j );
        }
    }

    test copy
    {
        IntList lst;
        int j;
        for( j = 0; j < 50; j++ )
        {
            lst.append( j );
        }
        IntList lst2 = lst;

        j = 0;
        for( IntList::iterator i = lst2.begin(); i; i++, j++ )
        {
            unitTest( *i == j );
        }
        lst2.clear();
        lst2 = lst;

        j = 0;
        for( IntList::iterator i = lst2.begin(); i; i++, j++ )
        {
            unitTest( *i == j );
        }
    }

    test sort1
    {
        IntList lst;

        lst.insertSorted( 5 );
        lst.insertSorted( 1 );
        lst.insertSorted( 10 );
        lst.insertSorted( 3 );

        unitTest( lst == IntList(1).append(3).append(5).append(10) );
    }

    test sort2
    {
        IntList lst;

        lst.insertSorted<Bu::__basicGTCmp<int> >( 5 );
        lst.insertSorted<Bu::__basicGTCmp<int> >( 1 );
        lst.insertSorted<Bu::__basicGTCmp<int> >( 10 );
        lst.insertSorted<Bu::__basicGTCmp<int> >( 3 );

        unitTest( lst == IntList(10).append(5).append(3).append(1) );
    }

    test sort3
    {
        IntList lst;
        Bu::__basicGTCmp<int> cmp;

        lst.insertSorted( cmp, 5 );
        lst.insertSorted( cmp, 1 );
        lst.insertSorted( cmp, 10 );
        lst.insertSorted( cmp, 3 );

        unitTest( lst == IntList(10).append(5).append(3).append(1) );
    }

    test sort4
    {
        IntList lst;

        lst.append( 5 );
        lst.append( 1 );
        lst.append( 10 );
        lst.append( 3 );

        lst.sort();

        unitTest( lst == IntList(1).append(3).append(5).append(10) );
    }

    test sort5
    {
        IntList lst;

        lst.append( 5 );
        lst.append( 1 );
        lst.append( 10 );
        lst.append( 3 );

        lst.sort<Bu::__basicGTCmp<int> >();

        unitTest( lst == IntList(10).append(5).append(3).append(1) );
    }

    test sort6
    {
        IntList lst;

        lst.append( 5 );
        lst.append( 1 );
        lst.append( 10 );
        lst.append( 3 );

        Bu::__basicGTCmp<int> x;
        lst.sort( x );

        unitTest( lst == IntList(10).append(5).append(3).append(1) );
    }
}