[poincare] integer comparison

This commit is contained in:
Romain Goyet
2015-09-21 12:05:30 +02:00
parent a3f5b5644a
commit 73de311edc
3 changed files with 26 additions and 1 deletions

View File

@@ -19,6 +19,8 @@ class Integer : public Expression {
// Arithmetic
Integer operator+(const Integer &other) const;
Integer operator*(const Integer &other) const;
bool operator<(const Integer &other) const;
bool operator==(const Integer &other) const;
/*virtual Expression ** children();

View File

@@ -87,8 +87,23 @@ Integer::~Integer() {
Integer::Integer(native_uint_t * digits, uint16_t numberOfDigits) :
m_numberOfDigits(numberOfDigits),
m_digits(digits) {
}
}
// TODO: factor code with "==", they are very similar
bool Integer::operator<(const Integer &other) const {
if (m_numberOfDigits != other.m_numberOfDigits) {
return (m_numberOfDigits < other.m_numberOfDigits);
}
for (uint16_t i = 0; i < m_numberOfDigits; i++) {
// Digits are stored most-significant last
native_uint_t digit = m_digits[m_numberOfDigits-i-1];
native_uint_t otherDigit = other.m_digits[m_numberOfDigits-i-1];
if (digit != otherDigit) {
return (digit < otherDigit);
}
}
return false;
}
bool Integer::operator==(const Integer &other) const {
if (other.m_numberOfDigits != m_numberOfDigits) {

View File

@@ -10,6 +10,14 @@ QUIZ_CASE(poincare_integer) {
assert(Integer("0b1011") == Integer(11));
}
QUIZ_CASE(poincare_integer_compare) {
assert(Integer(123) < Integer(456));
assert(!(Integer(123) < Integer(123)));
assert(Integer(123) < Integer("123456789123456789"));
assert(Integer("123456789123456788") < Integer("123456789123456789"));
assert(!(Integer("123456789123456789") < Integer("123456789123456788")));
}
QUIZ_CASE(poincare_integer_add) {
//assert(Integer((uint32_t)0) + Integer((uint32_t)0) == Integer((uint32_t)0));
assert(Integer(123) + Integer(456) == Integer(579));