Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
<ClCompile Include="..\..\..\test\CoinMpsIOTest.cpp" />
<ClCompile Include="..\..\..\test\CoinPackedMatrixTest.cpp" />
<ClCompile Include="..\..\..\test\CoinPackedVectorTest.cpp" />
<ClCompile Include="..\..\..\test\CoinRationalTest.cpp" />
<ClCompile Include="..\..\..\test\CoinShallowPackedVectorTest.cpp" />
<ClCompile Include="..\..\..\test\unitTest.cpp" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/CoinRational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ bool CoinRational::nearestRational_(double val, double maxdelta, int64_t maxdnom
{
double intpart;
if (floor(val)==val) {
numerator_ = val;
denominator_ = 1.0;
numerator_ = (int64_t) val;
denominator_ = 1;
return true;
}
double fracpart = fabs(modf(val, &intpart));
Expand Down Expand Up @@ -83,7 +83,7 @@ bool CoinRational::nearestRational_(double val, double maxdelta, int64_t maxdnom
assert(inaccuracy <= maxdelta);
}
#endif
numerator_ += std::abs(intpart) * denominator_;
numerator_ += ((int64_t) std::abs(intpart)) * denominator_;
if (val < 0)
numerator_ *= -1;
#if DEBUG_X > 1
Expand Down
8 changes: 8 additions & 0 deletions src/CoinRational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class COINUTILSLIB_EXPORT CoinRational
bool nearestRational_(double val, double maxdelta, int64_t maxdnom);
};

//#############################################################################
/** A function that tests the methods in the class. The
only reason for it not to be a member method is that this way it doesn't
have to be compiled into the library. And that's a gain, because the
library should be compiled with optimization on, but this method should be
compiled with debugging. */
void CoinRationalUnitTest();

#endif

/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
Expand Down
165 changes: 165 additions & 0 deletions test/CoinRationalTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifdef NDEBUG
#undef NDEBUG
#endif

#include "CoinUtilsConfig.h"

#ifdef COINUTILS_HAS_STDINT_H
#include <stdint.h>
#endif

#include <cassert>
#include <iostream>
#include "CoinRational.hpp"

void
CoinRationalUnitTest()
{

// Test default constructor
{
CoinRational a; // 0/1
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 0);
assert(a.getDenominator() == 1);
}

// Requires int64_t
// Test constructor with assignment
{
CoinRational a(4294967295, 4294967296);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 4294967295);
assert(a.getDenominator() == 4294967296);
}

// Requires int64_t
// Test constructor with assignment
{
CoinRational a(9223372036854775807, 4294967299);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 9223372036854775807);
assert(a.getDenominator() == 4294967299);
}

// Requires int64_t
// Test constructor with nearestRational calls
{
CoinRational a(2147483699.5, 0.00001, 4294967299);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 4294967399);
assert(a.getDenominator() == 2);
}

// Test constructor with nearestRational calls
{
CoinRational a(-3.0, 0.0001, 100);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == -3);
assert(a.getDenominator() == 1);
}

{
CoinRational a(3.0, 0.0001, 100);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 3);
assert(a.getDenominator() == 1);
}

{
// return 0/1 if best is more than maxdelta tolerance
CoinRational a(0.367879441, 0.0001, 100); // 1/e
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 32);
assert(a.getDenominator() == 87);
}

{
CoinRational a(10.367879441, 0.0001, 100); // 10 + 1/e
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 902);
assert(a.getDenominator() == 87);
}

{
// return 0/1 if best is more than maxdelta tolerance
CoinRational a(0.367879441, 0.000001, 100); // 1/e
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 0);
assert(a.getDenominator() == 1);
}

{
CoinRational a(3.0 / 7.0, 0.0001, 100);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 3);
assert(a.getDenominator() == 7);
}

{
CoinRational a(7.0 / 3.0, 0.0001, 100);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 7);
assert(a.getDenominator() == 3);
}

{
double sqrt13 = sqrt(13);
CoinRational a(sqrt13, 0.01, 20);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 18);
assert(a.getDenominator() == 5);
}

{
double sqrt13 = sqrt(13);
CoinRational a(sqrt13, 0.002, 30);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 101);
assert(a.getDenominator() == 28);
}

{
CoinRational a(0.25, 0.1, 3);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 1);
assert(a.getDenominator() == 3);
}

{
CoinRational a(0.605551, 0.003, 30);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 17);
assert(a.getDenominator() == 28);
}

{
CoinRational a(0.605551, 0.001, 30);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 20);
assert(a.getDenominator() == 33); // oops, should be at most 30.
}

{
CoinRational a(0.58496250072, 0.00001, 253);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 179);
assert(a.getDenominator() == 306); // oops, should be at most 253. Expected 148/253, but this is apparently on purpose.
}

{
CoinRational a(19.0/11.0, 0.02, 10);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == 12);
assert(a.getDenominator() == 7);
}

{
CoinRational a(-19.0 / 11.0, 0.02, 10);
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
assert(a.getNumerator() == -12);
assert(a.getDenominator() == 7);
}
}
1 change: 1 addition & 0 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ unitTest_SOURCES = \
CoinMpsIOTest.cpp \
CoinPackedMatrixTest.cpp \
CoinPackedVectorTest.cpp \
CoinRationalTest.cpp \
CoinShallowPackedVectorTest.cpp \
unitTest.cpp

Expand Down
7 changes: 6 additions & 1 deletion test/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ am_unitTest_OBJECTS = CoinLpIOTest.$(OBJEXT) \
CoinIndexedVectorTest.$(OBJEXT) \
CoinMessageHandlerTest.$(OBJEXT) CoinModelTest.$(OBJEXT) \
CoinMpsIOTest.$(OBJEXT) CoinPackedMatrixTest.$(OBJEXT) \
CoinPackedVectorTest.$(OBJEXT) \
CoinPackedVectorTest.$(OBJEXT) CoinRationalTest.$(OBJEXT) \
CoinShallowPackedVectorTest.$(OBJEXT) unitTest.$(OBJEXT)
unitTest_OBJECTS = $(am_unitTest_OBJECTS)
am__DEPENDENCIES_1 =
Expand Down Expand Up @@ -155,6 +155,7 @@ am__depfiles_remade = ./$(DEPDIR)/CoinDenseVectorTest.Po \
./$(DEPDIR)/CoinModelTest.Po ./$(DEPDIR)/CoinMpsIOTest.Po \
./$(DEPDIR)/CoinPackedMatrixTest.Po \
./$(DEPDIR)/CoinPackedVectorTest.Po \
./$(DEPDIR)/CoinRationalTest.Po \
./$(DEPDIR)/CoinShallowPackedVectorTest.Po \
./$(DEPDIR)/unitTest.Po
am__mv = mv -f
Expand Down Expand Up @@ -361,6 +362,7 @@ unitTest_SOURCES = \
CoinMpsIOTest.cpp \
CoinPackedMatrixTest.cpp \
CoinPackedVectorTest.cpp \
CoinRationalTest.cpp \
CoinShallowPackedVectorTest.cpp \
unitTest.cpp

Expand Down Expand Up @@ -439,6 +441,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoinMpsIOTest.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoinPackedMatrixTest.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoinPackedVectorTest.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoinRationalTest.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoinShallowPackedVectorTest.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/unitTest.Po@am__quote@ # am--include-marker

Expand Down Expand Up @@ -579,6 +582,7 @@ distclean: distclean-am
-rm -f ./$(DEPDIR)/CoinMpsIOTest.Po
-rm -f ./$(DEPDIR)/CoinPackedMatrixTest.Po
-rm -f ./$(DEPDIR)/CoinPackedVectorTest.Po
-rm -f ./$(DEPDIR)/CoinRationalTest.Po
-rm -f ./$(DEPDIR)/CoinShallowPackedVectorTest.Po
-rm -f ./$(DEPDIR)/unitTest.Po
-rm -f Makefile
Expand Down Expand Up @@ -635,6 +639,7 @@ maintainer-clean: maintainer-clean-am
-rm -f ./$(DEPDIR)/CoinMpsIOTest.Po
-rm -f ./$(DEPDIR)/CoinPackedMatrixTest.Po
-rm -f ./$(DEPDIR)/CoinPackedVectorTest.Po
-rm -f ./$(DEPDIR)/CoinRationalTest.Po
-rm -f ./$(DEPDIR)/CoinShallowPackedVectorTest.Po
-rm -f ./$(DEPDIR)/unitTest.Po
-rm -f Makefile
Expand Down
4 changes: 4 additions & 0 deletions test/unitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "CoinPragma.hpp"
#include "CoinFinite.hpp"
#include "CoinError.hpp"
#include "CoinRational.hpp"
#include "CoinHelperFunctions.hpp"
#include "CoinSort.hpp"
#include "CoinShallowPackedVector.hpp"
Expand Down Expand Up @@ -189,6 +190,9 @@ int main (int argc, const char *argv[])
testingMessage( "Testing CoinError\n" );
CoinErrorUnitTest();

testingMessage("Testing CoinRational\n");
CoinRationalUnitTest();

testingMessage( "Testing CoinShallowPackedVector\n" );
CoinShallowPackedVectorUnitTest();

Expand Down
Loading