|
| 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 | +} |
0 commit comments