diff mbox series

[v5,2/5] drm/tests: Add test cases for drm_rect_calc_hscale()

Message ID 20230418230146.461129-3-arthurgrillo@riseup.net (mailing list archive)
State New, archived
Headers show
Series Create tests for the drm_rect functions | expand

Commit Message

Arthur Grillo April 18, 2023, 11:01 p.m. UTC
Insert parameterized test for the drm_rect_calc_hscale() to ensure
correctness and prevent future regressions. Besides the test for the
usual case, tests the exceptions.

Signed-off-by: Arthur Grillo <arthurgrillo@riseup.net>
---
 drivers/gpu/drm/tests/drm_rect_test.c | 72 +++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

Comments

Maíra Canal April 19, 2023, 12:12 a.m. UTC | #1
On 4/18/23 20:01, Arthur Grillo wrote:
> Insert parameterized test for the drm_rect_calc_hscale() to ensure
> correctness and prevent future regressions. Besides the test for the
> usual case, tests the exceptions.
> 
> Signed-off-by: Arthur Grillo <arthurgrillo@riseup.net>

Thanks for the quick fix!

Reviewed-by: Maíra Canal <mcanal@igalia.com>

Best Regards,
- Maíra Canal

> ---
>   drivers/gpu/drm/tests/drm_rect_test.c | 72 +++++++++++++++++++++++++++
>   1 file changed, 72 insertions(+)
> 
> diff --git a/drivers/gpu/drm/tests/drm_rect_test.c b/drivers/gpu/drm/tests/drm_rect_test.c
> index 33584f63be37..aae682ad9ee1 100644
> --- a/drivers/gpu/drm/tests/drm_rect_test.c
> +++ b/drivers/gpu/drm/tests/drm_rect_test.c
> @@ -10,6 +10,7 @@
>   #include <drm/drm_rect.h>
>   
>   #include <linux/string_helpers.h>
> +#include <linux/errno.h>
>   
>   static void drm_rect_compare(struct kunit *test, const struct drm_rect *r,
>   			     const struct drm_rect *expected)
> @@ -343,12 +344,83 @@ static void drm_test_rect_intersect(struct kunit *test)
>   	drm_rect_compare(test, &r1_aux, &params->expected_intersection);
>   }
>   
> +struct drm_rect_scale_case {
> +	const char *name;
> +	struct drm_rect src, dst;
> +	int min_range, max_range;
> +	int expected_scaling_factor;
> +};
> +
> +static const struct drm_rect_scale_case drm_rect_scale_cases[] = {
> +	{
> +		.name = "normal use",
> +		.src = DRM_RECT_INIT(0, 0, 2 << 16, 2 << 16),
> +		.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
> +		.min_range = 0, .max_range = INT_MAX,
> +		.expected_scaling_factor = 2,
> +	},
> +	{
> +		.name = "out of max range",
> +		.src = DRM_RECT_INIT(0, 0, 10 << 16, 10 << 16),
> +		.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
> +		.min_range = 3, .max_range = 5,
> +		.expected_scaling_factor = -ERANGE,
> +	},
> +	{
> +		.name = "out of min range",
> +		.src = DRM_RECT_INIT(0, 0, 2 << 16, 2 << 16),
> +		.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
> +		.min_range = 3, .max_range = 5,
> +		.expected_scaling_factor = -ERANGE,
> +	},
> +	{
> +		.name = "zero dst",
> +		.src = DRM_RECT_INIT(0, 0, 2 << 16, 2 << 16),
> +		.dst = DRM_RECT_INIT(0, 0, 0 << 16, 0 << 16),
> +		.min_range = 0, .max_range = INT_MAX,
> +		.expected_scaling_factor = 0,
> +	},
> +	{
> +		.name = "negative src",
> +		.src = DRM_RECT_INIT(0, 0, -(1 << 16), -(1 << 16)),
> +		.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
> +		.min_range = 0, .max_range = INT_MAX,
> +		.expected_scaling_factor = -EINVAL,
> +	},
> +	{
> +		.name = "negative dst",
> +		.src = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
> +		.dst = DRM_RECT_INIT(0, 0, -(1 << 16), -(1 << 16)),
> +		.min_range = 0, .max_range = INT_MAX,
> +		.expected_scaling_factor = -EINVAL,
> +	},
> +};
> +
> +static void drm_rect_scale_case_desc(const struct drm_rect_scale_case *t, char *desc)
> +{
> +	strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
> +}
> +
> +KUNIT_ARRAY_PARAM(drm_rect_scale, drm_rect_scale_cases, drm_rect_scale_case_desc);
> +
> +static void drm_test_rect_calc_hscale(struct kunit *test)
> +{
> +	const struct drm_rect_scale_case *params = test->param_value;
> +	int scaling_factor;
> +
> +	scaling_factor = drm_rect_calc_hscale(&params->src, &params->dst,
> +					      params->min_range, params->max_range);
> +
> +	KUNIT_EXPECT_EQ(test, scaling_factor, params->expected_scaling_factor);
> +}
> +
>   static struct kunit_case drm_rect_tests[] = {
>   	KUNIT_CASE(drm_test_rect_clip_scaled_div_by_zero),
>   	KUNIT_CASE(drm_test_rect_clip_scaled_not_clipped),
>   	KUNIT_CASE(drm_test_rect_clip_scaled_clipped),
>   	KUNIT_CASE(drm_test_rect_clip_scaled_signed_vs_unsigned),
>   	KUNIT_CASE_PARAM(drm_test_rect_intersect, drm_rect_intersect_gen_params),
> +	KUNIT_CASE_PARAM(drm_test_rect_calc_hscale, drm_rect_scale_gen_params),
>   	{ }
>   };
>
diff mbox series

Patch

diff --git a/drivers/gpu/drm/tests/drm_rect_test.c b/drivers/gpu/drm/tests/drm_rect_test.c
index 33584f63be37..aae682ad9ee1 100644
--- a/drivers/gpu/drm/tests/drm_rect_test.c
+++ b/drivers/gpu/drm/tests/drm_rect_test.c
@@ -10,6 +10,7 @@ 
 #include <drm/drm_rect.h>
 
 #include <linux/string_helpers.h>
+#include <linux/errno.h>
 
 static void drm_rect_compare(struct kunit *test, const struct drm_rect *r,
 			     const struct drm_rect *expected)
@@ -343,12 +344,83 @@  static void drm_test_rect_intersect(struct kunit *test)
 	drm_rect_compare(test, &r1_aux, &params->expected_intersection);
 }
 
+struct drm_rect_scale_case {
+	const char *name;
+	struct drm_rect src, dst;
+	int min_range, max_range;
+	int expected_scaling_factor;
+};
+
+static const struct drm_rect_scale_case drm_rect_scale_cases[] = {
+	{
+		.name = "normal use",
+		.src = DRM_RECT_INIT(0, 0, 2 << 16, 2 << 16),
+		.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
+		.min_range = 0, .max_range = INT_MAX,
+		.expected_scaling_factor = 2,
+	},
+	{
+		.name = "out of max range",
+		.src = DRM_RECT_INIT(0, 0, 10 << 16, 10 << 16),
+		.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
+		.min_range = 3, .max_range = 5,
+		.expected_scaling_factor = -ERANGE,
+	},
+	{
+		.name = "out of min range",
+		.src = DRM_RECT_INIT(0, 0, 2 << 16, 2 << 16),
+		.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
+		.min_range = 3, .max_range = 5,
+		.expected_scaling_factor = -ERANGE,
+	},
+	{
+		.name = "zero dst",
+		.src = DRM_RECT_INIT(0, 0, 2 << 16, 2 << 16),
+		.dst = DRM_RECT_INIT(0, 0, 0 << 16, 0 << 16),
+		.min_range = 0, .max_range = INT_MAX,
+		.expected_scaling_factor = 0,
+	},
+	{
+		.name = "negative src",
+		.src = DRM_RECT_INIT(0, 0, -(1 << 16), -(1 << 16)),
+		.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
+		.min_range = 0, .max_range = INT_MAX,
+		.expected_scaling_factor = -EINVAL,
+	},
+	{
+		.name = "negative dst",
+		.src = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
+		.dst = DRM_RECT_INIT(0, 0, -(1 << 16), -(1 << 16)),
+		.min_range = 0, .max_range = INT_MAX,
+		.expected_scaling_factor = -EINVAL,
+	},
+};
+
+static void drm_rect_scale_case_desc(const struct drm_rect_scale_case *t, char *desc)
+{
+	strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(drm_rect_scale, drm_rect_scale_cases, drm_rect_scale_case_desc);
+
+static void drm_test_rect_calc_hscale(struct kunit *test)
+{
+	const struct drm_rect_scale_case *params = test->param_value;
+	int scaling_factor;
+
+	scaling_factor = drm_rect_calc_hscale(&params->src, &params->dst,
+					      params->min_range, params->max_range);
+
+	KUNIT_EXPECT_EQ(test, scaling_factor, params->expected_scaling_factor);
+}
+
 static struct kunit_case drm_rect_tests[] = {
 	KUNIT_CASE(drm_test_rect_clip_scaled_div_by_zero),
 	KUNIT_CASE(drm_test_rect_clip_scaled_not_clipped),
 	KUNIT_CASE(drm_test_rect_clip_scaled_clipped),
 	KUNIT_CASE(drm_test_rect_clip_scaled_signed_vs_unsigned),
 	KUNIT_CASE_PARAM(drm_test_rect_intersect, drm_rect_intersect_gen_params),
+	KUNIT_CASE_PARAM(drm_test_rect_calc_hscale, drm_rect_scale_gen_params),
 	{ }
 };