diff -r 279dd3ca7a77 -r 5fbb9efebe4a test/logging_tests.c --- a/test/logging_tests.c Tue May 06 12:30:12 2014 +0200 +++ b/test/logging_tests.c Tue May 06 14:22:08 2014 +0200 @@ -27,27 +27,79 @@ */ #include "logging_tests.h" +#include -UCX_TEST(test_ucx_logger_log) { - char buffer[100]; +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_logger_info(logger, "allright"); + 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 == 4, + "incorrect number of registered log levels"); + + int level = UCX_LOGGER_ERROR; + UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level), + "[ERROR]") == 0, "invalid error level"); + level = UCX_LOGGER_WARN; + UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level), + "[WARNING]") == 0, "invalid warning level"); + level = UCX_LOGGER_INFO; + UCX_TEST_ASSERT(strcmp(ucx_map_int_get(logger->levels, level), + "[INFO]") == 0, "invalid info level"); + level = UCX_LOGGER_TRACE; + UCX_TEST_ASSERT(strcmp(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 = "%F:"; + + UCX_TEST_BEGIN + const uint line1 = __LINE__; ucx_logger_info(logger, "allright"); + ucx_logger_trace(logger, "dont log this!"); - ucx_logger_error(logger, "error %d!", 42); + + logger->mask |= UCX_LOGGER_TIMESTAMP; + time_t now = time(NULL); + char timestr[13]; + strftime(timestr, 12, "%F:", localtime(&now)); + const uint line2 = __LINE__; ucx_logger_error(logger, "error %d!", 42); + fseek(stream, 0, SEEK_SET); size_t r = fread(buffer, 1, 100, stream); - // TODO: completely rewrite this test - - size_t expected_length = 76; - UCX_TEST_ASSERT(r == expected_length && strncmp(buffer, - "[INFO] logging_tests.c:39 - allright\n" - "[ERROR] logging_tests.c:41 - error 42!\n", expected_length) == 0, "incorrect logs"); + const size_t expected_length = 87; + char expected[expected_length+1]; + 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