test/logging_tests.c

Thu, 11 Jul 2019 10:11:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 11 Jul 2019 10:11:43 +0200
branch
feature/array
changeset 341
b9715d7317c1
parent 296
e325716a442c
permissions
-rw-r--r--

resets revision number

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "logging_tests.h"
#include <time.h>

UCX_TEST(test_ucx_logger_new) {
    
    FILE *stream = tmpfile();
    UcxLogger *logger = ucx_logger_new(stream,
            UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL);
    
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(logger->stream == stream, "stream not set");
    UCX_TEST_ASSERT(logger->mask == (UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL),
        "mask not set");
    UCX_TEST_ASSERT(logger->level == UCX_LOGGER_INFO,
        "level not set");
    UCX_TEST_ASSERT(logger->writer == (write_func)fwrite,
        "writer not set to fwrite");
    UCX_TEST_ASSERT(strcmp(logger->dateformat, "%F %T %z ") == 0,
        "date format not set to \"%F %T %z\"");
    
    UCX_TEST_ASSERT(logger->levels->count == 5,
        "incorrect number of registered log levels");

    int level = UCX_LOGGER_ERROR;
    UCX_TEST_ASSERT(strcmp((char*)ucx_map_int_get(logger->levels, level),
        "[ERROR]") == 0, "invalid error level");
    level = UCX_LOGGER_WARN;
    UCX_TEST_ASSERT(strcmp((char*)ucx_map_int_get(logger->levels, level),
        "[WARNING]") == 0, "invalid warning level");
    level = UCX_LOGGER_DEBUG;
    UCX_TEST_ASSERT(strcmp((char*)ucx_map_int_get(logger->levels, level),
        "[DEBUG]") == 0, "invalid debug level");
    level = UCX_LOGGER_INFO;
    UCX_TEST_ASSERT(strcmp((char*)ucx_map_int_get(logger->levels, level),
        "[INFO]") == 0, "invalid info level");
    level = UCX_LOGGER_TRACE;
    UCX_TEST_ASSERT(strcmp((char*)ucx_map_int_get(logger->levels, level),
        "[TRACE]") == 0, "invalid trace level");

    UCX_TEST_END
    
    fclose(stream);
    ucx_logger_free(logger);
}

UCX_TEST(test_ucx_logger_log) {
    char buffer[100];
    
    FILE *stream = tmpfile();

    UcxLogger *logger = ucx_logger_new(stream,
            UCX_LOGGER_INFO, UCX_LOGGER_SOURCE | UCX_LOGGER_LEVEL);
    logger->dateformat = (char*) "%Y-%m-%d:";
    
    UCX_TEST_BEGIN
    const unsigned int line1 = __LINE__; ucx_logger_info(logger, "allright");
    
    ucx_logger_trace(logger, "dont log this!");
    
    logger->mask |= UCX_LOGGER_TIMESTAMP;
    time_t now = time(NULL);
    char timestr[13];
    strftime(timestr, 12, "%Y-%m-%d:", localtime(&now));
    const unsigned int line2 = __LINE__; ucx_logger_error(logger, "error %d!", 42);
    
    fseek(stream, 0, SEEK_SET);
    size_t r = fread(buffer, 1, 100, stream);
    
    const size_t expected_length = 87;
    char expected[88];
    snprintf(expected, expected_length+1,
        "[INFO] logging_tests.c:%u - allright\n"
        "[ERROR] %slogging_tests.c:%u - error 42!\n", line1, timestr, line2);

    UCX_TEST_ASSERT(r == expected_length, "incorrect log length");
    UCX_TEST_ASSERT(strncmp(buffer, expected, expected_length) == 0,
        "incorrect logs");

    UCX_TEST_END

    ucx_logger_free(logger);
    fclose(stream);
}

mercurial