source: branches/1.3x/libtransmission/utils-test.c @ 6489

Last change on this file since 6489 was 6489, checked in by charles, 14 years ago

(1.3x) lots of C correctness tweaks suggested by sparse/cgcc

File size: 2.6 KB
Line 
1#include <stdio.h> /* fprintf */
2#include <string.h> /* strcmp */
3#include "transmission.h"
4#include "utils.h"
5
6#define VERBOSE 0
7#define NUM_LOOPS 1
8#define SPEED_TEST 0
9
10#if SPEED_TEST
11#undef VERBOSE
12#define VERBOSE 0
13#undef NUM_LOOPS
14#define NUM_LOOPS 200
15#endif
16
17static int test = 0;
18
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}
30
31static int
32test_bitfields( void )
33{ 
34    int i;
35    int bitcount = 5000000;
36    tr_bitfield * field = tr_bitfieldNew( bitcount );
37
38    /* make every seventh one true */
39    for( i=0; i<bitcount; ++i )
40        if( !( i % 7 ) )
41            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
68
69    tr_bitfieldFree( field );
70    return 0;
71}
72
73
74
75int
76main( void )
77{
78    char *in, *out;
79    int len;
80    int i;
81    int l;
82
83    /* base64 */
84    in = "YOYO!";
85    out = tr_base64_encode( in, -1, &len );
86    check( out );
87    check( !strcmp( out, "WU9ZTyE=\n" ) );
88    check( len == 9 );
89    in = tr_base64_decode( out, -1, &len );
90    check( in );
91    check( !strcmp( in, "YOYO!" ) );
92    check( len == 5 );
93    tr_free( in );
94    tr_free( out );
95
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" ) );
102
103    /* simple bitfield tests */
104    for( l=0; l<NUM_LOOPS; ++l )
105        if(( i = test_bitfields( )))
106            return i;
107
108    return 0;
109}
Note: See TracBrowser for help on using the repository browser.