source: trunk/libtransmission/test-peer-id.c @ 12121

Last change on this file since 12121 was 12121, checked in by jordan, 11 years ago

(trunk libT) Add an enumeration for the peer id length. Use that enum for the peer_id fields in tr_session and tr_torrent.

This also avoids an extra malloc/free per-torrent and per-session, but mostly this tweak is for the extra readability of the PEER_ID_LEN=20 enum.

  • Property svn:keywords set to Date Rev Author Id
File size: 1.2 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include "transmission.h"
5#include "session.h"
6#include "utils.h"
7#include "version.h"
8
9#undef VERBOSE
10
11#ifdef VERBOSE
12  #define check( A ) \
13    { \
14        ++test; \
15        if( A ){ \
16            fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
17        } else { \
18            fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
19            return test; \
20        } \
21    }
22#else
23  #define check( A ) \
24    { \
25        ++test; \
26        if( !( A ) ){ \
27            fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
28            return test; \
29        } \
30    }
31#endif
32
33int
34main( void )
35{
36    int i;
37    int test = 0;
38    uint8_t peer_id[PEER_ID_LEN+1];
39
40    for( i = 0; i < 100000; ++i )
41    {
42        int j;
43        int val = 0;
44
45        tr_peerIdInit( peer_id );
46
47        check( strlen( (char*)peer_id ) == PEER_ID_LEN );
48        check( !memcmp( peer_id, PEERID_PREFIX, 8 ) );
49
50        for( j = 8; j < PEER_ID_LEN; ++j )
51        {
52            char tmp[2] = { peer_id[j], '\0' };
53            val += strtoul( tmp, NULL, 36 );
54        }
55
56        check( ( val % 36 ) == 0 );
57    }
58
59    return 0;
60}
61
Note: See TracBrowser for help on using the repository browser.