[libcamera-devel,2/3] test: Add a utils::split() test

Message ID 20200213130908.23638-3-kieran.bingham@ideasonboard.com
State Accepted
Headers show
Series
  • utils: Provide string splitting
Related show

Commit Message

Kieran Bingham Feb. 13, 2020, 1:09 p.m. UTC
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

The test constructs a string by joining substrings, splits it, and
verifies that the original and resulting substrings match.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 test/utils.cpp | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

Comments

Kieran Bingham Feb. 13, 2020, 1:25 p.m. UTC | #1
Hi Laurent,

On 13/02/2020 13:09, Kieran Bingham wrote:
> From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> 
> The test constructs a string by joining substrings, splits it, and
> verifies that the original and resulting substrings match.
> 

Adding tests! What madness is this!

> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  test/utils.cpp | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
> 
> diff --git a/test/utils.cpp b/test/utils.cpp
> index 9fe0d4775b73..db1fbdde847d 100644
> --- a/test/utils.cpp
> +++ b/test/utils.cpp
> @@ -7,6 +7,8 @@
>  
>  #include <iostream>
>  #include <sstream>
> +#include <string>
> +#include <vector>
>  
>  #include "test.h"
>  #include "utils.h"
> @@ -19,6 +21,7 @@ class UtilsTest : public Test
>  protected:
>  	int run()
>  	{
> +		/* utils::hex() test. */
>  		std::ostringstream os;
>  		std::string ref;
>  
> @@ -46,6 +49,28 @@ protected:
>  			return TestFail;
>  		}
>  
> +		/* utils::split() test. */
> +		std::vector<std::string> elements = {
> +			"/bin",
> +			"/usr/bin",
> +			"",
> +			"",
> +		};
> +
> +		std::string path;
> +		for (const auto &element : elements)
> +			path += (path.empty() ? "" : ":") + element;
> +
> +		std::vector<std::string> dirs;
> +
> +		for (const auto &dir : utils::split(path, ":"))
> +			dirs.push_back(dir);
> +
> +		if (dirs != elements) {
> +			cerr << "utils::split() test failed" << endl;
> +			return TestFail;
> +		}
> +
>  		return TestPass;
>  	}
>  };
>

Patch

diff --git a/test/utils.cpp b/test/utils.cpp
index 9fe0d4775b73..db1fbdde847d 100644
--- a/test/utils.cpp
+++ b/test/utils.cpp
@@ -7,6 +7,8 @@ 
 
 #include <iostream>
 #include <sstream>
+#include <string>
+#include <vector>
 
 #include "test.h"
 #include "utils.h"
@@ -19,6 +21,7 @@  class UtilsTest : public Test
 protected:
 	int run()
 	{
+		/* utils::hex() test. */
 		std::ostringstream os;
 		std::string ref;
 
@@ -46,6 +49,28 @@  protected:
 			return TestFail;
 		}
 
+		/* utils::split() test. */
+		std::vector<std::string> elements = {
+			"/bin",
+			"/usr/bin",
+			"",
+			"",
+		};
+
+		std::string path;
+		for (const auto &element : elements)
+			path += (path.empty() ? "" : ":") + element;
+
+		std::vector<std::string> dirs;
+
+		for (const auto &dir : utils::split(path, ":"))
+			dirs.push_back(dir);
+
+		if (dirs != elements) {
+			cerr << "utils::split() test failed" << endl;
+			return TestFail;
+		}
+
 		return TestPass;
 	}
 };