diff options
author | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
commit | 16cbf3c076a0aca6d47adaf3f719493674cf2363 (patch) | |
tree | e6530480020f62a2bdbf249988ec3e2a751d3927 /test | |
download | unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.gz unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.bz2 |
Initial version.
Diffstat (limited to 'test')
-rw-r--r-- | test/nxt_exp_approximation.c | 72 | ||||
-rw-r--r-- | test/nxt_gmtime_unit_test.c | 83 | ||||
-rw-r--r-- | test/nxt_lib_unit_test.c | 157 | ||||
-rw-r--r-- | test/nxt_lib_unit_test.h | 72 | ||||
-rw-r--r-- | test/nxt_lvlhsh_unit_test.c | 230 | ||||
-rw-r--r-- | test/nxt_malloc_unit_test.c | 122 | ||||
-rw-r--r-- | test/nxt_mem_cache_pool_unit_test.c | 75 | ||||
-rw-r--r-- | test/nxt_mem_zone_unit_test.c | 73 | ||||
-rw-r--r-- | test/nxt_msec_diff_unit_test.c | 45 | ||||
-rw-r--r-- | test/nxt_rbtree1.c | 382 | ||||
-rw-r--r-- | test/nxt_rbtree1.h | 73 | ||||
-rw-r--r-- | test/nxt_rbtree1_unit_test.c | 345 | ||||
-rw-r--r-- | test/nxt_rbtree_unit_test.c | 368 | ||||
-rw-r--r-- | test/nxt_sprintf_unit_test.c | 70 | ||||
-rw-r--r-- | test/nxt_term_parse_unit_test.c | 59 | ||||
-rw-r--r-- | test/nxt_utf8_file_name_test.c | 141 | ||||
-rw-r--r-- | test/nxt_utf8_unit_test.c | 190 |
17 files changed, 2557 insertions, 0 deletions
diff --git a/test/nxt_exp_approximation.c b/test/nxt_exp_approximation.c new file mode 100644 index 00000000..1f79c068 --- /dev/null +++ b/test/nxt_exp_approximation.c @@ -0,0 +1,72 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> +#include <math.h> + + +nxt_int_t +nxt_exp_approximation(nxt_thread_t *thr) +{ + double n, e0, e1, diff; + nxt_nsec_t start, end; + + nxt_thread_time_update(thr); + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "exp approximation unit test started"); + + for (n = 0.0; n > -20.0; n -= 0.00001) { + + e0 = nxt_event_conn_exponential_approximation(n); + e1 = exp(n); + + diff = fabs(e0 - e1); + + /* 0.028993 is max difference with libm exp(). */ + if (diff > 0.028993) { + nxt_log_alert(thr->log, + "exp approximation unit test failed: %0.6f %0.6f", + n, diff); + return NXT_ERROR; + } + } + + + nxt_thread_time_update(thr); + start = nxt_thread_monotonic_time(thr); + + e0 = 0; + for (n = 0.0; n > -20.0; n -= 0.00001) { + e0 += nxt_event_conn_exponential_approximation(n); + } + + nxt_thread_time_update(thr); + end = nxt_thread_monotonic_time(thr); + + /* e0 is passed but not output to eliminate optimization. */ + nxt_log_error(NXT_LOG_NOTICE, thr->log, "exp approximation: %0.1fns", + (end - start) / 20000000.0, e0); + + + nxt_thread_time_update(thr); + start = nxt_thread_monotonic_time(thr); + + e0 = 0; + for (n = 0.0; n > -20.0; n -= 0.000001) { + e0 += exp(n); + } + + nxt_thread_time_update(thr); + end = nxt_thread_monotonic_time(thr); + + /* e0 is passed but not output to eliminate optimization. */ + nxt_log_error(NXT_LOG_NOTICE, thr->log, "libm exp(): %0.1fns", + (end - start) / 20000000.0, e0); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "exp approximation unit test passed"); + return NXT_OK; +} diff --git a/test/nxt_gmtime_unit_test.c b/test/nxt_gmtime_unit_test.c new file mode 100644 index 00000000..eea916f3 --- /dev/null +++ b/test/nxt_gmtime_unit_test.c @@ -0,0 +1,83 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +#if (NXT_TIME_T_SIZE == 4) + +/* A 86400-fold number below 2^31. */ +#define NXT_GMTIME_MAX 2147472000 + +#else +/* + * March 19, 29398 is maximum valid data if nxt_uint_t + * is 4 bytes size whilst nxt_time_t is 8 bytes size. + */ +#define NXT_GMTIME_MAX 865550793600 +#endif + + +nxt_int_t +nxt_gmtime_unit_test(nxt_thread_t *thr) +{ + struct tm tm0, *tm1; + nxt_time_t s; + nxt_nsec_t start, end; + + nxt_thread_time_update(thr); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "gmtime unit test started"); + + for (s = 0; s < NXT_GMTIME_MAX; s += 86400) { + + nxt_gmtime(s, &tm0); + tm1 = gmtime(&s); + + if (tm0.tm_mday != tm1->tm_mday + || tm0.tm_mon != tm1->tm_mon + || tm0.tm_year != tm1->tm_year + || tm0.tm_yday != tm1->tm_yday + || tm0.tm_wday != tm1->tm_wday) + { + nxt_log_alert(thr->log, + "gmtime unit test failed: %T @ %02d.%02d.%d", + s, tm1->tm_mday, tm1->tm_mon + 1, + tm1->tm_year + 1900); + return NXT_ERROR; + } + } + + + nxt_thread_time_update(thr); + start = nxt_thread_monotonic_time(thr); + + for (s = 0; s < 10000000; s++) { + nxt_gmtime(s, &tm0); + } + + nxt_thread_time_update(thr); + end = nxt_thread_monotonic_time(thr); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "nxt_gmtime(): %0.1fns", + (end - start) / 10000000.0); + + + nxt_thread_time_update(thr); + start = nxt_thread_monotonic_time(thr); + + for (s = 0; s < 10000000; s++) { + (void) gmtime(&s); + } + + nxt_thread_time_update(thr); + end = nxt_thread_monotonic_time(thr); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "gmtime(): %0.1fns", + (end - start) / 10000000.0); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "gmtime unit test passed"); + return NXT_OK; +} diff --git a/test/nxt_lib_unit_test.c b/test/nxt_lib_unit_test.c new file mode 100644 index 00000000..28397187 --- /dev/null +++ b/test/nxt_lib_unit_test.c @@ -0,0 +1,157 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +extern char **environ; + + +/* The function is defined here to prevent inline optimizations. */ +static nxt_bool_t +nxt_msec_less(nxt_msec_t first, nxt_msec_t second) +{ + return (nxt_msec_diff(first, second) < 0); +} + + +int nxt_cdecl +main(int argc, char **argv) +{ + nxt_thread_t *thr; + + if (nxt_lib_start("lib_unit_test", argv, &environ) != NXT_OK) { + return 1; + } + + nxt_main_log.level = NXT_LOG_INFO; + + thr = nxt_thread(); + +#if (NXT_UNIT_TEST_RTDTSC) + + if (nxt_process_argv[1] != NULL + && nxt_memcmp(nxt_process_argv[1], "rbm", 3) == 0) + { + if (nxt_rbtree1_mb_start(thr) != NXT_OK) { + return 1; + } + + if (nxt_rbtree_mb_start(thr) != NXT_OK) { + return 1; + } + + if (nxt_lvlhsh_unit_test(thr, 500 * 1000, 0) != NXT_OK) { + return 1; + } + + nxt_rbtree1_mb_insert(thr); + nxt_rbtree_mb_insert(thr); + + if (nxt_lvlhsh_unit_test(thr, 500 * 1000, 0) != NXT_OK) { + return 1; + } + + nxt_rbtree1_mb_delete(thr); + nxt_rbtree_mb_delete(thr); + + return 0; + } + +#endif + +#if !(NXT_HAVE_ARC4RANDOM) + if (nxt_random_unit_test(thr) != NXT_OK) { + return 1; + } +#endif + + if (nxt_term_parse_unit_test(thr) != NXT_OK) { + return 1; + } + + if (nxt_msec_diff_unit_test(thr, nxt_msec_less) != NXT_OK) { + return 1; + } + + if (nxt_exp_approximation(thr) != NXT_OK) { + return 1; + } + + if (nxt_rbtree_unit_test(thr, 100 * 1000) != NXT_OK) { + return 1; + } + + if (nxt_rbtree_unit_test(thr, 1000 * 1000) != NXT_OK) { + return 1; + } + + if (nxt_rbtree1_unit_test(thr, 100 * 1000) != NXT_OK) { + return 1; + } + + if (nxt_rbtree1_unit_test(thr, 1000 * 1000) != NXT_OK) { + return 1; + } + + if (nxt_mem_cache_pool_unit_test(thr, 100, 40000, 128 - 1) != NXT_OK) { + return 1; + } + + if (nxt_mem_cache_pool_unit_test(thr, 100, 1000, 4096 - 1) != NXT_OK) { + return 1; + } + + if (nxt_mem_cache_pool_unit_test(thr, 1000, 100, 64 * 1024 - 1) != NXT_OK) { + return 1; + } + + if (nxt_mem_zone_unit_test(thr, 100, 20000, 128 - 1) != NXT_OK) { + return 1; + } + + if (nxt_mem_zone_unit_test(thr, 100, 10000, 4096 - 1) != NXT_OK) { + return 1; + } + + if (nxt_mem_zone_unit_test(thr, 1000, 40, 64 * 1024 - 1) != NXT_OK) { + return 1; + } + + if (nxt_lvlhsh_unit_test(thr, 2, 1) != NXT_OK) { + return 1; + } + + if (nxt_lvlhsh_unit_test(thr, 100 * 1000, 1) != NXT_OK) { + return 1; + } + + if (nxt_lvlhsh_unit_test(thr, 100 * 1000, 0) != NXT_OK) { + return 1; + } + + if (nxt_lvlhsh_unit_test(thr, 1000 * 1000, 1) != NXT_OK) { + return 1; + } + + if (nxt_gmtime_unit_test(thr) != NXT_OK) { + return 1; + } + + if (nxt_sprintf_unit_test(thr) != NXT_OK) { + return 1; + } + + if (nxt_malloc_unit_test(thr) != NXT_OK) { + return 1; + } + + if (nxt_utf8_unit_test(thr) != NXT_OK) { + return 1; + } + + return 0; +} diff --git a/test/nxt_lib_unit_test.h b/test/nxt_lib_unit_test.h new file mode 100644 index 00000000..bbb2d5f4 --- /dev/null +++ b/test/nxt_lib_unit_test.h @@ -0,0 +1,72 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#ifndef _NXT_LIB_UNIT_TEST_H_INCLUDED_ +#define _NXT_LIB_UNIT_TEST_H_INCLUDED_ + + +#define NXT_LIB_UNIT_TEST_STATIC + + +typedef nxt_bool_t (*nxt_msec_less_t)(nxt_msec_t first, nxt_msec_t second); + + +#define NXT_RBT_NODES 1500 + + +#if (__i386__ || __i386 || __amd64__ || __amd64) +#if (NXT_GCC || NXT_CLANG) + +#define NXT_UNIT_TEST_RTDTSC 1 + +nxt_inline uint64_t +nxt_rdtsc(void) +{ + uint32_t eax, edx; + + __asm__ volatile ("rdtsc" : "=a" (eax), "=d" (edx)); + + return ((uint64_t) edx << 32) | eax; +} + +#endif +#endif + + +nxt_int_t nxt_term_parse_unit_test(nxt_thread_t *thr); +nxt_int_t nxt_msec_diff_unit_test(nxt_thread_t *thr, nxt_msec_less_t); +nxt_int_t nxt_exp_approximation(nxt_thread_t *thr); +double nxt_event_conn_exponential_approximation(double n); + +nxt_int_t nxt_rbtree_unit_test(nxt_thread_t *thr, nxt_uint_t n); +nxt_int_t nxt_rbtree1_unit_test(nxt_thread_t *thr, nxt_uint_t n); + +#if (NXT_UNIT_TEST_RTDTSC) + +nxt_int_t nxt_rbtree_mb_start(nxt_thread_t *thr); +void nxt_rbtree_mb_insert(nxt_thread_t *thr); +void nxt_rbtree_mb_delete(nxt_thread_t *thr); + +nxt_int_t nxt_rbtree1_mb_start(nxt_thread_t *thr); +void nxt_rbtree1_mb_insert(nxt_thread_t *thr); +void nxt_rbtree1_mb_delete(nxt_thread_t *thr); + +#endif + +nxt_int_t nxt_mem_cache_pool_unit_test(nxt_thread_t *thr, nxt_uint_t runs, + nxt_uint_t nblocks, size_t max_size); +nxt_int_t nxt_mem_zone_unit_test(nxt_thread_t *thr, nxt_uint_t runs, + nxt_uint_t nblocks, size_t max_size); +nxt_int_t nxt_lvlhsh_unit_test(nxt_thread_t *thr, nxt_uint_t n, + nxt_bool_t use_pool); + +nxt_int_t nxt_gmtime_unit_test(nxt_thread_t *thr); +nxt_int_t nxt_sprintf_unit_test(nxt_thread_t *thr); +nxt_int_t nxt_malloc_unit_test(nxt_thread_t *thr); +nxt_int_t nxt_utf8_unit_test(nxt_thread_t *thr); + + +#endif /* _NXT_LIB_UNIT_TEST_H_INCLUDED_ */ diff --git a/test/nxt_lvlhsh_unit_test.c b/test/nxt_lvlhsh_unit_test.c new file mode 100644 index 00000000..115f3907 --- /dev/null +++ b/test/nxt_lvlhsh_unit_test.c @@ -0,0 +1,230 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +static nxt_int_t +nxt_lvlhsh_unit_test_key_test(nxt_lvlhsh_query_t *lhq, void *data) +{ + if (*(uintptr_t *) lhq->key.data == (uintptr_t) data) { + return NXT_OK; + } + + return NXT_DECLINED; +} + + +static void * +nxt_lvlhsh_unit_test_pool_alloc(void *pool, size_t size, nxt_uint_t nalloc) +{ + return nxt_mem_cache_align(pool, size, size); +} + + +static void +nxt_lvlhsh_unit_test_pool_free(void *pool, void *p, size_t size) +{ + nxt_mem_cache_free(pool, p); +} + + +static const nxt_lvlhsh_proto_t malloc_proto nxt_aligned(64) = { + //NXT_LVLHSH_LARGE_MEMALIGN, + NXT_LVLHSH_DEFAULT, + 0, + nxt_lvlhsh_unit_test_key_test, + nxt_lvlhsh_alloc, + nxt_lvlhsh_free, +}; + +static const nxt_lvlhsh_proto_t pool_proto nxt_aligned(64) = { + NXT_LVLHSH_LARGE_SLAB, + 0, + nxt_lvlhsh_unit_test_key_test, + nxt_lvlhsh_unit_test_pool_alloc, + nxt_lvlhsh_unit_test_pool_free, +}; + + +static nxt_int_t +nxt_lvlhsh_unit_test_add(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto, + void *pool, uintptr_t key) +{ + nxt_lvlhsh_query_t lhq; + + lhq.key_hash = key; + lhq.replace = 0; + lhq.key.len = sizeof(uintptr_t); + lhq.key.data = (u_char *) &key; + lhq.value = (void *) key; + lhq.proto = proto; + lhq.pool = pool; + + switch (nxt_lvlhsh_insert(lh, &lhq)) { + + case NXT_OK: + return NXT_OK; + + case NXT_DECLINED: + nxt_thread_log_alert("lvlhsh unit test failed: " + "key %p is already in hash", key); + default: + return NXT_ERROR; + } +} + + +static nxt_int_t +nxt_lvlhsh_unit_test_get(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto, + uintptr_t key) +{ + nxt_lvlhsh_query_t lhq; + + lhq.key_hash = key; + lhq.key.len = sizeof(uintptr_t); + lhq.key.data = (u_char *) &key; + lhq.proto = proto; + + if (nxt_lvlhsh_find(lh, &lhq) == NXT_OK) { + + if (key == (uintptr_t) lhq.value) { + return NXT_OK; + } + } + + nxt_thread_log_alert("lvlhsh unit test failed: " + "key %p not found in hash", key); + + return NXT_ERROR; +} + + +static nxt_int_t +nxt_lvlhsh_unit_test_delete(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto, + void *pool, uintptr_t key) +{ + nxt_int_t ret; + nxt_lvlhsh_query_t lhq; + + lhq.key_hash = key; + lhq.key.len = sizeof(uintptr_t); + lhq.key.data = (u_char *) &key; + lhq.proto = proto; + lhq.pool = pool; + + ret = nxt_lvlhsh_delete(lh, &lhq); + + if (ret != NXT_OK) { + nxt_thread_log_alert("lvlhsh unit test failed: " + "key %p not found in hash", key); + } + + return ret; +} + + +nxt_int_t +nxt_lvlhsh_unit_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool) +{ + uintptr_t key; + nxt_nsec_t start, end; + nxt_uint_t i; + nxt_lvlhsh_t lh; + nxt_lvlhsh_each_t lhe; + nxt_mem_cache_pool_t *pool; + const nxt_lvlhsh_proto_t *proto; + + const size_t min_chunk_size = 32; + const size_t page_size = 1024; + const size_t page_alignment = 128; + const size_t cluster_size = 4096; + + nxt_thread_time_update(thr); + start = nxt_thread_monotonic_time(thr); + + if (use_pool) { + pool = nxt_mem_cache_pool_create(cluster_size, page_alignment, + page_size, min_chunk_size); + if (pool == NULL) { + return NXT_ERROR; + } + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "lvlhsh unit test started: %uD pool", n); + proto = &pool_proto; + + } else { + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "lvlhsh unit test started: %uD malloc", n); + proto = &malloc_proto; + pool = NULL; + } + + nxt_memzero(&lh, sizeof(nxt_lvlhsh_t)); + + key = 0; + for (i = 0; i < n; i++) { + key = nxt_murmur_hash2(&key, sizeof(uint32_t)); + + if (nxt_lvlhsh_unit_test_add(&lh, proto, pool, key) != NXT_OK) { + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "lvlhsh add unit test failed at %ui", i); + return NXT_ERROR; + } + } + + key = 0; + for (i = 0; i < n; i++) { + key = nxt_murmur_hash2(&key, sizeof(uint32_t)); + + if (nxt_lvlhsh_unit_test_get(&lh, proto, key) != NXT_OK) { + return NXT_ERROR; + } + } + + nxt_memzero(&lhe, sizeof(nxt_lvlhsh_each_t)); + lhe.proto = proto; + + for (i = 0; i < n + 1; i++) { + if (nxt_lvlhsh_each(&lh, &lhe) == NULL) { + break; + } + } + + if (i != n) { + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "lvlhsh each unit test failed at %ui of %ui", i, n); + return NXT_ERROR; + } + + key = 0; + for (i = 0; i < n; i++) { + key = nxt_murmur_hash2(&key, sizeof(uint32_t)); + + if (nxt_lvlhsh_unit_test_delete(&lh, proto, pool, key) != NXT_OK) { + return NXT_ERROR; + } + } + + if (pool != NULL) { + if (!nxt_mem_cache_pool_is_empty(pool)) { + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "mem cache pool is not empty"); + return NXT_ERROR; + } + + nxt_mem_cache_pool_destroy(pool); + } + + nxt_thread_time_update(thr); + end = nxt_thread_monotonic_time(thr); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "lvlhsh unit test passed: %0.3fs", + (end - start) / 1000000000.0); + + return NXT_OK; +} diff --git a/test/nxt_malloc_unit_test.c b/test/nxt_malloc_unit_test.c new file mode 100644 index 00000000..f1f67593 --- /dev/null +++ b/test/nxt_malloc_unit_test.c @@ -0,0 +1,122 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +#define TIMES 1000 + + +typedef struct { + size_t size; + size_t alignment; + nxt_bool_t tight; +} nxt_malloc_size_t; + + +static nxt_malloc_size_t * +nxt_malloc_test(nxt_thread_t *thr, nxt_malloc_size_t *last, size_t size, + nxt_uint_t times) +{ + size_t a, s, alignment; + uintptr_t n; + nxt_uint_t i, tight; + static u_char *p[TIMES + 1]; + + alignment = (size_t) -1; + tight = 0; + + for (i = 1; i < times; i++) { + + p[i] = nxt_malloc(size); + if (p[i] == NULL) { + return NULL; + } + + n = (uintptr_t) p[i]; + a = 0; + + while ((n & 1) == 0) { + a++; + n >>= 1; + } + + alignment = nxt_min(alignment, a); + } + + + for (i = 1; i < times; i++) { + s = size; + nxt_malloc_usable_size(p[i], s); + + if (p[i - 1] + s == p[i] || p[i - 1] == p[i] + s) { + tight++; + } + + nxt_free(p[i]); + } + + alignment = 1 << alignment; + +#if 0 + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "malloc: %uz, %uz, %ui", size, alignment, tight); +#endif + + while (last->alignment >= alignment) { + last--; + } + + last++; + + last->size = size; + last->alignment = alignment; + last->tight = times * 9 / 10 < tight; + + return last; +} + + +nxt_int_t +nxt_malloc_unit_test(nxt_thread_t *thr) +{ + size_t size; + nxt_malloc_size_t *last, *s; + static nxt_malloc_size_t sizes[100]; + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "malloc test started"); + + last = &sizes[0]; + + for (size = 1; size < 64; size++) { + last = nxt_malloc_test(thr, last, size, TIMES); + if (last == NULL) { + return NXT_ERROR; + } + } + + for (size = 64; size < 16384; size += 8) { + last = nxt_malloc_test(thr, last, size, TIMES / 4); + if (last == NULL) { + return NXT_ERROR; + } + } + + for (size = 16384; size < 512 * 1024 + 129; size += 128) { + last = nxt_malloc_test(thr, last, size, TIMES / 16); + if (last == NULL) { + return NXT_ERROR; + } + } + + for (s = &sizes[1]; s <= last; s++) { + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "malloc sizes: %uz-%uz alignment:%uz tight:%ui", + s[-1].size + 1, s->size, s->alignment, s->tight); + } + + return NXT_OK; +} diff --git a/test/nxt_mem_cache_pool_unit_test.c b/test/nxt_mem_cache_pool_unit_test.c new file mode 100644 index 00000000..4e4cefb7 --- /dev/null +++ b/test/nxt_mem_cache_pool_unit_test.c @@ -0,0 +1,75 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +nxt_int_t +nxt_mem_cache_pool_unit_test(nxt_thread_t *thr, nxt_uint_t runs, + nxt_uint_t nblocks, size_t max_size) +{ + void **blocks; + size_t total; + uint32_t size; + nxt_uint_t i, n; + nxt_mem_cache_pool_t *pool; + + const size_t min_chunk_size = 16; + const size_t page_size = 128; + const size_t page_alignment = 128; + const size_t cluster_size = page_size * 8; + + nxt_thread_time_update(thr); + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "mem cache pool unit test started, max:%uz", max_size); + + blocks = nxt_malloc(nblocks * sizeof(void *)); + if (blocks == NULL) { + return NXT_ERROR; + } + + pool = nxt_mem_cache_pool_create(cluster_size, page_alignment, + page_size, min_chunk_size); + if (pool == NULL) { + return NXT_ERROR; + } + + size = 0; + + for (i = 0; i < runs; i++) { + + total = 0; + + for (n = 0; n < nblocks; n++) { + size = nxt_murmur_hash2(&size, sizeof(uint32_t)); + + total += size & max_size; + blocks[n] = nxt_mem_cache_alloc(pool, size & max_size); + + if (blocks[n] == NULL) { + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "mem cache pool unit test failed: %uz", total); + return NXT_ERROR; + } + } + + for (n = 0; n < nblocks; n++) { + nxt_mem_cache_free(pool, blocks[n]); + } + } + + if (!nxt_mem_cache_pool_is_empty(pool)) { + nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem cache pool is not empty"); + return NXT_ERROR; + } + + nxt_mem_cache_pool_destroy(pool); + + nxt_thread_time_update(thr); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem cache pool unit test passed"); + + return NXT_OK; +} diff --git a/test/nxt_mem_zone_unit_test.c b/test/nxt_mem_zone_unit_test.c new file mode 100644 index 00000000..cce9b702 --- /dev/null +++ b/test/nxt_mem_zone_unit_test.c @@ -0,0 +1,73 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +nxt_int_t +nxt_mem_zone_unit_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks, + size_t max_size) +{ + void *start, **blocks; + size_t total, zone_size; + uint32_t size; + nxt_uint_t i, n; + nxt_mem_zone_t *zone; + const size_t page_size = 4096; + + nxt_thread_time_update(thr); + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "mem zone unit test started, max:%uz", max_size); + + zone_size = (max_size + 1) * nblocks; + + start = nxt_memalign(page_size, zone_size); + if (start == NULL) { + return NXT_ERROR; + } + + zone = nxt_mem_zone_init(start, zone_size, page_size); + if (zone == NULL) { + return NXT_ERROR; + } + + blocks = nxt_malloc(nblocks * sizeof(void *)); + if (blocks == NULL) { + return NXT_ERROR; + } + + size = 0; + + for (i = 0; i < runs; i++) { + + total = 0; + + for (n = 0; n < nblocks; n++) { + size = nxt_murmur_hash2(&size, sizeof(uint32_t)); + + total += size & max_size; + blocks[n] = nxt_mem_zone_alloc(zone, size & max_size); + + if (blocks[n] == NULL) { + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "mem zone unit test failed: %uz", total); + return NXT_ERROR; + } + } + + for (n = 0; n < nblocks; n++) { + nxt_mem_zone_free(zone, blocks[n]); + } + } + + nxt_free(blocks); + nxt_free(zone); + + nxt_thread_time_update(thr); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem zone unit test passed"); + + return NXT_OK; +} diff --git a/test/nxt_msec_diff_unit_test.c b/test/nxt_msec_diff_unit_test.c new file mode 100644 index 00000000..504cbb5a --- /dev/null +++ b/test/nxt_msec_diff_unit_test.c @@ -0,0 +1,45 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +static const nxt_msec_t pairs[] = { + + 0x00000000, 0x00000001, + 0x00000000, 0x7fffffff, + + 0x7fffffff, 0x80000000, + 0x7fffffff, 0x80000001, + + 0x80000000, 0x80000001, + 0x80000000, 0xffffffff, + + 0xffffffff, 0x00000000, + 0xffffffff, 0x00000001, +}; + + +nxt_int_t +nxt_msec_diff_unit_test(nxt_thread_t *thr, nxt_msec_less_t less) +{ + nxt_uint_t i; + + nxt_thread_time_update(thr); + + for (i = 0; i < nxt_nitems(pairs); i += 2) { + + if (!less(pairs[i], pairs[i + 1])) { + nxt_log_alert(thr->log, + "msec diff unit test failed: 0x%08XM 0x%08XM", + pairs[i], pairs[i + 1]); + return NXT_ERROR; + } + } + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "msec diff unit test passed"); + return NXT_OK; +} diff --git a/test/nxt_rbtree1.c b/test/nxt_rbtree1.c new file mode 100644 index 00000000..1ae059ab --- /dev/null +++ b/test/nxt_rbtree1.c @@ -0,0 +1,382 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) Nginx, Inc. + */ + + +#include <nxt_main.h> +#include "nxt_rbtree1.h" + + +/* + * The red-black tree code is based on the algorithm described in + * the "Introduction to Algorithms" by Cormen, Leiserson and Rivest. + */ + + +nxt_inline void nxt_rbtree1_left_rotate(nxt_rbtree1_node_t **root, + nxt_rbtree1_node_t *sentinel, nxt_rbtree1_node_t *node); +nxt_inline void nxt_rbtree1_right_rotate(nxt_rbtree1_node_t **root, + nxt_rbtree1_node_t *sentinel, nxt_rbtree1_node_t *node); + + +void +nxt_rbtree1_insert(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) +{ + nxt_rbtree1_node_t **root, *temp, *sentinel; + + /* a binary tree insert */ + + root = (nxt_rbtree1_node_t **) &tree->root; + sentinel = tree->sentinel; + + if (*root == sentinel) { + node->parent = NULL; + node->left = sentinel; + node->right = sentinel; + nxt_rbtree1_black(node); + *root = node; + + return; + } + + tree->insert(*root, node, sentinel); + + /* re-balance tree */ + + while (node != *root && nxt_rbtree1_is_red(node->parent)) { + + if (node->parent == node->parent->parent->left) { + temp = node->parent->parent->right; + + if (nxt_rbtree1_is_red(temp)) { + nxt_rbtree1_black(node->parent); + nxt_rbtree1_black(temp); + nxt_rbtree1_red(node->parent->parent); + node = node->parent->parent; + + } else { + if (node == node->parent->right) { + node = node->parent; + nxt_rbtree1_left_rotate(root, sentinel, node); + } + + nxt_rbtree1_black(node->parent); + nxt_rbtree1_red(node->parent->parent); + nxt_rbtree1_right_rotate(root, sentinel, node->parent->parent); + } + + } else { + temp = node->parent->parent->left; + + if (nxt_rbtree1_is_red(temp)) { + nxt_rbtree1_black(node->parent); + nxt_rbtree1_black(temp); + nxt_rbtree1_red(node->parent->parent); + node = node->parent->parent; + + } else { + if (node == node->parent->left) { + node = node->parent; + nxt_rbtree1_right_rotate(root, sentinel, node); + } + + nxt_rbtree1_black(node->parent); + nxt_rbtree1_red(node->parent->parent); + nxt_rbtree1_left_rotate(root, sentinel, node->parent->parent); + } + } + } + + nxt_rbtree1_black(*root); +} + + +void +nxt_rbtree1_insert_value(nxt_rbtree1_node_t *temp, nxt_rbtree1_node_t *node, + nxt_rbtree1_node_t *sentinel) +{ + nxt_rbtree1_node_t **p; + + for ( ;; ) { + + p = (node->key < temp->key) ? &temp->left : &temp->right; + + if (*p == sentinel) { + break; + } + + temp = *p; + } + + *p = node; + node->parent = temp; + node->left = sentinel; + node->right = sentinel; + nxt_rbtree1_red(node); +} + + +void +nxt_rbtree1_insert_timer_value(nxt_rbtree1_node_t *temp, + nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel) +{ + nxt_rbtree1_node_t **p; + + for ( ;; ) { + + /* + * Timer values + * 1) are spread in small range, usually several minutes, + * 2) and overflow each 49 days, if milliseconds are stored in 32 bits. + * The comparison takes into account that overflow. + */ + + /* node->key < temp->key */ + + p = ((nxt_rbtree1_key_int_t) (node->key - temp->key) < 0) + ? &temp->left : &temp->right; + + if (*p == sentinel) { + break; + } + + temp = *p; + } + + *p = node; + node->parent = temp; + node->left = sentinel; + node->right = sentinel; + nxt_rbtree1_red(node); +} + + +void +nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) +{ + nxt_uint_t red; + nxt_rbtree1_node_t **root, *sentinel, *subst, *temp, *w; + + /* a binary tree delete */ + + root = (nxt_rbtree1_node_t **) &tree->root; + sentinel = tree->sentinel; + + if (node->left == sentinel) { + temp = node->right; + subst = node; + + } else if (node->right == sentinel) { + temp = node->left; + subst = node; + + } else { + subst = nxt_rbtree1_min(node->right, sentinel); + + if (subst->left != sentinel) { + temp = subst->left; + } else { + temp = subst->right; + } + } + + if (subst == *root) { + *root = temp; + nxt_rbtree1_black(temp); + + /* DEBUG stuff */ + node->left = NULL; + node->right = NULL; + node->parent = NULL; + node->key = 0; + + return; + } + + red = nxt_rbtree1_is_red(subst); + + if (subst == subst->parent->left) { + subst->parent->left = temp; + + } else { + subst->parent->right = temp; + } + + if (subst == node) { + + temp->parent = subst->parent; + + } else { + + if (subst->parent == node) { + temp->parent = subst; + + } else { + temp->parent = subst->parent; + } + + subst->left = node->left; + subst->right = node->right; + subst->parent = node->parent; + nxt_rbtree1_copy_color(subst, node); + + if (node == *root) { + *root = subst; + + } else { + if (node == node->parent->left) { + node->parent->left = subst; + } else { + node->parent->right = subst; + } + } + + if (subst->left != sentinel) { + subst->left->parent = subst; + } + + if (subst->right != sentinel) { + subst->right->parent = subst; + } + } + + /* DEBUG stuff */ + node->left = NULL; + node->right = NULL; + node->parent = NULL; + node->key = 0; + + if (red) { + return; + } + + /* a delete fixup */ + + while (temp != *root && nxt_rbtree1_is_black(temp)) { + + if (temp == temp->parent->left) { + w = temp->parent->right; + + if (nxt_rbtree1_is_red(w)) { + nxt_rbtree1_black(w); + nxt_rbtree1_red(temp->parent); + nxt_rbtree1_left_rotate(root, sentinel, temp->parent); + w = temp->parent->right; + } + + if (nxt_rbtree1_is_black(w->left) && nxt_rbtree1_is_black(w->right)) + { + nxt_rbtree1_red(w); + temp = temp->parent; + + } else { + if (nxt_rbtree1_is_black(w->right)) { + nxt_rbtree1_black(w->left); + nxt_rbtree1_red(w); + nxt_rbtree1_right_rotate(root, sentinel, w); + w = temp->parent->right; + } + + nxt_rbtree1_copy_color(w, temp->parent); + nxt_rbtree1_black(temp->parent); + nxt_rbtree1_black(w->right); + nxt_rbtree1_left_rotate(root, sentinel, temp->parent); + temp = *root; + } + + } else { + w = temp->parent->left; + + if (nxt_rbtree1_is_red(w)) { + nxt_rbtree1_black(w); + nxt_rbtree1_red(temp->parent); + nxt_rbtree1_right_rotate(root, sentinel, temp->parent); + w = temp->parent->left; + } + + if (nxt_rbtree1_is_black(w->left) && nxt_rbtree1_is_black(w->right)) + { + nxt_rbtree1_red(w); + temp = temp->parent; + + } else { + if (nxt_rbtree1_is_black(w->left)) { + nxt_rbtree1_black(w->right); + nxt_rbtree1_red(w); + nxt_rbtree1_left_rotate(root, sentinel, w); + w = temp->parent->left; + } + + nxt_rbtree1_copy_color(w, temp->parent); + nxt_rbtree1_black(temp->parent); + nxt_rbtree1_black(w->left); + nxt_rbtree1_right_rotate(root, sentinel, temp->parent); + temp = *root; + } + } + } + + nxt_rbtree1_black(temp); +} + + +nxt_inline void +nxt_rbtree1_left_rotate(nxt_rbtree1_node_t **root, nxt_rbtree1_node_t *sentinel, + nxt_rbtree1_node_t *node) +{ + nxt_rbtree1_node_t *temp; + + temp = node->right; + node->right = temp->left; + + if (temp->left != sentinel) { + temp->left->parent = node; + } + + temp->parent = node->parent; + + if (node == *root) { + *root = temp; + + } else if (node == node->parent->left) { + node->parent->left = temp; + + } else { + node->parent->right = temp; + } + + temp->left = node; + node->parent = temp; +} + + +nxt_inline void +nxt_rbtree1_right_rotate(nxt_rbtree1_node_t **root, + nxt_rbtree1_node_t *sentinel, nxt_rbtree1_node_t *node) +{ + nxt_rbtree1_node_t *temp; + + temp = node->left; + node->left = temp->right; + + if (temp->right != sentinel) { + temp->right->parent = node; + } + + temp->parent = node->parent; + + if (node == *root) { + *root = temp; + + } else if (node == node->parent->right) { + node->parent->right = temp; + + } else { + node->parent->left = temp; + } + + temp->right = node; + node->parent = temp; +} diff --git a/test/nxt_rbtree1.h b/test/nxt_rbtree1.h new file mode 100644 index 00000000..60048dad --- /dev/null +++ b/test/nxt_rbtree1.h @@ -0,0 +1,73 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) Nginx, Inc. + */ + + +typedef nxt_uint_t nxt_rbtree1_key_t; +typedef nxt_int_t nxt_rbtree1_key_int_t; + + +typedef struct nxt_rbtree1_node_s nxt_rbtree1_node_t; + +struct nxt_rbtree1_node_s { + nxt_rbtree1_key_t key; + nxt_rbtree1_node_t *left; + nxt_rbtree1_node_t *right; + nxt_rbtree1_node_t *parent; + u_char color; + u_char data; +}; + + +typedef struct nxt_rbtree1_s nxt_rbtree1_t; + +typedef void (*nxt_rbtree1_insert_pt) (nxt_rbtree1_node_t *root, + nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel); + +struct nxt_rbtree1_s { + nxt_rbtree1_node_t *root; + nxt_rbtree1_node_t *sentinel; + nxt_rbtree1_insert_pt insert; +}; + + +#define nxt_rbtree1_init(tree, s, i) \ + nxt_rbtree1_sentinel_init(s); \ + (tree)->root = s; \ + (tree)->sentinel = s; \ + (tree)->insert = i + + +NXT_EXPORT void nxt_rbtree1_insert(nxt_rbtree1_t *tree, + nxt_rbtree1_node_t *node); +NXT_EXPORT void nxt_rbtree1_delete(nxt_rbtree1_t *tree, + nxt_rbtree1_node_t *node); +NXT_EXPORT void nxt_rbtree1_insert_value(nxt_rbtree1_node_t *root, + nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel); +NXT_EXPORT void nxt_rbtree1_insert_timer_value(nxt_rbtree1_node_t *root, + nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel); + + +#define nxt_rbtree1_red(node) ((node)->color = 1) +#define nxt_rbtree1_black(node) ((node)->color = 0) +#define nxt_rbtree1_is_red(node) ((node)->color) +#define nxt_rbtree1_is_black(node) (!nxt_rbtree1_is_red(node)) +#define nxt_rbtree1_copy_color(n1, n2) (n1->color = n2->color) + + +/* a sentinel must be black */ + +#define nxt_rbtree1_sentinel_init(node) nxt_rbtree1_black(node) + + +nxt_inline nxt_rbtree1_node_t * +nxt_rbtree1_min(nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel) +{ + while (node->left != sentinel) { + node = node->left; + } + + return node; +} diff --git a/test/nxt_rbtree1_unit_test.c b/test/nxt_rbtree1_unit_test.c new file mode 100644 index 00000000..c3e9f6de --- /dev/null +++ b/test/nxt_rbtree1_unit_test.c @@ -0,0 +1,345 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> +#include "nxt_rbtree1.h" + + +#define \ +nxt_rbtree1_is_empty(tree) \ + (((tree)->root) == (tree)->sentinel) + + +#define \ +nxt_rbtree1_is_there_successor(tree, node) \ + ((node) != (tree)->sentinel) + + +nxt_inline nxt_rbtree1_node_t * +nxt_rbtree1_node_successor(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) +{ + nxt_rbtree1_node_t *parent; + + if (node->right != tree->sentinel) { + return nxt_rbtree1_min(node->right, tree->sentinel); + } + + for ( ;; ) { + parent = node->parent; + + if (parent == NULL) { + return tree->sentinel; + } + + if (node == parent->left) { + return parent; + } + + node = parent; + } +} + + +static void nxt_rbtree1_unit_test_insert_value(nxt_rbtree1_node_t *temp, + nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel); +static nxt_int_t nxt_rbtree1_unit_test_compare(nxt_rbtree1_node_t *node1, + nxt_rbtree1_node_t *node2); +static int nxt_cdecl nxt_rbtree1_unit_test_sort_cmp(const void *one, + const void *two); +static nxt_rbtree1_node_t *nxt_rbtree1_unit_test_find(nxt_rbtree1_t *tree, + nxt_rbtree1_node_t *node); + + +nxt_int_t +nxt_rbtree1_unit_test(nxt_thread_t *thr, nxt_uint_t n) +{ + uint32_t key, *keys; + nxt_uint_t i; + nxt_nsec_t start, end; + nxt_rbtree1_t tree; + nxt_rbtree1_node_t *node, *nodes, sentinel; + + nxt_thread_time_update(thr); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "rbtree1 unit test started: %ui", n); + + nxt_rbtree1_init(&tree, &sentinel, nxt_rbtree1_unit_test_insert_value); + + nodes = nxt_malloc(n * sizeof(nxt_rbtree1_node_t)); + if (nodes == NULL) { + return NXT_ERROR; + } + + keys = nxt_malloc(n * sizeof(uint32_t)); + if (keys == NULL) { + nxt_free(keys); + return NXT_ERROR; + } + + key = 0; + + for (i = 0; i < n; i++) { + key = nxt_murmur_hash2(&key, sizeof(uint32_t)); + + keys[i] = key; + nodes[i].key = key; + } + + nxt_qsort(keys, n, sizeof(uint32_t), nxt_rbtree1_unit_test_sort_cmp); + + nxt_thread_time_update(thr); + start = nxt_thread_monotonic_time(thr); + + for (i = 0; i < n; i++) { + nxt_rbtree1_insert(&tree, &nodes[i]); + } + + for (i = 0; i < n; i++) { + if (nxt_rbtree1_unit_test_find(&tree, &nodes[i]) != &nodes[i]) { + nxt_log_alert(thr->log, "rbtree1 unit test failed: %08XD not found", + nodes[i].key); + goto fail; + } + } + + i = 0; + node = nxt_rbtree1_min(tree.root, tree.sentinel); + + while (nxt_rbtree1_is_there_successor(&tree, node)) { + + if (keys[i] != node->key) { + nxt_log_alert(thr->log, "rbtree1 unit test failed: %i: %08XD %08XD", + i, keys[i], node->key); + goto fail; + } + + i++; + node = nxt_rbtree1_node_successor(&tree, node); + } + + if (i != n) { + nxt_log_alert(thr->log, "rbtree1 unit test failed: %ui", i); + goto fail; + } + + for (i = 0; i < n; i++) { + nxt_rbtree1_delete(&tree, &nodes[i]); + nxt_memset(&nodes[i], 0xA5, sizeof(nxt_rbtree1_node_t)); + } + + nxt_thread_time_update(thr); + end = nxt_thread_monotonic_time(thr); + + if (!nxt_rbtree1_is_empty(&tree)) { + nxt_log_alert(thr->log, "rbtree1 unit test failed: tree is not empty"); + goto fail; + } + + nxt_free(keys); + nxt_free(nodes); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "rbtree1 unit test passed %0.3fs", + (end - start) / 1000000000.0); + + return NXT_OK; + +fail: + + nxt_free(keys); + nxt_free(nodes); + + return NXT_ERROR; +} + + +static void +nxt_rbtree1_unit_test_insert_value(nxt_rbtree1_node_t *temp, + nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel) +{ + nxt_rbtree1_node_t **p; + + for ( ;; ) { + nxt_prefetch(temp->left); + nxt_prefetch(temp->right); + + p = (node->key < temp->key) ? &temp->left : &temp->right; + + if (*p == sentinel) { + break; + } + + temp = *p; + } + + *p = node; + node->parent = temp; + node->left = sentinel; + node->right = sentinel; + nxt_rbtree1_red(node); +} + + +/* + * Subtraction cannot be used in these comparison functions because the key + * values are spread uniform in whole 0 .. 2^32 range but are not grouped + * around some value as timeout values are. + */ + +nxt_inline nxt_int_t +nxt_rbtree1_unit_test_compare(nxt_rbtree1_node_t *node1, + nxt_rbtree1_node_t *node2) +{ + if (node1->key < node2->key) { + return -1; + } + + if (node1->key == node2->key) { + return 0; + } + + return 1; +} + + +static int nxt_cdecl +nxt_rbtree1_unit_test_sort_cmp(const void *one, const void *two) +{ + const uint32_t *first, *second; + + first = one; + second = two; + + if (*first < *second) { + return -1; + } + + if (*first == *second) { + return 0; + } + + return 1; +} + + +static nxt_rbtree1_node_t * +nxt_rbtree1_unit_test_find(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) +{ + nxt_int_t n; + nxt_rbtree1_node_t *next, *sentinel; + + next = tree->root; + sentinel = tree->sentinel; + + while (next != sentinel) { + nxt_prefetch(next->left); + nxt_prefetch(next->right); + + n = nxt_rbtree1_unit_test_compare(node, next); + + if (n < 0) { + next = next->left; + + } else if (n > 0) { + next = next->right; + + } else { + return next; + } + } + + return NULL; +} + + +#if (NXT_UNIT_TEST_RTDTSC) + +#define NXT_RBT_STEP (21 * nxt_pagesize / 10 / sizeof(nxt_rbtree1_node_t)) + +static nxt_rbtree1_t mb_tree; +static nxt_rbtree1_node_t mb_sentinel; +static nxt_rbtree1_node_t *mb_nodes; + + +nxt_int_t +nxt_rbtree1_mb_start(nxt_thread_t *thr) +{ + uint32_t key; + uint64_t start, end; + nxt_uint_t i, n; + + n = NXT_RBT_STEP; + + mb_nodes = nxt_malloc(NXT_RBT_NODES * n * sizeof(nxt_rbtree1_node_t)); + if (mb_nodes == NULL) { + return NXT_ERROR; + } + + nxt_rbtree1_init(&mb_tree, &mb_sentinel, + nxt_rbtree1_unit_test_insert_value); + + key = 0; + + for (i = 0; i < NXT_RBT_NODES; i++) { + key = nxt_murmur_hash2(&key, sizeof(uint32_t)); + mb_nodes[n * i].key = key; + } + + for (i = 0; i < NXT_RBT_NODES - 2; i++) { + nxt_rbtree1_insert(&mb_tree, &mb_nodes[n * i]); + } + + n *= (NXT_RBT_NODES - 2); + + start = nxt_rdtsc(); + nxt_rbtree1_insert(&mb_tree, &mb_nodes[n]); + end = nxt_rdtsc(); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "rbtree1 mb cached insert: %L cycles", end - start); + + return NXT_OK; +} + + +void +nxt_rbtree1_mb_insert(nxt_thread_t *thr) +{ + uint64_t start, end; + nxt_uint_t n; + + n = NXT_RBT_STEP; + n *= (NXT_RBT_NODES - 1); + + start = nxt_rdtsc(); + nxt_rbtree1_insert(&mb_tree, &mb_nodes[n]); + end = nxt_rdtsc(); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "rbtree1 mb insert: %L cycles", end - start); +} + + +void +nxt_rbtree1_mb_delete(nxt_thread_t *thr) +{ + uint64_t start, end; + nxt_uint_t n; + + n = NXT_RBT_STEP; + n *= (NXT_RBT_NODES / 4 + 1); + + start = nxt_rdtsc(); + nxt_rbtree1_delete(&mb_tree, &mb_nodes[n]); + end = nxt_rdtsc(); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "rbtree1 mb delete: %L cycles", end - start); + + nxt_free(mb_nodes); +} + +#endif diff --git a/test/nxt_rbtree_unit_test.c b/test/nxt_rbtree_unit_test.c new file mode 100644 index 00000000..d5122e2a --- /dev/null +++ b/test/nxt_rbtree_unit_test.c @@ -0,0 +1,368 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +#define NXT_RBTREE_COMPARISON 1 + + +typedef struct { + NXT_RBTREE_NODE (node); + uint32_t key; +} nxt_rbtree_test_t; + + +#if (NXT_RBTREE_COMPARISON) +static nxt_int_t nxt_rbtree_unit_test_comparison(nxt_rbtree_node_t *node1, + nxt_rbtree_node_t *node2); +#else +static void nxt_rbtree_unit_test_insertion(nxt_rbtree_node_t *node, + nxt_rbtree_node_t *new_node, nxt_rbtree_node_t *sentinel); +static nxt_rbtree_test_t *nxt_rbtree_unit_test_find(nxt_rbtree_t *tree, + uint32_t key); +#endif +static nxt_int_t nxt_rbtree_unit_test_compare(uint32_t key1, uint32_t key2); +static int nxt_cdecl nxt_rbtree_unit_test_sort_cmp(const void *one, + const void *two); + + +nxt_int_t +nxt_rbtree_unit_test(nxt_thread_t *thr, nxt_uint_t n) +{ + void *mark; + uint32_t key, *keys; + nxt_uint_t i; + nxt_nsec_t start, end; + nxt_rbtree_t tree; + nxt_rbtree_node_t *node; + nxt_rbtree_test_t *items, *item; + + nxt_thread_time_update(thr); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "rbtree unit test started: %ui", n); + +#if (NXT_RBTREE_COMPARISON) + nxt_rbtree_init(&tree, nxt_rbtree_unit_test_comparison, NULL); +#else + nxt_rbtree_init(&tree, NULL, nxt_rbtree_unit_test_insertion); +#endif + + mark = tree.sentinel.right; + + items = nxt_malloc(n * sizeof(nxt_rbtree_test_t)); + if (items == NULL) { + return NXT_ERROR; + } + + keys = nxt_malloc(n * sizeof(uint32_t)); + if (keys == NULL) { + nxt_free(keys); + return NXT_ERROR; + } + + key = 0; + + for (i = 0; i < n; i++) { + key = nxt_murmur_hash2(&key, sizeof(uint32_t)); + + keys[i] = key; + items[i].key = key; + } + + nxt_qsort(keys, n, sizeof(uint32_t), nxt_rbtree_unit_test_sort_cmp); + + nxt_thread_time_update(thr); + start = nxt_thread_monotonic_time(thr); + + for (i = 0; i < n; i++) { + nxt_rbtree_insert(&tree, &items[i].node); + } + + for (i = 0; i < n; i++) { +#if (NXT_RBTREE_COMPARISON) + node = nxt_rbtree_find(&tree, &items[i].node); + + if (node != (nxt_rbtree_node_t *) &items[i].node) { +#else + if (nxt_rbtree_unit_test_find(&tree, items[i].key) != &items[i]) { +#endif + nxt_log_alert(thr->log, "rbtree unit test failed: %08XD not found", + items[i].key); + goto fail; + } + } + + i = 0; + node = nxt_rbtree_min(&tree); + + while (nxt_rbtree_is_there_successor(&tree, node)) { + + item = (nxt_rbtree_test_t *) node; + + if (keys[i] != item->key) { + nxt_log_alert(thr->log, "rbtree unit test failed: %i: %08XD %08XD", + i, keys[i], item->key); + goto fail; + } + + i++; + node = nxt_rbtree_node_successor(&tree, node); + } + + if (i != n) { + nxt_log_alert(thr->log, "rbtree unit test failed: %ui", i); + goto fail; + } + + for (i = 0; i < n; i++) { + nxt_rbtree_delete(&tree, &items[i].node); + nxt_memset(&items[i], 0xA5, sizeof(nxt_rbtree_test_t)); + } + + nxt_thread_time_update(thr); + end = nxt_thread_monotonic_time(thr); + + if (!nxt_rbtree_is_empty(&tree)) { + nxt_log_alert(thr->log, "rbtree unit test failed: tree is not empty"); + goto fail; + } + + /* Check that the sentinel callback was not modified. */ + + if (mark != tree.sentinel.right) { + nxt_log_alert(thr->log, "rbtree sentinel unit test failed"); + goto fail; + } + + nxt_free(keys); + nxt_free(items); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "rbtree unit test passed %0.3fs", + (end - start) / 1000000000.0); + + return NXT_OK; + +fail: + + nxt_free(keys); + nxt_free(items); + + return NXT_ERROR; +} + + +#if (NXT_RBTREE_COMPARISON) + +static nxt_int_t +nxt_rbtree_unit_test_comparison(nxt_rbtree_node_t *node1, + nxt_rbtree_node_t *node2) +{ + nxt_rbtree_test_t *item1, *item2; + + item1 = (nxt_rbtree_test_t *) node1; + item2 = (nxt_rbtree_test_t *) node2; + + return nxt_rbtree_unit_test_compare(item1->key, item2->key); +} + +#else + +static void +nxt_rbtree_unit_test_insertion(nxt_rbtree_node_t *node, + nxt_rbtree_node_t *new_node, nxt_rbtree_node_t *sentinel) +{ + nxt_int_t n; + nxt_rbtree_node_t **child; + nxt_rbtree_test_t *item, *new_item; + + new_item = (nxt_rbtree_test_t *) new_node; + child = &node; + + while (*child != sentinel) { + node = *child; + + nxt_prefetch(node->left); + nxt_prefetch(node->right); + + item = (nxt_rbtree_test_t *) node; + + n = nxt_rbtree_unit_test_compare(new_item->key, item->key); + child = (n < 0) ? &node->left : &node->right; + } + + *child = new_node; + + new_node->parent = node; +} + + +static nxt_rbtree_test_t * +nxt_rbtree_unit_test_find(nxt_rbtree_t *tree, uint32_t key) +{ + nxt_int_t n; + nxt_rbtree_node_t *next, *sentinel; + nxt_rbtree_test_t *item; + + next = nxt_rbtree_root(tree); + sentinel = nxt_rbtree_sentinel(tree); + + while (next != sentinel) { + nxt_prefetch(next->left); + nxt_prefetch(next->right); + + item = (nxt_rbtree_test_t *) next; + + n = nxt_rbtree_unit_test_compare(key, item->key); + + if (n < 0) { + next = next->left; + + } else if (n > 0) { + next = next->right; + + } else { + return (nxt_rbtree_test_t *) next; + } + } + + return NULL; +} + +#endif + + +/* + * Subtraction cannot be used in these comparison functions because + * the key values are spread uniform in whole 0 .. 2^32 range but are + * not grouped around some value as timeout values are. + */ + +static nxt_int_t +nxt_rbtree_unit_test_compare(uint32_t key1, uint32_t key2) +{ + if (key1 < key2) { + return -1; + } + + if (key1 == key2) { + return 0; + } + + return 1; +} + + +static int nxt_cdecl +nxt_rbtree_unit_test_sort_cmp(const void *one, const void *two) +{ + const uint32_t *first, *second; + + first = one; + second = two; + + if (*first < *second) { + return -1; + } + + if (*first == *second) { + return 0; + } + + return 1; +} + + +#if (NXT_UNIT_TEST_RTDTSC) + +#define NXT_RBT_STEP (21 * nxt_pagesize / 10 / sizeof(nxt_rbtree_test_t)) + +static nxt_rbtree_t mb_tree; +static nxt_rbtree_test_t *mb_nodes; + + +nxt_int_t +nxt_rbtree_mb_start(nxt_thread_t *thr) +{ + uint32_t key; + uint64_t start, end; + nxt_uint_t i, n; + + n = NXT_RBT_STEP; + + mb_nodes = nxt_malloc(NXT_RBT_NODES * n * sizeof(nxt_rbtree_test_t)); + if (mb_nodes == NULL) { + return NXT_ERROR; + } + +#if (NXT_RBTREE_COMPARISON) + nxt_rbtree_init(&mb_tree, nxt_rbtree_unit_test_comparison, NULL); +#else + nxt_rbtree_init(&mb_tree, NULL, nxt_rbtree_unit_test_insertion); +#endif + + key = 0; + + for (i = 0; i < NXT_RBT_NODES; i++) { + key = nxt_murmur_hash2(&key, sizeof(uint32_t)); + mb_nodes[n * i].key = key; + } + + for (i = 0; i < NXT_RBT_NODES - 2; i++) { + nxt_rbtree_insert(&mb_tree, &mb_nodes[n * i].node); + } + + n *= (NXT_RBT_NODES - 2); + + start = nxt_rdtsc(); + nxt_rbtree_insert(&mb_tree, &mb_nodes[n].node); + end = nxt_rdtsc(); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "rbtree mb cached insert: %L cycles", end - start); + + return NXT_OK; +} + + +void +nxt_rbtree_mb_insert(nxt_thread_t *thr) +{ + uint64_t start, end; + nxt_uint_t n; + + n = NXT_RBT_STEP; + n *= (NXT_RBT_NODES - 1); + + start = nxt_rdtsc(); + nxt_rbtree_insert(&mb_tree, &mb_nodes[n].node); + end = nxt_rdtsc(); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "rbtree mb insert: %L cycles", end - start); +} + + +void +nxt_rbtree_mb_delete(nxt_thread_t *thr) +{ + uint64_t start, end; + nxt_uint_t n; + + n = NXT_RBT_STEP; + n *= (NXT_RBT_NODES / 4 + 1); + + start = nxt_rdtsc(); + nxt_rbtree_delete(&mb_tree, &mb_nodes[n].node); + end = nxt_rdtsc(); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, + "rbtree mb delete: %L cycles", end - start); + + nxt_free(mb_nodes); +} + +#endif diff --git a/test/nxt_sprintf_unit_test.c b/test/nxt_sprintf_unit_test.c new file mode 100644 index 00000000..02434791 --- /dev/null +++ b/test/nxt_sprintf_unit_test.c @@ -0,0 +1,70 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +typedef struct { + const char *format; + const char *test; + double number; +} nxt_sprintf_double_test_t; + + +static const nxt_sprintf_double_test_t double_test[] = +{ + { "%3.5f", "1234.56700", 1234.567 }, + { "%3.0f", "1234", 1234.567 }, + { "%f", "1234.567", 1234.567 }, + { "%f", "0.1", 0.1 }, + { "%f", "0.000001", 0.000001 }, + { "%f", "4503599627370495", 4503599627370495.0 }, +}; + + +static nxt_int_t +nxt_sprintf_test_double(u_char *buf, u_char *end, const char *fmt, + const char *test, double n) +{ + u_char *p; + + p = nxt_sprintf(buf, end, fmt, n); + *p = '\0'; + + return nxt_strcmp(buf, test); +} + + +nxt_int_t +nxt_sprintf_unit_test(nxt_thread_t *thr) +{ + nxt_int_t ret; + nxt_uint_t i; + u_char *end, buf[64]; + + nxt_thread_time_update(thr); + + end = buf + 64; + + for (i = 0; i < nxt_nitems(double_test); i++) { + + ret = nxt_sprintf_test_double(buf, end, double_test[i].format, + double_test[i].test, + double_test[i].number); + + if (ret == NXT_OK) { + continue; + } + + nxt_log_alert(thr->log, "nxt_sprintf(\"%s\") failed: \"%s\" vs \"%s\"", + double_test[i].format, double_test[i].test, buf); + + return NXT_ERROR; + } + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "nxt_sprintf() unit test passed"); + return NXT_OK; +} diff --git a/test/nxt_term_parse_unit_test.c b/test/nxt_term_parse_unit_test.c new file mode 100644 index 00000000..539d74b9 --- /dev/null +++ b/test/nxt_term_parse_unit_test.c @@ -0,0 +1,59 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +typedef struct { + nxt_str_t string; + nxt_bool_t is_sec; + nxt_int_t value; +} nxt_term_parse_test_t; + + +static const nxt_term_parse_test_t terms[] = { + { nxt_string("1y"), 1, 365 * 24 * 60 * 60 }, + { nxt_string("1w"), 1, 7 * 24 * 60 * 60 }, + { nxt_string("1w"), 0, 7 * 24 * 60 * 60 * 1000 }, + { nxt_string("1w 1d"), 0, 8 * 24 * 60 * 60 * 1000 }, + { nxt_string("1w d"), 0, -1 }, + { nxt_string("w"), 0, -1 }, + { nxt_string("1d 1w"), 0, -1 }, + { nxt_string("25d"), 0, -2 }, + { nxt_string("300"), 1, 300 }, + { nxt_string("300"), 0, 300000 }, + { nxt_string("300s"), 1, 300 }, + { nxt_string("300ms"), 0, 300 }, + { nxt_string("1y 1M 1w1d1h1m1s"), 1, + (((((365 + 30 + 7 + 1) * 24 + 1) * 60) + 1) * 60) + 1 }, +}; + + +nxt_int_t +nxt_term_parse_unit_test(nxt_thread_t *thr) +{ + nxt_int_t val; + nxt_uint_t i; + const nxt_str_t *s; + + nxt_thread_time_update(thr); + + for (i = 0; i < nxt_nitems(terms); i++) { + + s = &terms[i].string; + val = nxt_term_parse(s->data, s->len, terms[i].is_sec); + + if (val != terms[i].value) { + nxt_log_alert(thr->log, + "term parse unit test failed: \"%V\": %i %i", + s, terms[i].value, val); + return NXT_ERROR; + } + } + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "term parse unit test passed"); + return NXT_OK; +} diff --git a/test/nxt_utf8_file_name_test.c b/test/nxt_utf8_file_name_test.c new file mode 100644 index 00000000..a0ed91ec --- /dev/null +++ b/test/nxt_utf8_file_name_test.c @@ -0,0 +1,141 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +extern char **environ; + + +static nxt_int_t nxt_utf8_file_name_test(nxt_thread_t *thr); + + +int nxt_cdecl +main(int argc, char **argv) +{ + nxt_thread_t *thr; + + if (nxt_lib_start("utf8_file_name_test", argv, &environ) != NXT_OK) { + return 1; + } + + nxt_main_log.level = NXT_LOG_INFO; + + thr = nxt_thread(); + + if (nxt_utf8_file_name_test(thr) != NXT_OK) { + return 1; + } + + return 0; +} + + +static nxt_int_t +nxt_utf8_file_name_test(nxt_thread_t *thr) +{ + u_char *p, test[4], buf[32]; + ssize_t n; + uint32_t uc, lc; + nxt_int_t ret; + nxt_file_t uc_file, lc_file; + const u_char *pp; + nxt_file_name_t uc_name[10], lc_name[10]; + static const u_char utf8[4] = "UTF8"; + + nxt_thread_time_update(thr); + + uc_name[0] = 'u'; + uc_name[1] = 't'; + uc_name[2] = 'f'; + uc_name[3] = '8'; + uc_name[4] = '_'; + + lc_name[0] = 'u'; + lc_name[1] = 't'; + lc_name[2] = 'f'; + lc_name[3] = '8'; + lc_name[4] = '_'; + + nxt_memzero(&uc_file, sizeof(nxt_file_t)); + + uc_file.name = uc_name; + uc_file.log_level = NXT_LOG_ALERT; + + nxt_memzero(&lc_file, sizeof(nxt_file_t)); + + lc_file.name = lc_name; + + for (uc = 0x41; uc < 0x110000; uc++) { + + p = nxt_utf8_encode(&uc_name[5], uc); + + if (p == NULL) { + nxt_log_alert(thr->log, "nxt_utf8_encode(%05uxD) failed", uc); + return NXT_ERROR; + } + + *p = '\0'; + + pp = &uc_name[5]; + lc = nxt_utf8_lowcase(&pp, p); + + if (lc == 0xffffffff) { + nxt_log_alert(thr->log, "nxt_utf8_lowcase(%05uxD) failed: %05uxD", + uc, lc); + return NXT_ERROR; + } + + if (uc == lc) { + continue; + } + + p = nxt_utf8_encode(&lc_name[5], lc); + + if (p == NULL) { + nxt_log_alert(thr->log, "nxt_utf8_encode(%05uxD) failed", lc); + return NXT_ERROR; + } + + *p = '\0'; + + ret = nxt_file_open(&uc_file, NXT_FILE_WRONLY, NXT_FILE_TRUNCATE, + NXT_FILE_DEFAULT_ACCESS); + if (ret != NXT_OK) { + return NXT_ERROR; + } + + if (nxt_file_write(&uc_file, utf8, 4, 0) != 4) { + return NXT_ERROR; + } + + nxt_file_close(&uc_file); + + ret = nxt_file_open(&lc_file, NXT_FILE_RDONLY, NXT_FILE_OPEN, + NXT_FILE_DEFAULT_ACCESS); + + if (ret == NXT_OK) { + n = nxt_file_read(&lc_file, test, 4, 0); + + nxt_file_close(&lc_file); + + if (n != 4 || nxt_memcmp(utf8, test, 4) != 0) { + nxt_log_alert(thr->log, "nxt_file_read() mismatch"); + + nxt_file_delete(lc_file.name); + } + + p = nxt_sprintf(buf, buf + 32, "%04uXD; C; %04uXD;%n", uc, lc); + + nxt_fd_write(nxt_stdout, buf, p - buf); + } + + nxt_file_delete(uc_file.name); + } + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "utf8 file name unit test passed"); + return NXT_OK; +} diff --git a/test/nxt_utf8_unit_test.c b/test/nxt_utf8_unit_test.c new file mode 100644 index 00000000..3bba9681 --- /dev/null +++ b/test/nxt_utf8_unit_test.c @@ -0,0 +1,190 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +#define NXT_UTF8_START_TEST 0xc2 +//#define NXT_UTF8_START_TEST 0 + + +static u_char invalid[] = { + + /* Invalid first byte less than 0xc2. */ + 1, 0x80, 0x00, 0x00, 0x00, + 1, 0xc0, 0x00, 0x00, 0x00, + 2, 0xc0, 0x00, 0x00, 0x00, + 3, 0xc0, 0x00, 0x00, 0x00, + 4, 0xc0, 0x00, 0x00, 0x00, + + /* Invalid 0x0x110000 value. */ + 4, 0xf4, 0x90, 0x80, 0x80, + + /* Incomplete length. */ + 2, 0xe0, 0xaf, 0xb5, 0x00, + + /* Overlong values. */ + 2, 0xc0, 0x80, 0x00, 0x00, + 2, 0xc1, 0xb3, 0x00, 0x00, + 3, 0xe0, 0x80, 0x80, 0x00, + 3, 0xe0, 0x81, 0xb3, 0x00, + 3, 0xe0, 0x90, 0x9a, 0x00, + 4, 0xf0, 0x80, 0x8a, 0x80, + 4, 0xf0, 0x80, 0x81, 0xb3, + 4, 0xf0, 0x80, 0xaf, 0xb5, +}; + + +static nxt_int_t +nxt_utf8_overlong(nxt_thread_t *thr, u_char *overlong, size_t len) +{ + u_char *p, utf8[4]; + size_t size; + uint32_t u, d; + nxt_uint_t i; + const u_char *pp; + + pp = overlong; + + d = nxt_utf8_decode(&pp, overlong + len); + + len = pp - overlong; + + if (d != 0xffffffff) { + p = nxt_utf8_encode(utf8, d); + + size = (p != NULL) ? p - utf8 : 0; + + if (len != size || nxt_memcmp(overlong, utf8, size) != 0) { + + u = 0; + for (i = 0; i < len; i++) { + u = (u << 8) + overlong[i]; + } + + nxt_log_alert(thr->log, + "nxt_utf8_decode(%05uxD, %uz) failed: %05uxD, %uz", + u, len, d, size); + + return NXT_ERROR; + } + } + + return NXT_OK; +} + + +nxt_int_t +nxt_utf8_unit_test(nxt_thread_t *thr) +{ + u_char *p, utf8[4]; + size_t len; + int32_t n; + uint32_t u, d; + nxt_uint_t i, k, l, m; + const u_char *pp; + + nxt_thread_time_update(thr); + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "utf8 unit test started"); + + /* Test valid UTF-8. */ + + for (u = 0; u < 0x110000; u++) { + + p = nxt_utf8_encode(utf8, u); + + if (p == NULL) { + nxt_log_alert(thr->log, "nxt_utf8_encode(%05uxD) failed", u); + return NXT_ERROR; + } + + pp = utf8; + + d = nxt_utf8_decode(&pp, p); + + if (u != d) { + nxt_log_alert(thr->log, "nxt_utf8_decode(%05uxD) failed: %05uxD", + u, d); + return NXT_ERROR; + } + } + + /* Test some invalid UTF-8. */ + + for (i = 0; i < sizeof(invalid); i += 5) { + + len = invalid[i]; + utf8[0] = invalid[i + 1]; + utf8[1] = invalid[i + 2]; + utf8[2] = invalid[i + 3]; + utf8[3] = invalid[i + 4]; + + pp = utf8; + + d = nxt_utf8_decode(&pp, utf8 + len); + + if (d != 0xffffffff) { + + u = 0; + for (i = 0; i < len; i++) { + u = (u << 8) + utf8[i]; + } + + nxt_log_alert(thr->log, + "nxt_utf8_decode(%05uxD, %uz) failed: %05uxD", + u, len, d); + return NXT_ERROR; + } + } + + /* Test all overlong UTF-8. */ + + for (i = NXT_UTF8_START_TEST; i < 256; i++) { + utf8[0] = i; + + if (nxt_utf8_overlong(thr, utf8, 1) != NXT_OK) { + return NXT_ERROR; + } + + for (k = 0; k < 256; k++) { + utf8[1] = k; + + if (nxt_utf8_overlong(thr, utf8, 2) != NXT_OK) { + return NXT_ERROR; + } + + for (l = 0; l < 256; l++) { + utf8[2] = l; + + if (nxt_utf8_overlong(thr, utf8, 3) != NXT_OK) { + return NXT_ERROR; + } + + for (m = 0; m < 256; m++) { + utf8[3] = m; + + if (nxt_utf8_overlong(thr, utf8, 4) != NXT_OK) { + return NXT_ERROR; + } + } + } + } + } + + n = nxt_utf8_casecmp((u_char *) "ABC АБВ ΑΒΓ", + (u_char *) "abc абв αβγ", + sizeof("ABC АБВ ΑΒΓ") - 1, + sizeof("abc абв αβγ") - 1); + + if (n != 0) { + nxt_log_alert(thr->log, "nxt_utf8_casecmp() failed"); + return NXT_ERROR; + } + + nxt_log_error(NXT_LOG_NOTICE, thr->log, "utf8 unit test passed"); + return NXT_OK; +} |