Introduce "nike" (#2609)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 11 Dec 2018 07:21:29 +0000 (16:21 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 11 Dec 2018 07:21:29 +0000 (16:21 +0900)
* Add a "nike" library

This commit introduces a new library called "nike", which will serve a
collection of numeric value comparison routines.

The current implementation includes one comparison routine.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Fix a typo (ABSOLUATE -> ABSOLUTE)

* Add a comment over NaN comparision rule

contrib/nike/.FORMATCHECKED [new file with mode: 0644]
contrib/nike/CMakeLists.txt [new file with mode: 0644]
contrib/nike/README.md [new file with mode: 0644]
contrib/nike/include/nike/AbsoluteEpsilonEqual.h [new file with mode: 0644]
contrib/nike/src/AbsoluteEpsilonEqual.cpp [new file with mode: 0644]
contrib/nike/src/AbsoluteEpsilonEqual.test.cpp [new file with mode: 0644]

diff --git a/contrib/nike/.FORMATCHECKED b/contrib/nike/.FORMATCHECKED
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/contrib/nike/CMakeLists.txt b/contrib/nike/CMakeLists.txt
new file mode 100644 (file)
index 0000000..57feffa
--- /dev/null
@@ -0,0 +1,17 @@
+file(GLOB_RECURSE SOURCES "src/*.cpp")
+file(GLOB_RECURSE TESTS "src/*.test.cpp")
+list(REMOVE_ITEM SOURCES ${TESTS})
+
+add_library(nike STATIC ${SOURCES})
+target_include_directories(nike PUBLIC include)
+
+nncc_find_package(GTest QUIET)
+
+if(NOT GTest_FOUND)
+  return()
+endif(NOT GTest_FOUND)
+
+add_executable(nike_test ${TESTS})
+target_link_libraries(nike_test nike)
+target_link_libraries(nike_test gtest_main)
+add_test(nike_test nike_test)
diff --git a/contrib/nike/README.md b/contrib/nike/README.md
new file mode 100644 (file)
index 0000000..d002a29
--- /dev/null
@@ -0,0 +1,4 @@
+## What is "nike"?
+
+"nike" is a collection of **numeric** value comparison routines.
+- "nike" is a combination of two words: "numeric" and "dike". FYI, "dike" is the goddess of justice in ancient Greek culture.
diff --git a/contrib/nike/include/nike/AbsoluteEpsilonEqual.h b/contrib/nike/include/nike/AbsoluteEpsilonEqual.h
new file mode 100644 (file)
index 0000000..7125b57
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * 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.
+ */
+
+#ifndef __NIKE_ABSOLUTE_EPSILON_EQUAL_H__
+#define __NIKE_ABSOLUTE_EPSILON_EQUAL_H__
+
+namespace nike
+{
+
+class AbsoluteEpsilonEqualFunctor
+{
+public:
+  friend AbsoluteEpsilonEqualFunctor absolute_epsilon_equal(float);
+
+private:
+  AbsoluteEpsilonEqualFunctor(float tolerance) : _tolerance{tolerance}
+  {
+    // DO NOTHING
+  }
+
+public:
+  bool operator()(float lhs, float rhs) const;
+
+private:
+  float _tolerance;
+};
+
+/**
+ * @note AbsoluteEpsilonEqualFunctor uses its own rule for NaN values.
+ *
+ * For example, "NAN == NAN" is false but "absolute_epsilon_equal(0.001f)(NAN, NAN)" is true.
+ */
+AbsoluteEpsilonEqualFunctor absolute_epsilon_equal(float tolerance);
+
+} // namespace nike
+
+#endif // __NIKE_ABSOLUTE_EPSILON_EQUAL_H__
diff --git a/contrib/nike/src/AbsoluteEpsilonEqual.cpp b/contrib/nike/src/AbsoluteEpsilonEqual.cpp
new file mode 100644 (file)
index 0000000..5877ee8
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * 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 "nike/AbsoluteEpsilonEqual.h"
+
+#include <cmath>
+#include <cfloat>
+
+namespace nike
+{
+
+bool AbsoluteEpsilonEqualFunctor::operator()(float lhs, float rhs) const
+{
+  if (std::isnan(lhs) && std::isnan(rhs))
+  {
+    return true;
+  }
+
+  const auto diff = std::fabs(lhs - rhs);
+
+  return diff <= _tolerance;
+}
+
+AbsoluteEpsilonEqualFunctor absolute_epsilon_equal(float tolerance)
+{
+  return AbsoluteEpsilonEqualFunctor{tolerance};
+}
+
+} // namespace nike
diff --git a/contrib/nike/src/AbsoluteEpsilonEqual.test.cpp b/contrib/nike/src/AbsoluteEpsilonEqual.test.cpp
new file mode 100644 (file)
index 0000000..8475375
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * 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 "nike/AbsoluteEpsilonEqual.h"
+
+#include <cmath> // For "NAN"
+
+#include <gtest/gtest.h>
+
+TEST(AbsoluteEpsilonEqualTest, tolerance)
+{
+  auto equal = nike::absolute_epsilon_equal(0.001f);
+
+  ASSERT_TRUE(equal(NAN, NAN));
+  ASSERT_TRUE(equal(1.0f, 1.0f));
+  ASSERT_FALSE(equal(1.0f, 2.0f));
+  ASSERT_TRUE(equal(1.0f, 1.0f + 0.0001f));
+}