// SPDX-License-Identifier: GPL-2.0 /* * KUnit API to save and access test attributes * * Copyright (C) 2023, Google LLC. * Author: Rae Moar <rmoar@google.com>
*/
/* Options for printing attributes: * PRINT_ALWAYS - attribute is printed for every test case and suite if set * PRINT_SUITE - attribute is printed for every suite if set but not for test cases * PRINT_NEVER - attribute is never printed
*/ enum print_ops {
PRINT_ALWAYS,
PRINT_SUITE,
PRINT_NEVER,
};
/** * struct kunit_attr - represents a test attribute and holds flexible * helper functions to interact with attribute. * * @name: name of test attribute, eg. speed * @get_attr: function to return attribute value given a test * @to_string: function to return string representation of given * attribute value * @filter: function to indicate whether a given attribute value passes a * filter * @attr_default: default attribute value used during filtering * @print: value of enum print_ops to indicate when to print attribute
*/ struct kunit_attr { constchar *name; void *(*get_attr)(void *test_or_suite, bool is_test); constchar *(*to_string)(void *attr, bool *to_free); int (*filter)(void *attr, constchar *input, int *err); void *attr_default; enum print_ops print;
};
/* * Returns whether the inputted integer value matches the filter given * by the operation string and inputted integer.
*/ staticint int_filter(long val, constchar *op, int input, int *err)
{ if (!strncmp(op, "<=", 2)) return (val <= input); elseif (!strncmp(op, ">=", 2)) return (val >= input); elseif (!strncmp(op, "!=", 2)) return (val != input); elseif (!strncmp(op, ">", 1)) return (val > input); elseif (!strncmp(op, "<", 1)) return (val < input); elseif (!strncmp(op, "=", 1)) return (val == input);
*err = -EINVAL;
pr_err("kunit executor: invalid filter operation: %s\n", op); returnfalse;
}
/* * Returns whether the inputted enum value "attr" matches the filter given * by the input string. Note: the str_list includes the corresponding string * list to the enum values.
*/ staticint attr_enum_filter(void *attr, constchar *input, int *err, constchar * const str_list[], int max)
{ int i, j, input_int = -1; long test_val = (long)attr; constchar *input_val = NULL;
for (i = 0; input[i]; i++) { if (!strchr(op_list, input[i])) {
input_val = input + i; break;
}
}
if (!input_val) {
*err = -EINVAL;
pr_err("kunit executor: filter value not found: %s\n", input); returnfalse;
}
for (j = 0; j <= max; j++) { if (!strcmp(input_val, str_list[j]))
input_int = j;
}
/* * Returns whether the inputted string value (attr) matches the filter given * by the input string.
*/ staticint attr_string_filter(void *attr, constchar *input, int *err)
{ char *str = attr;
// Suites get their module attribute from their first test_case if (test) return ((void *) test->module_name); elseif (kunit_suite_num_test_cases(suite) > 0) return ((void *) suite->test_cases[0].module_name); else return (void *) "";
}
/* Allocate memory for new copy of suite and list of test cases */
copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL); if (!copy) return ERR_PTR(-ENOMEM);
/* Save filtering result on default value */
default_result = filter.attr->filter(filter.attr->attr_default, filter.input, err); if (*err) goto err;
/* Save suite attribute value and filtering result on that value */
suite_val = filter.attr->get_attr((void *)suite, false);
suite_result = filter.attr->filter(suite_val, filter.input, err); if (*err) goto err;
/* For each test case, save test case if passes filtering. */
kunit_suite_for_each_test_case(suite, test_case) {
test_val = filter.attr->get_attr((void *) test_case, true);
test_result = filter.attr->filter(filter.attr->get_attr(test_case, true),
filter.input, err); if (*err) goto err;
/* * If attribute value of test case is set, filter on that value. * If not, filter on suite value if set. If not, filter on * default value.
*/
result = false; if (test_val) { if (test_result)
result = true;
} elseif (suite_val) { if (suite_result)
result = true;
} elseif (default_result) {
result = true;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.