소스 검색

Extended STLUtils class

- added "getListElementAt" method to get an element at specific
index
- extended tests for STLUtils class
patrick 4 년 전
부모
커밋
edacb1a573
2개의 변경된 파일43개의 추가작업 그리고 0개의 파일을 삭제
  1. 19 0
      source/utils/STLUtils.hpp
  2. 24 0
      test/cases/utils/STLUtilsTest.cpp

+ 19 - 0
source/utils/STLUtils.hpp

@@ -24,6 +24,25 @@ namespace ls_std {
       static bool contains(container _container, const dataType& _value) {
       static bool contains(container _container, const dataType& _value) {
         return std::find(_container.begin(), _container.end(), _value) != _container.end();
         return std::find(_container.begin(), _container.end(), _value) != _container.end();
       }
       }
+
+      template<class dataType>
+      static dataType getListElementAt(const std::list<dataType>& _list, size_t _index) {
+        dataType value {};
+        size_t counter {};
+
+        if(_index < _list.size()) {
+          for(const auto& _value : _list) {
+            if(counter == _index) {
+              value = _value;
+              break;
+            }
+
+            counter++;
+          }
+        }
+
+        return value;
+      }
   };
   };
 }
 }
 
 

+ 24 - 0
test/cases/utils/STLUtilsTest.cpp

@@ -50,4 +50,28 @@ namespace {
     ASSERT_FALSE((ls_std::STLUtils::contains(names, "Lena")));
     ASSERT_FALSE((ls_std::STLUtils::contains(names, "Lena")));
     ASSERT_FALSE((ls_std::STLUtils::contains(names, "Mirco")));
     ASSERT_FALSE((ls_std::STLUtils::contains(names, "Mirco")));
   }
   }
+
+  TEST_F(STLUtilsTest, getListElementAt)
+  {
+    std::list<int> values{1, 13, 7, 8};
+    std::list<std::string> names{"Tim", "Alex", "Nadine"};
+
+    ASSERT_EQ(1, (ls_std::STLUtils::getListElementAt(values, 0)));
+    ASSERT_EQ(13, (ls_std::STLUtils::getListElementAt(values, 1)));
+    ASSERT_EQ(7, (ls_std::STLUtils::getListElementAt(values, 2)));
+    ASSERT_EQ(8, (ls_std::STLUtils::getListElementAt(values, 3)));
+
+    ASSERT_STREQ("Tim", ls_std::STLUtils::getListElementAt(names, 0).c_str());
+    ASSERT_STREQ("Alex", ls_std::STLUtils::getListElementAt(names, 1).c_str());
+    ASSERT_STREQ("Nadine", ls_std::STLUtils::getListElementAt(names, 2).c_str());
+  }
+
+  TEST_F(STLUtilsTest, getListElementAtNegative)
+  {
+    std::list<int> values{1, 13, 7, 8};
+    std::list<std::string> names{"Tim", "Alex", "Nadine"};
+
+    ASSERT_EQ(0, (ls_std::STLUtils::getListElementAt(values, 15)));
+    ASSERT_STREQ("", ls_std::STLUtils::getListElementAt(names, 15).c_str());
+  }
 }
 }