/
dolpement
/
programming-and-algorithmization
Обзор
Документация
Войти
/
dolpement
/
programming-and-algorithmization
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
rational_prj/rational_test.cpp
63 строки
2 KB
dolpement
programming
13 мар 2025, 21:11
13 мар 2025, 21:11
24f0ba5
Код
Авторство
О чём код?
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include <rational.hpp> #include "doctest.h" #include <stdexcept> TEST_CASE("Rational constructors") { CHECK(Rational(0, 7) == Rational(0, 1)); CHECK(Rational(8) == Rational(8, 1)); CHECK(Rational(-4, -6) == Rational(2, 3)); CHECK(Rational(3, -4) == Rational(-3, 4)); CHECK_THROWS(Rational(1, 0)); } TEST_CASE("Bool operation") { CHECK(Rational(2, 5) == Rational(2, 5)); CHECK(Rational(2, 7)!= Rational(1, 4)); CHECK(Rational(4, 5)>Rational(2, 7)); CHECK(Rational(2, 9)<Rational(6, 7)); CHECK(Rational(1, 4)>=Rational(1, 4)); CHECK(Rational(1, 3)>=Rational(2, 9)); CHECK(Rational(5, 8)<=Rational(2, 3)); CHECK(Rational(4, 7)<=Rational(4, 7)); } TEST_CASE("Operators"){ Rational r1(2, 5); Rational r2(0, 3); int64_t r3 = 5; CHECK(r1+r2 == r1); CHECK(r1 + Rational(-1, 10) == Rational(3, 10)); CHECK(r1 + Rational(3, 7) == Rational(29, 35)); CHECK(r1+r3 == Rational(27, 5)); CHECK(r1 - r2 == r1); CHECK(r3 - r1 == Rational(23, 5)); CHECK(r1 - Rational(3, 5) == Rational(-1, 5)); CHECK(r1 - Rational(-3, 5) == Rational(1, 1)); CHECK(r1*r2 == Rational(0, 15)); CHECK(r1*r3 == Rational(2, 1)); CHECK(r1*Rational(3, 7) == Rational(6, 35)); CHECK(r1*Rational(-5, 9) == Rational(-10, 45)); CHECK(r1/r3 == Rational(2, 1)); CHECK(r1/Rational(4, 3) == Rational(6, 20)); CHECK(Rational(-6, 13)/r1 == Rational(-30, 26)); } TEST_CASE("input and output") { Rational r1(2, 5); std::ostringstream oss; oss << r1; CHECK(oss.str() == "2/5"); std::istringstream iss("1/2"); Rational r3; iss >> r3; CHECK(r3 == Rational(1, 2)); }