| 1 | #include "mycpp/gc_mops.h"
|
| 2 |
|
| 3 | #include "mycpp/runtime.h"
|
| 4 | #include "vendor/greatest.h"
|
| 5 |
|
| 6 | TEST bigint_test() {
|
| 7 | // You need to instantiate it as a BigInt, the constant (1) doesn't work
|
| 8 | // And also use %ld
|
| 9 |
|
| 10 | mops::BigInt i = mops::BigInt{1} << 31;
|
| 11 | log("bad i = %d", i); // bug
|
| 12 | log("good i = %ld", i);
|
| 13 | log("");
|
| 14 |
|
| 15 | mops::BigInt i2 = mops::BigInt{1} << 32;
|
| 16 | log("good i2 = %ld", i2);
|
| 17 | log("");
|
| 18 |
|
| 19 | mops::BigInt i3 = i2 + i2;
|
| 20 | log("good i3 = %ld", i3);
|
| 21 | log("");
|
| 22 |
|
| 23 | int64_t j = int64_t{1} << 31;
|
| 24 | log("bad j = %d", j); // bug
|
| 25 | log("good j = %ld", j);
|
| 26 |
|
| 27 | PASS();
|
| 28 | }
|
| 29 |
|
| 30 | TEST static_cast_test() {
|
| 31 | // These conversion ops are currently implemented by static_cast<>
|
| 32 |
|
| 33 | auto big = mops::BigInt{1} << 31;
|
| 34 |
|
| 35 | // Turns into a negative number
|
| 36 | int i = mops::BigTruncate(big);
|
| 37 | log("i = %d", i);
|
| 38 |
|
| 39 | // Truncates float to int. TODO: Test out Oils behavior.
|
| 40 | float f = 3.14f;
|
| 41 | auto fbig = mops::FromFloat(f);
|
| 42 | log("%f -> %ld", f, fbig);
|
| 43 |
|
| 44 | f = 3.99f;
|
| 45 | fbig = mops::FromFloat(f);
|
| 46 | log("%f = %ld", f, fbig);
|
| 47 |
|
| 48 | // OK this is an exact integer
|
| 49 | f = mops::ToFloat(big);
|
| 50 | log("f = %f", f);
|
| 51 | ASSERT_EQ_FMT(f, 2147483648.0, "%f");
|
| 52 |
|
| 53 | PASS();
|
| 54 | }
|
| 55 |
|
| 56 | TEST conversion_test() {
|
| 57 | mops::BigInt int_min{INT64_MIN};
|
| 58 | mops::BigInt int_max{INT64_MAX};
|
| 59 | BigStr* int_str;
|
| 60 |
|
| 61 | int_str = mops::ToStr(15);
|
| 62 | ASSERT(str_equals0("15", int_str));
|
| 63 | print(mops::ToStr(int_min));
|
| 64 | print(mops::ToStr(int_max));
|
| 65 | print(kEmptyString);
|
| 66 |
|
| 67 | int_str = mops::ToOctal(15);
|
| 68 | ASSERT(str_equals0("17", int_str));
|
| 69 | print(mops::ToOctal(int_min));
|
| 70 | print(mops::ToOctal(int_max));
|
| 71 | print(kEmptyString);
|
| 72 |
|
| 73 | int_str = mops::ToHexLower(15);
|
| 74 | ASSERT(str_equals0("f", int_str));
|
| 75 | print(mops::ToHexLower(int_min));
|
| 76 | print(mops::ToHexLower(int_max));
|
| 77 | print(kEmptyString);
|
| 78 |
|
| 79 | int_str = mops::ToHexUpper(15);
|
| 80 | ASSERT(str_equals0("F", int_str));
|
| 81 | print(mops::ToHexUpper(int_min));
|
| 82 | print(mops::ToHexUpper(int_max));
|
| 83 | print(kEmptyString);
|
| 84 |
|
| 85 | PASS();
|
| 86 | }
|
| 87 |
|
| 88 | GREATEST_MAIN_DEFS();
|
| 89 |
|
| 90 | int main(int argc, char** argv) {
|
| 91 | gHeap.Init();
|
| 92 |
|
| 93 | GREATEST_MAIN_BEGIN();
|
| 94 |
|
| 95 | RUN_TEST(bigint_test);
|
| 96 | RUN_TEST(static_cast_test);
|
| 97 | RUN_TEST(conversion_test);
|
| 98 |
|
| 99 | gHeap.CleanProcessExit();
|
| 100 |
|
| 101 | GREATEST_MAIN_END();
|
| 102 |
|
| 103 | return 0;
|
| 104 | }
|