// rat-tst.cpp (C) 2001, 2007 adolfo@di-mare.com /** \file rat-tst.cpp \brief Programa de prueba para la clase \c rational. \author Adolfo Di Mare \date 2007 */ // Todo esto permite compilar con BC++ v3.1 #ifndef __BORLANDC__ // Definida para Borland C++ #include "BUnit.h" #else #if (__BORLANDC__ > 0x0410) // Identifica a BC++ v3.1 y anterior #include "BUnit.h" #else // Es BC++ v3.1 class TestCase { public: int failureCount() const { return 0; } public: const char * toString () const { return ""; } public: const char * report () const { return ""; } }; #include #define assertTrue( B ) assert( B ); #endif #endif #define INCLUDE_IOMANIP // setw() // ADH_port.h ==> #include #include // rand() // ==> #include #include "rational.h" USING_namespace(ADH); // ADH_port.h ==> using namespace ADH; USING_namespace(std); // ADH_port.h ==> using namespace std; /// Método de ordenamiento de selección. /// - Deja el arregle \c A[] ordenado. /// - Trabaja en el rango \c [0..n-1]. template void selection_sort(T* A, size_t n) { for (size_t i=0, m=n-1; i bool Ordenado (T* A, size_t n) { for (size_t i = 0; i < n-1; ++i) { if (A[i] > A[i+1]) { return false; } } return true; } // Ordenado() /// Clase simple para probar \c rational. /// - Esta clase no usa variables de instancia, pues su método /// \c TestCase::run() implementa el caso de prueba completo. class rational_Test : public TestCase { public: bool run(); }; // rational_Test bool rational_Test::run() { rational r, // r.rational(); s(1), // s.rational(1); t = 1, // t.rational(1); u(1,2); // u.rational(1,2); assertTrue( r == 0 ); assertTrue( s == 1 ); assertTrue( t == s ); assertTrue( u == rational(1,2) ); rational a = t; assertTrue( a == t ); a = t; assertTrue( a == t ); (a += s * 2) += 4; assertTrue( a == 7 ); assertTrue( s == 1 ); assertTrue( 1 == s ); a = rational(4, -5); rational b(-10, 6); a *= b; assertTrue( a == rational( 4*-10,-5*6) ); a=b; assertTrue( a == b ); assertTrue( b == a ); a = rational(4, -5); a += b; assertTrue( a == rational( 4*6 + -5*-10, -5*6 ) ); a -= b; assertTrue( a == rational(-4, 5) ); a = b; assertTrue( a == b ); a -= a; assertTrue( a == 0 ); assertTrue( 0 == a ); rational c(15); assertTrue( 15 == c ); a /= c; assertTrue( a == 0 ); assertTrue( 0 == a ); a = 1234567l; assertTrue( a == 1234567l ); long d = 4; a = d; assertTrue( a == d ); assertTrue( d == a ); a += (b = 5); assertTrue( a == d+5 ); assertTrue( b == 5 ); a = rational (4, -5); b = rational (5, -4); assertTrue( a*b == 1 ); assertTrue( a/b == rational( -4*4 , -5*5 ) ); assertTrue( b/a == rational( -5*5 , -4*4 ) ); rational tot = 0; for (int i = 0; i <= 50; ++i) { tot += rational(i, i+1); } assertTrue( tot == rational( 64263385L, 206760032L ) ); assertTrue( rational( 1,4) == rational().fromString("[ 1 / - - 4 ]")); assertTrue( rational(-1,4) == rational().fromString("[-1 / - - 4 ]")); assertTrue( rational(-1,4) == rational().fromString("[ - 1 / -- 4 ]")); assertTrue( rational( 1,4) == rational().fromString("[ - 1 / - 4 ]")); { // selection_sort() rational VEC[17]; size_t nVEC = sizeof(VEC) / sizeof(*VEC); size_t i; srand( 1492 ); for (i=0; i