diff options
author | Andrey Zelenkov <zelenkov@nginx.com> | 2017-11-21 18:55:28 +0300 |
---|---|---|
committer | Andrey Zelenkov <zelenkov@nginx.com> | 2017-11-21 18:55:28 +0300 |
commit | 78a77c3e38593a5dd1ba0970d036ed5f5100f88d (patch) | |
tree | 3011f02b551c623a926e85ed1532986b947da300 /src/test/nxt_sprintf_test.c | |
parent | 89a1c66dd0c396cda30a960e790817d28dc9a2de (diff) | |
download | unit-78a77c3e38593a5dd1ba0970d036ed5f5100f88d.tar.gz unit-78a77c3e38593a5dd1ba0970d036ed5f5100f88d.tar.bz2 |
Tests: move existing tests to "src" folder.
Diffstat (limited to 'src/test/nxt_sprintf_test.c')
-rw-r--r-- | src/test/nxt_sprintf_test.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/test/nxt_sprintf_test.c b/src/test/nxt_sprintf_test.c new file mode 100644 index 00000000..7c6e2019 --- /dev/null +++ b/src/test/nxt_sprintf_test.c @@ -0,0 +1,71 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> +#include "nxt_tests.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_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() test passed"); + return NXT_OK; +} |