From aebd94612723d6bff4bc13686d918f27d3dcae90 Mon Sep 17 00:00:00 2001
From: Steven Murray <steven.murray@cern.ch>
Date: Sat, 15 Feb 2014 00:21:19 +0100
Subject: [PATCH] Added the following unit test:

    castor/utils/UtilsTest.
---
 castor/server/DaemonTest.cpp |   2 +-
 castor/utils/UtilsTest.cpp   | 203 +++++++++++++++++++++++++++++++++++
 test/CMakeLists.txt          |   1 +
 3 files changed, 205 insertions(+), 1 deletion(-)
 create mode 100644 castor/utils/UtilsTest.cpp

diff --git a/castor/server/DaemonTest.cpp b/castor/server/DaemonTest.cpp
index aef2a1df4f..467b6eed11 100644
--- a/castor/server/DaemonTest.cpp
+++ b/castor/server/DaemonTest.cpp
@@ -19,7 +19,7 @@
  *
  * 
  *
- * @author Castor Dev team, castor-dev@cern.ch
+ * @author Steven.Murray@cern.ch
  *****************************************************************************/
 
 #include "castor/log/DummyLogger.hpp"
diff --git a/castor/utils/UtilsTest.cpp b/castor/utils/UtilsTest.cpp
new file mode 100644
index 0000000000..dc4429398b
--- /dev/null
+++ b/castor/utils/UtilsTest.cpp
@@ -0,0 +1,203 @@
+/******************************************************************************
+ *                castor/utils/UtilsTest.cpp
+ *
+ * This file is part of the Castor project.
+ * See http://castor.web.cern.ch/castor
+ *
+ * Copyright (C) 2003  CERN
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 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.  See the
+ * GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ *
+ *
+ * @author Steven.Murray@cern.ch
+ *****************************************************************************/
+
+#include "castor/utils/utils.hpp"
+
+#include <gtest/gtest.h>
+#include <list>
+#include <stdlib.h>
+#include <string>
+#include <sys/time.h>
+#include <unistd.h>
+#include <vector>
+
+namespace UnitTests {
+
+/**
+ * Tests the good day senario of passing a multi-column string to the
+ * splitString() method.
+ */
+TEST(castor_utils, testGoodDaySplitString) {
+  using namespace castor::utils;
+  const std::string line("col0 col1 col2 col3 col4 col5 col6 col7");
+  std::vector<std::string> columns;
+
+  ASSERT_NO_THROW(splitString(line, ' ', columns));
+  ASSERT_EQ((std::vector<std::string>::size_type)8, columns.size());
+  ASSERT_EQ(std::string("col0"), columns[0]);
+  ASSERT_EQ(std::string("col1"), columns[1]);
+  ASSERT_EQ(std::string("col2"), columns[2]);
+  ASSERT_EQ(std::string("col3"), columns[3]);
+  ASSERT_EQ(std::string("col4"), columns[4]);
+  ASSERT_EQ(std::string("col5"), columns[5]);
+  ASSERT_EQ(std::string("col6"), columns[6]);
+  ASSERT_EQ(std::string("col7"), columns[7]);
+}
+
+/**
+ * Test the case of an empty string being passed to the splitString() method.
+ */
+TEST(castor_utls, testSplitStringWithEmptyString) {
+  using namespace castor::utils;
+  const std::string emptyString;
+  std::vector<std::string> columns;
+
+  ASSERT_NO_THROW(splitString(emptyString, ' ', columns));
+  ASSERT_EQ((std::vector<std::string>::size_type)0, columns.size());
+}
+
+/**
+ * Test the case of a non-empty string containing no separator character
+ * passed to the splitString() method.
+ */
+TEST(castor_utils, testSplitStringWithNoSeparatorInString) {
+  using namespace castor::utils;
+  const std::string stringContainingNoSeparator =
+    "stringContainingNoSeparator";
+  std::vector<std::string> columns;
+
+  ASSERT_NO_THROW(splitString(stringContainingNoSeparator, ' ', columns));
+  ASSERT_EQ((std::vector<std::string>::size_type)1, columns.size());
+  ASSERT_EQ(stringContainingNoSeparator, columns[0]);
+}
+
+TEST(castor_utils, testTimevalGreaterThan_BigSecSmallSec_BigUsecSmallUsec) {
+  using namespace castor::utils;
+  const timeval bigger   = {6, 5};
+  const timeval smaller  = {5, 4};
+  const bool    expected = true;
+
+  ASSERT_EQ(expected, timevalGreaterThan(bigger, smaller));
+}
+
+TEST(castor_utils, testTimevalGreaterThan_BigSecSmallSec_BigUsecSmallUsec_swapped) {
+  using namespace castor::utils;
+  const timeval bigger   = {6, 5};
+  const timeval smaller  = {5, 4};
+  const bool    expected = false;
+
+  ASSERT_EQ(expected, timevalGreaterThan(smaller, bigger));
+}
+
+TEST (castor_utils, testTimevalGreaterThan_BigSecSmallSec_SmallUsecBigUsec) {
+  using namespace castor::utils;
+  const timeval bigger   = {4, 3};
+  const timeval smaller  = {2, 7};
+  const bool    expected = true;
+
+  ASSERT_EQ(expected, timevalGreaterThan(bigger, smaller));
+}
+
+TEST(castor_utils, testTimevalGreaterThan_BigSecSmallSec_SmallUsecBigUsec_swapped) {
+  using namespace castor::utils;
+  const timeval bigger   = {4, 3};
+  const timeval smaller  = {2, 7};
+  const bool    expected = false;
+
+  ASSERT_EQ(expected, timevalGreaterThan(smaller, bigger));
+}
+
+TEST(castor_utils, testTimevalGreaterThan_EqualSec_EqualUsec) {
+  using namespace castor::utils;
+  const timeval a         = {8, 9};
+  const timeval b         = {8, 9};
+  const bool    expected  = false;
+
+  ASSERT_EQ(expected, timevalGreaterThan(a, b));
+}
+
+TEST(castor_utils, testTimevalAbsDiff_BigSecSmallSec_BigUsecSmallUsec) {
+  using namespace castor::utils;
+  const timeval bigger   = {6, 5};
+  const timeval smaller  = {5, 4};
+  const timeval expected = {1, 1};
+  const timeval actual   = timevalAbsDiff(bigger,
+    smaller);
+  const bool    isAMatch = expected.tv_sec == actual.tv_sec &&
+    expected.tv_usec == actual.tv_usec;
+
+  ASSERT_EQ(true, isAMatch);
+}
+
+TEST(castor_utils, testTimevalAbsDiff_BigSecSmallSec_BigUsecSmallUsec_swapped) {
+  using namespace castor::utils;
+  const timeval bigger   = {6, 5};
+  const timeval smaller  = {5, 4};
+  const timeval expected = {1, 1};
+  const timeval actual   = timevalAbsDiff(smaller,
+    bigger);
+  const bool    isAMatch = expected.tv_sec == actual.tv_sec &&
+    expected.tv_usec == actual.tv_usec;
+
+  ASSERT_EQ(true, isAMatch);
+}
+
+TEST(castor_utils, testTimevalAbsDiff_BigSecSmallSec_SmallUsecBigUsec) {
+  using namespace castor::utils;
+  const timeval bigger   = {4, 3};
+  const timeval smaller  = {2, 7};
+  const timeval expected = {1, 999996};
+  const timeval actual   = timevalAbsDiff(bigger,
+    smaller);
+  const bool    isAMatch = expected.tv_sec == actual.tv_sec &&
+    expected.tv_usec == actual.tv_usec;
+
+  ASSERT_EQ(true, isAMatch);
+}
+
+TEST(castor_utils, testTimevalAbsDiff_BigSecSmallSec_SmallUsecBigUsec_swapped) {
+  using namespace castor::utils;
+  const timeval bigger   = {4, 3};
+  const timeval smaller  = {2, 7};
+  const timeval expected = {1, 999996};
+  const timeval actual   = timevalAbsDiff(smaller,
+    bigger);
+  const bool    isAMatch = expected.tv_sec == actual.tv_sec &&
+    expected.tv_usec == actual.tv_usec;
+
+  ASSERT_EQ(true, isAMatch);
+}
+
+TEST(castor_utils, testTimevalAbsDiff_EqualSec_EqualUsec) {
+  using namespace castor::utils;
+  const timeval a        = {8, 9};
+  const timeval b        = {8, 9};
+  const timeval expected = {0, 0};
+  const timeval actual   = timevalAbsDiff(a, b);
+  const bool    isAMatch = expected.tv_sec == actual.tv_sec &&
+    expected.tv_usec == actual.tv_usec;
+
+  ASSERT_EQ(true, isAMatch);
+}
+
+TEST(castor_utils, testTimevalToDouble) {
+  using namespace castor::utils;
+  const timeval tv       = {1234, 999992};
+  const double  expected = 1234.999992;
+  const double  actual   = timevalToDouble(tv);
+
+  ASSERT_EQ(expected, actual);
+}
+
+} // namespace UnitTests
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index dee4031e50..3653e02633 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -35,6 +35,7 @@ add_executable(castorUnitTests
   ../castor/tape/tapeserver/exception/ExceptionTest.cpp
   ../castor/tape/tapeserver/file/StructuresTest.cpp
   ../castor/tape/tapeserver/threading/ThreadingTests.cpp
+  ../castor/utils/UtilsTest.cpp
 )
 set_property(SOURCE ../SCSI/StructuresTest.cpp
   PROPERTY COMPILE_FLAGS -fno-strict-aliasing
-- 
GitLab