test/logging_tests.c

changeset 390
d345541018fa
parent 389
92e482410453
child 391
f094a53c1178
     1.1 --- a/test/logging_tests.c	Mon Dec 30 09:54:10 2019 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,111 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
     1.8 - *
     1.9 - * Redistribution and use in source and binary forms, with or without
    1.10 - * modification, are permitted provided that the following conditions are met:
    1.11 - *
    1.12 - *   1. Redistributions of source code must retain the above copyright
    1.13 - *      notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *   2. Redistributions in binary form must reproduce the above copyright
    1.16 - *      notice, this list of conditions and the following disclaimer in the
    1.17 - *      documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 - * POSSIBILITY OF SUCH DAMAGE.
    1.30 - */
    1.31 -
    1.32 -#include "logging_tests.h"
    1.33 -#include <time.h>
    1.34 -
    1.35 -UCX_TEST(test_ucx_logger_new) {
    1.36 -    
    1.37 -    FILE *stream = tmpfile();
    1.38 -    UcxLogger *logger = ucx_logger_new(stream,
    1.39 -            UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL);
    1.40 -    
    1.41 -    UCX_TEST_BEGIN
    1.42 -    UCX_TEST_ASSERT(logger->stream == stream, "stream not set");
    1.43 -    UCX_TEST_ASSERT(logger->mask == (UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL),
    1.44 -        "mask not set");
    1.45 -    UCX_TEST_ASSERT(logger->level == UCX_LOGGER_INFO,
    1.46 -        "level not set");
    1.47 -    UCX_TEST_ASSERT(logger->writer == (write_func)fwrite,
    1.48 -        "writer not set to fwrite");
    1.49 -    UCX_TEST_ASSERT(strcmp(logger->dateformat, "%F %T %z ") == 0,
    1.50 -        "date format not set to \"%F %T %z\"");
    1.51 -    
    1.52 -    UCX_TEST_ASSERT(logger->levels->count == 5,
    1.53 -        "incorrect number of registered log levels");
    1.54 -
    1.55 -    int level = UCX_LOGGER_ERROR;
    1.56 -    UCX_TEST_ASSERT(strcmp((char*)ucx_map_int_get(logger->levels, level),
    1.57 -        "[ERROR]") == 0, "invalid error level");
    1.58 -    level = UCX_LOGGER_WARN;
    1.59 -    UCX_TEST_ASSERT(strcmp((char*)ucx_map_int_get(logger->levels, level),
    1.60 -        "[WARNING]") == 0, "invalid warning level");
    1.61 -    level = UCX_LOGGER_DEBUG;
    1.62 -    UCX_TEST_ASSERT(strcmp((char*)ucx_map_int_get(logger->levels, level),
    1.63 -        "[DEBUG]") == 0, "invalid debug level");
    1.64 -    level = UCX_LOGGER_INFO;
    1.65 -    UCX_TEST_ASSERT(strcmp((char*)ucx_map_int_get(logger->levels, level),
    1.66 -        "[INFO]") == 0, "invalid info level");
    1.67 -    level = UCX_LOGGER_TRACE;
    1.68 -    UCX_TEST_ASSERT(strcmp((char*)ucx_map_int_get(logger->levels, level),
    1.69 -        "[TRACE]") == 0, "invalid trace level");
    1.70 -
    1.71 -    UCX_TEST_END
    1.72 -    
    1.73 -    fclose(stream);
    1.74 -    ucx_logger_free(logger);
    1.75 -}
    1.76 -
    1.77 -UCX_TEST(test_ucx_logger_log) {
    1.78 -    char buffer[100];
    1.79 -    
    1.80 -    FILE *stream = tmpfile();
    1.81 -
    1.82 -    UcxLogger *logger = ucx_logger_new(stream,
    1.83 -            UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL);
    1.84 -    logger->dateformat = (char*) "%Y-%m-%d:";
    1.85 -    
    1.86 -    UCX_TEST_BEGIN
    1.87 -    const unsigned int line1 = __LINE__; ucx_logger_info(logger, "allright");
    1.88 -    
    1.89 -    ucx_logger_trace(logger, "dont log this!");
    1.90 -    
    1.91 -    logger->mask |= UCX_LOGGER_TIMESTAMP;
    1.92 -    time_t now = time(NULL);
    1.93 -    char timestr[13];
    1.94 -    strftime(timestr, 12, "%Y-%m-%d:", localtime(&now));
    1.95 -    const unsigned int line2 = __LINE__; ucx_logger_error(logger, "error %d!", 42);
    1.96 -    
    1.97 -    fseek(stream, 0, SEEK_SET);
    1.98 -    size_t r = fread(buffer, 1, 100, stream);
    1.99 -    
   1.100 -    const size_t expected_length = 87;
   1.101 -    char expected[88];
   1.102 -    snprintf(expected, expected_length+1,
   1.103 -        "[INFO] logging_tests.c:%u - allright\n"
   1.104 -        "[ERROR] %slogging_tests.c:%u - error 42!\n", line1, timestr, line2);
   1.105 -
   1.106 -    UCX_TEST_ASSERT(r == expected_length, "incorrect log length");
   1.107 -    UCX_TEST_ASSERT(strncmp(buffer, expected, expected_length) == 0,
   1.108 -        "incorrect logs");
   1.109 -
   1.110 -    UCX_TEST_END
   1.111 -
   1.112 -    ucx_logger_free(logger);
   1.113 -    fclose(stream);
   1.114 -}

mercurial