configure.ac | 1 + osaf/libs/core/common/Makefile.am | 2 +- osaf/libs/core/common/tests/Makefile.am | 51 + osaf/libs/core/common/tests/mock_clock_gettime.cc | 45 + osaf/libs/core/common/tests/mock_clock_gettime.h | 33 + osaf/libs/core/common/tests/mock_clock_nanosleep.cc | 66 ++ osaf/libs/core/common/tests/mock_clock_nanosleep.h | 33 + osaf/libs/core/common/tests/mock_syslog.cc | 23 + osaf/libs/core/common/tests/mock_syslog.h | 23 + osaf/libs/core/common/tests/osaf_clock_gettime_test.cc | 73 ++ osaf/libs/core/common/tests/osaf_nanosleep_test.cc | 81 ++ osaf/libs/core/common/tests/osaf_normalize_timespec_test.cc | 81 ++ osaf/libs/core/common/tests/osaf_time_test.cc | 390 ++++++++++++ osaf/libs/core/common/tests/osaf_timespec_add_test.cc | 62 + osaf/libs/core/common/tests/osaf_timespec_compare_test.cc | 75 ++ osaf/libs/core/common/tests/osaf_timespec_convert_test.cc | 100 +++ osaf/libs/core/common/tests/osaf_timespec_subtract_test.cc | 62 + 17 files changed, 1200 insertions(+), 1 deletions(-)
diff --git a/configure.ac b/configure.ac --- a/configure.ac +++ b/configure.ac @@ -705,6 +705,7 @@ AC_CONFIG_FILES([ osaf/libs/core/include/Makefile osaf/libs/core/common/Makefile osaf/libs/core/common/include/Makefile + osaf/libs/core/common/tests/Makefile osaf/libs/core/leap/Makefile osaf/libs/core/leap/include/Makefile osaf/libs/core/mbcsv/Makefile diff --git a/osaf/libs/core/common/Makefile.am b/osaf/libs/core/common/Makefile.am --- a/osaf/libs/core/common/Makefile.am +++ b/osaf/libs/core/common/Makefile.am @@ -18,7 +18,7 @@ include $(top_srcdir)/Makefile.common MAINTAINERCLEANFILES = Makefile.in -SUBDIRS = include +SUBDIRS = include tests noinst_LTLIBRARIES = libopensaf_common.la diff --git a/osaf/libs/core/common/tests/Makefile.am b/osaf/libs/core/common/tests/Makefile.am new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/Makefile.am @@ -0,0 +1,51 @@ +# -*- OpenSAF -*- +# +# (C) Copyright 2015 The OpenSAF Foundation +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed +# under the GNU Lesser General Public License Version 2.1, February 1999. +# The complete license can be accessed from the following location: +# http://opensource.org/licenses/lgpl-license.php +# See the Copying file included with the OpenSAF distribution for full +# licensing terms. +# +# Author(s): Ericsson AB +# + +include $(top_srcdir)/Makefile.common + +MAINTAINERCLEANFILES = Makefile.in + +TESTS = core_common_test + +check_PROGRAMS = $(TESTS) + +core_common_test_CXXFLAGS =$(AM_CXXFLAGS) + +core_common_test_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + -I$(top_srcdir)/osaf/libs/core/common/include \ + -I$(GTEST_DIR)/include + +core_common_test_LDFLAGS = \ + -pthread -lrt \ + $(top_builddir)/osaf/libs/core/common/libopensaf_common_la-osaf_time.o \ + $(top_builddir)/osaf/libs/core/common/libopensaf_common_la-osaf_utility.o + +core_common_test_SOURCES = \ + osaf_clock_gettime_test.cc \ + osaf_nanosleep_test.cc \ + osaf_normalize_timespec_test.cc \ + osaf_timespec_add_test.cc \ + osaf_timespec_compare_test.cc \ + osaf_timespec_convert_test.cc \ + osaf_timespec_subtract_test.cc \ + mock_clock_gettime.cc \ + mock_clock_nanosleep.cc \ + mock_syslog.cc + +core_common_test_LDADD = \ + $(GTEST_DIR)/lib/libgtest.la \ + $(GTEST_DIR)/lib/libgtest_main.la diff --git a/osaf/libs/core/common/tests/mock_clock_gettime.cc b/osaf/libs/core/common/tests/mock_clock_gettime.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/mock_clock_gettime.cc @@ -0,0 +1,45 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#include "mock_clock_gettime.h" +#include <cerrno> +#include "osaf_time.h" + +timespec realtime_clock; +timespec monotonic_clock; +MockClockGettime mock_clock_gettime; + +int clock_gettime(clockid_t clock_id, struct timespec* tp) { + struct timespec* clock_source = nullptr; + if (clock_id == CLOCK_REALTIME) { + clock_source = &realtime_clock; + } else if (clock_id == CLOCK_MONOTONIC) { + clock_source = &monotonic_clock; + } else { + errno = EINVAL; + return -1; + } + if (tp == nullptr) { + errno = EFAULT; + return -1; + } + if (tp != nullptr && clock_source != nullptr) *tp = *clock_source; + osaf_timespec_add(&realtime_clock, &mock_clock_gettime.execution_time, &realtime_clock); + osaf_timespec_add(&monotonic_clock, &mock_clock_gettime.execution_time, &monotonic_clock); + if (mock_clock_gettime.return_value < 0) errno = mock_clock_gettime.errno_value; + return mock_clock_gettime.return_value; +} diff --git a/osaf/libs/core/common/tests/mock_clock_gettime.h b/osaf/libs/core/common/tests/mock_clock_gettime.h new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/mock_clock_gettime.h @@ -0,0 +1,33 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#ifndef OPENSAF_BASE_TESTS_MOCK_CLOCK_GETTIME_H_ +#define OPENSAF_BASE_TESTS_MOCK_CLOCK_GETTIME_H_ + +#include <time.h> + +struct MockClockGettime { + struct timespec execution_time; + int return_value; + int errno_value; +}; + +extern timespec realtime_clock; +extern timespec monotonic_clock; +extern MockClockGettime mock_clock_gettime; + +#endif diff --git a/osaf/libs/core/common/tests/mock_clock_nanosleep.cc b/osaf/libs/core/common/tests/mock_clock_nanosleep.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/mock_clock_nanosleep.cc @@ -0,0 +1,66 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#include "mock_clock_nanosleep.h" +#include <cerrno> +#include "osaf_time.h" +#include "mock_clock_gettime.h" + +int current_nanosleep_index = 0; +int number_of_nanosleep_instances = 1; +MockClockNanosleep mock_clock_nanosleep[kMockClockNanosleepInstances]; + +int clock_nanosleep(clockid_t clock_id, int flags, + const struct timespec *request, + struct timespec *remain) { + MockClockNanosleep& mock_instance = mock_clock_nanosleep[current_nanosleep_index++]; + if (current_nanosleep_index >= number_of_nanosleep_instances) current_nanosleep_index = 0; + struct timespec* clock_source = nullptr; + if (clock_id == CLOCK_REALTIME) { + clock_source = &realtime_clock; + } else if (clock_id == CLOCK_MONOTONIC) { + clock_source = &monotonic_clock; + } else { + errno = EINVAL; + return -1; + } + if (request->tv_sec < 0 || request->tv_nsec < 0 || request->tv_nsec >= kNanosPerSec) { + errno = EINVAL; + return -1; + } + struct timespec sleep_duration = kZeroSeconds; + if (flags == 0) { + sleep_duration = *request; + } else if (flags == TIMER_ABSTIME) { + if (osaf_timespec_compare(request, clock_source) >= 0) { + osaf_timespec_subtract(request, clock_source, &sleep_duration); + } + } else { + errno = EINVAL; + return -1; + } + if (mock_instance.return_value < 0 && mock_instance.errno_value == EINTR) { + osaf_nanos_to_timespec(osaf_timespec_to_nanos(&sleep_duration) / 2, &sleep_duration); + } + if (flags == 0 && remain != nullptr) { + osaf_timespec_subtract(request, &sleep_duration, remain); + } + osaf_timespec_add(&realtime_clock, &sleep_duration, &realtime_clock); + osaf_timespec_add(&monotonic_clock, &sleep_duration, &monotonic_clock); + if (mock_instance.return_value < 0) errno = mock_instance.errno_value; + return mock_instance.return_value; +} diff --git a/osaf/libs/core/common/tests/mock_clock_nanosleep.h b/osaf/libs/core/common/tests/mock_clock_nanosleep.h new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/mock_clock_nanosleep.h @@ -0,0 +1,33 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#ifndef OPENSAF_BASE_TESTS_MOCK_CLOCK_NANOSLEEP_H_ +#define OPENSAF_BASE_TESTS_MOCK_CLOCK_NANOSLEEP_H_ + +#include <time.h> + +struct MockClockNanosleep { + int return_value; + int errno_value; +}; + +static const int kMockClockNanosleepInstances = 3; +extern int current_nanosleep_index; +extern int number_of_nanosleep_instances; +extern MockClockNanosleep mock_clock_nanosleep[kMockClockNanosleepInstances]; + +#endif diff --git a/osaf/libs/core/common/tests/mock_syslog.cc b/osaf/libs/core/common/tests/mock_syslog.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/mock_syslog.cc @@ -0,0 +1,23 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#include "mock_syslog.h" + +void syslog(int priority, const char *format, ...) { + (void) priority; + (void) format; +} diff --git a/osaf/libs/core/common/tests/mock_syslog.h b/osaf/libs/core/common/tests/mock_syslog.h new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/mock_syslog.h @@ -0,0 +1,23 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#ifndef OPENSAF_BASE_TESTS_MOCK_SYSLOG_H_ +#define OPENSAF_BASE_TESTS_MOCK_SYSLOG_H_ + +#include <syslog.h> + +#endif diff --git a/osaf/libs/core/common/tests/osaf_clock_gettime_test.cc b/osaf/libs/core/common/tests/osaf_clock_gettime_test.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/osaf_clock_gettime_test.cc @@ -0,0 +1,73 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#include <cerrno> +#include "osaf_time.h" +#include "osaf_utility.h" +#include "mock_clock_gettime.h" +#include "gtest/gtest.h" + +TEST(OsafClockGettime, ReadRealtimeClock) { + time_t sec = 123456789; + long nsec = 987654321; + realtime_clock = { sec, nsec }; + monotonic_clock = { 0, 0 }; + mock_clock_gettime.return_value = 0; + + struct timespec ts = { 0, 0 }; + osaf_clock_gettime(CLOCK_REALTIME, &ts); + + EXPECT_EQ(ts.tv_sec, sec); + EXPECT_EQ(ts.tv_nsec, nsec); +} + +TEST(OsafClockGettime, ReadMonotonicClock) { + time_t sec = 212121212; + long nsec = 565656565; + realtime_clock = { 0, 0 }; + monotonic_clock = { sec, nsec }; + mock_clock_gettime.return_value = 0; + + struct timespec ts = { 0, 0 }; + osaf_clock_gettime(CLOCK_MONOTONIC, &ts); + + EXPECT_EQ(ts.tv_sec, sec); + EXPECT_EQ(ts.tv_nsec, nsec); +} + +TEST(OsafClockGettime, FailWithEFAULT) { + mock_clock_gettime.return_value = -1; + mock_clock_gettime.errno_value = EFAULT; + + ASSERT_EXIT(osaf_clock_gettime(CLOCK_REALTIME, NULL), ::testing::KilledBySignal(SIGABRT), ""); +} + +TEST(OsafClockGettime, FailWithEINVAL) { + mock_clock_gettime.return_value = -1; + mock_clock_gettime.errno_value = EINVAL; + + struct timespec ts; + ASSERT_EXIT(osaf_clock_gettime(CLOCK_REALTIME, &ts), ::testing::KilledBySignal(SIGABRT), ""); +} + +TEST(OsafClockGettime, FailWithEPERM) { + mock_clock_gettime.return_value = -1; + mock_clock_gettime.errno_value = EPERM; + + struct timespec ts; + ASSERT_EXIT(osaf_clock_gettime(CLOCK_REALTIME, &ts), ::testing::KilledBySignal(SIGABRT), ""); +} diff --git a/osaf/libs/core/common/tests/osaf_nanosleep_test.cc b/osaf/libs/core/common/tests/osaf_nanosleep_test.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/osaf_nanosleep_test.cc @@ -0,0 +1,81 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#include <cerrno> +#include "osaf_time.h" +#include "osaf_utility.h" +#include "mock_clock_gettime.h" +#include "mock_clock_nanosleep.h" +#include "gtest/gtest.h" + +TEST(OsafNanosleep, SleepTenMilliseconds) { + time_t start_sec = 1234; + long start_nsec = 5678; + monotonic_clock = { start_sec, start_nsec }; + mock_clock_gettime.execution_time = kZeroSeconds; + mock_clock_gettime.return_value = 0; + current_nanosleep_index = 0; + number_of_nanosleep_instances = 2; + mock_clock_nanosleep[0].return_value = 0; + + timespec sleep_duration = kTenMilliseconds; + osaf_nanosleep(&sleep_duration); + + EXPECT_EQ(monotonic_clock.tv_sec, start_sec); + EXPECT_EQ(monotonic_clock.tv_nsec, start_nsec + 10000000); + EXPECT_EQ(current_nanosleep_index, 1); +} + +TEST(OsafNanosleep, SleepOneHourWithInterrupt) { + monotonic_clock = kZeroSeconds; + mock_clock_gettime.execution_time = kZeroSeconds; + mock_clock_gettime.return_value = 0; + current_nanosleep_index = 0; + number_of_nanosleep_instances = 3; + mock_clock_nanosleep[0].return_value = -1; + mock_clock_nanosleep[0].errno_value = EINTR; + mock_clock_nanosleep[1].return_value = 0; + + timespec sleep_duration = kOneHour; + osaf_nanosleep(&sleep_duration); + + EXPECT_EQ(monotonic_clock.tv_sec, 60 * 60); + EXPECT_EQ(monotonic_clock.tv_nsec, 0); + EXPECT_EQ(current_nanosleep_index, 2); +} + +TEST(OsafNanosleep, FailWithEFAULT) { + mock_clock_gettime.return_value = 0; + current_nanosleep_index = 0; + number_of_nanosleep_instances = 1; + mock_clock_nanosleep[0].return_value = -1; + mock_clock_nanosleep[0].errno_value = EFAULT; + + timespec sleep_duration = kOneSecond; + ASSERT_EXIT(osaf_nanosleep(&sleep_duration), ::testing::KilledBySignal(SIGABRT), ""); +} + +TEST(OsafNanosleep, FailWithEINVAL) { + mock_clock_gettime.return_value = 0; + current_nanosleep_index = 0; + number_of_nanosleep_instances = 1; + mock_clock_nanosleep[0].return_value = -1; + mock_clock_nanosleep[0].errno_value = EINVAL; + + timespec sleep_duration = kOneSecond; + ASSERT_EXIT(osaf_nanosleep(&sleep_duration), ::testing::KilledBySignal(SIGABRT), ""); +} diff --git a/osaf/libs/core/common/tests/osaf_normalize_timespec_test.cc b/osaf/libs/core/common/tests/osaf_normalize_timespec_test.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/osaf_normalize_timespec_test.cc @@ -0,0 +1,81 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#include "osaf_time.h" +#include "gtest/gtest.h" + +TEST(OsafNormalizeTimespec, Zero) { + const timespec zero_seconds = kZeroSeconds; + timespec result; + osaf_normalize_timespec(&zero_seconds, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &kZeroSeconds), 0); +} + +TEST(OsafNormalizeTimespec, AlreadyNormalized) { + const timespec already_normalized = { 123456789, 987654321 }; + timespec result; + osaf_normalize_timespec(&already_normalized, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &already_normalized), 0); +} + +TEST(OsafNormalizeTimespec, NanoseconsdGreaterThanOneBillion) { + const timespec not_normalized = { 123456789, 1987654321 }; + const timespec normalized = { 123456790, 987654321 }; + timespec result; + osaf_normalize_timespec(¬_normalized, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &normalized), 0); +} + +TEST(OsafNormalizeTimespec, NanoseconsdLessThanZero) { + const timespec not_normalized = { 123456789, -100 }; + const timespec normalized = { 123456788, 1000000000 - 100 }; + timespec result; + osaf_normalize_timespec(¬_normalized, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &normalized), 0); +} + +TEST(OsafNormalizeTimespec, NanoseconsGreaterThan2Billions) { + const timespec not_normalized = { 123456789, 2101234567 }; + const timespec normalized = { 123456789 + 2, 101234567 }; + timespec result; + osaf_normalize_timespec(¬_normalized, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &normalized), 0); +} + +TEST(OsafNormalizeTimespec, NanoseconsLessThanMinus2Billions) { + const timespec not_normalized = { 123456789, -2000000567 }; + const timespec normalized = { 123456789 - 2 - 1, 1000000000 - 567 }; + timespec result; + osaf_normalize_timespec(¬_normalized, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &normalized), 0); +} + +TEST(OsafNormalizeTimespec, BothParametersAtSameAddress) { + const timespec not_normalized = { 123456789, -2000000567 }; + const timespec normalized = { 123456789 - 2 - 1, 1000000000 - 567 }; + timespec result = not_normalized; + osaf_normalize_timespec(&result, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &normalized), 0); +} + +TEST(OsafNormalizeTimespec, NegativeSecondsAndNanoseconds) { + const timespec not_normalized = { -123, -1 }; + const timespec normalized = { -124, 999999999 }; + timespec result; + osaf_normalize_timespec(¬_normalized, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &normalized), 0); +} diff --git a/osaf/libs/core/common/tests/osaf_time_test.cc b/osaf/libs/core/common/tests/osaf_time_test.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/osaf_time_test.cc @@ -0,0 +1,390 @@ +// Copyright 2005, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * 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. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// 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 +// OWNER 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. + +// A sample program demonstrating using Google C++ testing framework. +// +// Author: w...@google.com (Zhanyong Wan) + + +// This sample shows how to write a simple unit test for a function, +// using Google C++ testing framework. +// +// Writing a unit test using Google C++ testing framework is easy as 1-2-3: + + +// Step 1. Include necessary header files such that the stuff your +// test logic needs is declared. +// +// Don't forget gtest.h, which declares the testing framework. + +#include <limits.h> +#include "osaf_time.h" +#include "osaf_utility.h" +#include "gtest/gtest.h" +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <signal.h> +#include <sys/time.h> + +static long counter; + +void osaf_abort(long i_cause) { + fprintf(stderr, "osaf_abort(%ld) called\n", i_cause); + abort(); +} + +void alarm_handler(int sig) { + ++counter; +} + +// Step 2. Use the TEST macro to define your tests. +// +// TEST has two parameters: the test case name and the test name. +// After using the macro, you should define your test logic between a +// pair of braces. You can use a bunch of macros to indicate the +// success or failure of a test. EXPECT_TRUE and EXPECT_EQ are +// examples of such macros. For a complete list, see gtest.h. +// +// <TechnicalDetails> +// +// In Google Test, tests are grouped into test cases. This is how we +// keep test code organized. You should put logically related tests +// into the same test case. +// +// The test case name and the test name should both be valid C++ +// identifiers. And you should not use underscore (_) in the names. +// +// Google Test guarantees that each test you define is run exactly +// once, but it makes no guarantee on the order the tests are +// executed. Therefore, you should write your tests in such a way +// that their results don't depend on their order. +// +// </TechnicalDetails> + + +// Tests Factorial(). + +// Tests factorial of negative numbers. +TEST(osaf_time_test, osaf_clock_gettime) { + struct timespec ts1 = { 10, 10 }; + struct timespec ts2 = { 9, 9 }; + struct timespec ts3 = { 11, 11 }; + const struct timespec ts_9ms = { 0, 9000000 }; + const struct timespec ts_10ms = { 0, 10000000 }; + const struct timespec ts_900ms = { 0, 900000000 }; + + osaf_clock_gettime(CLOCK_MONOTONIC, &ts1); + usleep(10000); + osaf_clock_gettime(CLOCK_MONOTONIC, &ts2); + + osaf_timespec_subtract(&ts2, &ts1, &ts3); + + EXPECT_GE(osaf_timespec_compare(&ts2, &ts1), 0); + EXPECT_EQ(osaf_timespec_compare(&ts3, &ts_9ms), 1); + EXPECT_GE(osaf_timespec_compare(&ts3, &ts_10ms), 0); + EXPECT_EQ(osaf_timespec_compare(&ts3, &ts_900ms), -1); +} + +// Tests factorial of negative numbers. +TEST(osaf_time_test, osaf_nanosleep) { + struct timespec ts1 = { 10, 10 }; + struct timespec ts2 = { 9, 9 }; + struct timespec ts3 = { 11, 11 }; + const struct timespec ts_1_233ms = { 1, 233000000 }; + const struct timespec ts_1_234ms = { 1, 234000000 }; + const struct timespec ts_1_567ms = { 1, 567000000 }; + + counter = 0; + signal(SIGALRM, alarm_handler); + struct itimerval itv = { + {0, 379}, + {0, 379} + }; + setitimer(ITIMER_REAL, &itv, NULL); + + osaf_clock_gettime(CLOCK_MONOTONIC, &ts1); + osaf_nanosleep(&ts_1_234ms); + osaf_clock_gettime(CLOCK_MONOTONIC, &ts2); + + osaf_timespec_subtract(&ts2, &ts1, &ts3); + + printf("counter: %ld\n", counter); + + EXPECT_EQ(osaf_timespec_compare(&ts3, &ts_1_233ms), 1); + EXPECT_GE(osaf_timespec_compare(&ts3, &ts_1_234ms), 0); + EXPECT_EQ(osaf_timespec_compare(&ts3, &ts_1_567ms), -1); + // EXPECT_LE(counter, 1234000 / 379); + // EXPECT_GT(counter, 1000); + + signal(SIGALRM, SIG_IGN); + struct itimerval itv2 = { + {0, 0}, + {0, 0} + }; + setitimer(ITIMER_REAL, &itv2, NULL); +} + +TEST(osaf_time_test, osaf_normalize_timespec) { + struct timespec ts = { 1, 1 }; + + const struct timespec ts0 = { 0, 0 }; // should be same after normalization + osaf_normalize_timespec(&ts0, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts0), 0); + + const struct timespec ts1 = { 123456789, 987654321 }; // should be same after normalization + osaf_normalize_timespec(&ts1, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts1), 0); + + // Positive tv_nsec. tv_sec needs to stepped exactly than one second + const struct timespec ts2 = { 123456789, 1987654321 }; + const struct timespec ts2_nrm = { 123456790, 987654321 }; // ts2 normalized + osaf_normalize_timespec(&ts2, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts2_nrm), 0); + + // Negative tv_nsec. tv_sec needs to stepped exactly than one second + const struct timespec ts3 = { 123456789, -100 }; + const struct timespec ts3_nrm = { 123456788, 1000000000 - 100 }; // ts3 normalized + osaf_normalize_timespec(&ts3, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts3_nrm), 0); + + // Positive tv_nsec. tv_sec needs to stepped with more than one second + const struct timespec ts4 = { 123456789, 123987654321 }; + const struct timespec ts4_nrm = { 123456789 + 123, 987654321 }; // ts4 normalized + osaf_normalize_timespec(&ts4, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts4_nrm), 0); + + // Negative tv_nsec. tv_sec needs to stepped with more than one second + const struct timespec ts5 = { 123456789, -456000000567 }; + const struct timespec ts5_nrm = { 123456789 - 456 - 1, 1000000000 - 567 }; // ts5 normalized + osaf_normalize_timespec(&ts5, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts5_nrm), 0); + + // Check that normalization works if both both parameters refer to the same memory location + ts = ts5; + osaf_normalize_timespec(&ts, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts5_nrm), 0); + + // Negative tv_sec and tv_nsec. + const struct timespec ts6 = { -123, -1 }; + const struct timespec ts6_nrm = { -124, 999999999 }; // ts6 normalized + osaf_normalize_timespec(&ts6, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts6_nrm), 0); +} + +// Tests factorial of negative numbers. +TEST(osaf_time_test, osaf_timespec_add) { + struct timespec ts; + const struct timespec ts_0_0s = { 0, 0 }; + const struct timespec ts_0_1s = { 0, 100000000 }; + const struct timespec ts_2_4s = { 2, 400000000 }; + const struct timespec ts_4_8s = { 4, 800000000 }; + const struct timespec ts_9_6s = { 9, 600000000 }; + const struct timespec ts_9_7s = { 9, 700000000 }; + const struct timespec ts_19_3s = { 19, 300000000 }; + const struct timespec ts1 = { 3715800020576315623, 358743382 }; + const struct timespec ts2 = { 3637143377279394477, 719128783 }; + const struct timespec ts3 = { 7352943397855710101, 77872165 }; // sum of ts1 + ts2 + + ts = ts_0_0s; + + osaf_timespec_add(&ts, &ts_0_0s, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_0_0s), 0); + + osaf_timespec_add(&ts, &ts, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_0_0s), 0); + + osaf_timespec_add(&ts, &ts_2_4s, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_2_4s), 0); + + osaf_timespec_add(&ts, &ts, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_4_8s), 0); + + osaf_timespec_add(&ts, &ts, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_9_6s), 0); + + osaf_timespec_add(&ts_0_0s, &ts, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_9_6s), 0); + + osaf_timespec_add(&ts_0_1s, &ts, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_9_7s), 0); + + osaf_timespec_add(&ts, &ts_9_6s, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_19_3s), 0); + + osaf_timespec_add(&ts1, &ts2, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts3), 0); + +} + +// Tests factorial of negative numbers. +TEST(osaf_time_test, osaf_timespec_subtract) { + struct timespec ts; + const struct timespec ts_0_0s = { 0, 0 }; + const struct timespec ts_0_9s = { 0, 900000000 }; + const struct timespec ts_1_0s = { 1, 0 }; + const struct timespec ts_0_1s = { 0, 100000000 }; + const struct timespec ts_0_2s = { 0, 200000000 }; + const struct timespec ts_1_1s = { 1, 100000000 }; + const struct timespec ts1 = { 3715800020576315623, 358743382 }; + const struct timespec ts2 = { 3637143377279394477, 639614599 }; + const struct timespec ts3 = { 78656643296921145, 719128783 }; // difference of ts1 - ts2 + + ts = ts1; + + osaf_timespec_subtract(&ts, &ts, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_0_0s), 0); + + osaf_timespec_subtract(&ts_1_1s, &ts_0_1s, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_1_0s), 0); + + osaf_timespec_subtract(&ts_1_1s, &ts_1_0s, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_0_1s), 0); + + osaf_timespec_subtract(&ts_1_1s, &ts_0_2s, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_0_9s), 0); + + osaf_timespec_subtract(&ts1, &ts2, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts3), 0); + + osaf_timespec_subtract(&ts, &ts_0_0s, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts3), 0); +} + +// Tests factorial of negative numbers. +TEST(osaf_time_test, osaf_timespec_compare) { + const struct timespec ts_0_0s = { 0, 0 }; + const struct timespec ts_0_1s = { 0, 100000000 }; + const struct timespec ts_0_9s = { 0, 900000000 }; + const struct timespec ts_1_0s = { 1, 0 }; + const struct timespec ts_1_1s = { 1, 100000000 }; + + EXPECT_EQ(osaf_timespec_compare(&ts_0_0s, &ts_0_0s), 0); + EXPECT_EQ(osaf_timespec_compare(&ts_0_0s, &ts_0_1s), -1); + EXPECT_EQ(osaf_timespec_compare(&ts_0_1s, &ts_0_0s), 1); + EXPECT_EQ(osaf_timespec_compare(&ts_0_0s, &ts_1_0s), -1); + EXPECT_EQ(osaf_timespec_compare(&ts_1_0s, &ts_0_0s), 1); + + EXPECT_EQ(osaf_timespec_compare(&ts_1_0s, &ts_1_0s), 0); + EXPECT_EQ(osaf_timespec_compare(&ts_1_0s, &ts_1_1s), -1); + EXPECT_EQ(osaf_timespec_compare(&ts_1_1s, &ts_1_0s), 1); + + EXPECT_EQ(osaf_timespec_compare(&ts_0_9s, &ts_0_9s), 0); + EXPECT_EQ(osaf_timespec_compare(&ts_0_9s, &ts_1_1s), -1); + EXPECT_EQ(osaf_timespec_compare(&ts_0_9s, &ts_0_1s), 1); + EXPECT_EQ(osaf_timespec_compare(&ts_1_1s, &ts_0_9s), 1); + EXPECT_EQ(osaf_timespec_compare(&ts_0_1s, &ts_0_9s), -1); +} + +// Tests factorial of negative numbers. +TEST(osaf_time_test, osaf_timespec_to_timeval) { + const struct timespec ts = { 3715800020576315623, 358743382 }; + const struct timeval tv_expected = { 3715800020576315623, 358743 }; + struct timeval tv = { 0, 0 }; + + osaf_timespec_to_timeval(&ts, &tv); + EXPECT_EQ(tv.tv_sec, tv_expected.tv_sec); + EXPECT_EQ(tv.tv_usec, tv_expected.tv_usec); +} + +TEST(osaf_time_test, osaf_timeval_to_timespec) { + const struct timeval tv = { 3715800020576315623, 358743 }; + const struct timespec ts_expected = { 3715800020576315623, 358743000 }; + struct timespec ts = { 0, 0}; + + osaf_timeval_to_timespec(&tv, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_expected), 0); +} + +TEST(osaf_time_test, osaf_millis_to_timespec) { + const struct timespec ts_expected = { 13725125428499100, 961000000 }; + struct timespec ts = { 0, 0}; + + osaf_millis_to_timespec(13725125428499100961ull, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_expected), 0); +} + +TEST(osaf_time_test, osaf_micros_to_timespec) { + const struct timespec ts_expected = { 13725125428499, 100961000 }; + struct timespec ts = { 0, 0}; + + osaf_micros_to_timespec(13725125428499100961ull, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_expected), 0); +} + +TEST(osaf_time_test, osaf_nanos_to_timespec) { + const struct timespec ts_expected = { 13725125428, 499100961 }; + struct timespec ts = { 0, 0}; + + osaf_nanos_to_timespec(13725125428499100961ull, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_expected), 0); +} + +TEST(osaf_time_test, osaf_double_to_timespec) { + const struct timespec ts_expected = { 15714713, 125433700 }; + const struct timespec ts_expected2 = { -15714714, 874566300 }; + struct timespec ts = { 0, 0}; + + osaf_double_to_timespec(15714713.1254337, &ts); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_expected), 0); + osaf_double_to_timespec(-15714713.1254337, &ts); + // printf("%lld %lld\n", (long long) ts.tv_sec, (long long) ts.tv_nsec); + EXPECT_EQ(osaf_timespec_compare(&ts, &ts_expected2), 0); +} + +TEST(osaf_time_test, osaf_timespec_to_millis) { + const struct timespec ts = { 13725125428499100, 961923266 }; + + uint64_t result = osaf_timespec_to_millis(&ts); + EXPECT_EQ(result, 13725125428499100961ull); +} + +TEST(osaf_time_test, osaf_timespec_to_micros) { + const struct timespec ts = { 13725125428499, 100961923 }; + + uint64_t result = osaf_timespec_to_micros(&ts); + EXPECT_EQ(result, 13725125428499100961ull); +} + +TEST(osaf_time_test, osaf_timespec_to_nanos) { + const struct timespec ts = { 13725125428, 499100961 }; + + uint64_t result = osaf_timespec_to_nanos(&ts); + EXPECT_EQ(result, 13725125428499100961ull); +} + +TEST(osaf_time_test, osaf_timespec_to_double) { + const struct timespec ts = { 15714713, 125433700 }; + const struct timespec ts2 = { -15714714, 874566300 }; + + double result1 = osaf_timespec_to_double(&ts); + double result2 = osaf_timespec_to_double(&ts2); + EXPECT_EQ(result1, 15714713.1254337); + EXPECT_EQ(result2, -15714713.1254337); +} diff --git a/osaf/libs/core/common/tests/osaf_timespec_add_test.cc b/osaf/libs/core/common/tests/osaf_timespec_add_test.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/osaf_timespec_add_test.cc @@ -0,0 +1,62 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#include "osaf_time.h" +#include "gtest/gtest.h" + +static const timespec kTwoDotFourSeconds = { 2, 400000000 }; +static const timespec kFourDotEightSeconds = { 4, 800000000 }; +static const timespec kNineDotSixSeconds = { 9, 600000000 }; +static const timespec number1 = { 576315623, 358743382 }; +static const timespec number2 = { 1279394477, 719128783 }; +static const timespec sum = { 1855710101, 77872165 }; + +TEST(OsafTimespecAdd, ZeroPlusZero) { + timespec result; + osaf_timespec_add(&kZeroSeconds, &kZeroSeconds, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &kZeroSeconds), 0); +} + +TEST(OsafTimespecAdd, TwoDotFourAddedWithItself) { + timespec result = kTwoDotFourSeconds; + osaf_timespec_add(&result, &result, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &kFourDotEightSeconds), 0); +} + +TEST(OsafTimespecAdd, FourDotEightAddedWithItself) { + timespec result = kFourDotEightSeconds; + osaf_timespec_add(&result, &result, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &kNineDotSixSeconds), 0); +} + +TEST(OsafTimespecAdd, AddRandomNumbers) { + timespec result; + osaf_timespec_add(&number1, &number2, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &sum), 0); +} + +TEST(OsafTimespecAdd, FirstParameterAndResultAtSameAddress) { + timespec result = number1; + osaf_timespec_add(&result, &number2, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &sum), 0); +} + +TEST(OsafTimespecAdd, SecondParameterAndResultAtSameAddress) { + timespec result = number2; + osaf_timespec_add(&number1, &result, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &sum), 0); +} diff --git a/osaf/libs/core/common/tests/osaf_timespec_compare_test.cc b/osaf/libs/core/common/tests/osaf_timespec_compare_test.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/osaf_timespec_compare_test.cc @@ -0,0 +1,75 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#include "osaf_time.h" +#include "gtest/gtest.h" + +static const timespec kZeroDotOneSeconds = { 0, 100000000 }; +static const timespec kZeroDotNineSeconds = { 0, 900000000 }; +static const timespec kOneDotOneSeconds = { 1, 100000000 }; + +TEST(OsafTimespecCompare, ZeroWithZero) { + EXPECT_EQ(osaf_timespec_compare(&kZeroSeconds, &kZeroSeconds), 0); +} + +TEST(OsafTimespecCompare, ZeroWithZeroDotOne) { + EXPECT_EQ(osaf_timespec_compare(&kZeroSeconds, &kZeroDotOneSeconds), -1); +} + +TEST(OsafTimespecCompare, ZeroDotOneWithZero) { + EXPECT_EQ(osaf_timespec_compare(&kZeroDotOneSeconds, &kZeroSeconds), 1); +} + +TEST(OsafTimespecCompare, ZeroWithOne) { + EXPECT_EQ(osaf_timespec_compare(&kZeroSeconds, &kOneSecond), -1); +} + +TEST(OsafTimespecCompare, OneWithZero) { + EXPECT_EQ(osaf_timespec_compare(&kOneSecond, &kZeroSeconds), 1); +} + +TEST(OsafTimespecCompare, OneWithOne) { + EXPECT_EQ(osaf_timespec_compare(&kOneSecond, &kOneSecond), 0); +} + +TEST(OsafTimespecCompare, OneWithOneDotOne) { + EXPECT_EQ(osaf_timespec_compare(&kOneSecond, &kOneDotOneSeconds), -1); +} + +TEST(OsafTimespecCompare, OneDotOneWithOne) { + EXPECT_EQ(osaf_timespec_compare(&kOneDotOneSeconds, &kOneSecond), 1); +} + +TEST(OsafTimespecCompare, ZeroDotNineWithZeroDotNine) { + EXPECT_EQ(osaf_timespec_compare(&kZeroDotNineSeconds, &kZeroDotNineSeconds), 0); +} + +TEST(OsafTimespecCompare, ZeroDotNineWithOneDotOne) { + EXPECT_EQ(osaf_timespec_compare(&kZeroDotNineSeconds, &kOneDotOneSeconds), -1); +} + +TEST(OsafTimespecCompare, ZeroDotNineWithZeroDotOne) { + EXPECT_EQ(osaf_timespec_compare(&kZeroDotNineSeconds, &kZeroDotOneSeconds), 1); +} + +TEST(OsafTimespecCompare, OneDotOneWithZeroDotNine) { + EXPECT_EQ(osaf_timespec_compare(&kOneDotOneSeconds, &kZeroDotNineSeconds), 1); +} + +TEST(OsafTimespecCompare, ZeroDotOneWithZeroDotNine) { + EXPECT_EQ(osaf_timespec_compare(&kZeroDotOneSeconds, &kZeroDotNineSeconds), -1); +} diff --git a/osaf/libs/core/common/tests/osaf_timespec_convert_test.cc b/osaf/libs/core/common/tests/osaf_timespec_convert_test.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/osaf_timespec_convert_test.cc @@ -0,0 +1,100 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#include "osaf_time.h" +#include "gtest/gtest.h" + +static const timespec kTimespec = { 1576315623, 358743382 }; +static const timeval kTimeval = { 1576315623, 358743 }; +static const timespec kTruncatedTimespec = { 1576315623, 358743000 }; + +TEST(OsafTimespecConvert, TimespecToTimeval) { + timeval result = { 0, 0 }; + osaf_timespec_to_timeval(&kTimespec, &result); + EXPECT_EQ(result.tv_sec, kTimeval.tv_sec); + EXPECT_EQ(result.tv_usec, kTimeval.tv_usec); +} + +TEST(OsafTimespecConvert, TimevalToTimespec) { + timespec result = { 0, 0 }; + osaf_timeval_to_timespec(&kTimeval, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &kTruncatedTimespec), 0); +} + +TEST(OsafTimespecConvert, MillisToTimespec) { + const timespec expected_result = { 1428499100, 961000000 }; + timespec result = { 0, 0 }; + osaf_millis_to_timespec(1428499100961ull, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &expected_result), 0); +} + +TEST(OsafTimespecConvert, MicrosToTimespec) { + const timespec expected_result = { 2125428499, 100961000 }; + timespec result = { 0, 0 }; + osaf_micros_to_timespec(2125428499100961ull, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &expected_result), 0); +} + +TEST(OsafTimespecConvert, NanosToTimespec) { + const struct timespec expected_result = { 1725125428, 499100961 }; + timespec result = { 0, 0 }; + + osaf_nanos_to_timespec(1725125428499100961ull, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &expected_result), 0); +} + +TEST(OsafTimespecConvert, DoubleToTimespec) { + const struct timespec expected_result = { 15714713, 125433700 }; + const struct timespec expected_result2 = { -15714714, 874566300 }; + timespec result = { 0, 0 }; + + osaf_double_to_timespec(15714713.1254337, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &expected_result), 0); + osaf_double_to_timespec(-15714713.1254337, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &expected_result2), 0); +} + +TEST(OsafTimespecConvert, TimespecToMillis) { + const struct timespec ts = { 1428499100, 961923266 }; + + uint64_t result = osaf_timespec_to_millis(&ts); + EXPECT_EQ(result, 1428499100961ull); +} + +TEST(OsafTimespecConvert, TimespecToMicros) { + const struct timespec ts = { 1125428499, 100961923 }; + + uint64_t result = osaf_timespec_to_micros(&ts); + EXPECT_EQ(result, 1125428499100961ull); +} + +TEST(OsafTimespecConvert, TimespecToNanos) { + const struct timespec ts = { 1725125428, 499100961 }; + + uint64_t result = osaf_timespec_to_nanos(&ts); + EXPECT_EQ(result, 1725125428499100961ull); +} + +TEST(OsafTimespecConvert, TimespecToDouble) { + const struct timespec ts = { 15714713, 125433700 }; + const struct timespec ts2 = { -15714714, 874566300 }; + + double result1 = osaf_timespec_to_double(&ts); + double result2 = osaf_timespec_to_double(&ts2); + EXPECT_EQ(result1, 15714713.1254337); + EXPECT_EQ(result2, -15714713.1254337); +} diff --git a/osaf/libs/core/common/tests/osaf_timespec_subtract_test.cc b/osaf/libs/core/common/tests/osaf_timespec_subtract_test.cc new file mode 100644 --- /dev/null +++ b/osaf/libs/core/common/tests/osaf_timespec_subtract_test.cc @@ -0,0 +1,62 @@ +/* -*- OpenSAF -*- + * + * (C) Copyright 2015 The OpenSAF Foundation + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed + * under the GNU Lesser General Public License Version 2.1, February 1999. + * The complete license can be accessed from the following location: + * http://opensource.org/licenses/lgpl-license.php + * See the Copying file included with the OpenSAF distribution for full + * licensing terms. + * + * Author(s): Ericsson AB + * + */ + +#include "osaf_time.h" +#include "gtest/gtest.h" + +static const timespec kOneDotOneSeconds = { 1, 100000000 }; +static const timespec kZeroDotTwoSeconds = { 0, 200000000 }; +static const timespec kZeroDotNineSeconds = { 0, 900000000 }; +static const timespec number1 = { 1576315623, 358743382 }; +static const timespec number2 = { 279394477, 639614599 }; +static const timespec difference = { 1296921145, 719128783 }; + +TEST(OsafTimespecSubtract, ZeroMinusZero) { + timespec result; + osaf_timespec_subtract(&kZeroSeconds, &kZeroSeconds, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &kZeroSeconds), 0); +} + +TEST(OsafTimespecSubtract, FifteenMinusFive) { + timespec result = kFifteenSeconds; + osaf_timespec_subtract(&result, &kFiveSeconds, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &kTenSeconds), 0); +} + +TEST(OsafTimespecSubtract, OneDotOneMinusZeroDotTwo) { + timespec result; + osaf_timespec_subtract(&kOneDotOneSeconds, &kZeroDotTwoSeconds, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &kZeroDotNineSeconds), 0); +} + +TEST(OsafTimespecSubtract, SubtractRandomNumbers) { + timespec result; + osaf_timespec_subtract(&number1, &number2, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &difference), 0); +} + +TEST(OsafTimespecSubtract, FirstParameterAndResultAtSameAddress) { + timespec result = number1; + osaf_timespec_subtract(&result, &number2, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &difference), 0); +} + +TEST(OsafTimespecSubtract, SecondParameterAndResultAtSameAddress) { + timespec result = number2; + osaf_timespec_subtract(&number1, &result, &result); + EXPECT_EQ(osaf_timespec_compare(&result, &difference), 0); +} ------------------------------------------------------------------------------ _______________________________________________ Opensaf-devel mailing list Opensaf-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/opensaf-devel