/* * This file Copyright (C) 2013-2014 Mnemosyne LLC * * It may be used under the GNU GPL versions 2 or 3 * or any future license endorsed by Mnemosyne LLC. * * $Id$ */ #include /* INT_MAX */ #include /* sqrt () */ #include /* strlen () */ #include /* setenv (), unsetenv () */ #ifdef _WIN32 #include #define setenv(key, value, unused) SetEnvironmentVariableA (key, value) #define unsetenv(key) SetEnvironmentVariableA (key, NULL) #endif #include "transmission.h" #include "ConvertUTF.h" /* tr_utf8_validate*/ #include "platform.h" #include "crypto.h" #include "utils.h" #include "web.h" #define NUM_LOOPS 1 #define SPEED_TEST 0 #if SPEED_TEST #define VERBOSE #undef NUM_LOOPS #define NUM_LOOPS 200 #endif #include "libtransmission-test.h" static int test_base64 (void) { int len; char *in, *out; /* base64 */ out = tr_base64_encode ("YOYO!", -1, &len); check_streq ("WU9ZTyE=", out); check_int_eq (8, len); in = tr_base64_decode (out, -1, &len); check_streq ("YOYO!", in); check_int_eq (5, len); tr_free (in); tr_free (out); out = tr_base64_encode (NULL, 0, &len); check (out == NULL); check_int_eq (0, len); return 0; } static int test_strip_positional_args (void) { const char * in; const char * out; const char * expected; in = "Hello %1$s foo %2$.*f"; expected = "Hello %s foo %.*f"; out = tr_strip_positional_args (in); check_streq (expected, out); in = "Hello %1$'d foo %2$'f"; expected = "Hello %d foo %f"; out = tr_strip_positional_args (in); check_streq (expected, out); return 0; } static int test_strstrip (void) { char *in, *out; /* strstrip */ in = tr_strdup (" test "); out = tr_strstrip (in); check (in == out); check_streq ("test", out); tr_free (in); /* strstrip */ in = tr_strdup (" test test "); out = tr_strstrip (in); check (in == out); check_streq ("test test", out); tr_free (in); /* strstrip */ in = tr_strdup ("test"); out = tr_strstrip (in); check (in == out); check_streq ("test", out); tr_free (in); return 0; } static int test_buildpath (void) { char * out; out = tr_buildPath ("foo", "bar", NULL); check_streq ("foo" TR_PATH_DELIMITER_STR "bar", out); tr_free (out); out = tr_buildPath ("", "foo", "bar", NULL); check_streq (TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar", out); tr_free (out); return 0; } static int test_utf8 (void) { const char * in; char * out; in = "hello world"; out = tr_utf8clean (in, -1); check_streq (in, out); tr_free (out); in = "hello world"; out = tr_utf8clean (in, 5); check_streq ("hello", out); tr_free (out); /* this version is not utf-8 */ in = " "; out = tr_utf8clean (in, 17); check (out != NULL); check ((strlen (out) == 17) || (strlen (out) == 32)); check (tr_utf8_validate (out, -1, NULL)); tr_free (out); /* same string, but utf-8 clean */ in = "Òðóäíî áûòü Áîãîì"; out = tr_utf8clean (in, -1); check (out != NULL); check (tr_utf8_validate (out, -1, NULL)); check_streq (in, out); tr_free (out); return 0; } static int test_numbers (void) { int i; int count; int * numbers; numbers = tr_parseNumberRange ("1-10,13,16-19", -1, &count); check_int_eq (15, count); check_int_eq (1, numbers[0]); check_int_eq (6, numbers[5]); check_int_eq (10, numbers[9]); check_int_eq (13, numbers[10]); check_int_eq (16, numbers[11]); check_int_eq (19, numbers[14]); tr_free (numbers); numbers = tr_parseNumberRange ("1-5,3-7,2-6", -1, &count); check (count == 7); check (numbers != NULL); for (i=0; i buf[i]) lowest_high = buf[i]; check (highest_low <= lowest_high); return 0; } static int test_quickfindFirst (void) { size_t i; const size_t k = 10; const size_t n = 100; const size_t n_trials = 1000; int * buf = tr_new (int, n); for (i=0; i= 0); check (val < 100); } return 0; } static char * test_strdup_printf_valist (const char * fmt, ...) { va_list args; char * ret; va_start (args, fmt); ret = tr_strdup_vprintf (fmt, args); va_end (args); return ret; } static int test_strdup_printf (void) { char * s, * s2, * s3; s = tr_strdup_printf ("%s", "test"); check_streq ("test", s); tr_free (s); s = tr_strdup_printf ("%d %s %c %u", -1, "0", '1', 2); check_streq ("-1 0 1 2", s); tr_free (s); s3 = tr_malloc0 (4098); memset (s3, '-', 4097); s3[2047] = 't'; s3[2048] = 'e'; s3[2049] = 's'; s3[2050] = 't'; s2 = tr_malloc0 (4096); memset (s2, '-', 4095); s2[2047] = '%'; s2[2048] = 's'; s = tr_strdup_printf (s2, "test"); check_streq (s3, s); tr_free (s); tr_free (s2); s = tr_strdup_printf ("%s", s3); check_streq (s3, s); tr_free (s); tr_free (s3); s = test_strdup_printf_valist ("\n-%s-%s-%s-\n", "\r", "\t", "\b"); check_streq ("\n-\r-\t-\b-\n", s); tr_free (s); return 0; } static int test_env (void) { const char * test_key = "TR_TEST_ENV"; int x; char * s; unsetenv (test_key); check (!tr_env_key_exists (test_key)); x = tr_env_get_int (test_key, 123); check_int_eq (123, x); s = tr_env_get_string (test_key, NULL); check (s == NULL); s = tr_env_get_string (test_key, "a"); check_streq ("a", s); tr_free (s); setenv (test_key, "", 1); check (tr_env_key_exists (test_key)); x = tr_env_get_int (test_key, 456); check_int_eq (456, x); s = tr_env_get_string (test_key, NULL); check_streq ("", s); tr_free (s); s = tr_env_get_string (test_key, "b"); check_streq ("", s); tr_free (s); setenv (test_key, "135", 1); check (tr_env_key_exists (test_key)); x = tr_env_get_int (test_key, 789); check_int_eq (135, x); s = tr_env_get_string (test_key, NULL); check_streq ("135", s); tr_free (s); s = tr_env_get_string (test_key, "c"); check_streq ("135", s); tr_free (s); return 0; } int main (void) { const testFunc tests[] = { test_array, test_base64, test_buildpath, test_cryptoRand, test_hex, test_lowerbound, test_quickfindFirst, test_memmem, test_numbers, test_strip_positional_args, test_strdup_printf, test_strstrip, test_truncd, test_url, test_utf8, test_env }; return runTests (tests, NUM_TESTS (tests)); }