[AppContext] Fix key is case sensitive
authorJihoon Lee <jhoon.it.lee@samsung.com>
Mon, 16 Nov 2020 06:59:20 +0000 (15:59 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 30 Nov 2020 04:41:05 +0000 (13:41 +0900)
In current semantics, type key should be case insensitive however case
was sensitive, this patch fixes the issue.

**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>
nntrainer/app_context.h
test/ccapi/unittest_ccapi.cpp

index 86598a4..227d8f8 100644 (file)
@@ -15,6 +15,7 @@
 #ifndef __APP_CONTEXT_H__
 #define __APP_CONTEXT_H__
 
+#include <algorithm>
 #include <functional>
 #include <memory>
 #include <mutex>
@@ -124,7 +125,11 @@ public:
     auto &str_map = std::get<StrIndexType<T>>(index);
     auto &int_map = std::get<IntIndexType>(index);
 
-    const std::string &assigned_key = key == "" ? factory({})->getType() : key;
+    std::string assigned_key = key == "" ? factory({})->getType() : key;
+
+    std::transform(assigned_key.begin(), assigned_key.end(),
+                   assigned_key.begin(),
+                   [](unsigned char c) { return std::tolower(c); });
 
     const std::lock_guard<std::mutex> lock(factory_mutex);
     if (str_map.find(assigned_key) != str_map.end()) {
@@ -187,11 +192,17 @@ public:
     auto &index = std::get<IndexType<T>>(factory_map);
     auto &str_map = std::get<StrIndexType<T>>(index);
 
-    const auto &entry = str_map.find(key);
+    std::string lower_key;
+    lower_key.resize(key.length());
+
+    std::transform(key.begin(), key.end(), lower_key.begin(),
+                   [](unsigned char c) { return std::tolower(c); });
+
+    const auto &entry = str_map.find(lower_key);
 
     if (entry == str_map.end()) {
       std::stringstream ss;
-      ss << "Key is not found for the object. Key: " << key;
+      ss << "Key is not found for the object. Key: " << lower_key;
       throw exception::not_supported(ss.str().c_str());
     }
 
index 5dfdf3b..5912a78 100644 (file)
@@ -21,7 +21,7 @@
 #include <optimizer.h>
 
 /**
- * @brief Neural Network Model Contruct Test
+ * @brief Neural Network Model Construct Test
  */
 TEST(ccapi_model, construct_01_n) {
   EXPECT_THROW(ml::train::createModel(ml::train::ModelType::UNKNOWN),
@@ -32,14 +32,14 @@ TEST(ccapi_model, construct_01_n) {
 }
 
 /**
- * @brief Neural Network Model Contruct Test
+ * @brief Neural Network Model Construct Test
  */
 TEST(ccapi_model, construct_02_p) {
   EXPECT_NO_THROW(ml::train::createModel(ml::train::ModelType::NEURAL_NET));
 }
 
 /**
- * @brief Neural Network Layer Contruct Test
+ * @brief Neural Network Layer Construct Test
  */
 TEST(ccapi_layer, construct_01_n) {
   EXPECT_THROW(ml::train::createLayer("unknown type"), std::invalid_argument);