File: test_bignum.cpp

package info (click to toggle)
duckdb 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 299,196 kB
  • sloc: cpp: 865,414; ansic: 57,292; python: 18,871; sql: 12,663; lisp: 11,751; yacc: 7,412; lex: 1,682; sh: 747; makefile: 558
file content (83 lines) | stat: -rw-r--r-- 4,216 bytes parent folder | download | duplicates (3)
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
#include "test_helpers.hpp"
#include "duckdb/common/types/bignum.hpp"
#include "catch.hpp"

using namespace duckdb;
using namespace std;

TEST_CASE("Test Bignum::FromByteArray", "[api]") {
	{ // 0
		uint8_t data[] {0};
		idx_t size = 1;
		bool is_negative = false;
		auto str = Bignum::FromByteArray(data, size, is_negative);
		REQUIRE(str.size() == size + 3);
		REQUIRE(uint8_t(str[0]) == 0x80);
		REQUIRE(uint8_t(str[1]) == 0x00);
		REQUIRE(uint8_t(str[2]) == uint8_t(size));
		for (idx_t i = 0; i < size; i++) {
			REQUIRE(uint8_t(str[3 + i]) == data[i]);
		}
	}
	{ // -1
		uint8_t data[] {1};
		idx_t size = 1;
		bool is_negative = true;
		auto str = Bignum::FromByteArray(data, size, is_negative);
		REQUIRE(str.size() == size + 3);
		REQUIRE(uint8_t(str[0]) == 0x7f);
		REQUIRE(uint8_t(str[1]) == 0xff);
		REQUIRE(uint8_t(str[2]) == uint8_t(~size));
		for (idx_t i = 0; i < size; i++) {
			REQUIRE(uint8_t(str[3 + i]) == uint8_t(~data[i]));
		}
	}
	{ // max bignum == max double == 2^1023 * (1 + (1 − 2^−52)) == 2^1024 - 2^971 ==
		// 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
		uint8_t data[] {
		    // little endian
		    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		};
		idx_t size = 128;
		bool is_negative = false;
		auto str = Bignum::FromByteArray(data, size, is_negative);
		REQUIRE(str.size() == size + 3);
		REQUIRE(uint8_t(str[0]) == 0x80);
		REQUIRE(uint8_t(str[1]) == 0x00);
		REQUIRE(uint8_t(str[2]) == uint8_t(size));
		for (idx_t i = 0; i < size; i++) {
			REQUIRE(uint8_t(str[3 + i]) == data[i]);
		}
	}
	{ // min bignum == min double == -(2^1023 * (1 + (1 − 2^−52))) == -(2^1024 - 2^971) ==
		// -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
		uint8_t data[] {
		    // little endian (absolute value)
		    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		};
		idx_t size = 128;
		bool is_negative = true;
		auto str = Bignum::FromByteArray(data, size, is_negative);
		REQUIRE(str.size() == size + 3);
		REQUIRE(uint8_t(str[0]) == 0x7f);
		REQUIRE(uint8_t(str[1]) == 0xff);
		REQUIRE(uint8_t(str[2]) == uint8_t(~size));
		for (idx_t i = 0; i < size; i++) {
			REQUIRE(uint8_t(str[3 + i]) == uint8_t(~data[i]));
		}
	}
}