summaryrefslogtreecommitdiff
path: root/source/tests/bigint.test.c
blob: d7375f5cf35dee1f11a31e68a7ccd355af1108f6 (plain)
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
#define KIT_BIGINT_SIZE 256
#include "../kit/bigint.h"

#define KIT_TEST_FILE bigint
#include "../kit/test.h"

TEST("bigint size check") {
  REQUIRE_EQ(sizeof(u8), 1);
  REQUIRE_EQ(sizeof(u16), 2);
  REQUIRE_EQ(sizeof(u32), 4);
  REQUIRE_EQ(sizeof(u64), 8);
  REQUIRE_EQ(sizeof(i8), 1);
  REQUIRE_EQ(sizeof(i16), 2);
  REQUIRE_EQ(sizeof(i32), 4);
  REQUIRE_EQ(sizeof(i64), 8);
  REQUIRE_EQ(sizeof(bigint_t), 256);
}

TEST("bigint bin hex") {
  REQUIRE(bi_equal(HEX("10"), BIN("10000")));
  REQUIRE(bi_equal(HEX("20"), BIN("100000")));
  REQUIRE(bi_equal(HEX("40"), BIN("1000000")));
  REQUIRE(bi_equal(HEX("80"), BIN("10000000")));
  REQUIRE(bi_equal(HEX("310"), BIN("1100010000")));
  REQUIRE(bi_equal(HEX("420"), BIN("10000100000")));
  REQUIRE(bi_equal(HEX("540"), BIN("10101000000")));
  REQUIRE(bi_equal(HEX("780"), BIN("11110000000")));
}

TEST("bigint hex add") {
  REQUIRE(bi_equal(
      bi_add(HEX("4242424242424242424242424242424242424242"),
             HEX("1111111111111111111111111111111111111111")),
      HEX("5353535353535353535353535353535353535353")));
}

TEST("bigint hex sub") {
  REQUIRE(bi_equal(
      bi_sub(HEX("4242424242424242424242424242424242424242"),
             HEX("1111111111111111111111111111111111111111")),
      HEX("3131313131313131313131313131313131313131")));
}

TEST("bigint base58") {
  REQUIRE(bi_equal(BASE58("31"), bi_u32(58 * 2)));
}

TEST("bigint base58 add") {
  REQUIRE(bi_equal(
      bi_add(BASE58("4242424242424242424242424242424242424242"),
             BASE58("2222222222222222222222222222222222222222")),
      BASE58("5353535353535353535353535353535353535353")));
}

TEST("bigint base58 sub") {
  REQUIRE(bi_equal(
      bi_sub(BASE58("4242424242424242424242424242424242424242"),
             BASE58("2222222222222222222222222222222222222222")),
      BASE58("3131313131313131313131313131313131313131")));
}

TEST("bigint base58 mul") {
  REQUIRE(bi_equal(bi_mul(BASE58("2111111111111111111111"),
                          BASE58("foofoofoofoofoo")),
                   BASE58("foofoofoofoofoo111111111111111111111")));
}

TEST("bigint div") {
  REQUIRE(
      bi_equal(bi_div(bi_i32(-1), bi_i32(-1)).quotient, bi_i32(1)));
  REQUIRE(
      bi_equal(bi_div(bi_i32(-1), bi_i32(-1)).remainder, bi_i32(0)));
  REQUIRE(
      bi_equal(bi_div(bi_i32(-3), bi_i32(2)).quotient, bi_i32(-2)));
  REQUIRE(
      bi_equal(bi_div(bi_i32(-3), bi_i32(2)).remainder, bi_i32(1)));
  REQUIRE(
      bi_equal(bi_div(bi_i32(3), bi_i32(-2)).quotient, bi_i32(-1)));
  REQUIRE(
      bi_equal(bi_div(bi_i32(3), bi_i32(-2)).remainder, bi_i32(1)));
  REQUIRE(
      bi_equal(bi_div(bi_i32(-3), bi_i32(4)).quotient, bi_i32(-1)));
  REQUIRE(
      bi_equal(bi_div(bi_i32(-3), bi_i32(4)).remainder, bi_i32(3)));
  REQUIRE(
      bi_equal(bi_div(bi_i32(3), bi_i32(-4)).quotient, bi_i32(0)));
  REQUIRE(
      bi_equal(bi_div(bi_i32(3), bi_i32(-4)).remainder, bi_i32(3)));

  REQUIRE(
      bi_equal(bi_div(HEX("100"), HEX("10")).quotient, HEX("10")));

  REQUIRE(bi_equal(bi_div(bi_mul(BASE58("foofoofoofoofoofoo"),
                                 BASE58("barbarbarbarbarbar")),
                          BASE58("barbarbarbarbarbar"))
                       .quotient,
                   BASE58("foofoofoofoofoofoo")));

  REQUIRE(bi_equal(bi_div(bi_mul(BASE58("foofoofoofoofoofoofoofoo"),
                                 BASE58("barbarbarbarbarbar")),
                          BASE58("barbarbarbarbarbar"))
                       .quotient,
                   BASE58("foofoofoofoofoofoofoofoo")));

  REQUIRE(bi_equal(
      bi_div(
          bi_mul(BASE58("foofoofoofoofoofoofoofoofoofoofoofoofoofoofo"
                        "ofoofoofoo"),
                 BASE58("barbarbarbarbarbarbarbarbarbarbarbarbarbarba"
                        "rbar")),
          BASE58("barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar"))
          .quotient,
      BASE58(
          "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo")));
}

#undef KIT_TEST_FILE