File: list.test.h

package info (click to toggle)
libwibble 0.1.19
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 832 kB
  • ctags: 1,940
  • sloc: cpp: 9,798; makefile: 163; perl: 84; sh: 11
file content (161 lines) | stat: -rw-r--r-- 4,174 bytes parent folder | download
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
// -*- C++ -*-

#include <wibble/list.h>
#include <wibble/test.h>

using namespace wibble;

struct TestList {
    struct My {
        typedef int Type;
        int i, max;
        int head() const { return i; }

        My tail() const {
            My t = *this;
            if ( i < max )
                t.i ++;
            if ( i > max )
                t.i --;
            return t;
        }

        bool empty() const { return i == max; }

        My( int j = 0, int m = 0 ) : i( j ), max( m ) {}
    };

    struct My2 {
        typedef int Type;
        int i, max, rep, repmax;
        int head() const { return i; }

        My2 tail() const {
            My2 t = *this;
            if ( rep > 0 )
                t.rep --;
            else {
                t.rep = repmax;
                if ( i < max )
                    t.i ++;
                if ( i > max )
                    t.i --;
            }
            return t;
        }

        bool empty() const { return i == max; }

        My2( int j = 0, int m = 0, int r = 0 ) : i( j ), max( m ),
                                                 rep( r ), repmax( r ) {}
    };

    static bool odd( int i ) {
        return i % 2 == 1;
    }

    template< typename List >
    void checkOddList( List l ) {
        int i = 0;
        while ( !l.empty() ) {
            assert( odd( l.head() ) );
            l = l.tail();
            ++ i;
        }
        assert_eq( i, 512 );
    }

    template< typename List >
    void checkListSorted( List l )
    {
        if ( l.empty() )
            return;
        typename List::Type last = l.head();
        while ( !l.empty() ) {
            assert( last <= l.head() );
            last = l.head();
            l = l.tail();
        }
    }

    Test count() {
        My list( 512, 1024 );
        assert_eq( list::count( list ), 512 );
        list = My( 0, 1024 );
        assert_eq( list::count( list ), 1024 );
    }

    Test filtered() {
        My list( 1, 1024 );
        checkOddList( list::filter( list, odd ) );
        assert_eq( list::count( list::filter( list, odd ) ), 512 );
    }

    Test sorted() {
        My list( 1, 1024 );
        assert_eq( list::count( list ), list::count( list::sort( list ) ) );
        checkListSorted( list );
        checkListSorted( list::sort( list ) );
        {
            ExpectFailure fail;
            checkListSorted( My( 100, 0 ) );
        }
        checkListSorted( list::sort( My( 100, 0 ) ) );
    }

    Test take() {
        My list( 0, 1024 );
        assert_eq( list::count( list ), 1024 );
        assert_eq( list::count( list::take( 50, list ) ), 50 );
    }

    Test unique() {
        My2 list( 0, 20, 3 );
        assert_eq( list::count( list ), 80 );
        assert_eq( list::count( list::unique( list ) ), 20 );
        assert_eq( list::unique( list ).head(), 0 );
        assert_eq( list::unique( list ).tail().head(), 1 );
    }

    Test stl() {
        My list( 0, 1024 );
        std::vector< int > vec;
        std::copy( list::begin( list ), list::end( list ),
                   std::back_inserter( vec ) ); 
        for ( int i = 0; i < 1024; ++i )
            assert_eq( vec[i], i );
    }

    static int mul2add1( int a ) {
        return a * 2 + 1;
    }

    Test map() {
        My list( 0, 512 );
        checkOddList( 
            list::map( list, std::ptr_fun( mul2add1 ) ) );
    }

    Test empty() {
        assert( list::Empty< int >().empty() );
    }

    Test single() {
        assert_eq( list::singular( 0 ).head(), 0 );
        assert( list::singular( 0 ).tail().empty() );
    }

    Test append() {
        assert_eq( list::append( list::singular( 0 ),
                                 list::singular( 1 ) ).head(), 0 );
        assert_eq( list::append( list::singular( 0 ),
                                 list::singular( 1 ) ).tail().head(), 1 );
        assert( list::append( list::singular( 0 ),
                              list::singular( 1 ) ).tail().tail().empty() );
    }

    Test appendCount() {
        assert_eq( list::count( list::append( My( 0, 10 ),
                                              My2( 0, 5, 1 ) ) ), 20 );
    }
};