2 #include <stdio.h> /* fprintf */
3 #include <string.h> /* strcmp */
5 #include "transmission.h"
7 #include "ConvertUTF.h" /* tr_utf8_validate*/
31 fprintf( stderr, "PASS test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
33 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
42 fprintf( stderr, "FAIL test #%d (%s, %d)\n", test, __FILE__, __LINE__ ); \
49 test_bitfields( void )
52 unsigned int bitcount
= 5000000;
53 tr_bitfield
* field
= tr_bitfieldNew( bitcount
);
55 /* test tr_bitfieldAdd */
56 for( i
= 0; i
< bitcount
; ++i
)
58 tr_bitfieldAdd( field
, i
);
59 for( i
= 0; i
< bitcount
; ++i
)
60 check( tr_bitfieldHas( field
, i
) == ( !( i
% 7 ) ) );
62 /* test tr_bitfieldAddRange */
63 tr_bitfieldAddRange( field
, 0, bitcount
);
64 for( i
= 0; i
< bitcount
; ++i
)
65 check( tr_bitfieldHas( field
, i
) );
67 /* test tr_bitfieldRemRange in the middle of a boundary */
68 tr_bitfieldRemRange( field
, 4, 21 );
69 for( i
= 0; i
< 64; ++i
)
70 check( tr_bitfieldHas( field
, i
) == ( ( i
< 4 ) || ( i
>= 21 ) ) );
72 /* test tr_bitfieldRemRange on the boundaries */
73 tr_bitfieldAddRange( field
, 0, 64 );
74 tr_bitfieldRemRange( field
, 8, 24 );
75 for( i
= 0; i
< 64; ++i
)
76 check( tr_bitfieldHas( field
, i
) == ( ( i
< 8 ) || ( i
>= 24 ) ) );
78 /* test tr_bitfieldRemRange when begin & end is on the same word */
79 tr_bitfieldAddRange( field
, 0, 64 );
80 tr_bitfieldRemRange( field
, 4, 5 );
81 for( i
= 0; i
< 64; ++i
)
82 check( tr_bitfieldHas( field
, i
) == ( ( i
< 4 ) || ( i
>= 5 ) ) );
84 /* test tr_bitfieldAddRange */
85 tr_bitfieldRemRange( field
, 0, 64 );
86 tr_bitfieldAddRange( field
, 4, 21 );
87 for( i
= 0; i
< 64; ++i
)
88 check( tr_bitfieldHas( field
, i
) == ( ( 4 <= i
) && ( i
< 21 ) ) );
90 /* test tr_bitfieldAddRange on the boundaries */
91 tr_bitfieldRemRange( field
, 0, 64 );
92 tr_bitfieldAddRange( field
, 8, 24 );
93 for( i
= 0; i
< 64; ++i
)
94 check( tr_bitfieldHas( field
, i
) == ( ( 8 <= i
) && ( i
< 24 ) ) );
96 /* test tr_bitfieldAddRange when begin & end is on the same word */
97 tr_bitfieldRemRange( field
, 0, 64 );
98 tr_bitfieldAddRange( field
, 4, 5 );
99 for( i
= 0; i
< 64; ++i
)
100 check( tr_bitfieldHas( field
, i
) == ( ( 4 <= i
) && ( i
< 5 ) ) );
102 tr_bitfieldFree( field
);
107 test_strip_positional_args( void )
111 const char * expected
;
113 in
= "Hello %1$s foo %2$.*f";
114 expected
= "Hello %s foo %.*f";
115 out
= tr_strip_positional_args( in
);
117 check( !strcmp( out
, expected
) )
119 in
= "Hello %1$'d foo %2$'f";
120 expected
= "Hello %d foo %f";
121 out
= tr_strip_positional_args( in
);
123 check( !strcmp( out
, expected
) )
129 test_strstrip( void )
134 in
= tr_strdup( " test " );
135 out
= tr_strstrip( in
);
137 check( !strcmp( in
, "test" ) );
141 in
= tr_strdup( " test test " );
142 out
= tr_strstrip( in
);
144 check( !strcmp( in
, "test test" ) );
148 in
= tr_strdup( "test" );
149 out
= tr_strstrip( in
);
151 check( !strcmp( in
, "test" ) );
158 test_buildpath( void )
162 out
= tr_buildPath( "foo", "bar", NULL
);
163 check( !strcmp( out
, "foo" TR_PATH_DELIMITER_STR
"bar" ) );
166 out
= tr_buildPath( "", "foo", "bar", NULL
);
167 check( !strcmp( out
, TR_PATH_DELIMITER_STR
"foo" TR_PATH_DELIMITER_STR
"bar" ) );
180 out
= tr_utf8clean( in
, -1 );
182 check( !strcmp( out
, in
) )
186 out
= tr_utf8clean( in
, 5 );
188 check( !strcmp( out
, "hello" ) )
191 /* this version is not utf-8 */
192 in
= "Òðóäíî áûòü Áîãîì";
193 out
= tr_utf8clean( in
, 17 );
195 check( ( strlen( out
) == 17 ) || ( strlen( out
) == 32 ) )
196 check( tr_utf8_validate( out
, -1, NULL
) )
199 /* same string, but utf-8 clean */
200 in
= "Òðóäíî áûòü Áîãîì";
201 out
= tr_utf8clean( in
, -1 );
203 check( tr_utf8_validate( out
, -1, NULL
) )
204 check ( !strcmp( in
, out
) )
217 numbers
= tr_parseNumberRange( "1-10,13,16-19", -1, &count
);
218 check( count
== 15 );
219 check( numbers
!= NULL
);
220 check( numbers
[0] == 1 );
221 check( numbers
[5] == 6 );
222 check( numbers
[9] == 10 );
223 check( numbers
[10] == 13 );
224 check( numbers
[11] == 16 );
225 check( numbers
[14] == 19 );
228 numbers
= tr_parseNumberRange( "1-5,3-7,2-6", -1, &count
);
230 check( numbers
!= NULL
);
231 for( i
=0; i
<count
; ++i
)
232 check( numbers
[i
] == i
+1 );
235 numbers
= tr_parseNumberRange( "1-Hello", -1, &count
);
237 check( numbers
== NULL
);
239 numbers
= tr_parseNumberRange( "1-", -1, &count
);
241 check( numbers
== NULL
);
243 numbers
= tr_parseNumberRange( "Hello", -1, &count
);
245 check( numbers
== NULL
);
251 compareInts( const void * va
, const void * vb
)
253 const int a
= *(const int *)va
;
254 const int b
= *(const int*)vb
;
259 test_lowerbound( void )
262 const int A
[] = { 1, 2, 3, 3, 3, 5, 8 };
263 const int expected_pos
[] = { 0, 1, 2, 5, 5, 6, 6, 6, 7, 7 };
264 const int expected_exact
[] = { TRUE
, TRUE
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
, FALSE
, FALSE
};
265 const int N
= sizeof(A
) / sizeof(A
[0]);
267 for( i
=1; i
<=10; ++i
)
270 const int pos
= tr_lowerBound( &i
, A
, N
, sizeof(int), compareInts
, &exact
);
273 fprintf( stderr
, "searching for %d. ", i
);
274 fprintf( stderr
, "result: index = %d, ", pos
);
276 fprintf( stderr
, "A[%d] == %d\n", pos
, A
[pos
] );
278 fprintf( stderr
, "which is off the end.\n" );
280 check( pos
== expected_pos
[i
-1] )
281 check( exact
== expected_exact
[i
-1] )
290 char const haystack
[12] = "abcabcabcabc";
291 char const needle
[3] = "cab";
293 check( tr_memmem( haystack
, sizeof haystack
, haystack
, sizeof haystack
) == haystack
)
294 check( tr_memmem( haystack
, sizeof haystack
, needle
, sizeof needle
) == haystack
+ 2 )
295 check( tr_memmem( needle
, sizeof needle
, haystack
, sizeof haystack
) == NULL
)
296 check( tr_memmem( haystack
, sizeof haystack
, "", 0) == haystack
)
297 check( tr_memmem( haystack
, sizeof haystack
, NULL
, 0) == haystack
)
298 check( tr_memmem( haystack
, 0, "", 0) == haystack
)
309 /*uint8_t sha2[20];*/
311 memcpy( hex1
, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41 );
312 tr_hex_to_sha1( sha1
, hex1
);
313 tr_sha1_to_hex( hex2
, sha1
);
314 check( !strcmp( hex1
, hex2
) )
323 int array
[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
324 int n
= sizeof( array
) / sizeof( array
[0] );
326 tr_removeElementFromArray( array
, 5u, sizeof( int ), n
-- );
328 check( array
[i
] == ( i
<5 ? i
: i
+1 ) );
330 tr_removeElementFromArray( array
, 0u, sizeof( int ), n
-- );
332 check( array
[i
] == ( i
<4 ? i
+1 : i
+2 ) );
334 tr_removeElementFromArray( array
, n
-1, sizeof( int ), n
); n
--;
336 check( array
[i
] == ( i
<4 ? i
+1 : i
+2 ) );
351 url
= "http://www.some-tracker.org/some/path";
352 check( !tr_urlParse( url
, -1, &scheme
, &host
, &port
, &path
) )
353 check( !strcmp( scheme
, "http" ) )
354 check( !strcmp( host
, "www.some-tracker.org" ) )
355 check( !strcmp( path
, "/some/path" ) )
361 url
= "http://www.some-tracker.org:80/some/path";
362 check( !tr_urlParse( url
, -1, &scheme
, &host
, &port
, &path
) )
363 check( !strcmp( scheme
, "http" ) )
364 check( !strcmp( host
, "www.some-tracker.org" ) )
365 check( !strcmp( path
, "/some/path" ) )
371 url
= "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2";
372 str
= tr_http_unescape( url
, strlen( url
) );
373 check( !strcmp( str
, "http://www.example.com/~user/?test=1&test1=2" ) )
383 const double nan
= sqrt( -1 );
385 tr_snprintf( buf
, sizeof( buf
), "%.2f%%", 99.999 );
386 check( !strcmp( buf
, "100.00%" ) );
388 tr_snprintf( buf
, sizeof( buf
), "%.2f%%", tr_truncd( 99.999, 2 ) );
389 check( !strcmp( buf
, "99.99%" ) );
391 tr_snprintf( buf
, sizeof( buf
), "%.4f", tr_truncd( 403650.656250, 4 ) );
392 check( !strcmp( buf
, "403650.6562" ) );
394 tr_snprintf( buf
, sizeof( buf
), "%.2f", tr_truncd( 2.15, 2 ) );
395 check( !strcmp( buf
, "2.15" ) );
397 tr_snprintf( buf
, sizeof( buf
), "%.2f", tr_truncd( 2.05, 2 ) );
398 check( !strcmp( buf
, "2.05" ) );
400 tr_snprintf( buf
, sizeof( buf
), "%.2f", tr_truncd( 3.3333, 2 ) );
401 check( !strcmp( buf
, "3.33" ) );
403 tr_snprintf( buf
, sizeof( buf
), "%.0f", tr_truncd( 3.3333, 0 ) );
404 check( !strcmp( buf
, "3" ) );
406 tr_snprintf( buf
, sizeof( buf
), "%.2f", tr_truncd( nan
, 2 ) );
407 check( !strcmp( buf
, "nan" ) );
421 out
= tr_base64_encode( "YOYO!", -1, &len
);
423 check( !strcmp( out
, "WU9ZTyE=\n" ) );
425 in
= tr_base64_decode( out
, -1, &len
);
427 check( !strcmp( in
, "YOYO!" ) );
431 out
= tr_base64_encode( NULL
, 0, &len
);
432 check( out
== NULL
);
435 if( ( i
= test_hex( ) ) )
437 if( ( i
= test_lowerbound( ) ) )
439 if( ( i
= test_strip_positional_args( ) ) )
441 if( ( i
= test_strstrip( ) ) )
443 if( ( i
= test_buildpath( ) ) )
445 if( ( i
= test_utf8( ) ) )
447 if( ( i
= test_numbers( ) ) )
449 if( ( i
= test_memmem( ) ) )
451 if( ( i
= test_array( ) ) )
453 if( ( i
= test_url( ) ) )
455 if( ( i
= test_truncd( ) ) )
458 /* test that tr_cryptoRandInt() stays in-bounds */
459 for( i
= 0; i
< 100000; ++i
)
461 const int val
= tr_cryptoRandInt( 100 );
466 /* simple bitfield tests */
467 for( l
= 0; l
< NUM_LOOPS
; ++l
)
468 if( ( i
= test_bitfields( ) ) )