ipaddress 0.2.5 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/crunchy.d.ts +130 -0
- package/dist/src/crunchy.d.ts.map +1 -0
- package/dist/src/crunchy.js +616 -0
- package/dist/src/index.d.ts +13 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +21 -0
- package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -7
- package/dist/src/ip_bits.d.ts.map +1 -0
- package/dist/src/ip_bits.js +133 -0
- package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
- package/dist/src/ip_version.d.ts.map +1 -0
- package/dist/src/ip_version.js +6 -0
- package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +38 -35
- package/dist/src/ipaddress.d.ts.map +1 -0
- package/dist/src/ipaddress.js +1243 -0
- package/dist/src/ipv4.d.ts +15 -0
- package/dist/src/ipv4.d.ts.map +1 -0
- package/dist/src/ipv4.js +187 -0
- package/dist/src/ipv6.d.ts +13 -0
- package/dist/src/ipv6.d.ts.map +1 -0
- package/dist/src/ipv6.js +190 -0
- package/dist/src/ipv6_loopback.d.ts +6 -0
- package/dist/src/ipv6_loopback.d.ts.map +1 -0
- package/dist/src/ipv6_loopback.js +51 -0
- package/dist/src/ipv6_mapped.d.ts +6 -0
- package/dist/src/ipv6_mapped.d.ts.map +1 -0
- package/dist/src/ipv6_mapped.js +159 -0
- package/dist/src/ipv6_unspec.d.ts +6 -0
- package/dist/src/ipv6_unspec.d.ts.map +1 -0
- package/dist/src/ipv6_unspec.js +57 -0
- package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
- package/dist/src/prefix.d.ts.map +1 -0
- package/dist/src/prefix.js +142 -0
- package/dist/src/prefix128.d.ts +7 -0
- package/dist/src/prefix128.d.ts.map +1 -0
- package/dist/src/prefix128.js +39 -0
- package/dist/src/prefix32.d.ts +7 -0
- package/dist/src/prefix32.d.ts.map +1 -0
- package/dist/src/prefix32.js +29 -0
- package/dist/src/rle.d.ts +19 -0
- package/dist/src/rle.d.ts.map +1 -0
- package/dist/src/rle.js +73 -0
- package/dist/test/test_crunchy_impls.d.ts +2 -0
- package/dist/test/test_crunchy_impls.d.ts.map +1 -0
- package/dist/test/test_crunchy_impls.js +150 -0
- package/dist/test/test_crunchy_with32bit.d.ts +2 -0
- package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
- package/dist/test/test_crunchy_with32bit.js +382 -0
- package/dist/test/test_ipaddress.d.ts +2 -0
- package/dist/test/test_ipaddress.d.ts.map +1 -0
- package/dist/test/test_ipaddress.js +182 -0
- package/dist/test/test_ipv4.d.ts +2 -0
- package/dist/test/test_ipv4.d.ts.map +1 -0
- package/dist/test/test_ipv4.js +561 -0
- package/dist/test/test_ipv6.d.ts +2 -0
- package/dist/test/test_ipv6.d.ts.map +1 -0
- package/dist/test/test_ipv6.js +313 -0
- package/dist/test/test_ipv6_loopback.d.ts +2 -0
- package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
- package/dist/test/test_ipv6_loopback.js +40 -0
- package/dist/test/test_ipv6_mapped.d.ts +2 -0
- package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
- package/dist/test/test_ipv6_mapped.js +89 -0
- package/dist/test/test_ipv6_unspec.d.ts +2 -0
- package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
- package/dist/test/test_ipv6_unspec.js +39 -0
- package/dist/test/test_prefix128.d.ts +2 -0
- package/dist/test/test_prefix128.d.ts.map +1 -0
- package/dist/test/test_prefix128.js +35 -0
- package/dist/test/test_prefix32.d.ts +2 -0
- package/dist/test/test_prefix32.d.ts.map +1 -0
- package/dist/test/test_prefix32.js +126 -0
- package/dist/test/test_rle.d.ts +2 -0
- package/dist/test/test_rle.d.ts.map +1 -0
- package/dist/test/test_rle.js +141 -0
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/vitest.config.js +11 -0
- package/package.json +41 -37
- package/src/crunchy.ts +731 -0
- package/src/index.ts +23 -0
- package/src/ip_bits.ts +148 -0
- package/src/ip_version.ts +6 -0
- package/src/ipaddress.ts +1347 -0
- package/src/ipv4.ts +203 -0
- package/src/ipv6.ts +202 -0
- package/src/ipv6_loopback.ts +55 -0
- package/src/ipv6_mapped.ts +163 -0
- package/src/ipv6_unspec.ts +59 -0
- package/src/prefix.ts +170 -0
- package/src/prefix128.ts +42 -0
- package/src/prefix32.ts +32 -0
- package/src/rle.ts +85 -0
- package/test/test_crunchy_impls.ts +179 -0
- package/test/test_crunchy_with32bit.ts +510 -0
- package/test/test_ipaddress.ts +208 -0
- package/test/test_ipv4.ts +596 -0
- package/test/test_ipv6.ts +325 -0
- package/test/test_ipv6_loopback.ts +52 -0
- package/test/test_ipv6_mapped.ts +101 -0
- package/test/test_ipv6_unspec.ts +51 -0
- package/test/test_prefix128.ts +39 -0
- package/test/test_prefix32.ts +130 -0
- package/test/test_rle.ts +144 -0
- package/LICENSE +0 -20
- package/README.md +0 -960
- package/crunchy.d.ts +0 -55
- package/index.d.ts +0 -12
- package/ipaddress.js +0 -2869
- package/ipaddress.js.map +0 -1
- package/ipv4.d.ts +0 -14
- package/ipv6.d.ts +0 -12
- package/ipv6_loopback.d.ts +0 -5
- package/ipv6_mapped.d.ts +0 -5
- package/ipv6_unspec.d.ts +0 -5
- package/prefix128.d.ts +0 -6
- package/prefix32.d.ts +0 -6
- package/rle.d.ts +0 -14
package/src/prefix.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import IpBits from "./ip_bits.js";
|
|
2
|
+
import Crunchy from "./crunchy.js";
|
|
3
|
+
|
|
4
|
+
export type From = (source: Prefix, num: number) => Prefix;
|
|
5
|
+
|
|
6
|
+
interface PrefixParams {
|
|
7
|
+
num: number;
|
|
8
|
+
ip_bits: IpBits;
|
|
9
|
+
net_mask: Crunchy;
|
|
10
|
+
vt_from: From;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class Prefix {
|
|
14
|
+
num: number;
|
|
15
|
+
ip_bits: IpBits;
|
|
16
|
+
net_mask: Crunchy;
|
|
17
|
+
vt_from: From;
|
|
18
|
+
|
|
19
|
+
constructor(obj: PrefixParams) {
|
|
20
|
+
this.num = obj.num;
|
|
21
|
+
this.ip_bits = obj.ip_bits;
|
|
22
|
+
this.net_mask = obj.net_mask;
|
|
23
|
+
this.vt_from = obj.vt_from;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public clone(): Prefix {
|
|
27
|
+
return new Prefix({
|
|
28
|
+
num: this.num,
|
|
29
|
+
ip_bits: this.ip_bits,
|
|
30
|
+
net_mask: this.net_mask,
|
|
31
|
+
vt_from: this.vt_from,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public eq(other: Prefix): boolean {
|
|
36
|
+
return this.ip_bits.version == other.ip_bits.version && this.num == other.num;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public ne(other: Prefix): boolean {
|
|
40
|
+
return !this.eq(other);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public cmp(oth: Prefix): number {
|
|
44
|
+
if (this.ip_bits.version < oth.ip_bits.version) {
|
|
45
|
+
return -1;
|
|
46
|
+
} else if (this.ip_bits.version > oth.ip_bits.version) {
|
|
47
|
+
return 1;
|
|
48
|
+
} else {
|
|
49
|
+
if (this.num < oth.num) {
|
|
50
|
+
return -1;
|
|
51
|
+
} else if (this.num > oth.num) {
|
|
52
|
+
return 1;
|
|
53
|
+
} else {
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// #[allow(dead_code)]
|
|
60
|
+
public from(num: number): Prefix {
|
|
61
|
+
return this.vt_from(this, num);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public to_ip_str(): string {
|
|
65
|
+
return this.ip_bits.vt_as_compressed_string(this.ip_bits, this.net_mask);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public size(): Crunchy {
|
|
69
|
+
return Crunchy.one().shl(this.ip_bits.bits - this.num);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public static new_netmask(prefix: number, bits: number): Crunchy {
|
|
73
|
+
let mask = Crunchy.zero();
|
|
74
|
+
const host_prefix = bits - prefix;
|
|
75
|
+
for (let i = 0; i < prefix; ++i) {
|
|
76
|
+
// console.log(">>>", i, host_prefix, mask);
|
|
77
|
+
mask = mask.add(Crunchy.one().shl(host_prefix + i));
|
|
78
|
+
}
|
|
79
|
+
return mask;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public netmask(): Crunchy {
|
|
83
|
+
return this.net_mask;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public get_prefix(): number {
|
|
87
|
+
return this.num;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The hostmask is the contrary of the subnet mask,
|
|
91
|
+
// as it shows the bits that can change within the
|
|
92
|
+
// hosts
|
|
93
|
+
//
|
|
94
|
+
// prefix = IPAddress::Prefix32.new 24
|
|
95
|
+
//
|
|
96
|
+
// prefix.hostmask
|
|
97
|
+
// // => "0.0.0.255"
|
|
98
|
+
//
|
|
99
|
+
public host_mask(): Crunchy {
|
|
100
|
+
let ret = Crunchy.zero();
|
|
101
|
+
for (let _ = 0; _ < this.ip_bits.bits - this.num; _++) {
|
|
102
|
+
ret = ret.shl(1).add(Crunchy.one());
|
|
103
|
+
}
|
|
104
|
+
return ret;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//
|
|
108
|
+
// Returns the length of the host portion
|
|
109
|
+
// of a netmask.
|
|
110
|
+
//
|
|
111
|
+
// prefix = Prefix128.new 96
|
|
112
|
+
//
|
|
113
|
+
// prefix.host_prefix
|
|
114
|
+
// // => 128
|
|
115
|
+
//
|
|
116
|
+
public host_prefix(): number {
|
|
117
|
+
return this.ip_bits.bits - this.num;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
//
|
|
121
|
+
// Transforms the prefix into a string of bits
|
|
122
|
+
// representing the netmask
|
|
123
|
+
//
|
|
124
|
+
// prefix = IPAddress::Prefix128.new 64
|
|
125
|
+
//
|
|
126
|
+
// prefix.bits
|
|
127
|
+
// // => "1111111111111111111111111111111111111111111111111111111111111111"
|
|
128
|
+
// "0000000000000000000000000000000000000000000000000000000000000000"
|
|
129
|
+
//
|
|
130
|
+
public bits(): string {
|
|
131
|
+
return this.netmask().toString(2);
|
|
132
|
+
}
|
|
133
|
+
// #[allow(dead_code)]
|
|
134
|
+
// public net_mask(&self) -> BigUint {
|
|
135
|
+
// return (self.in_mask.clone() >> (self.host_prefix() as usize)) << (self.host_prefix() as usize);
|
|
136
|
+
// }
|
|
137
|
+
|
|
138
|
+
public to_s(): string {
|
|
139
|
+
return this.get_prefix().toString();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// #[allow(dead_code)]
|
|
143
|
+
// public inspect(&self) -> String {
|
|
144
|
+
// return self.to_s();
|
|
145
|
+
// }
|
|
146
|
+
public to_i(): number {
|
|
147
|
+
return this.get_prefix();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public add_prefix(other: Prefix): Prefix {
|
|
151
|
+
return this.from(this.get_prefix() + other.get_prefix());
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public add(other: number): Prefix {
|
|
155
|
+
return this.from(this.get_prefix() + other);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public sub_prefix(other: Prefix): Prefix {
|
|
159
|
+
return this.sub(other.get_prefix());
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public sub(other: number): Prefix {
|
|
163
|
+
if (other > this.get_prefix()) {
|
|
164
|
+
return this.from(other - this.get_prefix());
|
|
165
|
+
}
|
|
166
|
+
return this.from(this.get_prefix() - other);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export default Prefix;
|
package/src/prefix128.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import Prefix from "./prefix.js";
|
|
2
|
+
import IpBits from "./ip_bits.js";
|
|
3
|
+
|
|
4
|
+
const Prefix128 = {
|
|
5
|
+
// #[derive(Ord,PartialOrd,Eq,PartialEq,Debug,Copy,Clone)]
|
|
6
|
+
// pub struct Prefix128 {
|
|
7
|
+
// }
|
|
8
|
+
//
|
|
9
|
+
// impl Prefix128 {
|
|
10
|
+
//
|
|
11
|
+
// Creates a new prefix object for 128 bits IPv6 addresses
|
|
12
|
+
//
|
|
13
|
+
// prefix = IPAddressPrefix128.new 64
|
|
14
|
+
// // => 64
|
|
15
|
+
//
|
|
16
|
+
// #[allow(unused_comparisons)]
|
|
17
|
+
create(num: number): Prefix | null {
|
|
18
|
+
if (num <= 128) {
|
|
19
|
+
// static _FROM: &'static (Fn(&Prefix, usize) -> Result<Prefix, String>) = &from;
|
|
20
|
+
// static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &Prefix128::to_ip_str;
|
|
21
|
+
const ip_bits = IpBits.v6();
|
|
22
|
+
const bits = ip_bits.bits;
|
|
23
|
+
return new Prefix({
|
|
24
|
+
num,
|
|
25
|
+
ip_bits,
|
|
26
|
+
net_mask: Prefix.new_netmask(num, bits),
|
|
27
|
+
vt_from: Prefix128.from, // vt_to_ip_str: _TO_IP_STR
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
from(my: Prefix, num: number): Prefix {
|
|
34
|
+
const ret = Prefix128.create(num);
|
|
35
|
+
if (ret === null) {
|
|
36
|
+
throw new Error(`invalid ipv6 prefix: ${num}`);
|
|
37
|
+
}
|
|
38
|
+
return ret;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default Prefix128;
|
package/src/prefix32.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import Prefix from "./prefix.js";
|
|
2
|
+
import IpBits from "./ip_bits.js";
|
|
3
|
+
|
|
4
|
+
const Prefix32 = {
|
|
5
|
+
from(my: Prefix, num: number): Prefix {
|
|
6
|
+
const ret = Prefix32.create(num);
|
|
7
|
+
if (ret === null) {
|
|
8
|
+
throw new Error(`invalid ipv4 prefix: ${num}`);
|
|
9
|
+
}
|
|
10
|
+
return ret;
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
create(num: number): Prefix | null {
|
|
14
|
+
if (num >= 0 && num <= 32) {
|
|
15
|
+
// static _FROM: &'static (Fn(&::prefix::Prefix, usize) -> Result<::prefix::Prefix, String>) =
|
|
16
|
+
// &from;
|
|
17
|
+
// static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &to_ip_str;
|
|
18
|
+
const ip_bits = IpBits.v4();
|
|
19
|
+
const bits = ip_bits.bits;
|
|
20
|
+
return new Prefix({
|
|
21
|
+
num,
|
|
22
|
+
ip_bits,
|
|
23
|
+
net_mask: Prefix.new_netmask(num, bits),
|
|
24
|
+
vt_from: Prefix32.from,
|
|
25
|
+
// vt_to_ip_str: _TO_IP_STR,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export default Prefix32;
|
package/src/rle.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
class Last {
|
|
2
|
+
val?: Rle;
|
|
3
|
+
max_poses: number[][] = [];
|
|
4
|
+
ret: Rle[] = [];
|
|
5
|
+
|
|
6
|
+
public handle_last() {
|
|
7
|
+
if (this.val == null) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const _last = this.val;
|
|
11
|
+
let max_rles = this.max_poses[_last.part];
|
|
12
|
+
if (max_rles == null) {
|
|
13
|
+
max_rles = this.max_poses[_last.part] = [];
|
|
14
|
+
}
|
|
15
|
+
// console.log(_last.part, this.max_poses);
|
|
16
|
+
for (const idx in max_rles) {
|
|
17
|
+
const prev = this.ret[max_rles[idx]];
|
|
18
|
+
if (prev.cnt > _last.cnt) {
|
|
19
|
+
// console.log(`>>>>> last=${_last}->${idx}->prev=${prev}`);
|
|
20
|
+
_last.max = false;
|
|
21
|
+
} else if (prev.cnt == _last.cnt) {
|
|
22
|
+
// nothing
|
|
23
|
+
} else if (prev.cnt < _last.cnt) {
|
|
24
|
+
// console.log(`<<<<< last=${_last}->${idx}->prev=${prev}`);
|
|
25
|
+
prev.max = false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// println!("push:{}:{:?}", self.ret.len(), _last);
|
|
29
|
+
max_rles.push(this.ret.length);
|
|
30
|
+
_last.pos = this.ret.length;
|
|
31
|
+
this.ret.push(_last);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface RleParams {
|
|
36
|
+
part: number;
|
|
37
|
+
pos: number;
|
|
38
|
+
cnt: number;
|
|
39
|
+
max: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class Rle {
|
|
43
|
+
public part: number;
|
|
44
|
+
public pos: number;
|
|
45
|
+
public cnt: number;
|
|
46
|
+
public max: boolean;
|
|
47
|
+
|
|
48
|
+
constructor(obj: RleParams) {
|
|
49
|
+
this.part = obj.part;
|
|
50
|
+
this.pos = obj.pos;
|
|
51
|
+
this.cnt = obj.cnt;
|
|
52
|
+
this.max = obj.max;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public toString() {
|
|
56
|
+
return `<Rle@part:${this.part},pos:${this.pos},cnt:${this.cnt},max:${this.max}>`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public eq(other: Rle): boolean {
|
|
60
|
+
return this.part == other.part && this.pos == other.pos && this.cnt == other.cnt && this.max == other.max;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public ne(other: Rle): boolean {
|
|
64
|
+
return !this.eq(other);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public static code(parts: number[]): Rle[] {
|
|
68
|
+
const last = new Last();
|
|
69
|
+
// println!("code");
|
|
70
|
+
for (const part of parts) {
|
|
71
|
+
// const part = parts[i]
|
|
72
|
+
// console.log(`part:${part}`);
|
|
73
|
+
if (last.val && last.val.part == part) {
|
|
74
|
+
last.val.cnt += 1;
|
|
75
|
+
} else {
|
|
76
|
+
last.handle_last();
|
|
77
|
+
last.val = new Rle({ part, pos: 0, cnt: 1, max: true });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
last.handle_last();
|
|
81
|
+
return last.ret;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default Rle;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// Exercises the shared CrunchyBase public API against both
|
|
2
|
+
// implementations, so they're guaranteed to behave identically for
|
|
3
|
+
// everything the rest of the codebase actually relies on. Internals
|
|
4
|
+
// specific to one implementation (e.g. CrunchyWith32Bit's 8/14-bit
|
|
5
|
+
// limb representation) are covered separately in
|
|
6
|
+
// test_crunchy_with32bit.ts.
|
|
7
|
+
import { CrunchyBase, CrunchyWithBigInt, CrunchyWith32Bit } from "../src/crunchy.js";
|
|
8
|
+
|
|
9
|
+
const impls: [string, CrunchyBase][] = [
|
|
10
|
+
["CrunchyWithBigInt", CrunchyWithBigInt.bootstrap()],
|
|
11
|
+
["CrunchyWith32Bit", new CrunchyWith32Bit()],
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
describe.each(impls)("Crunchy impl: %s", (_name, c) => {
|
|
15
|
+
describe("#compare", () => {
|
|
16
|
+
it("Should confirm equality", () => {
|
|
17
|
+
const x = c.parse("12345678901234567890");
|
|
18
|
+
const y = c.parse("12345678901234567890");
|
|
19
|
+
assert.isTrue(x.eq(y));
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("Should find first is greater than second", () => {
|
|
23
|
+
const x = c.parse("12345678901234567890");
|
|
24
|
+
const y = c.parse("1234567890");
|
|
25
|
+
assert.isTrue(x.gt(y));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("Should find first is less than second", () => {
|
|
29
|
+
const x = c.parse("1234567890");
|
|
30
|
+
const y = c.parse("12345678901234567890");
|
|
31
|
+
assert.isTrue(x.lt(y));
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("#add", () => {
|
|
36
|
+
it("Should add numbers", () => {
|
|
37
|
+
const x = c.parse("12345678911234567891");
|
|
38
|
+
const y = c.parse("12345678901234567890");
|
|
39
|
+
assert.equal(x.add(y).toString(), "24691357812469135781");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("Should add numbers, first longer than second", () => {
|
|
43
|
+
const x = c.parse("12345678901234567890");
|
|
44
|
+
const y = c.parse("1234567890");
|
|
45
|
+
assert.equal(x.add(y).toString(), "12345678902469135780");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("Should add numbers, second longer than first", () => {
|
|
49
|
+
const x = c.parse("1234567890");
|
|
50
|
+
const y = c.parse("12345678901234567890");
|
|
51
|
+
assert.equal(x.add(y).toString(), "12345678902469135780");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("Should add zero to number", () => {
|
|
55
|
+
const x = c.parse("12345678901234567890");
|
|
56
|
+
const y = c.zero();
|
|
57
|
+
assert.equal(x.add(y).toString(), "12345678901234567890");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("Should add two zeros", () => {
|
|
61
|
+
assert.equal(c.zero().add(c.zero()).toString(), "0");
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("#sub", () => {
|
|
66
|
+
it("Should subtract numbers", () => {
|
|
67
|
+
const x = c.parse("12345678901234567890");
|
|
68
|
+
const y = c.parse("1234567890");
|
|
69
|
+
assert.equal(x.sub(y).toString(), "12345678900000000000");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("Should subtract two equal numbers expecting zero", () => {
|
|
73
|
+
const x = c.parse("20");
|
|
74
|
+
const y = c.parse("20");
|
|
75
|
+
assert.equal(x.sub(y).toString(), "0");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("Should subtract zero", () => {
|
|
79
|
+
const x = c.parse("244137007161");
|
|
80
|
+
assert.equal(x.sub(c.zero()).toString(), "244137007161");
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("#leftShift", () => {
|
|
85
|
+
it("Should left shift a number", () => {
|
|
86
|
+
const x = c.from_number(22 * 256 + 11);
|
|
87
|
+
assert.equal(x.shl(5).toString(), "180576");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("Should left shift zero", () => {
|
|
91
|
+
assert.equal(c.zero().shl(80).toString(), "0");
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("#rightShift", () => {
|
|
96
|
+
it("Should right shift a number", () => {
|
|
97
|
+
const x = c.from_number(22 * 256 + 11);
|
|
98
|
+
assert.equal(x.shr(5).toString(), "176");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("Should right shift a number out of existence", () => {
|
|
102
|
+
const x = c.from_number(22 * 256 + 11);
|
|
103
|
+
assert.equal(x.shr(20).toString(), "0");
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("#mod", () => {
|
|
108
|
+
it("Should calculate modulo", () => {
|
|
109
|
+
const x = c.parse("123456789012345");
|
|
110
|
+
const y = c.from_number(97);
|
|
111
|
+
assert.equal(x.mod(y).toString(), "12");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("Should calculate modulo of number smaller than modulus", () => {
|
|
115
|
+
const x = c.from_number(97);
|
|
116
|
+
const y = c.parse("123456789012345");
|
|
117
|
+
assert.equal(x.mod(y).toString(), "97");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("Should calculate modulo of modulus", () => {
|
|
121
|
+
const x = c.from_number(241);
|
|
122
|
+
const y = c.from_number(241);
|
|
123
|
+
assert.equal(x.mod(y).toString(), "0");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("Should calculate modulo of zero", () => {
|
|
127
|
+
assert.equal(c.zero().mod(c.from_number(241)).toString(), "0");
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe("#from_string", () => {
|
|
132
|
+
it("Should parse a decimal string", () => {
|
|
133
|
+
const x = c.from_string("340282366920938463463374607431768211455");
|
|
134
|
+
assert.isOk(x);
|
|
135
|
+
assert.equal(x!.toString(), "340282366920938463463374607431768211455");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("Should parse a hex string", () => {
|
|
139
|
+
const x = c.from_string("ff", 16);
|
|
140
|
+
assert.isOk(x);
|
|
141
|
+
assert.equal(x!.toString(16), "ff");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("Should reject invalid characters", () => {
|
|
145
|
+
assert.isNull(c.from_string("12a45"));
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("Should reject invalid hex digits", () => {
|
|
149
|
+
assert.isNull(c.from_string("fg", 16));
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("#parse", () => {
|
|
154
|
+
it("Should throw on invalid input", () => {
|
|
155
|
+
assert.throws(() => c.parse("not-a-number"));
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("#create", () => {
|
|
160
|
+
it("Should produce a zero value with no argument", () => {
|
|
161
|
+
assert.isTrue(c.create().eq(c.zero()));
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("Should act like from_number() given a number", () => {
|
|
165
|
+
assert.isTrue(c.create(42).eq(c.from_number(42)));
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("Should act like parse() given a string", () => {
|
|
169
|
+
assert.isTrue(c.create("12345678901234567890").eq(c.parse("12345678901234567890")));
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe("#toString", () => {
|
|
174
|
+
it("Should round-trip a large number through a non-decimal radix", () => {
|
|
175
|
+
const x = c.parse("340282366920938463463374607431768211455");
|
|
176
|
+
assert.equal(c.from_string(x.toString(16), 16)!.toString(), x.toString());
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|