integer.cpp
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <quiz.h>
#include <poincare.h>
#include <assert.h>
using namespace Poincare;
QUIZ_CASE(poincare_integer) {
assert(Integer(123).isEqualTo(Integer(123)));
assert(Integer("123").isEqualTo(Integer(123)));
assert(!Integer("-123").isEqualTo(Integer(123)));
assert(Integer("-123").isEqualTo(Integer(-123)));
assert(Integer((int64_t)1234567891011121314).isEqualTo(Integer((int64_t)1234567891011121314)));
//FIXME: assert(Integer("0x2BABE") == Integer(178878));
//FIXME: assert(Integer("0b1011") == Integer(11));
}
QUIZ_CASE(poincare_integer_compare) {
assert(Integer(123).isLowerThan(Integer(456)));
assert(!Integer(123).isLowerThan(Integer(123)));
assert(!Integer(-123).isLowerThan(Integer(-123)));
assert(Integer(-100).isLowerThan(Integer(2)));
assert(Integer(-200).isLowerThan(Integer(-100)));
assert(Integer(123).isLowerThan(Integer("123456789123456789")));
assert(Integer(-123).isLowerThan(Integer("123456789123456789")));
assert(Integer("123456789123456788").isLowerThan(Integer("123456789123456789")));
assert(Integer("-1234567891234567892109209109").isLowerThan(Integer("123456789123456789")));
assert(!Integer("123456789123456789").isLowerThan(Integer("123456789123456788")));
}
QUIZ_CASE(poincare_integer_addition) {
assert(Integer::Addition(Integer("0"), Integer("0")).isEqualTo(Integer(0)));
assert(Integer::Addition(Integer(123), Integer(456)).isEqualTo(Integer(579)));
assert(Integer::Addition(Integer("123456789123456789"), Integer(1)).isEqualTo(Integer("123456789123456790")));
assert(Integer::Addition(Integer("-123456789123456789"), Integer("123456789123456789")).isEqualTo(Integer("0")));
assert(Integer::Addition(Integer("234"), Integer(-234)).isEqualTo(Integer(0)));
assert(Integer::Addition(Integer("18446744073709551616"), Integer("18446744073709551368")).isEqualTo(Integer("36893488147419102984")));
//2^64+2^64
assert(Integer::Addition(Integer("18446744073709551616"), Integer("18446744073709551616")).isEqualTo(Integer("36893488147419103232")));
//2^64+2^32
assert(Integer::Addition(Integer("18446744073709551616"), Integer("4294967296")).isEqualTo(Integer("18446744078004518912")));
//2^64+1
assert(Integer::Addition(Integer("18446744073709551616"), Integer("1")).isEqualTo(Integer("18446744073709551617")));
//2^32+2^32
assert(Integer::Addition(Integer("4294967296"), Integer("4294967296")).isEqualTo(Integer("8589934592")));
//2^32+1
assert(Integer::Addition(Integer("4294967296"), Integer("1")).isEqualTo(Integer("4294967297")));
//2^16+1
assert(Integer::Addition(Integer("65537"), Integer("1")).isEqualTo(Integer("65538")));
//2^16+2^16
assert(Integer::Addition(Integer("65537"), Integer("65537")).isEqualTo(Integer("131074")));
}
QUIZ_CASE(poincare_integer_subtraction) {
assert(Integer::Subtraction(Integer(123), Integer(23)).isEqualTo(Integer(100)));
assert(Integer::Subtraction(Integer("123456789123456789"), Integer("9999999999")).isEqualTo(Integer("123456779123456790")));
assert(Integer::Subtraction(Integer(23), Integer(100)).isEqualTo(Integer(-77)));
assert(Integer::Subtraction(Integer(23), Integer(23)).isEqualTo(Integer(0)));
assert(Integer::Subtraction(Integer(-23), Integer(-23)).isEqualTo(Integer(0)));
assert(Integer::Subtraction(Integer("-123456789123456789"), Integer("-123456789123456789")).isEqualTo(Integer(0)));
assert(Integer::Subtraction(Integer("123456789123456789"), Integer("123456789123456789")).isEqualTo(Integer(0)));
assert(Integer::Subtraction(Integer("18446744073709551616"), Integer("18446744073709551368")).isEqualTo(Integer(248)));
//2^64-2^64
assert(Integer::Subtraction(Integer("18446744073709551616"), Integer("18446744073709551616")).isEqualTo(Integer("0")));
//2^64-2^32
assert(Integer::Subtraction(Integer("18446744073709551616"), Integer("4294967296")).isEqualTo(Integer("18446744069414584320")));
//2^32-2^64
assert(Integer::Subtraction(Integer("4294967296"), Integer("18446744073709551616")).isEqualTo(Integer("-18446744069414584320")));
//2^64-1
assert(Integer::Subtraction(Integer("18446744073709551616"), Integer("1")).isEqualTo(Integer("18446744073709551615")));
//1-2^64
assert(Integer::Subtraction(Integer("1"), Integer("18446744073709551616")).isEqualTo(Integer("-18446744073709551615")));
//2^32-2^32
assert(Integer::Subtraction(Integer("4294967296"), Integer("4294967296")).isEqualTo(Integer("0")));
//2^32-1
assert(Integer::Subtraction(Integer("4294967296"), Integer("1")).isEqualTo(Integer("4294967295")));
//2^16-1
assert(Integer::Subtraction(Integer("65537"), Integer("1")).isEqualTo(Integer("65536")));
//2^16-2^16
assert(Integer::Subtraction(Integer("65537"), Integer("65537")).isEqualTo(Integer("0")));
}
QUIZ_CASE(poincare_integer_multiplication) {
assert(Integer::Multiplication(Integer(12), Integer(34)).isEqualTo(Integer(408)));
assert(Integer::Multiplication(Integer(56), Integer(0)).isEqualTo(Integer(0)));
assert(Integer::Multiplication(Integer(-12), Integer(34)).isEqualTo(Integer(-408)));
assert(Integer::Multiplication(Integer(-12), Integer(-34)).isEqualTo(Integer(408)));
assert(Integer::Multiplication(Integer(123456), Integer(654321)).isEqualTo(Integer("80779853376")));
assert(Integer::Multiplication(Integer("9999999999"), Integer("9999999999")).isEqualTo(Integer("99999999980000000001")));
assert(Integer::Multiplication(Integer("-23"), Integer("0")).isEqualTo(Integer("0")));
assert(Integer::Multiplication(Integer("-23456787654567765456"), Integer("0")).isEqualTo(Integer("0")));
}
QUIZ_CASE(poincare_integer_divide) {
assert(Integer::Division(Integer(8), Integer(4)).quotient.isEqualTo(Integer(2)) && Integer::Division(Integer(8), Integer(4)).remainder.isEqualTo(Integer(0)));
assert(Integer::Division(Integer("3293920983030066"), Integer(38928)).quotient.isEqualTo(Integer("84615726033")) && Integer::Division(Integer("3293920983030066"), Integer(38928)).remainder.isEqualTo(Integer(17442)));
assert(Integer::Division(Integer("3293920983030066"), Integer("389282362616")).quotient.isEqualTo(Integer(8461)) && Integer::Division(Integer("3293920983030066"), Integer("389282362616")).remainder.isEqualTo(Integer("202912936090")));
assert(Integer::Division(Integer("-18940566"), Integer("499030")).quotient.isEqualTo(Integer(-38)) && Integer::Division(Integer("-18940566"), Integer("499030")).remainder.isEqualTo(Integer("22574")));
assert(Integer::Division(Integer("234567909876"), Integer("-234567898")).quotient.isEqualTo(Integer(-1000)) && Integer::Division(Integer("234567909876"), Integer("-234567898")).remainder.isEqualTo(Integer("11876")));
assert(Integer::Division(Integer("-567"), Integer("-12")).quotient.isEqualTo(Integer(48)) && Integer::Division(Integer("-567"), Integer("-12")).remainder.isEqualTo(Integer("9")));
assert(Integer::Division(Integer("-576"), Integer("-12")).quotient.isEqualTo(Integer(48)) && Integer::Division(Integer("-576"), Integer("-12")).remainder.isEqualTo(Integer("0")));
assert(Integer::Division(Integer("576"), Integer("-12")).quotient.isEqualTo(Integer(-48)) && Integer::Division(Integer("576"), Integer("-12")).remainder.isEqualTo(Integer("0")));
assert(Integer::Division(Integer("-576"), Integer("12")).quotient.isEqualTo(Integer(-48)) && Integer::Division(Integer("-576"), Integer("12")).remainder.isEqualTo(Integer("0")));
assert(Integer::Division(Integer("12345678910111213141516171819202122232425"), Integer("10")).remainder.isEqualTo(Integer("5")));
assert(Integer::Division(Integer("12345678910111213141516171819202122232425"), Integer("10")).quotient.isEqualTo(Integer("1234567891011121314151617181920212223242")));
assert(Integer::Division(Integer("1234567891011121314151617181920212223242"), Integer("10")).quotient.isEqualTo(Integer("123456789101112131415161718192021222324")) && Integer::Division(Integer("1234567891011121314151617181920212223242"), Integer("10")).remainder.isEqualTo(Integer("2")));
assert(Integer::Division(Integer("123456789101112131415161718192021222324"), Integer("10")).quotient.isEqualTo(Integer("12345678910111213141516171819202122232")) && Integer::Division(Integer("123456789101112131415161718192021222324"), Integer("10")).remainder.isEqualTo(Integer("4")));
assert(Integer::Division(Integer("12345678910111213141516171819202122232"), Integer("10")).quotient.isEqualTo(Integer("1234567891011121314151617181920212223")) && Integer::Division(Integer("12345678910111213141516171819202122232"), Integer("10")).remainder.isEqualTo(Integer("2")));
assert(Integer::Division(Integer("1234567891011121314151617181920212223"), Integer("10")).quotient.isEqualTo(Integer("123456789101112131415161718192021222")) && Integer::Division(Integer("1234567891011121314151617181920212223"), Integer("10")).remainder.isEqualTo(Integer("3")));
assert(Integer::Division(Integer("123456789101112131415161718192021222"), Integer("10")).quotient.isEqualTo(Integer("12345678910111213141516171819202122")) && Integer::Division(Integer("123456789101112131415161718192021222"), Integer("10")).remainder.isEqualTo(Integer("2")));
assert(Integer::Division(Integer("12345678910111213141516171819202122"), Integer("10")).quotient.isEqualTo(Integer("1234567891011121314151617181920212")) && Integer::Division(Integer("12345678910111213141516171819202122"), Integer("10")).remainder.isEqualTo(Integer("2")));
assert(Integer::Division(Integer("0"), Integer("-10")).quotient.isEqualTo(Integer("0")) && Integer::Division(Integer("0"), Integer("-10")).remainder.isEqualTo(Integer("0")));
assert(Integer::Division(Integer("0"), Integer("-123456789098760")).quotient.isEqualTo(Integer("0")) && Integer::Division(Integer("0"), Integer("-123456789098760")).remainder.isEqualTo(Integer("0")));
assert(Integer::Division(Integer("2305843009213693952"), Integer("2305843009213693921")).quotient.isEqualTo(Integer("1")) && Integer::Division(Integer("2305843009213693952"), Integer("2305843009213693921")).remainder.isEqualTo(Integer("31")));
}
template<typename T>
void assert_integer_evals_to(int i, T result) {
assert(Integer(i).approximate<T>() == result);
}
QUIZ_CASE(poincare_integer_evaluate) {
assert_integer_evals_to(1, 1.0f);
assert_integer_evals_to(12345678, 12345678.0);
assert_integer_evals_to(0, 0.0f);
assert_integer_evals_to(-0, 0.0);
assert_integer_evals_to(-1, -1.0f);
assert_integer_evals_to(12345678, 12345678.0);
}