improved logger tests

Tue, 06 May 2014 14:22:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 06 May 2014 14:22:08 +0200
changeset 170
5fbb9efebe4a
parent 169
279dd3ca7a77
child 171
49cebb8eceff

improved logger tests

test/logging_tests.c file | annotate | diff | comparison | revisions
test/logging_tests.h file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
ucx/logging.h file | annotate | diff | comparison | revisions
     1.1 --- a/test/logging_tests.c	Tue May 06 12:30:12 2014 +0200
     1.2 +++ b/test/logging_tests.c	Tue May 06 14:22:08 2014 +0200
     1.3 @@ -27,27 +27,79 @@
     1.4   */
     1.5  
     1.6  #include "logging_tests.h"
     1.7 +#include <time.h>
     1.8 +
     1.9 +UCX_TEST(test_ucx_logger_new) {
    1.10 +    
    1.11 +    FILE *stream = tmpfile();
    1.12 +    UcxLogger *logger = ucx_logger_new(stream,
    1.13 +            UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL);
    1.14 +    
    1.15 +    UCX_TEST_BEGIN
    1.16 +    UCX_TEST_ASSERT(logger->stream == stream, "stream not set");
    1.17 +    UCX_TEST_ASSERT(logger->mask == (UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL),
    1.18 +        "mask not set");
    1.19 +    UCX_TEST_ASSERT(logger->level == UCX_LOGGER_INFO,
    1.20 +        "level not set");
    1.21 +    UCX_TEST_ASSERT(logger->writer == (write_func)fwrite,
    1.22 +        "writer not set to fwrite");
    1.23 +    UCX_TEST_ASSERT(strcmp(logger->dateformat, "%F %T %z ") == 0,
    1.24 +        "date format not set to \"%F %T %z\"");
    1.25 +    
    1.26 +    UCX_TEST_ASSERT(logger->levels->count == 4,
    1.27 +        "incorrect number of registered log levels");
    1.28 +
    1.29 +    int level = UCX_LOGGER_ERROR;
    1.30 +    UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
    1.31 +        "[ERROR]") == 0, "invalid error level");
    1.32 +    level = UCX_LOGGER_WARN;
    1.33 +    UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
    1.34 +        "[WARNING]") == 0, "invalid warning level");
    1.35 +    level = UCX_LOGGER_INFO;
    1.36 +    UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
    1.37 +        "[INFO]") == 0, "invalid info level");
    1.38 +    level = UCX_LOGGER_TRACE;
    1.39 +    UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level),
    1.40 +        "[TRACE]") == 0, "invalid trace level");
    1.41 +
    1.42 +    UCX_TEST_END
    1.43 +    
    1.44 +    fclose(stream);
    1.45 +    ucx_logger_free(logger);
    1.46 +}
    1.47  
    1.48  UCX_TEST(test_ucx_logger_log) {
    1.49      char buffer[100];
    1.50 +    
    1.51      FILE *stream = tmpfile();
    1.52  
    1.53      UcxLogger *logger = ucx_logger_new(stream,
    1.54              UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL);
    1.55 +    logger->dateformat = "%F:";
    1.56      
    1.57      UCX_TEST_BEGIN
    1.58 -    ucx_logger_info(logger, "allright");
    1.59 +    const uint line1 = __LINE__; ucx_logger_info(logger, "allright");
    1.60 +    
    1.61      ucx_logger_trace(logger, "dont log this!");
    1.62 -    ucx_logger_error(logger, "error %d!", 42);
    1.63 +    
    1.64 +    logger->mask |= UCX_LOGGER_TIMESTAMP;
    1.65 +    time_t now = time(NULL);
    1.66 +    char timestr[13];
    1.67 +    strftime(timestr, 12, "%F:", localtime(&now));
    1.68 +    const uint line2 = __LINE__; ucx_logger_error(logger, "error %d!", 42);
    1.69 +    
    1.70      fseek(stream, 0, SEEK_SET);
    1.71      size_t r = fread(buffer, 1, 100, stream);
    1.72      
    1.73 -    // TODO: completely rewrite this test
    1.74 -    
    1.75 -    size_t expected_length = 76;
    1.76 -    UCX_TEST_ASSERT(r == expected_length && strncmp(buffer,
    1.77 -            "[INFO] logging_tests.c:39 - allright\n"
    1.78 -            "[ERROR] logging_tests.c:41 - error 42!\n", expected_length) == 0, "incorrect logs");
    1.79 +    const size_t expected_length = 87;
    1.80 +    char expected[expected_length+1];
    1.81 +    snprintf(expected, expected_length+1,
    1.82 +        "[INFO] logging_tests.c:%u - allright\n"
    1.83 +        "[ERROR] %slogging_tests.c:%u - error 42!\n", line1, timestr, line2);
    1.84 +
    1.85 +    UCX_TEST_ASSERT(r == expected_length, "incorrect log length");
    1.86 +    UCX_TEST_ASSERT(strncmp(buffer, expected, expected_length) == 0,
    1.87 +        "incorrect logs");
    1.88  
    1.89      UCX_TEST_END
    1.90  
     2.1 --- a/test/logging_tests.h	Tue May 06 12:30:12 2014 +0200
     2.2 +++ b/test/logging_tests.h	Tue May 06 14:22:08 2014 +0200
     2.3 @@ -36,6 +36,7 @@
     2.4  extern "C" {
     2.5  #endif
     2.6  
     2.7 +UCX_TEST(test_ucx_logger_new);
     2.8  UCX_TEST(test_ucx_logger_log);
     2.9  
    2.10  #ifdef	__cplusplus
     3.1 --- a/test/main.c	Tue May 06 12:30:12 2014 +0200
     3.2 +++ b/test/main.c	Tue May 06 14:22:08 2014 +0200
     3.3 @@ -127,6 +127,7 @@
     3.4          ucx_test_register(suite, test_sstrprefixsuffix);
     3.5          
     3.6          /* UcxLogger Tests */
     3.7 +        ucx_test_register(suite, test_ucx_logger_new);
     3.8          ucx_test_register(suite, test_ucx_logger_log);
     3.9          
    3.10          /* UcxList Tests */
     4.1 --- a/ucx/logging.h	Tue May 06 12:30:12 2014 +0200
     4.2 +++ b/ucx/logging.h	Tue May 06 14:22:08 2014 +0200
     4.3 @@ -97,7 +97,7 @@
     4.4      write_func writer;
     4.5  
     4.6      /**
     4.7 -     * The date format for timestamp outputs
     4.8 +     * The date format for timestamp outputs including the delimiter
     4.9       * (default: <code>"%F %T %z "</code>).
    4.10       * @see UCX_LOGGER_TIMESTAMP
    4.11       */

mercurial