Tests for utils 39/218239/2
authorLukasz Tabor <l.tabor@partner.samsung.com>
Wed, 20 Nov 2019 13:30:14 +0000 (14:30 +0100)
committerLukasz Tabor <l.tabor@partner.samsung.com>
Wed, 20 Nov 2019 15:03:28 +0000 (16:03 +0100)
Change-Id: Id43b664bc964ba7cdb018b77cdf0b1278cfd5bee

tests/no-ui-scenarios/UtilsTest.cpp [new file with mode: 0644]

diff --git a/tests/no-ui-scenarios/UtilsTest.cpp b/tests/no-ui-scenarios/UtilsTest.cpp
new file mode 100644 (file)
index 0000000..53303f6
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2017  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "utils.hpp"
+
+#include <gtest/gtest.h>
+
+TEST(utils, testRescale)
+{
+       int scaled_int = utils::rescale<int, int>(2, utils::Range<int>{-1, 3}, utils::Range<int>{0, 11});
+       ASSERT_EQ(8, scaled_int);
+
+       double scaled_double = utils::rescale<int>(1, utils::Range<int>{0, 3}, {0, 100.0});
+       ASSERT_DOUBLE_EQ(100.0 / 3.0, scaled_double);
+
+       ASSERT_NO_THROW(utils::rescale<int>(52, utils::Range<int>{1, 1}));
+}
+
+TEST(utils, testDoubleToPercent)
+{
+       auto str = utils::doubleToPercent(1.6180339);
+       ASSERT_STREQ("161%", str.c_str());
+
+       str = utils::doubleToPercent(-.6);
+       ASSERT_STREQ("-60%", str.c_str());
+}
+
+TEST(utils, testDoubleToString)
+{
+       auto str = utils::doubleToString(1.6180339, 0);
+       ASSERT_STREQ("2", str.c_str());
+
+       str = utils::doubleToString(-1.6180339, 0);
+       ASSERT_STREQ("-2", str.c_str());
+
+       str = utils::doubleToString(1.6180339, 5);
+       ASSERT_STREQ("1.61803", str.c_str());
+
+       str = utils::doubleToString(-1.6180339, 5);
+       ASSERT_STREQ("-1.61803", str.c_str());
+}
+
+TEST(utils, testStringSplitByDelimiter)
+{
+       const std::string str = "Some;sentence ; t\no;test!@#$%^&*()~;";
+       const char *words[] = {"Some", "sentence ", " t\no", "test!@#$%^&*()~"};
+
+       auto vec = utils::stringSplitByDelimiter(str, ';');
+       ASSERT_EQ(4, vec.size());
+       for (size_t i = 0; i < 4; i++) {
+               ASSERT_STREQ(words[i], vec[i].c_str());
+       }
+
+       vec = utils::stringSplitByDelimiter(str, '.');
+       ASSERT_EQ(1, vec.size());
+       ASSERT_STREQ(str.c_str(), vec[0].c_str());
+
+       vec = utils::stringSplitByDelimiter("", ';');
+       ASSERT_EQ(0, vec.size());
+}
+
+int main(int argc, char *argv[])
+{
+       try {
+               ::testing::InitGoogleTest(&argc, argv);
+               return RUN_ALL_TESTS();
+       } catch (...) {
+               return 1;
+       }
+}