Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_GCD_KUNIT_TEST) += gcd_kunit.o
obj-$(CONFIG_INT_LOG_KUNIT_TEST) += int_log_kunit.o
obj-$(CONFIG_INT_POW_KUNIT_TEST) += int_pow_kunit.o
obj-$(CONFIG_INT_SQRT_KUNIT_TEST) += int_sqrt_kunit.o
obj-$(CONFIG_PRIME_NUMBERS_KUNIT_TEST) += prime_numbers_kunit.o
obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational_kunit.o
+56
View File
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <kunit/test.h>
#include <linux/gcd.h>
#include <linux/limits.h>
struct test_case_params {
unsigned long val1;
unsigned long val2;
unsigned long expected_result;
const char *name;
};
static const struct test_case_params params[] = {
{ 48, 18, 6, "GCD of 48 and 18" },
{ 18, 48, 6, "GCD of 18 and 48" },
{ 56, 98, 14, "GCD of 56 and 98" },
{ 17, 13, 1, "Coprime numbers" },
{ 101, 103, 1, "Coprime numbers" },
{ 270, 192, 6, "GCD of 270 and 192" },
{ 0, 5, 5, "GCD with zero" },
{ 7, 0, 7, "GCD with zero reversed" },
{ 36, 36, 36, "GCD of identical numbers" },
{ ULONG_MAX, 1, 1, "GCD of max ulong and 1" },
{ ULONG_MAX, ULONG_MAX, ULONG_MAX, "GCD of max ulong values" },
};
static void get_desc(const struct test_case_params *tc, char *desc)
{
strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
}
KUNIT_ARRAY_PARAM(gcd, params, get_desc);
static void gcd_test(struct kunit *test)
{
const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
KUNIT_EXPECT_EQ(test, tc->expected_result, gcd(tc->val1, tc->val2));
}
static struct kunit_case math_gcd_test_cases[] = {
KUNIT_CASE_PARAM(gcd_test, gcd_gen_params),
{}
};
static struct kunit_suite gcd_test_suite = {
.name = "math-gcd",
.test_cases = math_gcd_test_cases,
};
kunit_test_suite(gcd_test_suite);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("math.gcd KUnit test suite");
MODULE_AUTHOR("Yu-Chun Lin <eleanor15x@gmail.com>");
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <kunit/test.h>
#include <linux/int_log.h>
struct test_case_params {
u32 value;
unsigned int expected_result;
const char *name;
};
/* The expected result takes into account the log error */
static const struct test_case_params intlog2_params[] = {
{0, 0, "Log base 2 of 0"},
{1, 0, "Log base 2 of 1"},
{2, 16777216, "Log base 2 of 2"},
{3, 26591232, "Log base 2 of 3"},
{4, 33554432, "Log base 2 of 4"},
{8, 50331648, "Log base 2 of 8"},
{16, 67108864, "Log base 2 of 16"},
{32, 83886080, "Log base 2 of 32"},
{U32_MAX, 536870911, "Log base 2 of MAX"},
};
static const struct test_case_params intlog10_params[] = {
{0, 0, "Log base 10 of 0"},
{1, 0, "Log base 10 of 1"},
{6, 13055203, "Log base 10 of 6"},
{10, 16777225, "Log base 10 of 10"},
{100, 33554450, "Log base 10 of 100"},
{1000, 50331675, "Log base 10 of 1000"},
{10000, 67108862, "Log base 10 of 10000"},
{U32_MAX, 161614247, "Log base 10 of MAX"}
};
static void get_desc(const struct test_case_params *tc, char *desc)
{
strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
}
KUNIT_ARRAY_PARAM(intlog2, intlog2_params, get_desc);
static void intlog2_test(struct kunit *test)
{
const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
KUNIT_EXPECT_EQ(test, tc->expected_result, intlog2(tc->value));
}
KUNIT_ARRAY_PARAM(intlog10, intlog10_params, get_desc);
static void intlog10_test(struct kunit *test)
{
const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
KUNIT_EXPECT_EQ(test, tc->expected_result, intlog10(tc->value));
}
static struct kunit_case math_int_log_test_cases[] = {
KUNIT_CASE_PARAM(intlog2_test, intlog2_gen_params),
KUNIT_CASE_PARAM(intlog10_test, intlog10_gen_params),
{}
};
static struct kunit_suite int_log_test_suite = {
.name = "math-int_log",
.test_cases = math_int_log_test_cases,
};
kunit_test_suites(&int_log_test_suite);
MODULE_DESCRIPTION("math.int_log KUnit test suite");
MODULE_LICENSE("GPL");
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <kunit/test.h>
#include <linux/math.h>
struct test_case_params {
u64 base;
unsigned int exponent;
u64 expected_result;
const char *name;
};
static const struct test_case_params params[] = {
{ 64, 0, 1, "Power of zero" },
{ 64, 1, 64, "Power of one"},
{ 0, 5, 0, "Base zero" },
{ 1, 64, 1, "Base one" },
{ 2, 2, 4, "Two squared"},
{ 2, 3, 8, "Two cubed"},
{ 5, 5, 3125, "Five raised to the fifth power" },
{ U64_MAX, 1, U64_MAX, "Max base" },
{ 2, 63, 9223372036854775808ULL, "Large result"},
};
static void get_desc(const struct test_case_params *tc, char *desc)
{
strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
}
KUNIT_ARRAY_PARAM(int_pow, params, get_desc);
static void int_pow_test(struct kunit *test)
{
const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
KUNIT_EXPECT_EQ(test, tc->expected_result, int_pow(tc->base, tc->exponent));
}
static struct kunit_case math_int_pow_test_cases[] = {
KUNIT_CASE_PARAM(int_pow_test, int_pow_gen_params),
{}
};
static struct kunit_suite int_pow_test_suite = {
.name = "math-int_pow",
.test_cases = math_int_pow_test_cases,
};
kunit_test_suites(&int_pow_test_suite);
MODULE_DESCRIPTION("math.int_pow KUnit test suite");
MODULE_LICENSE("GPL");
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <kunit/test.h>
#include <linux/limits.h>
#include <linux/math.h>
#include <linux/module.h>
#include <linux/string.h>
struct test_case_params {
unsigned long x;
unsigned long expected_result;
const char *name;
};
static const struct test_case_params params[] = {
{ 0, 0, "edge case: square root of 0" },
{ 1, 1, "perfect square: square root of 1" },
{ 2, 1, "non-perfect square: square root of 2" },
{ 3, 1, "non-perfect square: square root of 3" },
{ 4, 2, "perfect square: square root of 4" },
{ 5, 2, "non-perfect square: square root of 5" },
{ 6, 2, "non-perfect square: square root of 6" },
{ 7, 2, "non-perfect square: square root of 7" },
{ 8, 2, "non-perfect square: square root of 8" },
{ 9, 3, "perfect square: square root of 9" },
{ 15, 3, "non-perfect square: square root of 15 (N-1 from 16)" },
{ 16, 4, "perfect square: square root of 16" },
{ 17, 4, "non-perfect square: square root of 17 (N+1 from 16)" },
{ 80, 8, "non-perfect square: square root of 80 (N-1 from 81)" },
{ 81, 9, "perfect square: square root of 81" },
{ 82, 9, "non-perfect square: square root of 82 (N+1 from 81)" },
{ 255, 15, "non-perfect square: square root of 255 (N-1 from 256)" },
{ 256, 16, "perfect square: square root of 256" },
{ 257, 16, "non-perfect square: square root of 257 (N+1 from 256)" },
{ 2147483648, 46340, "large input: square root of 2147483648" },
{ 4294967295, 65535, "edge case: ULONG_MAX for 32-bit" },
};
static void get_desc(const struct test_case_params *tc, char *desc)
{
strscpy(desc, tc->name, KUNIT_PARAM_DESC_SIZE);
}
KUNIT_ARRAY_PARAM(int_sqrt, params, get_desc);
static void int_sqrt_test(struct kunit *test)
{
const struct test_case_params *tc = (const struct test_case_params *)test->param_value;
KUNIT_EXPECT_EQ(test, tc->expected_result, int_sqrt(tc->x));
}
static struct kunit_case math_int_sqrt_test_cases[] = {
KUNIT_CASE_PARAM(int_sqrt_test, int_sqrt_gen_params),
{}
};
static struct kunit_suite int_sqrt_test_suite = {
.name = "math-int_sqrt",
.test_cases = math_int_sqrt_test_cases,
};
kunit_test_suites(&int_sqrt_test_suite);
MODULE_DESCRIPTION("math.int_sqrt KUnit test suite");
MODULE_LICENSE("GPL");
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <kunit/test.h>
#include <linux/module.h>
#include <linux/prime_numbers.h>
#include "../prime_numbers_private.h"
static void dump_primes(void *ctx, const struct primes *p)
{
struct kunit_suite *suite = ctx;
kunit_info(suite, "primes.{last=%lu, .sz=%lu, .primes[]=...x%lx} = %*pbl",
p->last, p->sz, p->primes[BITS_TO_LONGS(p->sz) - 1], (int)p->sz, p->primes);
}
static void prime_numbers_test(struct kunit *test)
{
const unsigned long max = 65536;
unsigned long x, last, next;
for (last = 0, x = 2; x < max; x++) {
const bool slow = slow_is_prime_number(x);
const bool fast = is_prime_number(x);
KUNIT_ASSERT_EQ_MSG(test, slow, fast, "is-prime(%lu)", x);
if (!slow)
continue;
next = next_prime_number(last);
KUNIT_ASSERT_EQ_MSG(test, next, x, "next-prime(%lu)", last);
last = next;
}
}
static void kunit_suite_exit(struct kunit_suite *suite)
{
with_primes(suite, dump_primes);
}
static struct kunit_case prime_numbers_cases[] = {
KUNIT_CASE(prime_numbers_test),
{},
};
static struct kunit_suite prime_numbers_suite = {
.name = "math-prime_numbers",
.suite_exit = kunit_suite_exit,
.test_cases = prime_numbers_cases,
};
kunit_test_suite(prime_numbers_suite);
MODULE_AUTHOR("Intel Corporation");
MODULE_DESCRIPTION("Prime number library");
MODULE_LICENSE("GPL");
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: GPL-2.0
#include <kunit/test.h>
#include <linux/rational.h>
struct rational_test_param {
unsigned long num, den;
unsigned long max_num, max_den;
unsigned long exp_num, exp_den;
const char *name;
};
static const struct rational_test_param test_parameters[] = {
{ 1230, 10, 100, 20, 100, 1, "Exceeds bounds, semi-convergent term > 1/2 last term" },
{ 34567,100, 120, 20, 120, 1, "Exceeds bounds, semi-convergent term < 1/2 last term" },
{ 1, 30, 100, 10, 0, 1, "Closest to zero" },
{ 1, 19, 100, 10, 1, 10, "Closest to smallest non-zero" },
{ 27,32, 16, 16, 11, 13, "Use convergent" },
{ 1155, 7735, 255, 255, 33, 221, "Exact answer" },
{ 87, 32, 70, 32, 68, 25, "Semiconvergent, numerator limit" },
{ 14533, 4626, 15000, 2400, 7433, 2366, "Semiconvergent, denominator limit" },
};
static void get_desc(const struct rational_test_param *param, char *desc)
{
strscpy(desc, param->name, KUNIT_PARAM_DESC_SIZE);
}
/* Creates function rational_gen_params */
KUNIT_ARRAY_PARAM(rational, test_parameters, get_desc);
static void rational_test(struct kunit *test)
{
const struct rational_test_param *param = (const struct rational_test_param *)test->param_value;
unsigned long n = 0, d = 0;
rational_best_approximation(param->num, param->den, param->max_num, param->max_den, &n, &d);
KUNIT_EXPECT_EQ(test, n, param->exp_num);
KUNIT_EXPECT_EQ(test, d, param->exp_den);
}
static struct kunit_case rational_test_cases[] = {
KUNIT_CASE_PARAM(rational_test, rational_gen_params),
{}
};
static struct kunit_suite rational_test_suite = {
.name = "rational",
.test_cases = rational_test_cases,
};
kunit_test_suites(&rational_test_suite);
MODULE_DESCRIPTION("Rational fractions unit test");
MODULE_LICENSE("GPL v2");