grumble bikeshed grumble

This commit is contained in:
Charles Kerr 2008-01-07 17:52:50 +00:00
parent a8b9567feb
commit a7ff9218ab
4 changed files with 67 additions and 18 deletions

View File

@ -73,7 +73,8 @@ noinst_HEADERS = \
noinst_PROGRAMS = \ noinst_PROGRAMS = \
test-fastset test-fastset \
test-peer-id
TESTS = $(noinst_PROGRAMS) TESTS = $(noinst_PROGRAMS)
@ -86,6 +87,8 @@ TEST_LDADD = \
test_fastset_SOURCES = test-fastset.c test_fastset_SOURCES = test-fastset.c
test_fastset_LDADD = $(TEST_LDADD) test_fastset_LDADD = $(TEST_LDADD)
test_peer_id_SOURCES = test-peer-id.c
test_peer_id_LDADD = $(TEST_LDADD)

View File

@ -4,19 +4,22 @@
#include "peer-mgr.h" #include "peer-mgr.h"
#include "utils.h" #include "utils.h"
#define VERBOSE 0
#define check(A) { \ #define check(A) { \
++test; \ ++test; \
if (A) \ if (A) { \
if( VERBOSE ) \
fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
else { \ } else { \
fprintf( stderr, "FAILPASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \ if( VERBOSE ) \
fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
return test; \ return test; \
} \ } \
} }
int
int main( void ) main( void )
{ {
uint32_t i; uint32_t i;
int test = 0; int test = 0;

View File

@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "transmission.h"
#include "utils.h"
#define VERBOSE 0
#define check(A) { \
++test; \
if (A) { \
if( VERBOSE ) \
fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
} else { \
if( VERBOSE ) \
fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
return test; \
} \
}
int
main( void )
{
int i;
int test = 0;
for( i=0; i<100000; ++i )
{
int j;
int val = 0;
uint8_t * pch = tr_peerIdNew( );
check( strlen( (char*)pch ) == 20 );
check( !memcmp( pch, PEERID_PREFIX, 8 ) );
for( j=8; j<20; ++j ) {
char tmp[2] = { pch[j], '\0' };
val += strtoul( tmp, NULL, 36 );
}
check( ( val % 36 ) == 0 );
tr_free( pch );
}
return 0;
}

View File

@ -55,29 +55,25 @@ uint8_t*
tr_peerIdNew( void ) tr_peerIdNew( void )
{ {
int i; int i;
int val;
int total = 0; int total = 0;
uint8_t * buf = tr_new( uint8_t, 21 ); uint8_t * buf = tr_new( uint8_t, 21 );
const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz"; const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz";
const int base = strlen( pool ); const int base = 36;
memcpy( buf, PEERID_PREFIX, 8 ); memcpy( buf, PEERID_PREFIX, 8 );
for( i=8; i<19; ++i ) { for( i=8; i<19; ++i ) {
const int val = tr_rand( base ); val = tr_rand( base );
total += val; total += val;
buf[i] = pool[val]; buf[i] = pool[val];
} }
if( 1 ) { val = total % base ? base - (total % base) : 0;
int val = 0;
while( ( total + val ) % base )
++val;
buf[19] = pool[val];
total += val; total += val;
} buf[19] = pool[val];
assert( ( total % base ) == 0 );
buf[20] = '\0'; buf[20] = '\0';
return buf; return buf;
} }