2014-01-19 01:09:44 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-08-24 17:53:45 +00:00
|
|
|
#include <limits.h> /* INT_MAX */
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <math.h> /* sqrt() */
|
|
|
|
#include <string.h> /* strlen() */
|
|
|
|
#include <stdlib.h> /* setenv(), unsetenv() */
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#define setenv(key, value, unused) SetEnvironmentVariableA(key, value)
|
|
|
|
#define unsetenv(key) SetEnvironmentVariableA(key, NULL)
|
2014-09-21 18:05:14 +00:00
|
|
|
#endif
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2008-05-20 17:33:54 +00:00
|
|
|
#include "transmission.h"
|
2009-01-10 22:48:58 +00:00
|
|
|
#include "ConvertUTF.h" /* tr_utf8_validate*/
|
2008-10-30 19:47:00 +00:00
|
|
|
#include "platform.h"
|
2014-12-04 11:27:38 +00:00
|
|
|
#include "crypto-utils.h" /* tr_rand_int_weak */
|
2009-12-10 10:44:06 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "web.h"
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2008-06-02 15:52:16 +00:00
|
|
|
#define SPEED_TEST 0
|
|
|
|
|
|
|
|
#if SPEED_TEST
|
2017-04-19 12:04:45 +00:00
|
|
|
#define VERBOSE
|
2008-06-02 15:52:16 +00:00
|
|
|
#endif
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2012-08-18 16:07:05 +00:00
|
|
|
#include "libtransmission-test.h"
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_strip_positional_args(void)
|
2010-06-25 01:13:35 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* in;
|
|
|
|
char const* out;
|
|
|
|
char const* expected;
|
2010-06-25 01:13:35 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
in = "Hello %1$s foo %2$.*f";
|
|
|
|
expected = "Hello %s foo %.*f";
|
|
|
|
out = tr_strip_positional_args(in);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(out, ==, expected);
|
2010-06-25 01:13:35 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
in = "Hello %1$'d foo %2$'f";
|
|
|
|
expected = "Hello %d foo %f";
|
|
|
|
out = tr_strip_positional_args(in);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(out, ==, expected);
|
2010-06-25 01:13:35 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2010-06-25 01:13:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_strstrip(void)
|
2008-09-05 19:11:30 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
char* in;
|
|
|
|
char* out;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* strstrip */
|
|
|
|
in = tr_strdup(" test ");
|
|
|
|
out = tr_strstrip(in);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(in, ==, out);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(out, ==, "test");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(in);
|
|
|
|
|
|
|
|
/* strstrip */
|
|
|
|
in = tr_strdup(" test test ");
|
|
|
|
out = tr_strstrip(in);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(in, ==, out);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(out, ==, "test test");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(in);
|
|
|
|
|
|
|
|
/* strstrip */
|
|
|
|
in = tr_strdup("test");
|
|
|
|
out = tr_strstrip(in);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(in, ==, out);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(out, ==, "test");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(in);
|
|
|
|
|
|
|
|
return 0;
|
2008-09-05 19:11:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_buildpath(void)
|
2008-10-30 19:47:00 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* out;
|
2008-10-30 19:47:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
out = tr_buildPath("foo", "bar", NULL);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(out, ==, "foo" TR_PATH_DELIMITER_STR "bar");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(out);
|
2008-10-30 19:47:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
out = tr_buildPath("", "foo", "bar", NULL);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(out, ==, TR_PATH_DELIMITER_STR "foo" TR_PATH_DELIMITER_STR "bar");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(out);
|
2008-10-30 19:47:00 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2008-10-30 19:47:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_utf8(void)
|
2009-01-10 22:48:58 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* in;
|
2017-04-19 12:04:45 +00:00
|
|
|
char* out;
|
|
|
|
|
|
|
|
in = "hello world";
|
|
|
|
out = tr_utf8clean(in, TR_BAD_SIZE);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(out, ==, in);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(out);
|
|
|
|
|
|
|
|
in = "hello world";
|
|
|
|
out = tr_utf8clean(in, 5);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(out, ==, "hello");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(out);
|
|
|
|
|
|
|
|
/* this version is not utf-8 (but cp866) */
|
|
|
|
in = "\x92\xE0\xE3\xA4\xAD\xAE \xA1\xEB\xE2\xEC \x81\xAE\xA3\xAE\xAC";
|
|
|
|
out = tr_utf8clean(in, 17);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(out, !=, NULL);
|
2017-04-30 16:25:26 +00:00
|
|
|
check(strlen(out) == 17 || strlen(out) == 33);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_utf8_validate(out, TR_BAD_SIZE, NULL));
|
|
|
|
tr_free(out);
|
|
|
|
|
|
|
|
/* same string, but utf-8 clean */
|
|
|
|
in = "Трудно быть Богом";
|
|
|
|
out = tr_utf8clean(in, TR_BAD_SIZE);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(out, !=, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_utf8_validate(out, TR_BAD_SIZE, NULL));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(out, ==, in);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(out);
|
|
|
|
|
|
|
|
in = "\xF4\x00\x81\x82";
|
|
|
|
out = tr_utf8clean(in, 4);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(out, !=, NULL);
|
2017-04-30 16:25:26 +00:00
|
|
|
check(strlen(out) == 1 || strlen(out) == 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_utf8_validate(out, TR_BAD_SIZE, NULL));
|
|
|
|
tr_free(out);
|
|
|
|
|
|
|
|
in = "\xF4\x33\x81\x82";
|
|
|
|
out = tr_utf8clean(in, 4);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(out, !=, NULL);
|
2017-04-30 16:25:26 +00:00
|
|
|
check(strlen(out) == 4 || strlen(out) == 7);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_utf8_validate(out, TR_BAD_SIZE, NULL));
|
|
|
|
tr_free(out);
|
|
|
|
|
|
|
|
return 0;
|
2009-01-10 22:48:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_numbers(void)
|
2009-02-09 17:25:48 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int count;
|
|
|
|
int* numbers;
|
|
|
|
|
|
|
|
numbers = tr_parseNumberRange("1-10,13,16-19", TR_BAD_SIZE, &count);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(count, ==, 15);
|
|
|
|
check_int(numbers[0], ==, 1);
|
|
|
|
check_int(numbers[5], ==, 6);
|
|
|
|
check_int(numbers[9], ==, 10);
|
|
|
|
check_int(numbers[10], ==, 13);
|
|
|
|
check_int(numbers[11], ==, 16);
|
|
|
|
check_int(numbers[14], ==, 19);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(numbers);
|
|
|
|
|
|
|
|
numbers = tr_parseNumberRange("1-5,3-7,2-6", TR_BAD_SIZE, &count);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(count, ==, 7);
|
|
|
|
check_ptr(numbers, !=, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(numbers[i], ==, i + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(numbers);
|
|
|
|
|
|
|
|
numbers = tr_parseNumberRange("1-Hello", TR_BAD_SIZE, &count);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(count, ==, 0);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(numbers, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
numbers = tr_parseNumberRange("1-", TR_BAD_SIZE, &count);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(count, ==, 0);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(numbers, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
numbers = tr_parseNumberRange("Hello", TR_BAD_SIZE, &count);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(count, ==, 0);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(numbers, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
return 0;
|
2009-02-09 17:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int compareInts(void const* va, void const* vb)
|
2009-10-31 22:16:06 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const a = *(int const*)va;
|
|
|
|
int const b = *(int const*)vb;
|
2017-04-19 12:04:45 +00:00
|
|
|
return a - b;
|
2009-10-31 22:16:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_lowerbound(void)
|
2009-10-31 22:16:06 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const A[] = { 1, 2, 3, 3, 3, 5, 8 };
|
|
|
|
int const expected_pos[] = { 0, 1, 2, 5, 5, 6, 6, 6, 7, 7 };
|
|
|
|
bool const expected_exact[] = { true, true, true, false, true, false, false, true, false, false };
|
2017-05-13 22:38:31 +00:00
|
|
|
int const N = TR_N_ELEMENTS(A);
|
2009-10-31 22:16:06 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 1; i <= 10; i++)
|
2009-10-31 22:16:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool exact;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const pos = tr_lowerBound(&i, A, N, sizeof(int), compareInts, &exact);
|
2009-10-31 22:16:06 +00:00
|
|
|
|
|
|
|
#if 0
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "searching for %d. ", i);
|
|
|
|
fprintf(stderr, "result: index = %d, ", pos);
|
|
|
|
|
|
|
|
if (pos != N)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "A[%d] == %d\n", pos, A[pos]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "which is off the end.\n");
|
|
|
|
}
|
|
|
|
|
2009-10-31 22:16:06 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(pos, ==, expected_pos[i - 1]);
|
|
|
|
check_int(exact, ==, expected_exact[i - 1]);
|
2009-10-31 22:16:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2009-10-31 22:16:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int test_quickFindFirst_Iteration(size_t const k, size_t const n, int* buf, int range)
|
2012-12-28 20:07:50 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int highest_low;
|
|
|
|
int lowest_high;
|
|
|
|
|
|
|
|
/* populate buf with random ints */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
buf[i] = tr_rand_int_weak(range);
|
|
|
|
}
|
2012-12-28 20:07:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* find the best k */
|
|
|
|
tr_quickfindFirstK(buf, n, sizeof(int), compareInts, k);
|
2012-12-28 20:07:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* confirm that the smallest K ints are in the first slots K slots in buf */
|
2012-12-28 20:07:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
highest_low = INT_MIN;
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < k; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (highest_low < buf[i])
|
|
|
|
{
|
|
|
|
highest_low = buf[i];
|
|
|
|
}
|
|
|
|
}
|
2012-12-28 20:07:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
lowest_high = INT_MAX;
|
2012-12-28 20:07:50 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = k; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (lowest_high > buf[i])
|
|
|
|
{
|
|
|
|
lowest_high = buf[i];
|
|
|
|
}
|
|
|
|
}
|
2012-12-28 20:07:50 +00:00
|
|
|
|
2017-05-30 17:56:12 +00:00
|
|
|
check_int(highest_low, <=, lowest_high);
|
2012-12-28 20:07:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2012-12-28 20:07:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_quickfindFirst(void)
|
2012-12-28 20:07:50 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const k = 10;
|
|
|
|
size_t const n = 100;
|
|
|
|
size_t const n_trials = 1000;
|
2017-04-19 12:04:45 +00:00
|
|
|
int* buf = tr_new(int, n);
|
2012-12-28 20:07:50 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < n_trials; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(test_quickFindFirst_Iteration(k, n, buf, 100), ==, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-28 20:07:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(buf);
|
|
|
|
return 0;
|
2012-12-28 20:07:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_memmem(void)
|
2009-05-22 05:35:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char const haystack[12] = "abcabcabcabc";
|
|
|
|
char const needle[3] = "cab";
|
2009-05-22 05:35:51 +00:00
|
|
|
|
2017-05-30 17:56:12 +00:00
|
|
|
check_ptr(tr_memmem(haystack, sizeof(haystack), haystack, sizeof(haystack)), ==, haystack);
|
|
|
|
check_ptr(tr_memmem(haystack, sizeof(haystack), needle, sizeof(needle)), ==, haystack + 2);
|
|
|
|
check_ptr(tr_memmem(needle, sizeof(needle), haystack, sizeof(haystack)), ==, NULL);
|
2009-05-22 05:35:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2009-05-22 05:35:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_hex(void)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char hex1[41];
|
|
|
|
char hex2[41];
|
|
|
|
uint8_t binary[20];
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41);
|
|
|
|
tr_hex_to_binary(hex1, binary, 20);
|
|
|
|
tr_binary_to_hex(binary, hex2, 20);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(hex1, ==, hex2);
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2009-11-20 04:38:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_array(void)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
2017-05-13 22:38:31 +00:00
|
|
|
size_t n = TR_N_ELEMENTS(array);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_removeElementFromArray(array, 5u, sizeof(size_t), n--);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(array[i], ==, i < 5 ? i : i + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_removeElementFromArray(array, 0u, sizeof(size_t), n--);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(array[i], ==, i < 4 ? i + 1 : i + 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_removeElementFromArray(array, n - 1, sizeof(size_t), n);
|
|
|
|
n--;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(array[i], ==, i < 4 ? i + 1 : i + 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_url(void)
|
2009-12-10 09:13:08 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int port;
|
|
|
|
char* scheme;
|
|
|
|
char* host;
|
|
|
|
char* path;
|
|
|
|
char* str;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* url;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
url = "http://1";
|
|
|
|
check(tr_urlParse(url, TR_BAD_SIZE, &scheme, &host, &port, &path));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(scheme, ==, "http");
|
|
|
|
check_str(host, ==, "1");
|
|
|
|
check_str(path, ==, "/");
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(port, ==, 80);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(scheme);
|
|
|
|
tr_free(path);
|
|
|
|
tr_free(host);
|
|
|
|
|
|
|
|
url = "http://www.some-tracker.org/some/path";
|
|
|
|
check(tr_urlParse(url, TR_BAD_SIZE, &scheme, &host, &port, &path));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(scheme, ==, "http");
|
|
|
|
check_str(host, ==, "www.some-tracker.org");
|
|
|
|
check_str(path, ==, "/some/path");
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(port, ==, 80);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(scheme);
|
|
|
|
tr_free(path);
|
|
|
|
tr_free(host);
|
|
|
|
|
|
|
|
url = "http://www.some-tracker.org:80/some/path";
|
|
|
|
check(tr_urlParse(url, TR_BAD_SIZE, &scheme, &host, &port, &path));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(scheme, ==, "http");
|
|
|
|
check_str(host, ==, "www.some-tracker.org");
|
|
|
|
check_str(path, ==, "/some/path");
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(port, ==, 80);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(scheme);
|
|
|
|
tr_free(path);
|
|
|
|
tr_free(host);
|
|
|
|
|
|
|
|
url = "http%3A%2F%2Fwww.example.com%2F~user%2F%3Ftest%3D1%26test1%3D2";
|
|
|
|
str = tr_http_unescape(url, strlen(url));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(str, ==, "http://www.example.com/~user/?test=1&test1=2");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(str);
|
|
|
|
|
|
|
|
return 0;
|
2009-12-10 09:13:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_truncd(void)
|
2008-05-20 17:33:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char buf[32];
|
2017-04-20 16:02:19 +00:00
|
|
|
double const nan = sqrt(-1);
|
2008-05-20 17:33:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, sizeof(buf), "%.2f%%", 99.999);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(buf, ==, "100.00%");
|
2010-07-24 17:09:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, sizeof(buf), "%.2f%%", tr_truncd(99.999, 2));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(buf, ==, "99.99%");
|
2009-07-14 20:35:48 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, sizeof(buf), "%.4f", tr_truncd(403650.656250, 4));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(buf, ==, "403650.6562");
|
2010-07-24 17:09:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, sizeof(buf), "%.2f", tr_truncd(2.15, 2));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(buf, ==, "2.15");
|
2010-09-22 16:09:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, sizeof(buf), "%.2f", tr_truncd(2.05, 2));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(buf, ==, "2.05");
|
2010-10-17 18:29:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, sizeof(buf), "%.2f", tr_truncd(3.3333, 2));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(buf, ==, "3.33");
|
2010-10-21 23:47:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, sizeof(buf), "%.0f", tr_truncd(3.3333, 0));
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(buf, ==, "3");
|
2010-10-21 23:47:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if !(defined(_MSC_VER) || (defined(__MINGW32__) && defined(__MSVCRT__)))
|
|
|
|
/* FIXME: MSCVRT behaves differently in case of nan */
|
|
|
|
tr_snprintf(buf, sizeof(buf), "%.2f", tr_truncd(nan, 2));
|
|
|
|
check(strstr(buf, "nan") != NULL || strstr(buf, "NaN") != NULL);
|
2015-03-19 06:08:06 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
(void)nan;
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
2010-10-21 23:47:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2010-07-24 17:09:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char* test_strdup_printf_valist(char const* fmt, ...)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
va_list args;
|
|
|
|
char* ret;
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
ret = tr_strdup_vprintf(fmt, args);
|
|
|
|
va_end(args);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_strdup_printf(void)
|
2014-06-23 02:38:53 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
char* s;
|
|
|
|
char* s2;
|
|
|
|
char* s3;
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_strdup_printf("%s", "test");
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "test");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_strdup_printf("%d %s %c %u", -1, "0", '1', 2);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "-1 0 1 2");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s3 = tr_malloc0(4098);
|
|
|
|
memset(s3, '-', 4097);
|
|
|
|
s3[2047] = 't';
|
|
|
|
s3[2048] = 'e';
|
|
|
|
s3[2049] = 's';
|
|
|
|
s3[2050] = 't';
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s2 = tr_malloc0(4096);
|
|
|
|
memset(s2, '-', 4095);
|
|
|
|
s2[2047] = '%';
|
|
|
|
s2[2048] = 's';
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_strdup_printf(s2, "test");
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, s3);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s2);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_strdup_printf("%s", s3);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, s3);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s3);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
s = test_strdup_printf_valist("\n-%s-%s-%s-\n", "\r", "\t", "\b");
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "\n-\r-\t-\b-\n");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
2014-06-23 02:38:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2014-06-23 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_env(void)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* test_key = "TR_TEST_ENV";
|
2017-04-19 12:04:45 +00:00
|
|
|
int x;
|
|
|
|
char* s;
|
|
|
|
|
|
|
|
unsetenv(test_key);
|
|
|
|
|
|
|
|
check(!tr_env_key_exists(test_key));
|
|
|
|
x = tr_env_get_int(test_key, 123);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(x, ==, 123);
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_env_get_string(test_key, NULL);
|
2017-05-30 17:56:12 +00:00
|
|
|
check_str(s, ==, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_env_get_string(test_key, "a");
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "a");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
|
|
|
|
|
|
|
setenv(test_key, "", 1);
|
|
|
|
|
|
|
|
check(tr_env_key_exists(test_key));
|
|
|
|
x = tr_env_get_int(test_key, 456);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(x, ==, 456);
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_env_get_string(test_key, NULL);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
|
|
|
s = tr_env_get_string(test_key, "b");
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
|
|
|
|
|
|
|
setenv(test_key, "135", 1);
|
|
|
|
|
|
|
|
check(tr_env_key_exists(test_key));
|
|
|
|
x = tr_env_get_int(test_key, 789);
|
2017-05-29 20:22:36 +00:00
|
|
|
check_int(x, ==, 135);
|
2017-04-19 12:04:45 +00:00
|
|
|
s = tr_env_get_string(test_key, NULL);
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "135");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
|
|
|
s = tr_env_get_string(test_key, "c");
|
2017-05-29 19:48:02 +00:00
|
|
|
check_str(s, ==, "135");
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(s);
|
|
|
|
|
|
|
|
return 0;
|
2014-09-21 18:05:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int main(void)
|
2010-07-24 17:09:39 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
testFunc const tests[] =
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
test_array,
|
|
|
|
test_buildpath,
|
|
|
|
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));
|
2008-05-20 17:33:54 +00:00
|
|
|
}
|