#include <kunit/test.h>
#include <linux/polynomial.h>
struct polynomial_test_param {
const struct polynomial *poly;
long data;
long expected;
const char *name;
};
static const struct polynomial poly_constant = {
.total_divider = 1,
.terms = {
{0, 5, 1, 1},
}
};
static const struct polynomial poly_simple = {
.total_divider = 1,
.terms = {
{2, 2, 1, 1},
{1, 3, 1, 1},
{0, 5, 1, 1},
}
};
static const struct polynomial poly_negative_coef = {
.total_divider = 1,
.terms = {
{1, -5, 1, 1},
{0, 100, 1, 1},
}
};
static const struct polynomial poly_total_divider = {
.total_divider = 10,
.terms = {
{1, 150, 1, 1},
{0, 50, 1, 1},
}
};
static const struct polynomial poly_step_divider = {
.total_divider = 1,
.terms = {
{1, 1, 2, 1},
{0, 0, 1, 1},
}
};
static const struct polynomial poly_leftover = {
.total_divider = 1,
.terms = {
{2, 100, 10, 5},
{0, 0, 1, 1},
}
};
static const struct polynomial poly_cubic = {
.total_divider = 1,
.terms = {
{3, 2, 1, 1},
{0, 0, 1, 1},
}
};
static const struct polynomial poly_zero_coef = {
.total_divider = 1,
.terms = {
{2, 0, 1, 1},
{1, 4, 1, 1},
{0, 1, 1, 1},
}
};
static const struct polynomial poly_zero_total_divider = {
.total_divider = 0,
.terms = {
{0, 9, 1, 1},
}
};
static const struct polynomial_test_param test_params[] = {
{
.poly = &poly_constant,
.data = 0,
.expected = 5,
.name = "Constant polynomial at x=0",
},
{
.poly = &poly_constant,
.data = 42,
.expected = 5,
.name = "Constant polynomial is independent of input",
},
{
.poly = &poly_simple,
.data = 0,
.expected = 5,
.name = "Zero input yields constant term only",
},
{
.poly = &poly_simple,
.data = 10,
.expected = 235,
.name = "Simple quadratic at x=10",
},
{
.poly = &poly_negative_coef,
.data = 10,
.expected = 50,
.name = "Negative coefficient at x=10",
},
{
.poly = &poly_negative_coef,
.data = 20,
.expected = 0,
.name = "Negative coefficient result is zero",
},
{
.poly = &poly_total_divider,
.data = 3,
.expected = 50,
.name = "total_divider scales the final sum",
},
{
.poly = &poly_step_divider,
.data = 100,
.expected = 50,
.name = "Per-step divider halves input",
},
{
.poly = &poly_leftover,
.data = 30,
.expected = 180,
.name = "divider_leftover with quadratic term",
},
{
.poly = &poly_simple,
.data = 1,
.expected = 10,
.name = "Boundary: data=1 (unit input)",
},
{
.poly = &poly_simple,
.data = -1,
.expected = 4,
.name = "Boundary: data=-1 (negative unit input)",
},
{
.poly = &poly_simple,
.data = -3,
.expected = 14,
.name = "Boundary: negative data with quadratic",
},
{
.poly = &poly_zero_total_divider,
.data = 42,
.expected = 9,
.name = "Boundary: total_divider=0 defaults to 1",
},
{
.poly = &poly_zero_coef,
.data = 10,
.expected = 41,
.name = "Boundary: zero-coefficient term is inert",
},
{
.poly = &poly_cubic,
.data = 5,
.expected = 250,
.name = "Boundary: single cubic term",
},
{
.poly = &poly_cubic,
.data = -2,
.expected = -16,
.name = "Boundary: single cubic term, negative data",
},
};
static void get_desc(const struct polynomial_test_param *param, char *desc)
{
strscpy(desc, param->name, KUNIT_PARAM_DESC_SIZE);
}
KUNIT_ARRAY_PARAM(polynomial, test_params, get_desc);
static void polynomial_calc_test(struct kunit *test)
{
const struct polynomial_test_param *param = test->param_value;
KUNIT_EXPECT_EQ(test, polynomial_calc(param->poly, param->data),
param->expected);
}
static struct kunit_case polynomial_test_cases[] = {
KUNIT_CASE_PARAM(polynomial_calc_test, polynomial_gen_params),
{}
};
static struct kunit_suite polynomial_test_suite = {
.name = "math-polynomial",
.test_cases = polynomial_test_cases,
};
kunit_test_suites(&polynomial_test_suite);
MODULE_DESCRIPTION("math.polynomial_calc KUnit test suite");
MODULE_LICENSE("GPL"