From nobody Sat Sep 27 20:23:27 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD13630F55C for ; Tue, 26 Aug 2025 09:13:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756199636; cv=none; b=a3uoFDdPYP2YhbEUVHIHpl7zvAe2GUeQa1Zh70a0qQCDktJ46KwByWtc7WP+REsl/ouXM9II0c5FgOwM4R/CFIzDILZ+XmRaREQ09EesQKkwKCBL07mawdrvaxv81p1gx6uJ3db//0Dnca5JsEmbCnit1isLEw0bD9MO9galnqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756199636; c=relaxed/simple; bh=YYAqQOrOM9pO/vDtcPG9o4KAmJKgdt7NSEdpwQyFils=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=loiIKbYqHD8DFdxFddjA00W4blmJji0KV+Glco61q3h3UrHN+sS2yKX/P96GkSpgjPDNwZtUTDctWUeOnkhc1vw5mAiUF5Rp+IgfBiUy4lhzQgvNOlZQP/H1kM3rDsjk9nFCZxBfJJEoZXsRzmcV9PqOQ5x2h4LP1H/Pp5Uqy4s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--davidgow.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GxLzhGbw; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--davidgow.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GxLzhGbw" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b474b68cff7so4346497a12.1 for ; Tue, 26 Aug 2025 02:13:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1756199634; x=1756804434; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=r6YJBLokWG2Gb5OXsNbbrh9Xzkxl6kNwLj7uGaP0ytI=; b=GxLzhGbwnoXUIoD2LCD5GErKeZVHWnQ6UYKyvIJw1f6fawtHHu49nhEPY4EwKjIIod 6E3UJkna842bG7Zc3vLOScDfqRdqn6DOukKfenFaOJsKTr5dLLh6kuX9wMZIoPP0asUZ 2OaAKfJzDinjJTRhQGe2k7Estyd5gMRihIt6uOItGxBCfSEndZsQCAWMQQCoCXf17A6K SP7E8ypvOtudysiI+Cb2NPhvAnZSa+Z7U79TCop19n7j687h2xz9LGk9PdAM4F3668K1 rssnpAtC99Hnx/QlBcAT14oVeffxp7Jj8+DQFQ1+ZiRmq3/IcBJwxJnvrdoylZcf/q/E PoHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756199634; x=1756804434; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=r6YJBLokWG2Gb5OXsNbbrh9Xzkxl6kNwLj7uGaP0ytI=; b=nbJumnqJHI9ybtzUZpnwJ8QWhvQwM9OE3pfz65Ui21/eeLwvP0v/RPQsRW9GedISWf DfM9bLKGeh6LvT8znD8D2FyPC9hhgwtptr9k6xh/2cd7VtQ6rEZeHMz/45uB1o/1VW4p O0+blfQ/UuLAryFXwg5Cwqrrj2odEpX8TlXYFN9L+SQisS0aQT181iV+J6+2+VhnBSJF DpBg8bAD0ktrx2yulPQMVrL/CJoKj+4wt47pc+gZ7RbjqRCXPfUe7XWowMID+ZKvEeZV HXe+qn2KbQAtA82ecIZkdumMUno2lKW7Rhi2JNVcQqpP/xM0OwI88NuPYRfPj4Aq1Xw8 VPyg== X-Forwarded-Encrypted: i=1; AJvYcCUMAiZJd1Ahe+B53YJIFC1Lb+SQKu6kS80I4bkvRGAO+WG1nyx+mDBkkCJLLUMNja83vi3eIonNe2NbkrE=@vger.kernel.org X-Gm-Message-State: AOJu0YyBmGabRJ1TFZwhsL7BDlUTGOuEeCPOAhFeBUPu6qSWo16bLIX/ yx+MMx0BhteFxDOzvVpdLCFyP+M93jlFNR03gircYpypiTlfia7cjUgvQv2rHAy74GplPYJl1wf qv8QlOaHTP6mfqg== X-Google-Smtp-Source: AGHT+IFaxfaFtDgDXFqhlggWIN6qGO50AHV0jzc2236uvg1CHDn3C9T5tAHUO2oFudu0A5MM9qc/raG7mN2Efw== X-Received: from pjbpm5.prod.google.com ([2002:a17:90b:3c45:b0:31f:6ddd:ef5]) (user=davidgow job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:c202:b0:243:6f08:86b4 with SMTP id adf61e73a8af0-2436f088fe5mr8170082637.39.1756199634209; Tue, 26 Aug 2025 02:13:54 -0700 (PDT) Date: Tue, 26 Aug 2025 17:13:36 +0800 In-Reply-To: <20250826091341.1427123-1-davidgow@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250826091341.1427123-1-davidgow@google.com> X-Mailer: git-send-email 2.51.0.261.g7ce5a0a67e-goog Message-ID: <20250826091341.1427123-7-davidgow@google.com> Subject: [PATCH v4 6/7] kunit: Add example parameterized test with direct dynamic parameter array setup From: David Gow To: Marie Zhussupova , marievictoria875@gmail.com, rmoar@google.com, shuah@kernel.org, brendan.higgins@linux.dev Cc: mark.rutland@arm.com, elver@google.com, dvyukov@google.com, lucas.demarchi@intel.com, thomas.hellstrom@linux.intel.com, rodrigo.vivi@intel.com, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, kasan-dev@googlegroups.com, intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Stephen Rothwell , David Gow Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Marie Zhussupova Introduce example_params_test_with_init_dynamic_arr(). This new KUnit test demonstrates directly assigning a dynamic parameter array, using the kunit_register_params_array() macro, to a parameterized test context. It highlights the use of param_init() and param_exit() for initialization and exit of a parameterized test, and their registration to the test case with KUNIT_CASE_PARAM_WITH_INIT(). Reviewed-by: Rae Moar Reviewed-by: David Gow Signed-off-by: Marie Zhussupova Signed-off-by: David Gow --- Changes in v4: v3: https://lore.kernel.org/linux-kselftest/20250815103604.3857930-7-mariev= ic@google.com/ - No changes. Changes in v3: v2: https://lore.kernel.org/all/20250811221739.2694336-7-marievic@google.co= m/ - No changes. Changes in v2: v1: https://lore.kernel.org/all/20250729193647.3410634-9-marievic@google.co= m/ - kunit_array_gen_params() is now explicitly passed to KUNIT_CASE_PARAM_WITH_INIT() to be consistent with the parameterized test being defined by the existence of the generate_params() function. - param_init() was changed to output a log at the start of a parameterized test. - The parameter array was changed to be allocated using kunit_kmalloc_array= (), a KUnit memory allocation API, as that would be the preferred/easier meth= od. To still demonstrate a use of param_exit(), it now outputs a log at the e= nd of the parameterized test. - The comments and the commit message were changed to reflect the parameterized testing terminology. See the patch series cover letter change log for the definitions. --- lib/kunit/kunit-example-test.c | 104 +++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index 3e858367be01..9452b163956f 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -388,6 +388,107 @@ static void example_params_test_with_init(struct kuni= t *test) kunit_put_resource(res); } =20 +/* + * Helper function to create a parameter array of Fibonacci numbers. This = example + * highlights a parameter generation scenario that is: + * 1. Not feasible to fully pre-generate at compile time. + * 2. Challenging to implement with a standard generate_params() function, + * as it only provides the previous parameter, while Fibonacci requires + * access to two preceding values for calculation. + */ +static void *make_fibonacci_params(struct kunit *test, size_t seq_size) +{ + int *seq; + + if (seq_size <=3D 0) + return NULL; + /* + * Using kunit_kmalloc_array here ties the lifetime of the array to + * the parameterized test i.e. it will get automatically cleaned up + * by KUnit after the parameterized test finishes. + */ + seq =3D kunit_kmalloc_array(test, seq_size, sizeof(int), GFP_KERNEL); + + if (!seq) + return NULL; + if (seq_size >=3D 1) + seq[0] =3D 0; + if (seq_size >=3D 2) + seq[1] =3D 1; + for (int i =3D 2; i < seq_size; i++) + seq[i] =3D seq[i - 1] + seq[i - 2]; + return seq; +} + +/* + * This is an example of a function that provides a description for each o= f the + * parameters. + */ +static void example_param_dynamic_arr_get_desc(struct kunit *test, const v= oid *p, char *desc) +{ + const int *fib_num =3D p; + + snprintf(desc, KUNIT_PARAM_DESC_SIZE, "fibonacci param: %d", *fib_num); +} + +/* + * Example of a parameterized test param_init() function that registers a = dynamic + * array of parameters. + */ +static int example_param_init_dynamic_arr(struct kunit *test) +{ + size_t seq_size; + int *fibonacci_params; + + kunit_info(test, "initializing parameterized test\n"); + + seq_size =3D 6; + fibonacci_params =3D make_fibonacci_params(test, seq_size); + + if (!fibonacci_params) + return -ENOMEM; + + /* + * Passes the dynamic parameter array information to the parameterized te= st + * context struct kunit. The array and its metadata will be stored in + * test->parent->params_array. The array itself will be located in + * params_data.params. + * + * Note that you will need to pass kunit_array_gen_params() as the + * generator function to KUNIT_CASE_PARAM_WITH_INIT() when registering + * a parameter array this route. + */ + kunit_register_params_array(test, fibonacci_params, seq_size, + example_param_dynamic_arr_get_desc); + return 0; +} + +/* + * Example of a parameterized test param_exit() function that outputs a log + * at the end of the parameterized test. It could also be used for any oth= er + * teardown logic. + */ +static void example_param_exit_dynamic_arr(struct kunit *test) +{ + kunit_info(test, "exiting parameterized test\n"); +} + +/* + * Example of test that uses the registered dynamic array to perform asser= tions + * and expectations. + */ +static void example_params_test_with_init_dynamic_arr(struct kunit *test) +{ + const int *param =3D test->param_value; + int param_val; + + /* By design, param pointer will not be NULL. */ + KUNIT_ASSERT_NOT_NULL(test, param); + + param_val =3D *param; + KUNIT_EXPECT_EQ(test, param_val - param_val, 0); +} + /* * Here we make a list of all the test cases we want to add to the test su= ite * below. @@ -409,6 +510,9 @@ static struct kunit_case example_test_cases[] =3D { KUNIT_CASE_PARAM(example_params_test, example_gen_params), KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, kunit_array_gen= _params, example_param_init, NULL), + KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_dynamic_arr, + kunit_array_gen_params, example_param_init_dynamic_arr, + example_param_exit_dynamic_arr), KUNIT_CASE_SLOW(example_slow_test), {} }; --=20 2.51.0.261.g7ce5a0a67e-goog