Skip to content

Commit 5d621c0

Browse files
authored
Resolve CoinRational warnings and add tests (#246)
Resolve CoinRational warnings related to int64_t and add tests
1 parent 328760e commit 5d621c0

File tree

7 files changed

+188
-4
lines changed

7 files changed

+188
-4
lines changed

MSVisualStudio/v17/CoinUtilsUnitTest/CoinUtilsUnitTest.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
<ClCompile Include="..\..\..\test\CoinMpsIOTest.cpp" />
158158
<ClCompile Include="..\..\..\test\CoinPackedMatrixTest.cpp" />
159159
<ClCompile Include="..\..\..\test\CoinPackedVectorTest.cpp" />
160+
<ClCompile Include="..\..\..\test\CoinRationalTest.cpp" />
160161
<ClCompile Include="..\..\..\test\CoinShallowPackedVectorTest.cpp" />
161162
<ClCompile Include="..\..\..\test\unitTest.cpp" />
162163
</ItemGroup>

src/CoinRational.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ bool CoinRational::nearestRational_(double val, double maxdelta, int64_t maxdnom
2929
{
3030
double intpart;
3131
if (floor(val)==val) {
32-
numerator_ = val;
33-
denominator_ = 1.0;
32+
numerator_ = (int64_t) val;
33+
denominator_ = 1;
3434
return true;
3535
}
3636
double fracpart = fabs(modf(val, &intpart));
@@ -83,7 +83,7 @@ bool CoinRational::nearestRational_(double val, double maxdelta, int64_t maxdnom
8383
assert(inaccuracy <= maxdelta);
8484
}
8585
#endif
86-
numerator_ += std::abs(intpart) * denominator_;
86+
numerator_ += ((int64_t) std::abs(intpart)) * denominator_;
8787
if (val < 0)
8888
numerator_ *= -1;
8989
#if DEBUG_X > 1

src/CoinRational.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ class COINUTILSLIB_EXPORT CoinRational
4040
bool nearestRational_(double val, double maxdelta, int64_t maxdnom);
4141
};
4242

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

4553
/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2

test/CoinRationalTest.cpp

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// This code is licensed under the terms of the Eclipse Public License (EPL).
2+
3+
#ifdef NDEBUG
4+
#undef NDEBUG
5+
#endif
6+
7+
#include "CoinUtilsConfig.h"
8+
9+
#ifdef COINUTILS_HAS_STDINT_H
10+
#include <stdint.h>
11+
#endif
12+
13+
#include <cassert>
14+
#include <iostream>
15+
#include "CoinRational.hpp"
16+
17+
void
18+
CoinRationalUnitTest()
19+
{
20+
21+
// Test default constructor
22+
{
23+
CoinRational a; // 0/1
24+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
25+
assert(a.getNumerator() == 0);
26+
assert(a.getDenominator() == 1);
27+
}
28+
29+
// Requires int64_t
30+
// Test constructor with assignment
31+
{
32+
CoinRational a(4294967295, 4294967296);
33+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
34+
assert(a.getNumerator() == 4294967295);
35+
assert(a.getDenominator() == 4294967296);
36+
}
37+
38+
// Requires int64_t
39+
// Test constructor with assignment
40+
{
41+
CoinRational a(9223372036854775807, 4294967299);
42+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
43+
assert(a.getNumerator() == 9223372036854775807);
44+
assert(a.getDenominator() == 4294967299);
45+
}
46+
47+
// Requires int64_t
48+
// Test constructor with nearestRational calls
49+
{
50+
CoinRational a(2147483699.5, 0.00001, 4294967299);
51+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
52+
assert(a.getNumerator() == 4294967399);
53+
assert(a.getDenominator() == 2);
54+
}
55+
56+
// Test constructor with nearestRational calls
57+
{
58+
CoinRational a(-3.0, 0.0001, 100);
59+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
60+
assert(a.getNumerator() == -3);
61+
assert(a.getDenominator() == 1);
62+
}
63+
64+
{
65+
CoinRational a(3.0, 0.0001, 100);
66+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
67+
assert(a.getNumerator() == 3);
68+
assert(a.getDenominator() == 1);
69+
}
70+
71+
{
72+
// return 0/1 if best is more than maxdelta tolerance
73+
CoinRational a(0.367879441, 0.0001, 100); // 1/e
74+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
75+
assert(a.getNumerator() == 32);
76+
assert(a.getDenominator() == 87);
77+
}
78+
79+
{
80+
CoinRational a(10.367879441, 0.0001, 100); // 10 + 1/e
81+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
82+
assert(a.getNumerator() == 902);
83+
assert(a.getDenominator() == 87);
84+
}
85+
86+
{
87+
// return 0/1 if best is more than maxdelta tolerance
88+
CoinRational a(0.367879441, 0.000001, 100); // 1/e
89+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
90+
assert(a.getNumerator() == 0);
91+
assert(a.getDenominator() == 1);
92+
}
93+
94+
{
95+
CoinRational a(3.0 / 7.0, 0.0001, 100);
96+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
97+
assert(a.getNumerator() == 3);
98+
assert(a.getDenominator() == 7);
99+
}
100+
101+
{
102+
CoinRational a(7.0 / 3.0, 0.0001, 100);
103+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
104+
assert(a.getNumerator() == 7);
105+
assert(a.getDenominator() == 3);
106+
}
107+
108+
{
109+
double sqrt13 = sqrt(13);
110+
CoinRational a(sqrt13, 0.01, 20);
111+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
112+
assert(a.getNumerator() == 18);
113+
assert(a.getDenominator() == 5);
114+
}
115+
116+
{
117+
double sqrt13 = sqrt(13);
118+
CoinRational a(sqrt13, 0.002, 30);
119+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
120+
assert(a.getNumerator() == 101);
121+
assert(a.getDenominator() == 28);
122+
}
123+
124+
{
125+
CoinRational a(0.25, 0.1, 3);
126+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
127+
assert(a.getNumerator() == 1);
128+
assert(a.getDenominator() == 3);
129+
}
130+
131+
{
132+
CoinRational a(0.605551, 0.003, 30);
133+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
134+
assert(a.getNumerator() == 17);
135+
assert(a.getDenominator() == 28);
136+
}
137+
138+
{
139+
CoinRational a(0.605551, 0.001, 30);
140+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
141+
assert(a.getNumerator() == 20);
142+
assert(a.getDenominator() == 33); // oops, should be at most 30.
143+
}
144+
145+
{
146+
CoinRational a(0.58496250072, 0.00001, 253);
147+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
148+
assert(a.getNumerator() == 179);
149+
assert(a.getDenominator() == 306); // oops, should be at most 253. Expected 148/253, but this is apparently on purpose.
150+
}
151+
152+
{
153+
CoinRational a(19.0/11.0, 0.02, 10);
154+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
155+
assert(a.getNumerator() == 12);
156+
assert(a.getDenominator() == 7);
157+
}
158+
159+
{
160+
CoinRational a(-19.0 / 11.0, 0.02, 10);
161+
std::cout << "Testing " << a.getNumerator() << " / " << a.getDenominator() << std::endl;
162+
assert(a.getNumerator() == -12);
163+
assert(a.getDenominator() == 7);
164+
}
165+
}

test/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ unitTest_SOURCES = \
2020
CoinMpsIOTest.cpp \
2121
CoinPackedMatrixTest.cpp \
2222
CoinPackedVectorTest.cpp \
23+
CoinRationalTest.cpp \
2324
CoinShallowPackedVectorTest.cpp \
2425
unitTest.cpp
2526

test/Makefile.in

+6-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ am_unitTest_OBJECTS = CoinLpIOTest.$(OBJEXT) \
123123
CoinIndexedVectorTest.$(OBJEXT) \
124124
CoinMessageHandlerTest.$(OBJEXT) CoinModelTest.$(OBJEXT) \
125125
CoinMpsIOTest.$(OBJEXT) CoinPackedMatrixTest.$(OBJEXT) \
126-
CoinPackedVectorTest.$(OBJEXT) \
126+
CoinPackedVectorTest.$(OBJEXT) CoinRationalTest.$(OBJEXT) \
127127
CoinShallowPackedVectorTest.$(OBJEXT) unitTest.$(OBJEXT)
128128
unitTest_OBJECTS = $(am_unitTest_OBJECTS)
129129
am__DEPENDENCIES_1 =
@@ -155,6 +155,7 @@ am__depfiles_remade = ./$(DEPDIR)/CoinDenseVectorTest.Po \
155155
./$(DEPDIR)/CoinModelTest.Po ./$(DEPDIR)/CoinMpsIOTest.Po \
156156
./$(DEPDIR)/CoinPackedMatrixTest.Po \
157157
./$(DEPDIR)/CoinPackedVectorTest.Po \
158+
./$(DEPDIR)/CoinRationalTest.Po \
158159
./$(DEPDIR)/CoinShallowPackedVectorTest.Po \
159160
./$(DEPDIR)/unitTest.Po
160161
am__mv = mv -f
@@ -361,6 +362,7 @@ unitTest_SOURCES = \
361362
CoinMpsIOTest.cpp \
362363
CoinPackedMatrixTest.cpp \
363364
CoinPackedVectorTest.cpp \
365+
CoinRationalTest.cpp \
364366
CoinShallowPackedVectorTest.cpp \
365367
unitTest.cpp
366368

@@ -439,6 +441,7 @@ distclean-compile:
439441
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoinMpsIOTest.Po@am__quote@ # am--include-marker
440442
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoinPackedMatrixTest.Po@am__quote@ # am--include-marker
441443
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoinPackedVectorTest.Po@am__quote@ # am--include-marker
444+
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoinRationalTest.Po@am__quote@ # am--include-marker
442445
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CoinShallowPackedVectorTest.Po@am__quote@ # am--include-marker
443446
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/unitTest.Po@am__quote@ # am--include-marker
444447

@@ -579,6 +582,7 @@ distclean: distclean-am
579582
-rm -f ./$(DEPDIR)/CoinMpsIOTest.Po
580583
-rm -f ./$(DEPDIR)/CoinPackedMatrixTest.Po
581584
-rm -f ./$(DEPDIR)/CoinPackedVectorTest.Po
585+
-rm -f ./$(DEPDIR)/CoinRationalTest.Po
582586
-rm -f ./$(DEPDIR)/CoinShallowPackedVectorTest.Po
583587
-rm -f ./$(DEPDIR)/unitTest.Po
584588
-rm -f Makefile
@@ -635,6 +639,7 @@ maintainer-clean: maintainer-clean-am
635639
-rm -f ./$(DEPDIR)/CoinMpsIOTest.Po
636640
-rm -f ./$(DEPDIR)/CoinPackedMatrixTest.Po
637641
-rm -f ./$(DEPDIR)/CoinPackedVectorTest.Po
642+
-rm -f ./$(DEPDIR)/CoinRationalTest.Po
638643
-rm -f ./$(DEPDIR)/CoinShallowPackedVectorTest.Po
639644
-rm -f ./$(DEPDIR)/unitTest.Po
640645
-rm -f Makefile

test/unitTest.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "CoinPragma.hpp"
1717
#include "CoinFinite.hpp"
1818
#include "CoinError.hpp"
19+
#include "CoinRational.hpp"
1920
#include "CoinHelperFunctions.hpp"
2021
#include "CoinSort.hpp"
2122
#include "CoinShallowPackedVector.hpp"
@@ -189,6 +190,9 @@ int main (int argc, const char *argv[])
189190
testingMessage( "Testing CoinError\n" );
190191
CoinErrorUnitTest();
191192

193+
testingMessage("Testing CoinRational\n");
194+
CoinRationalUnitTest();
195+
192196
testingMessage( "Testing CoinShallowPackedVector\n" );
193197
CoinShallowPackedVectorUnitTest();
194198

0 commit comments

Comments
 (0)