[libcamera-devel,RFC,v2,2/9] libcamera: test: Add Value tests

Message ID 20190621161401.28337-3-kieran.bingham@ideasonboard.com
State Superseded
Headers show
Series
  • Libcamera Controls
Related show

Commit Message

Kieran Bingham June 21, 2019, 4:13 p.m. UTC
Add initial basic testing for the new Value APIs.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 test/meson.build |  1 +
 test/value.cpp   | 82 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+)
 create mode 100644 test/value.cpp

Comments

Laurent Pinchart June 22, 2019, 10:43 p.m. UTC | #1
Hi Kieran,

Thank you for the patch.

On Fri, Jun 21, 2019 at 05:13:54PM +0100, Kieran Bingham wrote:
> Add initial basic testing for the new Value APIs.
> 
> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> ---
>  test/meson.build |  1 +
>  test/value.cpp   | 82 ++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 83 insertions(+)
>  create mode 100644 test/value.cpp
> 
> diff --git a/test/meson.build b/test/meson.build
> index c36ac2479636..eff541ddc0a6 100644
> --- a/test/meson.build
> +++ b/test/meson.build
> @@ -15,6 +15,7 @@ public_tests = [
>      ['list-cameras',                    'list-cameras.cpp'],
>      ['signal',                          'signal.cpp'],
>      ['timer',                           'timer.cpp'],
> +    ['value',                           'value.cpp'],
>  ]
>  
>  internal_tests = [
> diff --git a/test/value.cpp b/test/value.cpp
> new file mode 100644
> index 000000000000..fea8cd2396cd
> --- /dev/null
> +++ b/test/value.cpp
> @@ -0,0 +1,82 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +/*
> + * Copyright (C) 2019, Google Inc.
> + *
> + * value.cpp - Value tests
> + */
> +
> +#include <iostream>
> +
> +#include <libcamera/value.h>
> +
> +#include "test.h"
> +
> +using namespace std;
> +using namespace libcamera;
> +
> +class Values : public Test
> +{
> +protected:
> +	int run()
> +	{
> +		Value integer(1234);
> +		Value boolean(true);
> +		Value string("test-string");
> +
> +		/* Just a string conversion output test... no validation */
> +		cout << "Int: " << integer.toString()
> +		     << " Bool: " << boolean.toString()
> +		     << " String: " << string.toString()
> +		     << endl;
> +
> +		/* Just a string conversion output test. Using overloaded << */
> +		cout << "Int: " << integer
> +		     << " Bool: " << boolean
> +		     << " String: " << string
> +		     << endl;

I think you should validate them both instead of printing them. You can
output to a stringstream and compare the result with an expected value.

> +
> +		if (integer.getInt() != 1234) {
> +			cerr << "Failed to get Integer" << endl;
> +			return TestFail;
> +		}
> +
> +		if (boolean.getBool() != true) {
> +			cerr << "Failed to get Boolean" << endl;
> +			return TestFail;
> +		}
> +
> +		if (string.getString() != "test-string") {
> +			cerr << "Failed to get string" << endl;
> +			return TestFail;
> +		}
> +
> +		/* Test an uninitialised value, and updating it. */
> +
> +		Value value;
> +		if (!value.isNull()) {
> +			cerr << "Empty value is non-null" << endl;
> +			return TestFail;
> +		}
> +
> +		value.set(true);
> +		if (value.isNull()) {
> +			cerr << "Failed to set an empty object" << endl;
> +			return TestFail;
> +		}
> +
> +		if (value.getBool() != true) {
> +			cerr << "Failed to get Booleans" << endl;
> +			return TestFail;
> +		}
> +
> +		value.set(10);
> +		if (value.getInt() != 10) {
> +			cerr << "Failed to get Integer" << endl;
> +			return TestFail;
> +		}
> +
> +		return TestPass;
> +	}
> +};
> +
> +TEST_REGISTER(Values)

Patch

diff --git a/test/meson.build b/test/meson.build
index c36ac2479636..eff541ddc0a6 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -15,6 +15,7 @@  public_tests = [
     ['list-cameras',                    'list-cameras.cpp'],
     ['signal',                          'signal.cpp'],
     ['timer',                           'timer.cpp'],
+    ['value',                           'value.cpp'],
 ]
 
 internal_tests = [
diff --git a/test/value.cpp b/test/value.cpp
new file mode 100644
index 000000000000..fea8cd2396cd
--- /dev/null
+++ b/test/value.cpp
@@ -0,0 +1,82 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * value.cpp - Value tests
+ */
+
+#include <iostream>
+
+#include <libcamera/value.h>
+
+#include "test.h"
+
+using namespace std;
+using namespace libcamera;
+
+class Values : public Test
+{
+protected:
+	int run()
+	{
+		Value integer(1234);
+		Value boolean(true);
+		Value string("test-string");
+
+		/* Just a string conversion output test... no validation */
+		cout << "Int: " << integer.toString()
+		     << " Bool: " << boolean.toString()
+		     << " String: " << string.toString()
+		     << endl;
+
+		/* Just a string conversion output test. Using overloaded << */
+		cout << "Int: " << integer
+		     << " Bool: " << boolean
+		     << " String: " << string
+		     << endl;
+
+		if (integer.getInt() != 1234) {
+			cerr << "Failed to get Integer" << endl;
+			return TestFail;
+		}
+
+		if (boolean.getBool() != true) {
+			cerr << "Failed to get Boolean" << endl;
+			return TestFail;
+		}
+
+		if (string.getString() != "test-string") {
+			cerr << "Failed to get string" << endl;
+			return TestFail;
+		}
+
+		/* Test an uninitialised value, and updating it. */
+
+		Value value;
+		if (!value.isNull()) {
+			cerr << "Empty value is non-null" << endl;
+			return TestFail;
+		}
+
+		value.set(true);
+		if (value.isNull()) {
+			cerr << "Failed to set an empty object" << endl;
+			return TestFail;
+		}
+
+		if (value.getBool() != true) {
+			cerr << "Failed to get Booleans" << endl;
+			return TestFail;
+		}
+
+		value.set(10);
+		if (value.getInt() != 10) {
+			cerr << "Failed to get Integer" << endl;
+			return TestFail;
+		}
+
+		return TestPass;
+	}
+};
+
+TEST_REGISTER(Values)