/
solidbase
/
C5_s21_decimal
Обзор
Документация
Войти
/
solidbase
/
C5_s21_decimal
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/tests/test_other.c
416 строк
12 KB
Dmitrii Isaev
earliema: refactor: rewrite comparison and other tests
01 апр 2026, 14:37
01 апр 2026, 14:37
8f53bfd
Код
Авторство
О чём код?
#include "test_libs.h" void test_negate(s21_decimal decimal, s21_decimal decimal_check) { s21_decimal result; int code = s21_negate(decimal, &result); int sign_check = decimal_check.bits[3] >> 31 & 1u; int sign_result = result.bits[3] >> 31 & 1u; ck_assert_int_eq(code, 0); ck_assert_int_eq(s21_is_equal(result, decimal_check), 1); ck_assert_int_eq(sign_check, sign_result); } void test_floor(s21_decimal decimal, s21_decimal decimal_check) { s21_decimal result; int code = s21_floor(decimal, &result); int sign_check = decimal_check.bits[3] >> 31 & 1u; int sign_result = result.bits[3] >> 31 & 1u; ck_assert_int_eq(code, 0); ck_assert_int_eq(s21_is_equal(result, decimal_check), 1); ck_assert_int_eq(sign_check, sign_result); } void test_round(s21_decimal decimal, s21_decimal decimal_check) { s21_decimal result; int code = s21_round(decimal, &result); int sign_check = decimal_check.bits[3] >> 31 & 1u; int sign_result = result.bits[3] >> 31 & 1u; ck_assert_int_eq(code, 0); ck_assert_int_eq(s21_is_equal(result, decimal_check), 1); ck_assert_int_eq(sign_check, sign_result); } void test_truncate(s21_decimal decimal, s21_decimal decimal_check) { s21_decimal result; int code = s21_truncate(decimal, &result); int sign_check = decimal_check.bits[3] >> 31 & 1u; int sign_result = result.bits[3] >> 31 & 1u; ck_assert_int_eq(code, 0); ck_assert_int_eq(result.bits[0] == decimal_check.bits[0] && result.bits[1] == decimal_check.bits[1] && result.bits[2] == decimal_check.bits[2] && result.bits[3] == decimal_check.bits[3], 1); ck_assert_int_eq(sign_check, sign_result); } // ============================================================ // s21_floor // ============================================================ // scale=0, positive -> unchanged START_TEST(test_floor_ok1) { s21_decimal d = {{42, 0, 0, 0}}; test_floor(d, d); } // scale=0, negative -> unchanged START_TEST(test_floor_ok2) { s21_decimal d = {{7, 0, 0, 0x80000000}}; test_floor(d, d); } // positive fractional -> truncate (floor == trunc for positive) START_TEST(test_floor_ok3) { // 1.9 -> 1 s21_decimal d = {{19, 0, 0, 0x00010000}}; s21_decimal e = {{1, 0, 0, 0}}; test_floor(d, e); } // positive no fractional digits (x.0) -> unchanged integer START_TEST(test_floor_ok4) { // 5.0 -> 5 s21_decimal d = {{50, 0, 0, 0x00010000}}; s21_decimal e = {{5, 0, 0, 0}}; test_floor(d, e); } // negative with fractional -> round toward -inf (add 1 to magnitude) START_TEST(test_floor_ok5) { // -1.1 -> -2 s21_decimal d = {{11, 0, 0, 0x80010000}}; s21_decimal e = {{2, 0, 0, 0x80000000}}; test_floor(d, e); } // negative no fractional digits (-x.0) -> unchanged START_TEST(test_floor_ok6) { // -3.0 -> -3 s21_decimal d = {{30, 0, 0, 0x80010000}}; s21_decimal e = {{3, 0, 0, 0x80000000}}; test_floor(d, e); } // 0.9 -> 0 START_TEST(test_floor_ok7) { s21_decimal d = {{9, 0, 0, 0x00010000}}; s21_decimal e = {{0, 0, 0, 0}}; test_floor(d, e); } // -0.1 -> -1 START_TEST(test_floor_ok8) { s21_decimal d = {{1, 0, 0, 0x80010000}}; s21_decimal e = {{1, 0, 0, 0x80000000}}; test_floor(d, e); } // multi-digit scale: 12.345 -> 12 START_TEST(test_floor_ok9) { s21_decimal d = {{12345, 0, 0, 0x00030000}}; s21_decimal e = {{12, 0, 0, 0}}; test_floor(d, e); } // multi-digit scale negative: -12.345 -> -13 START_TEST(test_floor_ok10) { s21_decimal d = {{12345, 0, 0, 0x80030000}}; s21_decimal e = {{13, 0, 0, 0x80000000}}; test_floor(d, e); } // ============================================================ // s21_round // ============================================================ // scale=0 -> unchanged START_TEST(test_round_ok1) { s21_decimal d = {{5, 0, 0, 0}}; test_round(d, d); } // x.4 -> round down START_TEST(test_round_ok2) { // 1.4 -> 1 s21_decimal d = {{14, 0, 0, 0x00010000}}; s21_decimal e = {{1, 0, 0, 0}}; test_round(d, e); } // x.6 -> round up START_TEST(test_round_ok3) { // 1.6 -> 2 s21_decimal d = {{16, 0, 0, 0x00010000}}; s21_decimal e = {{2, 0, 0, 0}}; test_round(d, e); } // banker's: x.5 where x is odd -> round up START_TEST(test_round_ok4) { // 3.5 -> 4 s21_decimal d = {{35, 0, 0, 0x00010000}}; s21_decimal e = {{4, 0, 0, 0}}; test_round(d, e); } // banker's: x.5 where x is even -> round down START_TEST(test_round_ok5) { // 4.5 -> 4 s21_decimal d = {{45, 0, 0, 0x00010000}}; s21_decimal e = {{4, 0, 0, 0}}; test_round(d, e); } // banker's: x.5 with nonzero tail -> always round up START_TEST(test_round_ok6) { // 2.50001 -> 3 s21_decimal d = {{250001, 0, 0, 0x00050000}}; s21_decimal e = {{3, 0, 0, 0}}; test_round(d, e); } // negative x.6 -> round up magnitude START_TEST(test_round_ok7) { // -1.6 -> -2 s21_decimal d = {{16, 0, 0, 0x80010000}}; s21_decimal e = {{2, 0, 0, 0x80000000}}; test_round(d, e); } // negative x.4 -> round down magnitude START_TEST(test_round_ok8) { // -1.4 -> -1 s21_decimal d = {{14, 0, 0, 0x80010000}}; s21_decimal e = {{1, 0, 0, 0x80000000}}; test_round(d, e); } // banker's negative: -4.5 -> -4 (even, round down) START_TEST(test_round_ok9) { s21_decimal d = {{45, 0, 0, 0x80010000}}; s21_decimal e = {{4, 0, 0, 0x80000000}}; test_round(d, e); } // multi-digit scale: 12.345 -> 12 START_TEST(test_round_ok10) { s21_decimal d = {{12345, 0, 0, 0x00030000}}; s21_decimal e = {{12, 0, 0, 0}}; test_round(d, e); } // ============================================================ // s21_truncate // ============================================================ // scale=0 -> unchanged START_TEST(test_truncate_ok1) { s21_decimal d = {{99, 0, 0, 0}}; test_truncate(d, d); } // positive: drop fractional part START_TEST(test_truncate_ok2) { // 1.9 -> 1 s21_decimal d = {{19, 0, 0, 0x00010000}}; s21_decimal e = {{1, 0, 0, 0}}; test_truncate(d, e); } // negative: drop fractional part (toward zero, NOT floor) START_TEST(test_truncate_ok3) { // -1.9 -> -1 s21_decimal d = {{19, 0, 0, 0x80010000}}; s21_decimal e = {{1, 0, 0, 0x80000000}}; test_truncate(d, e); } // trailing zeros removed: 5.00 -> 5 START_TEST(test_truncate_ok4) { s21_decimal d = {{500, 0, 0, 0x00020000}}; s21_decimal e = {{5, 0, 0, 0}}; test_truncate(d, e); } // multi-digit scale: 12.345 -> 12 START_TEST(test_truncate_ok5) { s21_decimal d = {{12345, 0, 0, 0x00030000}}; s21_decimal e = {{12, 0, 0, 0}}; test_truncate(d, e); } // multi-digit scale negative: -12.345 -> -12 START_TEST(test_truncate_ok6) { s21_decimal d = {{12345, 0, 0, 0x80030000}}; s21_decimal e = {{12, 0, 0, 0x80000000}}; test_truncate(d, e); } // 0.999... -> 0 START_TEST(test_truncate_ok7) { s21_decimal d = {{9, 0, 0, 0x00010000}}; s21_decimal e = {{0, 0, 0, 0}}; test_truncate(d, e); } // MAX with scale=0 -> unchanged START_TEST(test_truncate_ok8) { s21_decimal d = {{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0}}; test_truncate(d, d); } // ============================================================ // s21_negate // ============================================================ // positive -> negative START_TEST(test_negate_ok1) { s21_decimal d = {{5, 0, 0, 0}}; s21_decimal e = {{5, 0, 0, 0x80000000}}; test_negate(d, e); } // negative -> positive START_TEST(test_negate_ok2) { s21_decimal d = {{3, 0, 0, 0x80000000}}; s21_decimal e = {{3, 0, 0, 0}}; test_negate(d, e); } // +0 -> -0 START_TEST(test_negate_ok3) { s21_decimal d = {{0, 0, 0, 0}}; s21_decimal e = {{0, 0, 0, 0x80000000}}; test_negate(d, e); } // -0 -> +0 START_TEST(test_negate_ok4) { s21_decimal d = {{0, 0, 0, 0x80000000}}; s21_decimal e = {{0, 0, 0, 0}}; test_negate(d, e); } // with scale: -1.5 -> 1.5 START_TEST(test_negate_ok5) { s21_decimal d = {{15, 0, 0, 0x80010000}}; s21_decimal e = {{15, 0, 0, 0x00010000}}; test_negate(d, e); } // MAX -> -MAX START_TEST(test_negate_ok6) { s21_decimal d = {{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0}}; s21_decimal e = {{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x80000000}}; test_negate(d, e); } // ============================================================ // Fail cases (invalid input) // ============================================================ // NULL pointer START_TEST(test_floor_fail1) { s21_decimal d = {{1, 0, 0, 0}}; ck_assert_int_eq(s21_floor(d, NULL), 1); } // scale > 28 START_TEST(test_floor_fail2) { s21_decimal d = {{1, 0, 0, 0x1D0000}}; s21_decimal r; ck_assert_int_eq(s21_floor(d, &r), 1); } // invalid bits[0-15] START_TEST(test_floor_fail3) { s21_decimal d = {{0, 0, 0, 0x1C0001}}; s21_decimal r; ck_assert_int_eq(s21_floor(d, &r), 1); } START_TEST(test_round_fail1) { s21_decimal d = {{1, 0, 0, 0}}; ck_assert_int_eq(s21_round(d, NULL), 1); } START_TEST(test_round_fail2) { s21_decimal d = {{1, 0, 0, 0x1D0000}}; s21_decimal r; ck_assert_int_eq(s21_round(d, &r), 1); } START_TEST(test_round_fail3) { s21_decimal d = {{0, 0, 0, 1000000000}}; s21_decimal r; ck_assert_int_eq(s21_round(d, &r), 1); } START_TEST(test_truncate_fail1) { s21_decimal d = {{1, 0, 0, 0}}; ck_assert_int_eq(s21_truncate(d, NULL), 1); } START_TEST(test_truncate_fail2) { s21_decimal d = {{1, 0, 0, 0x1D0000}}; s21_decimal r; ck_assert_int_eq(s21_truncate(d, &r), 1); } START_TEST(test_truncate_fail3) { s21_decimal d = {{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}}; s21_decimal r; ck_assert_int_eq(s21_truncate(d, &r), 1); } START_TEST(test_negate_fail1) { s21_decimal d = {{1, 0, 0, 0}}; ck_assert_int_eq(s21_negate(d, NULL), 1); } START_TEST(test_negate_fail2) { s21_decimal d = {{1, 0, 0, 0x1D0000}}; s21_decimal r; ck_assert_int_eq(s21_negate(d, &r), 1); } START_TEST(test_negate_fail3) { s21_decimal d = {{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}}; s21_decimal r; ck_assert_int_eq(s21_negate(d, &r), 1); } // ============================================================ // Suite // ============================================================ Suite* suite_other(void) { Suite* other = suite_create("Other"); TCase* floor_tc = tcase_create("Floor"); tcase_add_test(floor_tc, test_floor_ok1); tcase_add_test(floor_tc, test_floor_ok2); tcase_add_test(floor_tc, test_floor_ok3); tcase_add_test(floor_tc, test_floor_ok4); tcase_add_test(floor_tc, test_floor_ok5); tcase_add_test(floor_tc, test_floor_ok6); tcase_add_test(floor_tc, test_floor_ok7); tcase_add_test(floor_tc, test_floor_ok8); tcase_add_test(floor_tc, test_floor_ok9); tcase_add_test(floor_tc, test_floor_ok10); suite_add_tcase(other, floor_tc); TCase* round_tc = tcase_create("Round"); tcase_add_test(round_tc, test_round_ok1); tcase_add_test(round_tc, test_round_ok2); tcase_add_test(round_tc, test_round_ok3); tcase_add_test(round_tc, test_round_ok4); tcase_add_test(round_tc, test_round_ok5); tcase_add_test(round_tc, test_round_ok6); tcase_add_test(round_tc, test_round_ok7); tcase_add_test(round_tc, test_round_ok8); tcase_add_test(round_tc, test_round_ok9); tcase_add_test(round_tc, test_round_ok10); suite_add_tcase(other, round_tc); TCase* truncate_tc = tcase_create("Trancate"); tcase_add_test(truncate_tc, test_truncate_ok1); tcase_add_test(truncate_tc, test_truncate_ok2); tcase_add_test(truncate_tc, test_truncate_ok3); tcase_add_test(truncate_tc, test_truncate_ok4); tcase_add_test(truncate_tc, test_truncate_ok5); tcase_add_test(truncate_tc, test_truncate_ok6); tcase_add_test(truncate_tc, test_truncate_ok7); tcase_add_test(truncate_tc, test_truncate_ok8); suite_add_tcase(other, truncate_tc); TCase* negate_tc = tcase_create("Negate"); tcase_add_test(negate_tc, test_negate_ok1); tcase_add_test(negate_tc, test_negate_ok2); tcase_add_test(negate_tc, test_negate_ok3); tcase_add_test(negate_tc, test_negate_ok4); tcase_add_test(negate_tc, test_negate_ok5); tcase_add_test(negate_tc, test_negate_ok6); suite_add_tcase(other, negate_tc); TCase* fail = tcase_create("Fail data"); tcase_add_test(fail, test_floor_fail1); tcase_add_test(fail, test_floor_fail2); tcase_add_test(fail, test_floor_fail3); tcase_add_test(fail, test_round_fail1); tcase_add_test(fail, test_round_fail2); tcase_add_test(fail, test_round_fail3); tcase_add_test(fail, test_truncate_fail1); tcase_add_test(fail, test_truncate_fail2); tcase_add_test(fail, test_truncate_fail3); tcase_add_test(fail, test_negate_fail1); tcase_add_test(fail, test_negate_fail2); tcase_add_test(fail, test_negate_fail3); suite_add_tcase(other, fail); return other; }