[AppContext] Integrate layer-devel
[platform/core/ml/nntrainer.git] / Applications / Custom / LayerPlugin / layer_plugin_test.cpp
1 // SPDX-License-Identifier: Apache-2.0
2 /**
3  * Copyright (C) 2020 Jihoon Lee <jhoon.it.lee@samsung.com>
4  *
5  * @file   layer_plugin_test.cpp
6  * @date   26 January 2021
7  * @brief  This file contains the execution part of LayerPlugin example
8  * @see    https://github.com/nnstreamer/nntrainer
9  * @author Jihoon Lee <jhoon.it.lee@samsung.com>
10  * @bug    No known bugs except for NYI items
11  *
12  */
13 #include <gtest/gtest.h>
14
15 #include <dlfcn.h>
16 #include <fstream>
17 #include <iostream>
18 #include <string>
19
20 #include <app_context.h>
21 #include <layer.h>
22 #include <layer_internal.h>
23
24 const char *NNTRAINER_PATH = std::getenv("NNTRAINER_PATH");
25
26 TEST(AppContext, DlRegisterOpen_p) {
27   ASSERT_NE(NNTRAINER_PATH, nullptr)
28     << "NNTRAINER_PATH environment value must be set";
29   auto ac = nntrainer::AppContext();
30
31   ac.registerLayerV1("libpow_layer.so", NNTRAINER_PATH);
32
33   auto layer = ac.createObject<nntrainer::LayerV1>("pow");
34
35   EXPECT_EQ(layer->getType(), "pow");
36 }
37
38 TEST(AppContext, DlRegisterWrongPath_n) {
39   ASSERT_NE(NNTRAINER_PATH, nullptr)
40     << "NNTRAINER_PATH environment value must be set";
41   auto ac = nntrainer::AppContext();
42
43   EXPECT_THROW(ac.registerLayerV1("wrong_name.so"), std::invalid_argument);
44 }
45
46 TEST(AppContext, DlRegisterDirectory_p) {
47   ASSERT_NE(NNTRAINER_PATH, nullptr)
48     << "NNTRAINER_PATH environment value must be set";
49   auto ac = nntrainer::AppContext();
50
51   ac.registerPluggableFromDirectory(NNTRAINER_PATH);
52
53   auto layer = ac.createObject<nntrainer::LayerV1>("pow");
54
55   EXPECT_EQ(layer->getType(), "pow");
56 }
57
58 TEST(AppContext, DlRegisterDirectory_n) {
59   auto ac = nntrainer::AppContext();
60
61   EXPECT_THROW(ac.registerPluggableFromDirectory("wrong path"),
62                std::invalid_argument);
63 }
64
65 TEST(AppContext, DefaultEnvironmentPath_p) {
66   /// as NNTRAINER_PATH is fed to the test, this should success without an
67   /// error
68   std::shared_ptr<ml::train::Layer> l = ml::train::createLayer("pow");
69   EXPECT_EQ(l->getType(), "pow");
70
71   auto layer = nntrainer::getLayerV1Devel(l);
72
73   std::ifstream input_file("does_not_exist");
74   EXPECT_NO_THROW(layer->read(input_file));
75   if (remove("does_not_exist")) {
76     std::cerr << "failed to remove file\n";
77   }
78
79   std::ofstream output_file("does_not_exist");
80   EXPECT_NO_THROW(layer->save(output_file));
81   if (remove("does_not_exist")) {
82     std::cerr << "failed to remove file\n";
83   }
84
85   EXPECT_NO_THROW(layer->getType());
86   EXPECT_NE(layer->setProperty({"invalid_values"}), ML_ERROR_NONE);
87   EXPECT_EQ(layer->checkValidation(), ML_ERROR_NONE);
88   EXPECT_EQ(layer->getOutputDimension()[0], nntrainer::TensorDim());
89   EXPECT_EQ(layer->getInputDimension()[0], nntrainer::TensorDim());
90 }
91
92 TEST(AppContext, DefaultEnvironmentPath_n) {
93   /// pow2 does not exist
94   EXPECT_THROW(ml::train::createLayer("pow2"), std::invalid_argument);
95 }