From c52d38c2a134488c5212f21d963a1ae442206fc1 Mon Sep 17 00:00:00 2001 From: "Christopher L. Conway" Date: Thu, 6 May 2010 20:08:04 +0000 Subject: Adding tests for Rational::fromDecimal --- src/util/rational.h | 23 +++++++++++++------ test/unit/Makefile.am | 3 ++- test/unit/util/bitvector_black.h | 4 ++-- test/unit/util/rational_black.h | 48 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 test/unit/util/rational_black.h diff --git a/src/util/rational.h b/src/util/rational.h index f50b4e63e..428f4f12f 100644 --- a/src/util/rational.h +++ b/src/util/rational.h @@ -10,13 +10,7 @@ ** See the file COPYING in the top-level source directory for licensing ** information. ** - ** A multiprecision rational constant. - ** This stores the rational as a pair of multiprecision integers, - ** one for the numerator and one for the denominator. - ** The number is always stored so that the gcd of the numerator and denominator - ** is 1. (This is referred to as referred to as canonical form in GMP's - ** literature.) A consquence is that that the numerator and denominator may be - ** different than the values used to construct the Rational. + ** Multi-precision rational constants. **/ #include "cvc4_public.h" @@ -30,6 +24,21 @@ namespace CVC4 { +/** + ** A multi-precision rational constant. + ** This stores the rational as a pair of multi-precision integers, + ** one for the numerator and one for the denominator. + ** The number is always stored so that the gcd of the numerator and denominator + ** is 1. (This is referred to as referred to as canonical form in GMP's + ** literature.) A consequence is that that the numerator and denominator may be + ** different than the values used to construct the Rational. + ** + ** NOTE: The correct way to create a Rational from an int is to use one of the + ** int numerator/int denominator constructors with the denominator 1. Trying + ** to construct a Rational with a single int, e.g., Rational(0), will put you + ** in danger of invoking the char* constructor, from whence you will segfault. + **/ + class Rational { private: /** diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 2ec5122f3..b3a8e12eb 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -27,7 +27,8 @@ UNIT_TESTS = \ util/exception_black \ util/integer_black \ util/integer_white \ - util/rational_white + util/rational_black \ + util/rational_white # Things that aren't tests but that tests rely on and need to # go into the distribution diff --git a/test/unit/util/bitvector_black.h b/test/unit/util/bitvector_black.h index f35107af0..08e1216f1 100644 --- a/test/unit/util/bitvector_black.h +++ b/test/unit/util/bitvector_black.h @@ -1,5 +1,5 @@ /********************* */ -/** integer_black.h +/** bitvector_black.h ** Original author: taking ** Major contributors: none ** Minor contributors (to current version): none @@ -10,7 +10,7 @@ ** See the file COPYING in the top-level source directory for licensing ** information. ** - ** Black box testing of CVC4::Integer. + ** Black box testing of CVC4::BitVector. **/ #include diff --git a/test/unit/util/rational_black.h b/test/unit/util/rational_black.h new file mode 100644 index 000000000..395a5099d --- /dev/null +++ b/test/unit/util/rational_black.h @@ -0,0 +1,48 @@ +/********************* */ +/** rational_black.h + ** Original author: cconway + ** Major contributors: none + ** Minor contributors (to current version): none + ** This file is part of the CVC4 prototype. + ** Copyright (c) 2009, 2010 The Analysis of Computer Systems Group (ACSys) + ** Courant Institute of Mathematical Sciences + ** New York University + ** See the file COPYING in the top-level source directory for licensing + ** information. + ** + ** Black box testing of CVC4::Rational. + **/ + +#include +#include + +#include "util/rational.h" + +using namespace CVC4; +using namespace std; + +const char* canReduce = "4547897890548754897897897897890789078907890/54878902347890234"; + +class RationalBlack : public CxxTest::TestSuite { +public: + + void testFromDecimal() { + TS_ASSERT_EQUALS( Rational(0,1), Rational::fromDecimal("0") ); + TS_ASSERT_EQUALS( Rational(1,1), Rational::fromDecimal("1") ); + TS_ASSERT_EQUALS( Rational(-1,1), Rational::fromDecimal("-1") ); + TS_ASSERT_EQUALS( Rational(3,2), Rational::fromDecimal("1.5") ); + TS_ASSERT_EQUALS( Rational(-3,2), Rational::fromDecimal("-1.5") ); + TS_ASSERT_EQUALS( Rational(7,10), Rational::fromDecimal(".7") ); + TS_ASSERT_EQUALS( Rational(-7,10), Rational::fromDecimal("-.7") ); + TS_ASSERT_EQUALS( Rational(5,1), Rational::fromDecimal("5.") ); + TS_ASSERT_EQUALS( Rational(-5,1), Rational::fromDecimal("-5.") ); + TS_ASSERT_EQUALS( Rational(12345,100), Rational::fromDecimal("123.45") ); + + TS_ASSERT_THROWS( Rational::fromDecimal("1.2.3");, const std::invalid_argument& ); + TS_ASSERT_THROWS( Rational::fromDecimal("1.2/3");, const std::invalid_argument& ); + TS_ASSERT_THROWS( Rational::fromDecimal("Hello, world!");, const std::invalid_argument& ); + + Rational(1); + } + +}; -- cgit v1.2.3