[Test/Prepare] Add layer semantics test
authorJihoon Lee <jhoon.it.lee@samsung.com>
Thu, 17 Jun 2021 06:15:31 +0000 (15:15 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Wed, 23 Jun 2021 07:42:19 +0000 (16:42 +0900)
For layer_v2, there will be more structured way to access tests.
This patch adds a skeleton for layer semantics test.

**Self evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test: [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Jihoon Lee <jhoon.it.lee@samsung.com>
test/unittest/layers/layers_common_tests.cpp
test/unittest/layers/layers_common_tests.h
test/unittest/layers/meson.build
test/unittest/layers/unittest_layers_fully_connected.cpp
test/unittest/layers/unittest_layers_impl.cpp

index 64849d3..f4a7235 100644 (file)
 
 #include <layers_common_tests.h>
 
-TEST_P(LayerGoldenTest, HelloWorld) { EXPECT_TRUE(true); }
+#include <layer_devel.h>
+
+constexpr unsigned SAMPLE_TRIES = 10;
+
+LayerSemantics::~LayerSemantics() {}
+
+void LayerSemantics::SetUp() {
+  auto f = std::get<0>(GetParam());
+  layer = std::move(f({}));
+  std::tie(std::ignore, expected_type, valid_properties, invalid_properties,
+           options) = GetParam();
+}
+
+void LayerSemantics::TearDown() {}
+
+TEST_P(LayerSemantics, createFromAppContext_pn) {}
 
-TEST_P(LayerCreateDestroyTest, HelloWorld) { EXPECT_TRUE(true); }
+TEST_P(LayerSemantics, setProperties_p) {
+  /// @todo check if setProperties does not collide with layerNode designated
+  /// properties
+}
+
+TEST_P(LayerSemantics, setPropertiesValidWithInvalid_n) {}
+
+TEST_P(LayerSemantics, setPropertiesValidInvalidOnly_n) {}
+
+TEST_P(LayerSemantics, finalizeTwice_p) {}
+
+TEST_P(LayerGoldenTest, HelloWorld) { EXPECT_TRUE(true); }
index d4e50fe..86df421 100644 (file)
 #ifndef __LAYERS_COMMON_TESTS_H__
 #define __LAYERS_COMMON_TESTS_H__
 
+#include <functional>
+#include <memory>
+#include <string>
+#include <vector>
+
 #include <gtest/gtest.h>
 
+namespace nntrainer {
+class Layer;
+}
+
+typedef enum {
+  AVAILABLE_FROM_APP_CONTEXT =
+    1 << 0, /**< set if layer is available from app context */
+} LayerCreateSetPropertyOptions;
+
+using LayerFactoryType = std::function<std::unique_ptr<nntrainer::Layer>(
+  const std::vector<std::string> &)>;
+
+using LayerSemanticsParamType =
+  std::tuple<LayerFactoryType /** layer factory */,
+             std::string /** Type of Layer */,
+             std::vector<std::string> /** Valid Properties */,
+             std::vector<std::string> /** Invalid Properties */,
+             unsigned int /** Options */
+             >;
+
 /**
- * @brief LayerCreateDestroyTest
- * @note NYI
- *
- * parameters required
- * 1. type
- * 2. a set of valid properties
- * 3. a set of invalid properties
+ * @brief LayerSemantics
+ * @note  This test suite includes
+ * @see   layers_common_test.cpp for details
+ * 1. Layer Creation
+ * 2. SetProperties
+ * 3. Semantics of Layer (eg) finalize twice is prohibited)
  */
-class LayerCreateDestroyTest : public ::testing::TestWithParam<const char *> {
+class LayerSemantics
+  : public ::testing::TestWithParam<LayerSemanticsParamType> {
 public:
   /**
+   * @brief Destroy the Layer Semantics object
+   *
+   */
+  virtual ~LayerSemantics();
+
+  /**
    * @brief SetUp test cases here
    *
    */
-  virtual void SetUp(){};
+  virtual void SetUp();
 
   /**
    * @brief do here if any memory needs to be released
    *
    */
-  virtual void TearDown(){};
+  virtual void TearDown();
+
+protected:
+  std::unique_ptr<nntrainer::Layer> layer;
+  std::string expected_type;
+  std::vector<std::string> valid_properties;
+  std::vector<std::string> invalid_properties;
+  unsigned int options;
 };
 
 /**
index 181ad34..b4c2476 100644 (file)
@@ -1,5 +1,6 @@
 test_target = [
   'layers_common_tests.cpp',
+  'unittest_layers_impl.cpp',
   'unittest_layers_fully_connected.cpp',
 ]
 
index 29223e8..f7d7877 100644 (file)
 
 #include <layers_common_tests.h>
 
-INSTANTIATE_TEST_CASE_P(
-  FullyConnected, LayerCreateDestroyTest,
-  ::testing::Values(
-    "golden1") /**< format of type, properties, num_batch, golden file name */);
+// INSTANTIATE_TEST_CASE_P(
+//   FullyConnected, LayerSemantics,
+//   ::testing::Values(
+//     "golden1") /**< format of type, properties, num_batch, golden file name
+//     */);
 
 INSTANTIATE_TEST_CASE_P(
   FullyConnected, LayerGoldenTest,
index d713535..d04e6ae 100644 (file)
 #include <tuple>
 
 #include <gtest/gtest.h>
-
 #include <layers_common_tests.h>
 
+#include <layer_context.h>
 #include <layer_impl.h>
+#include <string>
 
 namespace {
+
+using namespace nntrainer;
 /**
  * @brief Minimal implementation of layer impl to test layer impl itself
  *
  */
-class MockLayer : public nntrainer::LayerImpl {};
+class MockLayer final : public LayerImpl {
+public:
+  ~MockLayer() = default;
+
+  inline static const std::string type = "mock_";
+  const std::string getType() const override { return type; }
+  void finalize(InitContext &context) override { LayerImpl::finalize(context); }
+  void forwarding(RunContext &context, bool training = true) override {
+    /** do nothing */
+  }
+  void calcDerivative(RunContext &context) override { /** do nothing */
+  }
+
+  void setProperty(const std::vector<std::string> &values) override {
+    LayerImpl::setProperty(values);
+  }
+
+  void exportTo(Exporter &exporter,
+                const ExportMethods &method) const override {
+    LayerImpl::exportTo(exporter, method);
+  }
+};
 } // namespace
 
-INSTANTIATE_TEST_CASE_P(
-  LayerImpl, LayerCreateDestroyTest,
-  ::testing::Values(
-    "test") /**< format of type, properties, num_batch, golden file name */);
+auto semantic_tc = LayerSemanticsParamType(nntrainer::createLayer<MockLayer>,
+                                           MockLayer::type, {}, {}, 0);
+INSTANTIATE_TEST_CASE_P(LayerImpl, LayerSemantics,
+                        ::testing::Values(semantic_tc));
 
 INSTANTIATE_TEST_CASE_P(
   LayerImpl, LayerGoldenTest,