~sense/ubuntu/lucid/transmission/fix-497882

« back to all changes in this revision

Viewing changes to libtransmission/utils-test.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2008-11-28 15:33:48 UTC
  • mfrom: (1.1.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20081128153348-it70trfnxiroblmc
Tags: 1.40-0ubuntu1
* New upstream release (LP: #302672)
  - Tracker communication uses fewer resources
  - More accurate bandwidth limits
  - Reduce disk fragmentation by preallocating files (LP: #287726)
  - Stability, security and performance improvements to the RPC /
    Web UI server (closes LP: #290423)
  - Support compression when serving Web UI and RPC responses
  - Simplify the RPC whitelist
  - Fix bug that prevented handshakes with encrypted BitComet peers
  - Fix 1.3x bug that could re-download some data unnecessarily
    (LP: #295040)
  - Option to automatically update the blocklist weekly
  - Added off-hour bandwidth scheduling
  - Simplify file/priority selection in the details dialog
  - Fix a couple of crashes
  - New / updated translations
  - Don't inhibit hibernation by default (LP: #292929)
  - Use "close" animation when sending to notification area (LP: #130811)
  - Fix resize problems (LP: #269872)
  - Support "--version" option when launching from command line
    (LP: #292011)
  - Correctly parse announce URLs that have leading or trailing
    spaces (LP: #262411)
  - Display an error when "Open Torrent" fails (LP: #281463)
* Dropped 10_fix_crasher_from_upstream.dpatch: Fix is in this
  upstream release.
* debian/control: Don't just build-depend on libcurl-dev, which is
  a virtual package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include <stdio.h> /* fprintf */
2
2
#include <string.h> /* strcmp */
3
3
#include "transmission.h"
 
4
#include "platform.h"
4
5
#include "utils.h"
 
6
#include "crypto.h"
5
7
 
6
8
#define VERBOSE 0
7
9
#define NUM_LOOPS 1
8
10
#define SPEED_TEST 0
9
11
 
10
12
#if SPEED_TEST
11
 
#undef VERBOSE
12
 
#define VERBOSE 0
13
 
#undef NUM_LOOPS
14
 
#define NUM_LOOPS 200
 
13
 #undef VERBOSE
 
14
 #define VERBOSE 0
 
15
 #undef NUM_LOOPS
 
16
 #define NUM_LOOPS 200
15
17
#endif
16
18
 
17
19
static int test = 0;
18
20
 
19
 
#define check(A) { \
20
 
    ++test; \
21
 
    if (A) { \
22
 
        if( VERBOSE ) \
23
 
            fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
24
 
    } else { \
25
 
        if( VERBOSE ) \
26
 
            fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
27
 
        return test; \
28
 
    } \
29
 
}
 
21
#define check( A ) \
 
22
    { \
 
23
        ++test; \
 
24
        if( A ){ \
 
25
            if( VERBOSE ) \
 
26
                fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__,\
 
27
                         __LINE__ );\
 
28
        } else { \
 
29
            fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__,\
 
30
                     __LINE__ ); \
 
31
            return test; \
 
32
        } \
 
33
    }
30
34
 
31
35
static int
32
36
test_bitfields( void )
33
 
34
 
    int i;
35
 
    int bitcount = 5000000;
 
37
{
 
38
    unsigned int  i;
 
39
    unsigned int  bitcount = 5000000;
36
40
    tr_bitfield * field = tr_bitfieldNew( bitcount );
37
41
 
38
 
    /* make every seventh one true */
39
 
    for( i=0; i<bitcount; ++i )
 
42
    /* test tr_bitfieldAdd */
 
43
    for( i = 0; i < bitcount; ++i )
40
44
        if( !( i % 7 ) )
41
45
            tr_bitfieldAdd( field, i );
42
 
 
43
 
    /* check to see if `has' has the right bits */
44
 
    for( i=0; i<bitcount; ++i )
45
 
        check( tr_bitfieldHas( field, i ) == (!(i%7)) );
46
 
 
47
 
#if 0
48
 
    /* testing the "find next" function */
49
 
    check( tr_bitfieldFindTrue( field, 0, &pos ) );
50
 
    check( pos == 0 );
51
 
    check( tr_bitfieldFindTrue( field, 1, &pos ) );
52
 
    check( pos == 7 );
53
 
    check( tr_bitfieldFindTrue( field, 2, &pos ) );
54
 
    check( pos == 7 );
55
 
    check( tr_bitfieldFindTrue( field, 7, &pos ) );
56
 
    check( pos == 7 );
57
 
    check( tr_bitfieldFindTrue( field, 8, &pos ) );
58
 
    check( pos == 14 );
59
 
    check( tr_bitfieldFindTrue( field, 13, &pos ) );
60
 
    check( pos == 14 );
61
 
    check( tr_bitfieldFindTrue( field, 14, &pos ) );
62
 
    check( pos == 14 );
63
 
    check( tr_bitfieldFindTrue( field, 15, &pos ) );
64
 
    check( pos == 21 );
65
 
    check( tr_bitfieldFindTrue( field, 16, &pos ) );
66
 
    check( pos == 21 );
67
 
#endif
 
46
    for( i = 0; i < bitcount; ++i )
 
47
        check( tr_bitfieldHas( field, i ) == ( !( i % 7 ) ) );
 
48
 
 
49
    /* test tr_bitfieldAddRange */
 
50
    tr_bitfieldAddRange( field, 0, bitcount );
 
51
    for( i = 0; i < bitcount; ++i )
 
52
        check( tr_bitfieldHas( field, i ) );
 
53
 
 
54
    /* test tr_bitfieldRemRange in the middle of a boundary */
 
55
    tr_bitfieldRemRange( field, 4, 21 );
 
56
    for( i = 0; i < 64; ++i )
 
57
        check( tr_bitfieldHas( field, i ) == ( ( i < 4 ) || ( i >= 21 ) ) );
 
58
 
 
59
    /* test tr_bitfieldRemRange on the boundaries */
 
60
    tr_bitfieldAddRange( field, 0, 64 );
 
61
    tr_bitfieldRemRange( field, 8, 24 );
 
62
    for( i = 0; i < 64; ++i )
 
63
        check( tr_bitfieldHas( field, i ) == ( ( i < 8 ) || ( i >= 24 ) ) );
 
64
 
 
65
    /* test tr_bitfieldRemRange when begin & end is on the same word */
 
66
    tr_bitfieldAddRange( field, 0, 64 );
 
67
    tr_bitfieldRemRange( field, 4, 5 );
 
68
    for( i = 0; i < 64; ++i )
 
69
        check( tr_bitfieldHas( field, i ) == ( ( i < 4 ) || ( i >= 5 ) ) );
 
70
 
 
71
    /* test tr_bitfieldAddRange */
 
72
    tr_bitfieldRemRange( field, 0, 64 );
 
73
    tr_bitfieldAddRange( field, 4, 21 );
 
74
    for( i = 0; i < 64; ++i )
 
75
        check( tr_bitfieldHas( field, i ) == ( ( 4 <= i ) && ( i < 21 ) ) );
 
76
 
 
77
    /* test tr_bitfieldAddRange on the boundaries */
 
78
    tr_bitfieldRemRange( field, 0, 64 );
 
79
    tr_bitfieldAddRange( field, 8, 24 );
 
80
    for( i = 0; i < 64; ++i )
 
81
        check( tr_bitfieldHas( field, i ) == ( ( 8 <= i ) && ( i < 24 ) ) );
 
82
 
 
83
    /* test tr_bitfieldAddRange when begin & end is on the same word */
 
84
    tr_bitfieldRemRange( field, 0, 64 );
 
85
    tr_bitfieldAddRange( field, 4, 5 );
 
86
    for( i = 0; i < 64; ++i )
 
87
        check( tr_bitfieldHas( field, i ) == ( ( 4 <= i ) && ( i < 5 ) ) );
68
88
 
69
89
    tr_bitfieldFree( field );
70
90
    return 0;
71
91
}
72
92
 
73
 
 
 
93
static int
 
94
test_strstrip( void )
 
95
{
 
96
    char *in, *out;
 
97
 
 
98
    /* strstrip */
 
99
    in = tr_strdup( "   test    " );
 
100
    out = tr_strstrip( in );
 
101
    check( in == out );
 
102
    check( !strcmp( in, "test" ) );
 
103
    tr_free( in );
 
104
 
 
105
    /* strstrip */
 
106
    in = tr_strdup( " test test " );
 
107
    out = tr_strstrip( in );
 
108
    check( in == out );
 
109
    check( !strcmp( in, "test test" ) );
 
110
    tr_free( in );
 
111
 
 
112
    /* strstrip */
 
113
    in = tr_strdup( "test" );
 
114
    out = tr_strstrip( in );
 
115
    check( in == out );
 
116
    check( !strcmp( in, "test" ) );
 
117
    tr_free( in );
 
118
 
 
119
    return 0;
 
120
}
 
121
 
 
122
static int
 
123
test_buildpath( void )
 
124
{
 
125
    char * out;
 
126
 
 
127
    out = tr_buildPath( "foo", "bar", NULL );
 
128
    check( !strcmp( out, "foo" TR_PATH_DELIMITER_STR "bar" ) );
 
129
    tr_free( out );
 
130
 
 
131
    out = tr_buildPath( "", "foo", "bar", NULL );
 
132
    check( !strcmp( out, TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar" ) );
 
133
    tr_free( out );
 
134
 
 
135
    return 0;
 
136
}
74
137
 
75
138
int
76
139
main( void )
77
140
{
78
141
    char *in, *out;
79
 
    int len;
80
 
    int i;
81
 
    int l;
 
142
    int   len;
 
143
    int   i;
 
144
    int   l;
82
145
 
83
146
    /* base64 */
84
147
    in = "YOYO!";
93
156
    tr_free( in );
94
157
    tr_free( out );
95
158
 
96
 
    /* tr_stringEndsWith */
97
 
    check( tr_stringEndsWith( "the", "the" ) );
98
 
    check( tr_stringEndsWith( "dress", "dress" ) );
99
 
    check( tr_stringEndsWith( "address", "dress" ) );
100
 
    check( !tr_stringEndsWith( "foo.bin", "gfoo.bin" ) );
101
 
    check( !tr_stringEndsWith( "xyz", "xyw" ) );
 
159
    if( ( i = test_strstrip( ) ) )
 
160
        return i;
 
161
    if( ( i = test_buildpath( ) ) )
 
162
        return i;
 
163
 
 
164
    /* test that tr_cryptoRandInt() stays in-bounds */
 
165
    for( i = 0; i < 100000; ++i )
 
166
    {
 
167
        const int val = tr_cryptoRandInt( 100 );
 
168
        check( val >= 0 );
 
169
        check( val < 100 );
 
170
    }
102
171
 
103
172
    /* simple bitfield tests */
104
 
    for( l=0; l<NUM_LOOPS; ++l )
105
 
        if(( i = test_bitfields( )))
 
173
    for( l = 0; l < NUM_LOOPS; ++l )
 
174
        if( ( i = test_bitfields( ) ) )
106
175
            return i;
107
176
 
108
177
    return 0;
109
178
}
 
179