add append_parameters to utils and unit test
authorJim Morris <morris@wolfman.com>
Mon, 14 Sep 2015 00:07:37 +0000 (17:07 -0700)
committerJim Morris <morris@wolfman.com>
Mon, 14 Sep 2015 00:07:37 +0000 (17:07 -0700)
src/libs/utils.cpp
src/libs/utils.h
src/testframework/unittests/libs/TEST_utils.cpp

index 6c30a86..dc4ac19 100644 (file)
@@ -15,6 +15,8 @@
 #include <cstring>
 #include <stdio.h>
 #include <cstdlib>
+#include <sstream>
+
 using std::string;
 
 uint16_t get_checksum(const string &to_check)
@@ -199,6 +201,7 @@ string absolute_from_relative( string path )
     return cwd + '/' + path;
 }
 
+// FIXME this does not handle empty strings correctly
 //split a string on a delimiter, return a vector of the split tokens
 vector<string> split(const char *str, char c)
 {
@@ -216,6 +219,7 @@ vector<string> split(const char *str, char c)
     return result;
 }
 
+// FIXME this does not handle empty strings correctly
 // parse a number list "1.1,2.2,3.3" and return the numbers in a vector of floats
 vector<float> parse_number_list(const char *str)
 {
@@ -227,3 +231,12 @@ vector<float> parse_number_list(const char *str)
     }
     return r;
 }
+
+std::string append_parameters(std::map<char,float> params)
+{
+    std::ostringstream oss;
+    for(auto &i : params) {
+        oss << i.first << i.second << " ";
+    }
+    return oss.str();
+}
index 8d2756b..71c10b3 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdint.h>
 #include <string>
 #include <vector>
+#include <map>
 using std::string;
 using std::vector;
 
@@ -35,5 +36,6 @@ void system_reset( bool dfu= false );
 
 string absolute_from_relative( string path );
 
+std::string append_parameters(std::map<char,float> params);
 
 #endif
index df80cb7..167d089 100644 (file)
@@ -34,3 +34,11 @@ TEST(UtilsTest,parse_number_list)
     ASSERT_TRUE(v[1] == 2.2F);
     ASSERT_TRUE(v[2] == 3.3F);
 }
+
+TEST(UtilsTest,append_parameters)
+{
+    std::string str;
+
+    str= append_parameters({{'X', 1}, {'Y', 2}, {'Z', 3}});
+    ASSERT_EQUALS(str, "X1 Y2 Z3 ");
+}