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.
Files changed (118) hide show
  1. package/dist/src/crunchy.d.ts +130 -0
  2. package/dist/src/crunchy.d.ts.map +1 -0
  3. package/dist/src/crunchy.js +616 -0
  4. package/dist/src/index.d.ts +13 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +21 -0
  7. package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -7
  8. package/dist/src/ip_bits.d.ts.map +1 -0
  9. package/dist/src/ip_bits.js +133 -0
  10. package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
  11. package/dist/src/ip_version.d.ts.map +1 -0
  12. package/dist/src/ip_version.js +6 -0
  13. package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +38 -35
  14. package/dist/src/ipaddress.d.ts.map +1 -0
  15. package/dist/src/ipaddress.js +1243 -0
  16. package/dist/src/ipv4.d.ts +15 -0
  17. package/dist/src/ipv4.d.ts.map +1 -0
  18. package/dist/src/ipv4.js +187 -0
  19. package/dist/src/ipv6.d.ts +13 -0
  20. package/dist/src/ipv6.d.ts.map +1 -0
  21. package/dist/src/ipv6.js +190 -0
  22. package/dist/src/ipv6_loopback.d.ts +6 -0
  23. package/dist/src/ipv6_loopback.d.ts.map +1 -0
  24. package/dist/src/ipv6_loopback.js +51 -0
  25. package/dist/src/ipv6_mapped.d.ts +6 -0
  26. package/dist/src/ipv6_mapped.d.ts.map +1 -0
  27. package/dist/src/ipv6_mapped.js +159 -0
  28. package/dist/src/ipv6_unspec.d.ts +6 -0
  29. package/dist/src/ipv6_unspec.d.ts.map +1 -0
  30. package/dist/src/ipv6_unspec.js +57 -0
  31. package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
  32. package/dist/src/prefix.d.ts.map +1 -0
  33. package/dist/src/prefix.js +142 -0
  34. package/dist/src/prefix128.d.ts +7 -0
  35. package/dist/src/prefix128.d.ts.map +1 -0
  36. package/dist/src/prefix128.js +39 -0
  37. package/dist/src/prefix32.d.ts +7 -0
  38. package/dist/src/prefix32.d.ts.map +1 -0
  39. package/dist/src/prefix32.js +29 -0
  40. package/dist/src/rle.d.ts +19 -0
  41. package/dist/src/rle.d.ts.map +1 -0
  42. package/dist/src/rle.js +73 -0
  43. package/dist/test/test_crunchy_impls.d.ts +2 -0
  44. package/dist/test/test_crunchy_impls.d.ts.map +1 -0
  45. package/dist/test/test_crunchy_impls.js +150 -0
  46. package/dist/test/test_crunchy_with32bit.d.ts +2 -0
  47. package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
  48. package/dist/test/test_crunchy_with32bit.js +382 -0
  49. package/dist/test/test_ipaddress.d.ts +2 -0
  50. package/dist/test/test_ipaddress.d.ts.map +1 -0
  51. package/dist/test/test_ipaddress.js +182 -0
  52. package/dist/test/test_ipv4.d.ts +2 -0
  53. package/dist/test/test_ipv4.d.ts.map +1 -0
  54. package/dist/test/test_ipv4.js +561 -0
  55. package/dist/test/test_ipv6.d.ts +2 -0
  56. package/dist/test/test_ipv6.d.ts.map +1 -0
  57. package/dist/test/test_ipv6.js +313 -0
  58. package/dist/test/test_ipv6_loopback.d.ts +2 -0
  59. package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
  60. package/dist/test/test_ipv6_loopback.js +40 -0
  61. package/dist/test/test_ipv6_mapped.d.ts +2 -0
  62. package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
  63. package/dist/test/test_ipv6_mapped.js +89 -0
  64. package/dist/test/test_ipv6_unspec.d.ts +2 -0
  65. package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
  66. package/dist/test/test_ipv6_unspec.js +39 -0
  67. package/dist/test/test_prefix128.d.ts +2 -0
  68. package/dist/test/test_prefix128.d.ts.map +1 -0
  69. package/dist/test/test_prefix128.js +35 -0
  70. package/dist/test/test_prefix32.d.ts +2 -0
  71. package/dist/test/test_prefix32.d.ts.map +1 -0
  72. package/dist/test/test_prefix32.js +126 -0
  73. package/dist/test/test_rle.d.ts +2 -0
  74. package/dist/test/test_rle.d.ts.map +1 -0
  75. package/dist/test/test_rle.js +141 -0
  76. package/dist/vitest.config.d.ts +3 -0
  77. package/dist/vitest.config.d.ts.map +1 -0
  78. package/dist/vitest.config.js +11 -0
  79. package/package.json +41 -37
  80. package/src/crunchy.ts +731 -0
  81. package/src/index.ts +23 -0
  82. package/src/ip_bits.ts +148 -0
  83. package/src/ip_version.ts +6 -0
  84. package/src/ipaddress.ts +1347 -0
  85. package/src/ipv4.ts +203 -0
  86. package/src/ipv6.ts +202 -0
  87. package/src/ipv6_loopback.ts +55 -0
  88. package/src/ipv6_mapped.ts +163 -0
  89. package/src/ipv6_unspec.ts +59 -0
  90. package/src/prefix.ts +170 -0
  91. package/src/prefix128.ts +42 -0
  92. package/src/prefix32.ts +32 -0
  93. package/src/rle.ts +85 -0
  94. package/test/test_crunchy_impls.ts +179 -0
  95. package/test/test_crunchy_with32bit.ts +510 -0
  96. package/test/test_ipaddress.ts +208 -0
  97. package/test/test_ipv4.ts +596 -0
  98. package/test/test_ipv6.ts +325 -0
  99. package/test/test_ipv6_loopback.ts +52 -0
  100. package/test/test_ipv6_mapped.ts +101 -0
  101. package/test/test_ipv6_unspec.ts +51 -0
  102. package/test/test_prefix128.ts +39 -0
  103. package/test/test_prefix32.ts +130 -0
  104. package/test/test_rle.ts +144 -0
  105. package/LICENSE +0 -20
  106. package/README.md +0 -960
  107. package/crunchy.d.ts +0 -55
  108. package/index.d.ts +0 -12
  109. package/ipaddress.js +0 -2869
  110. package/ipaddress.js.map +0 -1
  111. package/ipv4.d.ts +0 -14
  112. package/ipv6.d.ts +0 -12
  113. package/ipv6_loopback.d.ts +0 -5
  114. package/ipv6_mapped.d.ts +0 -5
  115. package/ipv6_unspec.d.ts +0 -5
  116. package/prefix128.d.ts +0 -6
  117. package/prefix32.d.ts +0 -6
  118. package/rle.d.ts +0 -14
@@ -0,0 +1,57 @@
1
+ import IPAddress from "./ipaddress.js";
2
+ export const Ipv6Unspec = {
3
+ // The address with all zero bits is called the +unspecified+ address
4
+ // (corresponding to 0.0.0.0 in IPv4). It should be something like this:
5
+ //
6
+ // 0000:0000:0000:0000:0000:0000:0000:0000
7
+ //
8
+ // but, with the use of compression, it is usually written as just two
9
+ // colons:
10
+ //
11
+ // ::
12
+ //
13
+ // or, specifying the netmask:
14
+ //
15
+ // ::/128
16
+ //
17
+ // With IPAddress, create a new unspecified IPv6 address using its own
18
+ // subclass:
19
+ //
20
+ // ip = IPAddress::IPv6::Unspecified.new
21
+ //
22
+ // ip.to_s
23
+ // // => => "::/128"
24
+ //
25
+ // You can easily check if an IPv6 object is an unspecified address by
26
+ // using the IPv6// unspecified? method
27
+ //
28
+ // ip.unspecified?
29
+ // // => true
30
+ //
31
+ // An unspecified IPv6 address can also be created with the wrapper
32
+ // method, like we've seen before
33
+ //
34
+ // ip = IPAddress "::"
35
+ //
36
+ // ip.unspecified?
37
+ // // => true
38
+ //
39
+ // This address must never be assigned to an interface and is to be used
40
+ // only in software before the application has learned its host's source
41
+ // address appropriate for a pending connection. Routers must not forward
42
+ // packets with the unspecified address.
43
+ //
44
+ //
45
+ // Creates a new IPv6 unspecified address
46
+ //
47
+ // ip = IPAddress::IPv6::Unspecified.new
48
+ //
49
+ // ip.to_s
50
+ // // => => "::/128"
51
+ //
52
+ create() {
53
+ return IPAddress.parse("::");
54
+ },
55
+ // class IPv6::Unspecified
56
+ };
57
+ export default Ipv6Unspec;
@@ -1,16 +1,18 @@
1
- import IpBits from './ip_bits';
2
- import Crunchy from './crunchy';
3
- export interface From {
4
- (source: Prefix, num: number): Prefix;
1
+ import IpBits from "./ip_bits.js";
2
+ import Crunchy from "./crunchy.js";
3
+ export type From = (source: Prefix, num: number) => Prefix;
4
+ interface PrefixParams {
5
+ num: number;
6
+ ip_bits: IpBits;
7
+ net_mask: Crunchy;
8
+ vt_from: From;
5
9
  }
6
10
  export declare class Prefix {
7
11
  num: number;
8
12
  ip_bits: IpBits;
9
13
  net_mask: Crunchy;
10
14
  vt_from: From;
11
- constructor(obj: {
12
- [id: string]: any;
13
- });
15
+ constructor(obj: PrefixParams);
14
16
  clone(): Prefix;
15
17
  eq(other: Prefix): boolean;
16
18
  ne(other: Prefix): boolean;
@@ -32,3 +34,4 @@ export declare class Prefix {
32
34
  sub(other: number): Prefix;
33
35
  }
34
36
  export default Prefix;
37
+ //# sourceMappingURL=prefix.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prefix.d.ts","sourceRoot":"","sources":["../../src/prefix.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,cAAc,CAAC;AAClC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,MAAM,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;AAE3D,UAAU,YAAY;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,IAAI,CAAC;CACf;AAED,qBAAa,MAAM;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,IAAI,CAAC;gBAEF,GAAG,EAAE,YAAY;IAOtB,KAAK,IAAI,MAAM;IASf,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAI1B,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAI1B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAiBxB,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAIzB,SAAS,IAAI,MAAM;IAInB,IAAI,IAAI,OAAO;WAIR,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;IAUzD,OAAO,IAAI,OAAO;IAIlB,UAAU,IAAI,MAAM;IAapB,SAAS,IAAI,OAAO;IAiBpB,WAAW,IAAI,MAAM;IAcrB,IAAI,IAAI,MAAM;IAQd,IAAI,IAAI,MAAM;IAQd,IAAI,IAAI,MAAM;IAId,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIjC,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAI1B,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIjC,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;CAMlC;AAED,eAAe,MAAM,CAAC"}
@@ -0,0 +1,142 @@
1
+ import Crunchy from "./crunchy.js";
2
+ export class Prefix {
3
+ num;
4
+ ip_bits;
5
+ net_mask;
6
+ vt_from;
7
+ constructor(obj) {
8
+ this.num = obj.num;
9
+ this.ip_bits = obj.ip_bits;
10
+ this.net_mask = obj.net_mask;
11
+ this.vt_from = obj.vt_from;
12
+ }
13
+ clone() {
14
+ return new Prefix({
15
+ num: this.num,
16
+ ip_bits: this.ip_bits,
17
+ net_mask: this.net_mask,
18
+ vt_from: this.vt_from,
19
+ });
20
+ }
21
+ eq(other) {
22
+ return this.ip_bits.version == other.ip_bits.version && this.num == other.num;
23
+ }
24
+ ne(other) {
25
+ return !this.eq(other);
26
+ }
27
+ cmp(oth) {
28
+ if (this.ip_bits.version < oth.ip_bits.version) {
29
+ return -1;
30
+ }
31
+ else if (this.ip_bits.version > oth.ip_bits.version) {
32
+ return 1;
33
+ }
34
+ else {
35
+ if (this.num < oth.num) {
36
+ return -1;
37
+ }
38
+ else if (this.num > oth.num) {
39
+ return 1;
40
+ }
41
+ else {
42
+ return 0;
43
+ }
44
+ }
45
+ }
46
+ // #[allow(dead_code)]
47
+ from(num) {
48
+ return this.vt_from(this, num);
49
+ }
50
+ to_ip_str() {
51
+ return this.ip_bits.vt_as_compressed_string(this.ip_bits, this.net_mask);
52
+ }
53
+ size() {
54
+ return Crunchy.one().shl(this.ip_bits.bits - this.num);
55
+ }
56
+ static new_netmask(prefix, bits) {
57
+ let mask = Crunchy.zero();
58
+ const host_prefix = bits - prefix;
59
+ for (let i = 0; i < prefix; ++i) {
60
+ // console.log(">>>", i, host_prefix, mask);
61
+ mask = mask.add(Crunchy.one().shl(host_prefix + i));
62
+ }
63
+ return mask;
64
+ }
65
+ netmask() {
66
+ return this.net_mask;
67
+ }
68
+ get_prefix() {
69
+ return this.num;
70
+ }
71
+ // The hostmask is the contrary of the subnet mask,
72
+ // as it shows the bits that can change within the
73
+ // hosts
74
+ //
75
+ // prefix = IPAddress::Prefix32.new 24
76
+ //
77
+ // prefix.hostmask
78
+ // // => "0.0.0.255"
79
+ //
80
+ host_mask() {
81
+ let ret = Crunchy.zero();
82
+ for (let _ = 0; _ < this.ip_bits.bits - this.num; _++) {
83
+ ret = ret.shl(1).add(Crunchy.one());
84
+ }
85
+ return ret;
86
+ }
87
+ //
88
+ // Returns the length of the host portion
89
+ // of a netmask.
90
+ //
91
+ // prefix = Prefix128.new 96
92
+ //
93
+ // prefix.host_prefix
94
+ // // => 128
95
+ //
96
+ host_prefix() {
97
+ return this.ip_bits.bits - this.num;
98
+ }
99
+ //
100
+ // Transforms the prefix into a string of bits
101
+ // representing the netmask
102
+ //
103
+ // prefix = IPAddress::Prefix128.new 64
104
+ //
105
+ // prefix.bits
106
+ // // => "1111111111111111111111111111111111111111111111111111111111111111"
107
+ // "0000000000000000000000000000000000000000000000000000000000000000"
108
+ //
109
+ bits() {
110
+ return this.netmask().toString(2);
111
+ }
112
+ // #[allow(dead_code)]
113
+ // public net_mask(&self) -> BigUint {
114
+ // return (self.in_mask.clone() >> (self.host_prefix() as usize)) << (self.host_prefix() as usize);
115
+ // }
116
+ to_s() {
117
+ return this.get_prefix().toString();
118
+ }
119
+ // #[allow(dead_code)]
120
+ // public inspect(&self) -> String {
121
+ // return self.to_s();
122
+ // }
123
+ to_i() {
124
+ return this.get_prefix();
125
+ }
126
+ add_prefix(other) {
127
+ return this.from(this.get_prefix() + other.get_prefix());
128
+ }
129
+ add(other) {
130
+ return this.from(this.get_prefix() + other);
131
+ }
132
+ sub_prefix(other) {
133
+ return this.sub(other.get_prefix());
134
+ }
135
+ sub(other) {
136
+ if (other > this.get_prefix()) {
137
+ return this.from(other - this.get_prefix());
138
+ }
139
+ return this.from(this.get_prefix() - other);
140
+ }
141
+ }
142
+ export default Prefix;
@@ -0,0 +1,7 @@
1
+ import Prefix from "./prefix.js";
2
+ declare const Prefix128: {
3
+ create(num: number): Prefix | null;
4
+ from(my: Prefix, num: number): Prefix;
5
+ };
6
+ export default Prefix128;
7
+ //# sourceMappingURL=prefix128.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prefix128.d.ts","sourceRoot":"","sources":["../../src/prefix128.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,QAAA,MAAM,SAAS;gBAaD,MAAM,GAAG,MAAM,GAAG,IAAI;aAgBzB,MAAM,OAAO,MAAM,GAAG,MAAM;CAOtC,CAAC;AAEF,eAAe,SAAS,CAAC"}
@@ -0,0 +1,39 @@
1
+ import Prefix from "./prefix.js";
2
+ import IpBits from "./ip_bits.js";
3
+ const Prefix128 = {
4
+ // #[derive(Ord,PartialOrd,Eq,PartialEq,Debug,Copy,Clone)]
5
+ // pub struct Prefix128 {
6
+ // }
7
+ //
8
+ // impl Prefix128 {
9
+ //
10
+ // Creates a new prefix object for 128 bits IPv6 addresses
11
+ //
12
+ // prefix = IPAddressPrefix128.new 64
13
+ // // => 64
14
+ //
15
+ // #[allow(unused_comparisons)]
16
+ create(num) {
17
+ if (num <= 128) {
18
+ // static _FROM: &'static (Fn(&Prefix, usize) -> Result<Prefix, String>) = &from;
19
+ // static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &Prefix128::to_ip_str;
20
+ const ip_bits = IpBits.v6();
21
+ const bits = ip_bits.bits;
22
+ return new Prefix({
23
+ num,
24
+ ip_bits,
25
+ net_mask: Prefix.new_netmask(num, bits),
26
+ vt_from: Prefix128.from, // vt_to_ip_str: _TO_IP_STR
27
+ });
28
+ }
29
+ return null;
30
+ },
31
+ from(my, num) {
32
+ const ret = Prefix128.create(num);
33
+ if (ret === null) {
34
+ throw new Error(`invalid ipv6 prefix: ${num}`);
35
+ }
36
+ return ret;
37
+ },
38
+ };
39
+ export default Prefix128;
@@ -0,0 +1,7 @@
1
+ import Prefix from "./prefix.js";
2
+ declare const Prefix32: {
3
+ from(my: Prefix, num: number): Prefix;
4
+ create(num: number): Prefix | null;
5
+ };
6
+ export default Prefix32;
7
+ //# sourceMappingURL=prefix32.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prefix32.d.ts","sourceRoot":"","sources":["../../src/prefix32.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,QAAA,MAAM,QAAQ;aACH,MAAM,OAAO,MAAM,GAAG,MAAM;gBAQzB,MAAM,GAAG,MAAM,GAAG,IAAI;CAiBnC,CAAC;AAEF,eAAe,QAAQ,CAAC"}
@@ -0,0 +1,29 @@
1
+ import Prefix from "./prefix.js";
2
+ import IpBits from "./ip_bits.js";
3
+ const Prefix32 = {
4
+ from(my, num) {
5
+ const ret = Prefix32.create(num);
6
+ if (ret === null) {
7
+ throw new Error(`invalid ipv4 prefix: ${num}`);
8
+ }
9
+ return ret;
10
+ },
11
+ create(num) {
12
+ if (num >= 0 && num <= 32) {
13
+ // static _FROM: &'static (Fn(&::prefix::Prefix, usize) -> Result<::prefix::Prefix, String>) =
14
+ // &from;
15
+ // static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &to_ip_str;
16
+ const ip_bits = IpBits.v4();
17
+ const bits = ip_bits.bits;
18
+ return new Prefix({
19
+ num,
20
+ ip_bits,
21
+ net_mask: Prefix.new_netmask(num, bits),
22
+ vt_from: Prefix32.from,
23
+ // vt_to_ip_str: _TO_IP_STR,
24
+ });
25
+ }
26
+ return null;
27
+ },
28
+ };
29
+ export default Prefix32;
@@ -0,0 +1,19 @@
1
+ interface RleParams {
2
+ part: number;
3
+ pos: number;
4
+ cnt: number;
5
+ max: boolean;
6
+ }
7
+ declare class Rle {
8
+ part: number;
9
+ pos: number;
10
+ cnt: number;
11
+ max: boolean;
12
+ constructor(obj: RleParams);
13
+ toString(): string;
14
+ eq(other: Rle): boolean;
15
+ ne(other: Rle): boolean;
16
+ static code(parts: number[]): Rle[];
17
+ }
18
+ export default Rle;
19
+ //# sourceMappingURL=rle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rle.d.ts","sourceRoot":"","sources":["../../src/rle.ts"],"names":[],"mappings":"AAkCA,UAAU,SAAS;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,OAAO,CAAC;CACd;AAED,cAAM,GAAG;IACA,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,OAAO,CAAC;gBAER,GAAG,EAAE,SAAS;IAOnB,QAAQ;IAIR,EAAE,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO;IAIvB,EAAE,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO;WAIhB,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,GAAG,EAAE;CAgB3C;AAED,eAAe,GAAG,CAAC"}
@@ -0,0 +1,73 @@
1
+ class Last {
2
+ val;
3
+ max_poses = [];
4
+ ret = [];
5
+ handle_last() {
6
+ if (this.val == null) {
7
+ return;
8
+ }
9
+ const _last = this.val;
10
+ let max_rles = this.max_poses[_last.part];
11
+ if (max_rles == null) {
12
+ max_rles = this.max_poses[_last.part] = [];
13
+ }
14
+ // console.log(_last.part, this.max_poses);
15
+ for (const idx in max_rles) {
16
+ const prev = this.ret[max_rles[idx]];
17
+ if (prev.cnt > _last.cnt) {
18
+ // console.log(`>>>>> last=${_last}->${idx}->prev=${prev}`);
19
+ _last.max = false;
20
+ }
21
+ else if (prev.cnt == _last.cnt) {
22
+ // nothing
23
+ }
24
+ else if (prev.cnt < _last.cnt) {
25
+ // console.log(`<<<<< last=${_last}->${idx}->prev=${prev}`);
26
+ prev.max = false;
27
+ }
28
+ }
29
+ // println!("push:{}:{:?}", self.ret.len(), _last);
30
+ max_rles.push(this.ret.length);
31
+ _last.pos = this.ret.length;
32
+ this.ret.push(_last);
33
+ }
34
+ }
35
+ class Rle {
36
+ part;
37
+ pos;
38
+ cnt;
39
+ max;
40
+ constructor(obj) {
41
+ this.part = obj.part;
42
+ this.pos = obj.pos;
43
+ this.cnt = obj.cnt;
44
+ this.max = obj.max;
45
+ }
46
+ toString() {
47
+ return `<Rle@part:${this.part},pos:${this.pos},cnt:${this.cnt},max:${this.max}>`;
48
+ }
49
+ eq(other) {
50
+ return this.part == other.part && this.pos == other.pos && this.cnt == other.cnt && this.max == other.max;
51
+ }
52
+ ne(other) {
53
+ return !this.eq(other);
54
+ }
55
+ static code(parts) {
56
+ const last = new Last();
57
+ // println!("code");
58
+ for (const part of parts) {
59
+ // const part = parts[i]
60
+ // console.log(`part:${part}`);
61
+ if (last.val && last.val.part == part) {
62
+ last.val.cnt += 1;
63
+ }
64
+ else {
65
+ last.handle_last();
66
+ last.val = new Rle({ part, pos: 0, cnt: 1, max: true });
67
+ }
68
+ }
69
+ last.handle_last();
70
+ return last.ret;
71
+ }
72
+ }
73
+ export default Rle;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=test_crunchy_impls.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test_crunchy_impls.d.ts","sourceRoot":"","sources":["../../test/test_crunchy_impls.ts"],"names":[],"mappings":""}
@@ -0,0 +1,150 @@
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 { CrunchyWithBigInt, CrunchyWith32Bit } from "../src/crunchy.js";
8
+ const impls = [
9
+ ["CrunchyWithBigInt", CrunchyWithBigInt.bootstrap()],
10
+ ["CrunchyWith32Bit", new CrunchyWith32Bit()],
11
+ ];
12
+ describe.each(impls)("Crunchy impl: %s", (_name, c) => {
13
+ describe("#compare", () => {
14
+ it("Should confirm equality", () => {
15
+ const x = c.parse("12345678901234567890");
16
+ const y = c.parse("12345678901234567890");
17
+ assert.isTrue(x.eq(y));
18
+ });
19
+ it("Should find first is greater than second", () => {
20
+ const x = c.parse("12345678901234567890");
21
+ const y = c.parse("1234567890");
22
+ assert.isTrue(x.gt(y));
23
+ });
24
+ it("Should find first is less than second", () => {
25
+ const x = c.parse("1234567890");
26
+ const y = c.parse("12345678901234567890");
27
+ assert.isTrue(x.lt(y));
28
+ });
29
+ });
30
+ describe("#add", () => {
31
+ it("Should add numbers", () => {
32
+ const x = c.parse("12345678911234567891");
33
+ const y = c.parse("12345678901234567890");
34
+ assert.equal(x.add(y).toString(), "24691357812469135781");
35
+ });
36
+ it("Should add numbers, first longer than second", () => {
37
+ const x = c.parse("12345678901234567890");
38
+ const y = c.parse("1234567890");
39
+ assert.equal(x.add(y).toString(), "12345678902469135780");
40
+ });
41
+ it("Should add numbers, second longer than first", () => {
42
+ const x = c.parse("1234567890");
43
+ const y = c.parse("12345678901234567890");
44
+ assert.equal(x.add(y).toString(), "12345678902469135780");
45
+ });
46
+ it("Should add zero to number", () => {
47
+ const x = c.parse("12345678901234567890");
48
+ const y = c.zero();
49
+ assert.equal(x.add(y).toString(), "12345678901234567890");
50
+ });
51
+ it("Should add two zeros", () => {
52
+ assert.equal(c.zero().add(c.zero()).toString(), "0");
53
+ });
54
+ });
55
+ describe("#sub", () => {
56
+ it("Should subtract numbers", () => {
57
+ const x = c.parse("12345678901234567890");
58
+ const y = c.parse("1234567890");
59
+ assert.equal(x.sub(y).toString(), "12345678900000000000");
60
+ });
61
+ it("Should subtract two equal numbers expecting zero", () => {
62
+ const x = c.parse("20");
63
+ const y = c.parse("20");
64
+ assert.equal(x.sub(y).toString(), "0");
65
+ });
66
+ it("Should subtract zero", () => {
67
+ const x = c.parse("244137007161");
68
+ assert.equal(x.sub(c.zero()).toString(), "244137007161");
69
+ });
70
+ });
71
+ describe("#leftShift", () => {
72
+ it("Should left shift a number", () => {
73
+ const x = c.from_number(22 * 256 + 11);
74
+ assert.equal(x.shl(5).toString(), "180576");
75
+ });
76
+ it("Should left shift zero", () => {
77
+ assert.equal(c.zero().shl(80).toString(), "0");
78
+ });
79
+ });
80
+ describe("#rightShift", () => {
81
+ it("Should right shift a number", () => {
82
+ const x = c.from_number(22 * 256 + 11);
83
+ assert.equal(x.shr(5).toString(), "176");
84
+ });
85
+ it("Should right shift a number out of existence", () => {
86
+ const x = c.from_number(22 * 256 + 11);
87
+ assert.equal(x.shr(20).toString(), "0");
88
+ });
89
+ });
90
+ describe("#mod", () => {
91
+ it("Should calculate modulo", () => {
92
+ const x = c.parse("123456789012345");
93
+ const y = c.from_number(97);
94
+ assert.equal(x.mod(y).toString(), "12");
95
+ });
96
+ it("Should calculate modulo of number smaller than modulus", () => {
97
+ const x = c.from_number(97);
98
+ const y = c.parse("123456789012345");
99
+ assert.equal(x.mod(y).toString(), "97");
100
+ });
101
+ it("Should calculate modulo of modulus", () => {
102
+ const x = c.from_number(241);
103
+ const y = c.from_number(241);
104
+ assert.equal(x.mod(y).toString(), "0");
105
+ });
106
+ it("Should calculate modulo of zero", () => {
107
+ assert.equal(c.zero().mod(c.from_number(241)).toString(), "0");
108
+ });
109
+ });
110
+ describe("#from_string", () => {
111
+ it("Should parse a decimal string", () => {
112
+ const x = c.from_string("340282366920938463463374607431768211455");
113
+ assert.isOk(x);
114
+ assert.equal(x.toString(), "340282366920938463463374607431768211455");
115
+ });
116
+ it("Should parse a hex string", () => {
117
+ const x = c.from_string("ff", 16);
118
+ assert.isOk(x);
119
+ assert.equal(x.toString(16), "ff");
120
+ });
121
+ it("Should reject invalid characters", () => {
122
+ assert.isNull(c.from_string("12a45"));
123
+ });
124
+ it("Should reject invalid hex digits", () => {
125
+ assert.isNull(c.from_string("fg", 16));
126
+ });
127
+ });
128
+ describe("#parse", () => {
129
+ it("Should throw on invalid input", () => {
130
+ assert.throws(() => c.parse("not-a-number"));
131
+ });
132
+ });
133
+ describe("#create", () => {
134
+ it("Should produce a zero value with no argument", () => {
135
+ assert.isTrue(c.create().eq(c.zero()));
136
+ });
137
+ it("Should act like from_number() given a number", () => {
138
+ assert.isTrue(c.create(42).eq(c.from_number(42)));
139
+ });
140
+ it("Should act like parse() given a string", () => {
141
+ assert.isTrue(c.create("12345678901234567890").eq(c.parse("12345678901234567890")));
142
+ });
143
+ });
144
+ describe("#toString", () => {
145
+ it("Should round-trip a large number through a non-decimal radix", () => {
146
+ const x = c.parse("340282366920938463463374607431768211455");
147
+ assert.equal(c.from_string(x.toString(16), 16).toString(), x.toString());
148
+ });
149
+ });
150
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=test_crunchy_with32bit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test_crunchy_with32bit.d.ts","sourceRoot":"","sources":["../../test/test_crunchy_with32bit.ts"],"names":[],"mappings":""}