[libcamera-devel,2/3] test: file-descriptor: Add "fd move" constructor test

Message ID 20200518164804.10088-2-laurent.pinchart@ideasonboard.com
State Accepted
Commit c7463138c666d826de26969bf03819936a8cf664
Headers show
Series
  • [libcamera-devel,1/3] libcamera: file_descriptor: Implement move semantics for constructor
Related show

Commit Message

Laurent Pinchart May 18, 2020, 4:48 p.m. UTC
Add a test for the newly added "fd move" constructor.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 test/file-descriptor.cpp | 42 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

Comments

Niklas Söderlund May 19, 2020, 1:44 p.m. UTC | #1
Hi Laurent,

Thanks for your patch.

On 2020-05-18 19:48:03 +0300, Laurent Pinchart wrote:
> Add a test for the newly added "fd move" constructor.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  test/file-descriptor.cpp | 42 ++++++++++++++++++++++++++++++++++++----
>  1 file changed, 38 insertions(+), 4 deletions(-)
> 
> diff --git a/test/file-descriptor.cpp b/test/file-descriptor.cpp
> index 7477a843531c..aa3c896fb937 100644
> --- a/test/file-descriptor.cpp
> +++ b/test/file-descriptor.cpp
> @@ -56,16 +56,19 @@ protected:
>  		delete desc1_;
>  		desc1_ = nullptr;
>  
> -		/* Test creating FileDescriptor from numerical file descriptor. */
> +		/*
> +		 * Test creating FileDescriptor by copying numerical file
> +		 * descriptor.
> +		 */
>  		desc1_ = new FileDescriptor(fd_);
>  		if (desc1_->fd() == fd_) {
> -			std::cout << "Failed fd numerical check (int constructor)"
> +			std::cout << "Failed fd numerical check (lvalue ref constructor)"
>  				  << std::endl;
>  			return TestFail;
>  		}
>  
>  		if (!isValidFd(fd_) || !isValidFd(desc1_->fd())) {
> -			std::cout << "Failed fd validity after construction (int constructor)"
> +			std::cout << "Failed fd validity after construction (lvalue ref constructor)"
>  				  << std::endl;
>  			return TestFail;
>  		}
> @@ -76,7 +79,38 @@ protected:
>  		desc1_ = nullptr;
>  
>  		if (!isValidFd(fd_) || isValidFd(fd)) {
> -			std::cout << "Failed fd validity after destruction (int constructor)"
> +			std::cout << "Failed fd validity after destruction (lvalue ref constructor)"
> +				  << std::endl;
> +			return TestFail;
> +		}
> +
> +		/*
> +		 * Test creating FileDescriptor by taking ownership of
> +		 * numerical file descriptor.
> +		 */
> +		int dupFd = dup(fd_);
> +		int dupFdCopy = dupFd;
> +
> +		desc1_ = new FileDescriptor(std::move(dupFd));
> +		if (desc1_->fd() != dupFdCopy) {
> +			std::cout << "Failed fd numerical check (rvalue ref constructor)"
> +				  << std::endl;
> +			return TestFail;
> +		}
> +
> +		if (dupFd != -1 || !isValidFd(fd_) || !isValidFd(desc1_->fd())) {
> +			std::cout << "Failed fd validity after construction (rvalue ref constructor)"
> +				  << std::endl;
> +			return TestFail;
> +		}
> +
> +		fd = desc1_->fd();
> +
> +		delete desc1_;
> +		desc1_ = nullptr;
> +
> +		if (!isValidFd(fd_) || isValidFd(fd)) {
> +			std::cout << "Failed fd validity after destruction (rvalue ref constructor)"
>  				  << std::endl;
>  			return TestFail;
>  		}
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/test/file-descriptor.cpp b/test/file-descriptor.cpp
index 7477a843531c..aa3c896fb937 100644
--- a/test/file-descriptor.cpp
+++ b/test/file-descriptor.cpp
@@ -56,16 +56,19 @@  protected:
 		delete desc1_;
 		desc1_ = nullptr;
 
-		/* Test creating FileDescriptor from numerical file descriptor. */
+		/*
+		 * Test creating FileDescriptor by copying numerical file
+		 * descriptor.
+		 */
 		desc1_ = new FileDescriptor(fd_);
 		if (desc1_->fd() == fd_) {
-			std::cout << "Failed fd numerical check (int constructor)"
+			std::cout << "Failed fd numerical check (lvalue ref constructor)"
 				  << std::endl;
 			return TestFail;
 		}
 
 		if (!isValidFd(fd_) || !isValidFd(desc1_->fd())) {
-			std::cout << "Failed fd validity after construction (int constructor)"
+			std::cout << "Failed fd validity after construction (lvalue ref constructor)"
 				  << std::endl;
 			return TestFail;
 		}
@@ -76,7 +79,38 @@  protected:
 		desc1_ = nullptr;
 
 		if (!isValidFd(fd_) || isValidFd(fd)) {
-			std::cout << "Failed fd validity after destruction (int constructor)"
+			std::cout << "Failed fd validity after destruction (lvalue ref constructor)"
+				  << std::endl;
+			return TestFail;
+		}
+
+		/*
+		 * Test creating FileDescriptor by taking ownership of
+		 * numerical file descriptor.
+		 */
+		int dupFd = dup(fd_);
+		int dupFdCopy = dupFd;
+
+		desc1_ = new FileDescriptor(std::move(dupFd));
+		if (desc1_->fd() != dupFdCopy) {
+			std::cout << "Failed fd numerical check (rvalue ref constructor)"
+				  << std::endl;
+			return TestFail;
+		}
+
+		if (dupFd != -1 || !isValidFd(fd_) || !isValidFd(desc1_->fd())) {
+			std::cout << "Failed fd validity after construction (rvalue ref constructor)"
+				  << std::endl;
+			return TestFail;
+		}
+
+		fd = desc1_->fd();
+
+		delete desc1_;
+		desc1_ = nullptr;
+
+		if (!isValidFd(fd_) || isValidFd(fd)) {
+			std::cout << "Failed fd validity after destruction (rvalue ref constructor)"
 				  << std::endl;
 			return TestFail;
 		}