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 = \
test-fastset
test-fastset \
test-peer-id
TESTS = $(noinst_PROGRAMS)
@ -86,6 +87,8 @@ TEST_LDADD = \
test_fastset_SOURCES = test-fastset.c
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 "utils.h"
#define VERBOSE 0
#define check(A) { \
++test; \
if (A) \
fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
else { \
fprintf( stderr, "FAILPASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
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
main( void )
{
uint32_t i;
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 )
{
int i;
int val;
int total = 0;
uint8_t * buf = tr_new( uint8_t, 21 );
const char * pool = "0123456789abcdefghijklmnopqrstuvwxyz";
const int base = strlen( pool );
const int base = 36;
memcpy( buf, PEERID_PREFIX, 8 );
for( i=8; i<19; ++i ) {
const int val = tr_rand( base );
val = tr_rand( base );
total += val;
buf[i] = pool[val];
}
if( 1 ) {
int val = 0;
while( ( total + val ) % base )
++val;
buf[19] = pool[val];
total += val;
}
assert( ( total % base ) == 0 );
val = total % base ? base - (total % base) : 0;
total += val;
buf[19] = pool[val];
buf[20] = '\0';
return buf;
}