Changeset 13635
- Timestamp:
- Dec 9, 2012, 5:32:52 AM (8 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/Makefile.am
r13614 r13635 123 123 124 124 TESTS = \ 125 bitfield-test \ 125 126 blocklist-test \ 126 127 bencode-test \ … … 157 158 bencode_test_LDFLAGS = ${apps_ldflags} 158 159 160 bitfield_test_SOURCES = bitfield-test.c 161 bitfield_test_LDADD = ${apps_ldadd} 162 bitfield_test_LDFLAGS = ${apps_ldflags} 163 159 164 blocklist_test_SOURCES = blocklist-test.c 160 165 blocklist_test_LDADD = ${apps_ldadd} -
trunk/libtransmission/utils-test.c
r13625 r13635 3 3 4 4 #include "transmission.h" 5 #include "bitfield.h"6 5 #include "ConvertUTF.h" /* tr_utf8_validate*/ 7 6 #include "platform.h" … … 26 25 test_base64 (void) 27 26 { 28 char *in, *out; 29 int len; 30 31 /* base64 */ 32 out = tr_base64_encode ("YOYO!", -1, &len); 33 check_streq ("WU9ZTyE=", out); 34 check_int_eq (8, len); 35 in = tr_base64_decode (out, -1, &len); 36 check_streq ("YOYO!", in); 37 check_int_eq (5, len); 38 tr_free (in); 39 tr_free (out); 40 out = tr_base64_encode (NULL, 0, &len); 41 check (out == NULL); 42 check_int_eq (0, len); 43 44 return 0; 45 } 46 47 static int 48 test_bitfield_count_range (void) 49 { 50 int i; 51 int n; 52 int begin; 53 int end; 54 int count1; 55 int count2; 56 const int bitCount = 100 + tr_cryptoWeakRandInt (1000); 57 tr_bitfield bf; 58 59 /* generate a random bitfield */ 60 tr_bitfieldConstruct (&bf, bitCount); 61 for (i=0, n=tr_cryptoWeakRandInt (bitCount); i<n; ++i) 62 tr_bitfieldAdd (&bf, tr_cryptoWeakRandInt (bitCount)); 63 64 begin = tr_cryptoWeakRandInt (bitCount); 65 do { 66 end = tr_cryptoWeakRandInt (bitCount); 67 } while (end == begin); 68 if (end < begin) { 69 const int tmp = begin; 70 begin = end; 71 end = tmp; 27 int len; 28 char *in, *out; 29 30 /* base64 */ 31 out = tr_base64_encode ("YOYO!", -1, &len); 32 check_streq ("WU9ZTyE=", out); 33 check_int_eq (8, len); 34 in = tr_base64_decode (out, -1, &len); 35 check_streq ("YOYO!", in); 36 check_int_eq (5, len); 37 tr_free (in); 38 tr_free (out); 39 out = tr_base64_encode (NULL, 0, &len); 40 check (out == NULL); 41 check_int_eq (0, len); 42 43 return 0; 44 } 45 46 static int 47 test_strip_positional_args (void) 48 { 49 const char * in; 50 const char * out; 51 const char * expected; 52 53 in = "Hello %1$s foo %2$.*f"; 54 expected = "Hello %s foo %.*f"; 55 out = tr_strip_positional_args (in); 56 check_streq (expected, out); 57 58 in = "Hello %1$'d foo %2$'f"; 59 expected = "Hello %d foo %f"; 60 out = tr_strip_positional_args (in); 61 check_streq (expected, out); 62 63 return 0; 64 } 65 66 static int 67 test_strstrip (void) 68 { 69 char *in, *out; 70 71 /* strstrip */ 72 in = tr_strdup (" test "); 73 out = tr_strstrip (in); 74 check (in == out); 75 check_streq ("test", out); 76 tr_free (in); 77 78 /* strstrip */ 79 in = tr_strdup (" test test "); 80 out = tr_strstrip (in); 81 check (in == out); 82 check_streq ("test test", out); 83 tr_free (in); 84 85 /* strstrip */ 86 in = tr_strdup ("test"); 87 out = tr_strstrip (in); 88 check (in == out); 89 check_streq ("test", out); 90 tr_free (in); 91 92 return 0; 93 } 94 95 static int 96 test_buildpath (void) 97 { 98 char * out; 99 100 out = tr_buildPath ("foo", "bar", NULL); 101 check_streq ("foo" TR_PATH_DELIMITER_STR "bar", out); 102 tr_free (out); 103 104 out = tr_buildPath ("", "foo", "bar", NULL); 105 check_streq (TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar", out); 106 tr_free (out); 107 108 return 0; 109 } 110 111 static int 112 test_utf8 (void) 113 { 114 const char * in; 115 char * out; 116 117 in = "hello world"; 118 out = tr_utf8clean (in, -1); 119 check_streq (in, out); 120 tr_free (out); 121 122 in = "hello world"; 123 out = tr_utf8clean (in, 5); 124 check_streq ("hello", out); 125 tr_free (out); 126 127 /* this version is not utf-8 */ 128 in = "Òðóäíî áûòü Áîãîì"; 129 out = tr_utf8clean (in, 17); 130 check (out != NULL); 131 check ((strlen (out) == 17) || (strlen (out) == 32)); 132 check (tr_utf8_validate (out, -1, NULL)); 133 tr_free (out); 134 135 /* same string, but utf-8 clean */ 136 in = "ÃðóÀÃî áûòÌ Ãîãîì"; 137 out = tr_utf8clean (in, -1); 138 check (out != NULL); 139 check (tr_utf8_validate (out, -1, NULL)); 140 check_streq (in, out); 141 tr_free (out); 142 143 return 0; 144 } 145 146 static int 147 test_numbers (void) 148 { 149 int i; 150 int count; 151 int * numbers; 152 153 numbers = tr_parseNumberRange ("1-10,13,16-19", -1, &count); 154 check_int_eq (15, count); 155 check_int_eq (1, numbers[0]); 156 check_int_eq (6, numbers[5]); 157 check_int_eq (10, numbers[9]); 158 check_int_eq (13, numbers[10]); 159 check_int_eq (16, numbers[11]); 160 check_int_eq (19, numbers[14]); 161 tr_free (numbers); 162 163 numbers = tr_parseNumberRange ("1-5,3-7,2-6", -1, &count); 164 check (count == 7); 165 check (numbers != NULL); 166 for (i=0; i<count; ++i) 167 check_int_eq (i+1, numbers[i]); 168 tr_free (numbers); 169 170 numbers = tr_parseNumberRange ("1-Hello", -1, &count); 171 check_int_eq (0, count); 172 check (numbers == NULL); 173 174 numbers = tr_parseNumberRange ("1-", -1, &count); 175 check_int_eq (0, count); 176 check (numbers == NULL); 177 178 numbers = tr_parseNumberRange ("Hello", -1, &count); 179 check_int_eq (0, count); 180 check (numbers == NULL); 181 182 return 0; 183 } 184 185 static int 186 compareInts (const void * va, const void * vb) 187 { 188 const int a = *(const int *)va; 189 const int b = *(const int *)vb; 190 return a - b; 191 } 192 193 static int 194 test_lowerbound (void) 195 { 196 int i; 197 const int A[] = { 1, 2, 3, 3, 3, 5, 8 }; 198 const int expected_pos[] = { 0, 1, 2, 5, 5, 6, 6, 6, 7, 7 }; 199 const int expected_exact[] = { true, true, true, false, true, false, false, true, false, false }; 200 const int N = sizeof (A) / sizeof (A[0]); 201 202 for (i=1; i<=10; i++) 203 { 204 bool exact; 205 const int pos = tr_lowerBound (&i, A, N, sizeof (int), compareInts, &exact); 206 207 #if 0 208 fprintf (stderr, "searching for %d. ", i); 209 fprintf (stderr, "result: index = %d, ", pos); 210 if (pos != N) 211 fprintf (stderr, "A[%d] == %d\n", pos, A[pos]); 212 else 213 fprintf (stderr, "which is off the end.\n"); 214 #endif 215 check_int_eq (expected_pos[i-1], pos); 216 check_int_eq (expected_exact[i-1], exact); 72 217 } 73 218 74 count1 = 0; 75 for (i=begin; i<end; ++i) 76 if (tr_bitfieldHas (&bf, i)) 77 ++count1; 78 count2 = tr_bitfieldCountRange (&bf, begin, end); 79 check (count1 == count2); 80 81 tr_bitfieldDestruct (&bf); 82 return 0; 83 } 84 85 static int 86 test_bitfields (void) 87 { 88 unsigned int i; 89 unsigned int bitcount = 500; 90 tr_bitfield field; 91 92 tr_bitfieldConstruct (&field, bitcount); 93 94 /* test tr_bitfieldAdd */ 95 for (i = 0; i < bitcount; ++i) 96 if (! (i % 7)) 97 tr_bitfieldAdd (&field, i); 98 for (i = 0; i < bitcount; ++i) 99 check (tr_bitfieldHas (&field, i) == (! (i % 7))); 100 101 /* test tr_bitfieldAddRange */ 102 tr_bitfieldAddRange (&field, 0, bitcount); 103 for (i = 0; i < bitcount; ++i) 104 check (tr_bitfieldHas (&field, i)); 105 106 /* test tr_bitfieldRemRange in the middle of a boundary */ 107 tr_bitfieldRemRange (&field, 4, 21); 108 for (i = 0; i < 64; ++i) 109 check (tr_bitfieldHas (&field, i) == ((i < 4) || (i >= 21))); 110 111 /* test tr_bitfieldRemRange on the boundaries */ 112 tr_bitfieldAddRange (&field, 0, 64); 113 tr_bitfieldRemRange (&field, 8, 24); 114 for (i = 0; i < 64; ++i) 115 check (tr_bitfieldHas (&field, i) == ((i < 8) || (i >= 24))); 116 117 /* test tr_bitfieldRemRange when begin & end is on the same word */ 118 tr_bitfieldAddRange (&field, 0, 64); 119 tr_bitfieldRemRange (&field, 4, 5); 120 for (i = 0; i < 64; ++i) 121 check (tr_bitfieldHas (&field, i) == ((i < 4) || (i >= 5))); 122 123 /* test tr_bitfieldAddRange */ 124 tr_bitfieldRemRange (&field, 0, 64); 125 tr_bitfieldAddRange (&field, 4, 21); 126 for (i = 0; i < 64; ++i) 127 check (tr_bitfieldHas (&field, i) == ((4 <= i) && (i < 21))); 128 129 /* test tr_bitfieldAddRange on the boundaries */ 130 tr_bitfieldRemRange (&field, 0, 64); 131 tr_bitfieldAddRange (&field, 8, 24); 132 for (i = 0; i < 64; ++i) 133 check (tr_bitfieldHas (&field, i) == ((8 <= i) && (i < 24))); 134 135 /* test tr_bitfieldAddRange when begin & end is on the same word */ 136 tr_bitfieldRemRange (&field, 0, 64); 137 tr_bitfieldAddRange (&field, 4, 5); 138 for (i = 0; i < 64; ++i) 139 check (tr_bitfieldHas (&field, i) == ((4 <= i) && (i < 5))); 140 141 tr_bitfieldDestruct (&field); 142 return 0; 143 } 144 145 static int 146 test_strip_positional_args (void) 147 { 148 const char * in; 149 const char * out; 150 const char * expected; 151 152 in = "Hello %1$s foo %2$.*f"; 153 expected = "Hello %s foo %.*f"; 154 out = tr_strip_positional_args (in); 155 check_streq (expected, out); 156 157 in = "Hello %1$'d foo %2$'f"; 158 expected = "Hello %d foo %f"; 159 out = tr_strip_positional_args (in); 160 check_streq (expected, out); 161 162 return 0; 163 } 164 165 static int 166 test_strstrip (void) 167 { 168 char *in, *out; 169 170 /* strstrip */ 171 in = tr_strdup (" test "); 172 out = tr_strstrip (in); 173 check (in == out); 174 check_streq ("test", out); 175 tr_free (in); 176 177 /* strstrip */ 178 in = tr_strdup (" test test "); 179 out = tr_strstrip (in); 180 check (in == out); 181 check_streq ("test test", out); 182 tr_free (in); 183 184 /* strstrip */ 185 in = tr_strdup ("test"); 186 out = tr_strstrip (in); 187 check (in == out); 188 check_streq ("test", out); 189 tr_free (in); 190 191 return 0; 192 } 193 194 static int 195 test_buildpath (void) 196 { 197 char * out; 198 199 out = tr_buildPath ("foo", "bar", NULL); 200 check_streq ("foo" TR_PATH_DELIMITER_STR "bar", out); 201 tr_free (out); 202 203 out = tr_buildPath ("", "foo", "bar", NULL); 204 check_streq (TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar", out); 205 tr_free (out); 206 207 return 0; 208 } 209 210 static int 211 test_utf8 (void) 212 { 213 const char * in; 214 char * out; 215 216 in = "hello world"; 217 out = tr_utf8clean (in, -1); 218 check_streq (in, out); 219 tr_free (out); 220 221 in = "hello world"; 222 out = tr_utf8clean (in, 5); 223 check_streq ("hello", out); 224 tr_free (out); 225 226 /* this version is not utf-8 */ 227 in = "Òðóäíî áûòü Áîãîì"; 228 out = tr_utf8clean (in, 17); 229 check (out != NULL); 230 check ((strlen (out) == 17) || (strlen (out) == 32)); 231 check (tr_utf8_validate (out, -1, NULL)); 232 tr_free (out); 233 234 /* same string, but utf-8 clean */ 235 in = "ÃðóÀÃî áûòÌ Ãîãîì"; 236 out = tr_utf8clean (in, -1); 237 check (out != NULL); 238 check (tr_utf8_validate (out, -1, NULL)); 239 check_streq (in, out); 240 tr_free (out); 241 242 return 0; 243 } 244 245 static int 246 test_numbers (void) 247 { 248 int i; 249 int count; 250 int * numbers; 251 252 numbers = tr_parseNumberRange ("1-10,13,16-19", -1, &count); 253 check_int_eq (15, count); 254 check_int_eq (1, numbers[0]); 255 check_int_eq (6, numbers[5]); 256 check_int_eq (10, numbers[9]); 257 check_int_eq (13, numbers[10]); 258 check_int_eq (16, numbers[11]); 259 check_int_eq (19, numbers[14]); 260 tr_free (numbers); 261 262 numbers = tr_parseNumberRange ("1-5,3-7,2-6", -1, &count); 263 check (count == 7); 264 check (numbers != NULL); 265 for (i=0; i<count; ++i) 266 check_int_eq (i+1, numbers[i]); 267 tr_free (numbers); 268 269 numbers = tr_parseNumberRange ("1-Hello", -1, &count); 270 check_int_eq (0, count); 271 check (numbers == NULL); 272 273 numbers = tr_parseNumberRange ("1-", -1, &count); 274 check_int_eq (0, count); 275 check (numbers == NULL); 276 277 numbers = tr_parseNumberRange ("Hello", -1, &count); 278 check_int_eq (0, count); 279 check (numbers == NULL); 280 281 return 0; 282 } 283 284 static int 285 compareInts (const void * va, const void * vb) 286 { 287 const int a = * (const int *)va; 288 const int b = * (const int*)vb; 289 return a - b; 290 } 291 292 static int 293 test_lowerbound (void) 294 { 295 int i; 296 const int A[] = { 1, 2, 3, 3, 3, 5, 8 }; 297 const int expected_pos[] = { 0, 1, 2, 5, 5, 6, 6, 6, 7, 7 }; 298 const int expected_exact[] = { true, true, true, false, true, false, false, true, false, false }; 299 const int N = sizeof (A) / sizeof (A[0]); 300 301 for (i=1; i<=10; ++i) 219 return 0; 220 } 221 222 static int 223 test_memmem (void) 224 { 225 char const haystack[12] = "abcabcabcabc"; 226 char const needle[3] = "cab"; 227 228 check (tr_memmem (haystack, sizeof haystack, haystack, sizeof haystack) == haystack); 229 check (tr_memmem (haystack, sizeof haystack, needle, sizeof needle) == haystack + 2); 230 check (tr_memmem (needle, sizeof needle, haystack, sizeof haystack) == NULL); 231 232 return 0; 233 } 234 235 static int 236 test_hex (void) 237 { 238 char hex1[41]; 239 char hex2[41]; 240 uint8_t sha1[20]; 241 242 memcpy (hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41); 243 tr_hex_to_sha1 (sha1, hex1); 244 tr_sha1_to_hex (hex2, sha1); 245 check_streq (hex1, hex2); 246 247 return 0; 248 } 249 250 static int 251 test_array (void) 252 { 253 int i; 254 int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 255 int n = sizeof (array) / sizeof (array[0]); 256 257 tr_removeElementFromArray (array, 5u, sizeof (int), n--); 258 for (i=0; i<n; ++i) 259 check_int_eq ((i<5 ? i : i+1), array[i]); 260 261 tr_removeElementFromArray (array, 0u, sizeof (int), n--); 262 for (i=0; i<n; ++i) 263 check_int_eq ((i<4 ? i+1 : i+2), array[i]); 264 265 tr_removeElementFromArray (array, n-1, sizeof (int), n); n--; 266 for (i=0; i<n; ++i) 267 check_int_eq ((i<4 ? i+1 : i+2), array[i]); 268 269 return 0; 270 } 271 272 static int 273 test_url (void) 274 { 275 int port; 276 char * scheme; 277 char * host; 278 char * path; 279 char * str; 280 const char * url; 281 282 url = "http://1"; 283 check (!tr_urlParse (url, -1, &scheme, &host, &port, &path)); 284 check_streq ("http", scheme); 285 check_streq ("1", host); 286 check_streq ("/", path); 287 check_int_eq (80, port); 288 tr_free (scheme); 289 tr_free (path); 290 tr_free (host); 291 292 url = "http://www.some-tracker.org/some/path"; 293 check (!tr_urlParse (url, -1, &scheme, &host, &port, &path)); 294 check_streq ("http", scheme); 295 check_streq ("www.some-tracker.org", host); 296 check_streq ("/some/path", path); 297 check_int_eq (80, port); 298 tr_free (scheme); 299 tr_free (path); 300 tr_free (host); 301 302 url = "http://www.some-tracker.org:80/some/path"; 303 check (!tr_urlParse (url, -1, &scheme, &host, &port, &path)); 304 check_streq ("http", scheme); 305 check_streq ("www.some-tracker.org", host); 306 check_streq ("/some/path", path); 307 check_int_eq (80, port); 308 tr_free (scheme); 309 tr_free (path); 310 tr_free (host); 311 312 url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2"; 313 str = tr_http_unescape (url, strlen (url)); 314 check_streq ("http://www.example.com/~user/?test=1&test1=2", str); 315 tr_free (str); 316 317 return 0; 318 } 319 320 static int 321 test_truncd (void) 322 { 323 char buf[32]; 324 const double nan = sqrt (-1); 325 326 tr_snprintf (buf, sizeof (buf), "%.2f%%", 99.999); 327 check_streq ("100.00%", buf); 328 329 tr_snprintf (buf, sizeof (buf), "%.2f%%", tr_truncd (99.999, 2)); 330 check_streq ("99.99%", buf); 331 332 tr_snprintf (buf, sizeof (buf), "%.4f", tr_truncd (403650.656250, 4)); 333 check_streq ("403650.6562", buf); 334 335 tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (2.15, 2)); 336 check_streq ("2.15", buf); 337 338 tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (2.05, 2)); 339 check_streq ("2.05", buf); 340 341 tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (3.3333, 2)); 342 check_streq ("3.33", buf); 343 344 tr_snprintf (buf, sizeof (buf), "%.0f", tr_truncd (3.3333, 0)); 345 check_streq ("3", buf); 346 347 tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (nan, 2)); 348 check (strstr (buf, "nan") != NULL); 349 350 return 0; 351 } 352 353 static int 354 test_cryptoRand (void) 355 { 356 int i; 357 358 /* test that tr_cryptoRandInt () stays in-bounds */ 359 for (i = 0; i < 100000; ++i) 302 360 { 303 bool exact; 304 const int pos = tr_lowerBound (&i, A, N, sizeof (int), compareInts, &exact); 305 306 #if 0 307 fprintf (stderr, "searching for %d. ", i); 308 fprintf (stderr, "result: index = %d, ", pos); 309 if (pos != N) 310 fprintf (stderr, "A[%d] == %d\n", pos, A[pos]); 311 else 312 fprintf (stderr, "which is off the end.\n"); 313 #endif 314 check_int_eq (expected_pos[i-1], pos); 315 check_int_eq (expected_exact[i-1], exact); 361 const int val = tr_cryptoRandInt (100); 362 check (val >= 0); 363 check (val < 100); 316 364 } 317 365 318 return 0; 319 } 320 321 static int 322 test_memmem (void) 323 { 324 char const haystack[12] = "abcabcabcabc"; 325 char const needle[3] = "cab"; 326 327 check (tr_memmem (haystack, sizeof haystack, haystack, sizeof haystack) == haystack); 328 check (tr_memmem (haystack, sizeof haystack, needle, sizeof needle) == haystack + 2); 329 check (tr_memmem (needle, sizeof needle, haystack, sizeof haystack) == NULL); 330 331 return 0; 332 } 333 334 static int 335 test_hex (void) 336 { 337 char hex1[41]; 338 char hex2[41]; 339 uint8_t sha1[20]; 340 /*uint8_t sha2[20];*/ 341 342 memcpy (hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41); 343 tr_hex_to_sha1 (sha1, hex1); 344 tr_sha1_to_hex (hex2, sha1); 345 check_streq (hex1, hex2); 346 347 return 0; 348 } 349 350 static int 351 test_array (void) 352 { 353 int i; 354 int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 355 int n = sizeof (array) / sizeof (array[0]); 356 357 tr_removeElementFromArray (array, 5u, sizeof (int), n--); 358 for (i=0; i<n; ++i) 359 check_int_eq ((i<5 ? i : i+1), array[i]); 360 361 tr_removeElementFromArray (array, 0u, sizeof (int), n--); 362 for (i=0; i<n; ++i) 363 check_int_eq ((i<4 ? i+1 : i+2), array[i]); 364 365 tr_removeElementFromArray (array, n-1, sizeof (int), n); n--; 366 for (i=0; i<n; ++i) 367 check_int_eq ((i<4 ? i+1 : i+2), array[i]); 368 369 return 0; 370 } 371 372 static int 373 test_url (void) 374 { 375 int port; 376 char * scheme; 377 char * host; 378 char * path; 379 char * str; 380 const char * url; 381 382 url = "http://1"; 383 check (!tr_urlParse (url, -1, &scheme, &host, &port, &path)); 384 check_streq ("http", scheme); 385 check_streq ("1", host); 386 check_streq ("/", path); 387 check_int_eq (80, port); 388 tr_free (scheme); 389 tr_free (path); 390 tr_free (host); 391 392 url = "http://www.some-tracker.org/some/path"; 393 check (!tr_urlParse (url, -1, &scheme, &host, &port, &path)); 394 check_streq ("http", scheme); 395 check_streq ("www.some-tracker.org", host); 396 check_streq ("/some/path", path); 397 check_int_eq (80, port); 398 tr_free (scheme); 399 tr_free (path); 400 tr_free (host); 401 402 url = "http://www.some-tracker.org:80/some/path"; 403 check (!tr_urlParse (url, -1, &scheme, &host, &port, &path)); 404 check_streq ("http", scheme); 405 check_streq ("www.some-tracker.org", host); 406 check_streq ("/some/path", path); 407 check_int_eq (80, port); 408 tr_free (scheme); 409 tr_free (path); 410 tr_free (host); 411 412 url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2"; 413 str = tr_http_unescape (url, strlen (url)); 414 check_streq ("http://www.example.com/~user/?test=1&test1=2", str); 415 tr_free (str); 416 417 return 0; 418 } 419 420 static int 421 test_truncd (void) 422 { 423 char buf[32]; 424 const double nan = sqrt (-1); 425 426 tr_snprintf (buf, sizeof (buf), "%.2f%%", 99.999); 427 check_streq ("100.00%", buf); 428 429 tr_snprintf (buf, sizeof (buf), "%.2f%%", tr_truncd (99.999, 2)); 430 check_streq ("99.99%", buf); 431 432 tr_snprintf (buf, sizeof (buf), "%.4f", tr_truncd (403650.656250, 4)); 433 check_streq ("403650.6562", buf); 434 435 tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (2.15, 2)); 436 check_streq ("2.15", buf); 437 438 tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (2.05, 2)); 439 check_streq ("2.05", buf); 440 441 tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (3.3333, 2)); 442 check_streq ("3.33", buf); 443 444 tr_snprintf (buf, sizeof (buf), "%.0f", tr_truncd (3.3333, 0)); 445 check_streq ("3", buf); 446 447 tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (nan, 2)); 448 check (strstr (buf, "nan") != NULL); 449 450 return 0; 451 } 452 453 static int 454 test_cryptoRand (void) 455 { 456 int i; 457 458 /* test that tr_cryptoRandInt () stays in-bounds */ 459 for (i = 0; i < 100000; ++i) 460 { 461 const int val = tr_cryptoRandInt (100); 462 check (val >= 0); 463 check (val < 100); 464 } 465 466 return 0; 467 } 468 469 struct blah 470 { 471 uint8_t hash[SHA_DIGEST_LENGTH]; /* pieces hash */ 472 int8_t priority; /* TR_PRI_HIGH, _NORMAL, or _LOW */ 473 int8_t dnd; /* "do not download" flag */ 474 time_t timeChecked; /* the last time we tested this piece */ 475 }; 366 return 0; 367 } 476 368 477 369 int 478 370 main (void) 479 371 { 480 const testFunc tests[] = { 481 test_base64, test_hex, test_lowerbound, test_strip_positional_args, 482 test_strstrip, test_buildpath, test_utf8, test_numbers, test_memmem, 483 test_array, test_url, test_truncd, test_cryptoRand, 484 }; 485 int ret; 486 int l; 487 488 if ((ret = runTests (tests, NUM_TESTS (tests)))) 489 return ret; 490 491 /* simple bitfield tests */ 492 for (l = 0; l < NUM_LOOPS; ++l) 493 if ((ret = test_bitfields ())) 494 return ret; 495 496 /* bitfield count range */ 497 for (l=0; l<10000; ++l) 498 if ((ret = test_bitfield_count_range ())) 499 return ret; 500 501 return 0; 502 } 503 372 const testFunc tests[] = { test_array, 373 test_base64, 374 test_buildpath, 375 test_cryptoRand, 376 test_hex, 377 test_lowerbound, 378 test_memmem, 379 test_numbers, 380 test_strip_positional_args, 381 test_strstrip, 382 test_truncd, 383 test_url, 384 test_utf8 }; 385 386 return runTests (tests, NUM_TESTS (tests)); 387 } 388
Note: See TracChangeset
for help on using the changeset viewer.