From patchwork Sat Oct 10 14:53:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arpitha Raghunandan <98.arpi@gmail.com> X-Patchwork-Id: 11830453 X-Patchwork-Delegate: brendanhiggins@google.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0D6B116C1 for ; Sat, 10 Oct 2020 23:09:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E5D9220795 for ; Sat, 10 Oct 2020 23:09:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="pDMCrxd/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390289AbgJJW4w (ORCPT ); Sat, 10 Oct 2020 18:56:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731493AbgJJT3p (ORCPT ); Sat, 10 Oct 2020 15:29:45 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 681A0C05BD23; Sat, 10 Oct 2020 07:54:45 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id d1so1061953pfc.0; Sat, 10 Oct 2020 07:54:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/krkgGWRLPwrBOotoEiPAuKzZC5AWMQrtjDnDpAK2FI=; b=pDMCrxd/zMBAJCqjki45oqVCT+zX55BDa9Ycymnp0lMpeZBUVnoSz8ksHcktrZKRq7 lcOeufePSzUUMeU3fzBGlYBph5pcUaZQqt87oykxn6idISuIc7fhlRTpWA+RU5alacTr 4iegTO4ybojBhElULXjQToX8+ifPswiUXZ+drmKsH0JC6K16Ovy8cc+/Vyh+Lt9D+pr6 AsQ0CFbyOm1uvljB+Ej8t+Dufxz3p+heCtLN4ENRcAh3rtwWPkSWV8MalbKb96YHx6Qz Ygf3wFXOniHOUUoBDezcXXWwH+8Ot4HyVlxYYjg+UrVlZ2zYq0WM+MeLZFY3rgRr71Zi i7cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/krkgGWRLPwrBOotoEiPAuKzZC5AWMQrtjDnDpAK2FI=; b=ov1p1FO0LlXRApD/Y2ED73vo7wtir3Dmo2g8HvO32wdn2oSVacf4MQkWBNVz7g8QE+ Qjm9/s5HALYU1gEzU5QR1Lfx3Vk0uY9TUfLjfgRstAKPgyLuQj5ewhEX1qfHqLVyJweD 4US07FEzG8nUyBamQWO63SDc4eZ8C1QTGrNTVygWpx8SwK7zLZpL0ZYASlquS8dQCG9Z ln4ZOVsaGKUKCDrECLfxY9wz8c+3mv91nnIxTQGPqtBkuSAkM4GIRcweg9OKCg0Vj13r r0513OZvPjV91epuDncC/cLTNcewAtIGM0sT+jeHNGIYGDi1Rx9JfDMORxHhxaFjxGQG zZKw== X-Gm-Message-State: AOAM530XkHF2two3IVCkOjvv4GdPIXUBL4CK3ZfHvzz/Umx8LCwsXZGJ xBDl5gXxOPS+RncOKMtwkSY= X-Google-Smtp-Source: ABdhPJwkQW2le5RVdC4V9Sn8fTVfqQ39bAof6kT8+Bi38tuJ+vH393jZam3QMTBaK9swjMMH5rg9Zw== X-Received: by 2002:a63:3193:: with SMTP id x141mr7266188pgx.254.1602341684887; Sat, 10 Oct 2020 07:54:44 -0700 (PDT) Received: from arpitha-Inspiron-7570.lan ([106.51.240.187]) by smtp.gmail.com with ESMTPSA id gg24sm15772806pjb.44.2020.10.10.07.54.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Oct 2020 07:54:44 -0700 (PDT) From: Arpitha Raghunandan <98.arpi@gmail.com> To: brendanhiggins@google.com, skhan@linuxfoundation.org, yzaikin@google.com, elver@google.com, tytso@mit.edu, adilger.kernel@dilger.ca Cc: Arpitha Raghunandan <98.arpi@gmail.com>, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel-mentees@lists.linuxfoundation.org, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] kunit: Support for Parameterized Testing Date: Sat, 10 Oct 2020 20:23:57 +0530 Message-Id: <20201010145357.60886-1-98.arpi@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Implementation of support for parameterized testing in KUnit. Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com> --- include/kunit/test.h | 29 +++++++++++++++++++++++++++++ lib/kunit/test.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 59f3144f009a..4740d66269b4 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -140,10 +140,14 @@ struct kunit; struct kunit_case { void (*run_case)(struct kunit *test); const char *name; + void* (*get_params)(void); + int max_parameters_count; + int parameter_size; /* private: internal use only. */ bool success; char *log; + bool parameterized; }; static inline char *kunit_status_to_string(bool status) @@ -162,6 +166,11 @@ static inline char *kunit_status_to_string(bool status) */ #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name } +#define KUNIT_CASE_PARAM(test_name, getparams, count, size) \ + { .run_case = test_name, .name = #test_name, \ + .parameterized = true, .get_params = (void* (*)(void))getparams, \ + .max_parameters_count = count, .parameter_size = size } + /** * struct kunit_suite - describes a related collection of &struct kunit_case * @@ -206,6 +215,23 @@ struct kunit { /* private: internal use only. */ const char *name; /* Read only after initialization! */ char *log; /* Points at case log after initialization */ + bool parameterized; /* True for parameterized tests */ + /* param_values stores the test parameters + * for parameterized tests. + */ + void *param_values; + /* max_parameters_count indicates maximum number of + * parameters for parameterized tests. + */ + int max_parameters_count; + /* iterator_count is used by the iterator method + * for parameterized tests. + */ + int iterator_count; + /* parameter_size indicates size of a single test case + * for parameterized tests. + */ + int parameter_size; struct kunit_try_catch try_catch; /* * success starts as true, and may only be set to false during a @@ -225,6 +251,7 @@ struct kunit { }; void kunit_init_test(struct kunit *test, const char *name, char *log); +void kunit_init_param_test(struct kunit *test, struct kunit_case *test_case); int kunit_run_tests(struct kunit_suite *suite); @@ -237,6 +264,8 @@ int __kunit_test_suites_init(struct kunit_suite **suites); void __kunit_test_suites_exit(struct kunit_suite **suites); +void *get_test_case_parameters(struct kunit *test); + /** * kunit_test_suites() - used to register one or more &struct kunit_suite * with KUnit. diff --git a/lib/kunit/test.c b/lib/kunit/test.c index c36037200310..ab9e13c81d4a 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -142,6 +142,11 @@ unsigned int kunit_test_case_num(struct kunit_suite *suite, } EXPORT_SYMBOL_GPL(kunit_test_case_num); +static void kunit_print_failed_param(struct kunit *test) +{ + kunit_err(test, "\n\tTest failed at parameter: %d\n", test->iterator_count); +} + static void kunit_print_string_stream(struct kunit *test, struct string_stream *stream) { @@ -182,6 +187,9 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert) assert->format(assert, stream); + if (test->parameterized) + kunit_print_failed_param(test); + kunit_print_string_stream(test, stream); WARN_ON(string_stream_destroy(stream)); @@ -236,6 +244,18 @@ void kunit_init_test(struct kunit *test, const char *name, char *log) } EXPORT_SYMBOL_GPL(kunit_init_test); +void kunit_init_param_test(struct kunit *test, struct kunit_case *test_case) +{ + spin_lock_init(&test->lock); + INIT_LIST_HEAD(&test->resources); + test->parameterized = true; + test->param_values = (void *)(test_case->get_params()); + test->max_parameters_count = test_case->max_parameters_count; + test->parameter_size = test_case->parameter_size; + test->iterator_count = 0; +} +EXPORT_SYMBOL_GPL(kunit_init_param_test); + /* * Initializes and runs test case. Does not clean up or do post validations. */ @@ -254,7 +274,14 @@ static void kunit_run_case_internal(struct kunit *test, } } - test_case->run_case(test); + if (!test->parameterized) { + test_case->run_case(test); + } else { + int i; + + for (i = 0; i < test->max_parameters_count; i++) + test_case->run_case(test); + } } static void kunit_case_internal_cleanup(struct kunit *test) @@ -343,6 +370,8 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite, struct kunit test; kunit_init_test(&test, test_case->name, test_case->log); + if (test_case->parameterized) + kunit_init_param_test(&test, test_case); try_catch = &test.try_catch; kunit_try_catch_init(try_catch, @@ -407,6 +436,19 @@ void __kunit_test_suites_exit(struct kunit_suite **suites) } EXPORT_SYMBOL_GPL(__kunit_test_suites_exit); +/* + * Iterator method for the parameterized test cases + */ +void *get_test_case_parameters(struct kunit *test) +{ + int index = test->iterator_count * test->parameter_size; + + if (test->iterator_count != test->max_parameters_count) + test->iterator_count++; + return (test->param_values + index); +} +EXPORT_SYMBOL_GPL(get_test_case_parameters); + /* * Used for static resources and when a kunit_resource * has been created by * kunit_alloc_resource(). When an init function is supplied, @data is passed From patchwork Sat Oct 10 14:55:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arpitha Raghunandan <98.arpi@gmail.com> X-Patchwork-Id: 11830385 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B57261592 for ; Sat, 10 Oct 2020 22:56:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8FF8E20829 for ; Sat, 10 Oct 2020 22:56:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S0bHV9R8" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390282AbgJJW4w (ORCPT ); Sat, 10 Oct 2020 18:56:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731400AbgJJTT1 (ORCPT ); Sat, 10 Oct 2020 15:19:27 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B21AC05BD25; Sat, 10 Oct 2020 07:56:08 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id l18so726678pgg.0; Sat, 10 Oct 2020 07:56:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sGNQhmhArB3UYXjNX9OpITFlGmNkphwKVloKhJ0FzDk=; b=S0bHV9R8IhVO9EMs0iK7yrbR9/nG29nDS7qurazS5LxD8zbNK3TonOw+ODuwYkr0Xz iAz6LIcuk3FOv+wYDLTUYGdlgftQHT6fH4JA+X76wXWQzQNbuIfCB4pkkDfB11e/EXcS caPiNhdvF920dM+R+qVUOmbfG6kDe12QiytZyXlMmgMLBEnI62woruxD1K0mC+UiTHd0 gN+Jq8PKyKrY2n6lL494N13JOjevEzTZbFGelaqVhCsw8NVJhTUkHotnbUGCG58qaWs9 XaHEViNfP3r83XE3caXzP/F+2iRylYzLc4FhHD1DUBMleXO9DDAZAcvT6fDYhVHJQUGb lx/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sGNQhmhArB3UYXjNX9OpITFlGmNkphwKVloKhJ0FzDk=; b=Cn8UeYjPCMTxCh5fDnjKFPbOzIpAyK966BViEDTCYxek01hulEN+DaHOXZ+X2Js+3l +nOF8QLtzkGi+NqbuuWVATL9TjRhPmv9+fFX2/PUDB4cMQ7qUKdp6X19sFxv2iKiBKKc KihSmOa3VbLJzjo03PUxKUtPRYAaET+zlGBxkAaIZiy5EyRC+bbNiDMCuWKKASUZLBBp M5IPFFTwMon1myn54jO6ssS/TakIDv8YJStLpW031aB8vJlK1DBmqKUGZ8w16q4kX+Ll XcCz8eilqCyJdShdeaqTh79Ev2rlG6OyI+yVGzblgPSy/Qm1MKysmPAi+ESGbyxEl1I7 jmCQ== X-Gm-Message-State: AOAM532lf2SfHE/vpGSskgZDbDgGl3T2oUgPGXbfOjtbXuqduyW91OoO 7xsXaTMHyATb3RLIM+23JxI= X-Google-Smtp-Source: ABdhPJwCEv+ay9FATziABv5KBkBcSRyRNFrawVj/C+EPxIiukIjt9wNklPDflMWdoEMYv4NU11s6cg== X-Received: by 2002:a17:90a:b902:: with SMTP id p2mr10309905pjr.15.1602341768089; Sat, 10 Oct 2020 07:56:08 -0700 (PDT) Received: from arpitha-Inspiron-7570.lan ([106.51.240.187]) by smtp.gmail.com with ESMTPSA id j19sm14304252pfi.51.2020.10.10.07.56.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Oct 2020 07:56:07 -0700 (PDT) From: Arpitha Raghunandan <98.arpi@gmail.com> To: brendanhiggins@google.com, skhan@linuxfoundation.org, yzaikin@google.com, elver@google.com, tytso@mit.edu, adilger.kernel@dilger.ca Cc: Arpitha Raghunandan <98.arpi@gmail.com>, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel-mentees@lists.linuxfoundation.org, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] fs: ext4: Modify inode-test.c to use KUnit parameterized testing feature Date: Sat, 10 Oct 2020 20:25:44 +0530 Message-Id: <20201010145544.61034-1-98.arpi@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201010145357.60886-1-98.arpi@gmail.com> References: <20201010145357.60886-1-98.arpi@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Modifies fs/ext4/inode-test.c to use the parameterized testing feature of KUnit. Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com> Reported-by: kernel test robot --- fs/ext4/inode-test.c | 64 +++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/fs/ext4/inode-test.c b/fs/ext4/inode-test.c index d62d802c9c12..691ef0a4ffe1 100644 --- a/fs/ext4/inode-test.c +++ b/fs/ext4/inode-test.c @@ -72,6 +72,8 @@ #define UPPER_BOUND_NONNEG_EXTRA_BITS_1_CASE\ "2446-05-10 Upper bound of 32bit >=0 timestamp. All extra sec bits on" +#define NUMBER_OF_TESTCASES 16 + struct timestamp_expectation { const char *test_case_name; struct timespec64 expected; @@ -101,7 +103,36 @@ static time64_t get_32bit_time(const struct timestamp_expectation * const test) */ static void inode_test_xtimestamp_decoding(struct kunit *test) { - const struct timestamp_expectation test_data[] = { + struct timespec64 timestamp; + + struct timestamp_expectation *test_data = + (struct timestamp_expectation *)get_test_case_parameters(test); + + timestamp.tv_sec = get_32bit_time(test_data); + ext4_decode_extra_time(×tamp, + cpu_to_le32(test_data->extra_bits)); + + KUNIT_EXPECT_EQ_MSG(test, + test_data->expected.tv_sec, + timestamp.tv_sec, + CASE_NAME_FORMAT, + test_data->test_case_name, + test_data->msb_set, + test_data->lower_bound, + test_data->extra_bits); + KUNIT_EXPECT_EQ_MSG(test, + test_data->expected.tv_nsec, + timestamp.tv_nsec, + CASE_NAME_FORMAT, + test_data->test_case_name, + test_data->msb_set, + test_data->lower_bound, + test_data->extra_bits); +} + +struct timestamp_expectation *get_test_parameters(void) +{ + static struct timestamp_expectation test_data[] = { { .test_case_name = LOWER_BOUND_NEG_NO_EXTRA_BITS_CASE, .msb_set = true, @@ -231,36 +262,13 @@ static void inode_test_xtimestamp_decoding(struct kunit *test) .expected = {.tv_sec = 0x37fffffffLL, .tv_nsec = 0L}, } }; - - struct timespec64 timestamp; - int i; - - for (i = 0; i < ARRAY_SIZE(test_data); ++i) { - timestamp.tv_sec = get_32bit_time(&test_data[i]); - ext4_decode_extra_time(×tamp, - cpu_to_le32(test_data[i].extra_bits)); - - KUNIT_EXPECT_EQ_MSG(test, - test_data[i].expected.tv_sec, - timestamp.tv_sec, - CASE_NAME_FORMAT, - test_data[i].test_case_name, - test_data[i].msb_set, - test_data[i].lower_bound, - test_data[i].extra_bits); - KUNIT_EXPECT_EQ_MSG(test, - test_data[i].expected.tv_nsec, - timestamp.tv_nsec, - CASE_NAME_FORMAT, - test_data[i].test_case_name, - test_data[i].msb_set, - test_data[i].lower_bound, - test_data[i].extra_bits); - } + return test_data; } static struct kunit_case ext4_inode_test_cases[] = { - KUNIT_CASE(inode_test_xtimestamp_decoding), + KUNIT_CASE_PARAM(inode_test_xtimestamp_decoding, + get_test_parameters, NUMBER_OF_TESTCASES, + sizeof(struct timestamp_expectation)), {} };