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,382 @@
1
+ import { CrunchyWith32Bit } from "../src/crunchy.js";
2
+ const c32 = new CrunchyWith32Bit();
3
+ describe("#compare", function () {
4
+ it("Should confirm equality", function () {
5
+ const x = c32.from_string("12345678901234567890");
6
+ const y = c32.from_string("12345678901234567890");
7
+ assert.isTrue(x.eq(y));
8
+ // this.done();
9
+ });
10
+ it("Should find first is greater than second", function () {
11
+ const x = c32.from_string("12345678901234567890");
12
+ const y = c32.from_string("1234567890");
13
+ assert.isTrue(x.gt(y));
14
+ // this.done();
15
+ });
16
+ it("Should find first is less than second", function () {
17
+ const x = c32.from_string("1234567890");
18
+ const y = c32.from_string("12345678901234567890");
19
+ assert.isTrue(x.lt(y));
20
+ // this.done();
21
+ });
22
+ });
23
+ describe("#add", function () {
24
+ it("Should add numbers", function () {
25
+ const x = CrunchyWith32Bit.from_8bit([242, 62]);
26
+ const y = CrunchyWith32Bit.from_8bit([42, 2]);
27
+ assert.deepEqual(x.add(y).to_8bit(), [1, 28, 64]);
28
+ // x.should.eql([242, 62]);
29
+ // y.should.eql([42, 2]);
30
+ });
31
+ it("Should add numbers, first longer than second", function () {
32
+ const x = CrunchyWith32Bit.from_8bit([26, 255, 230, 17]);
33
+ const y = CrunchyWith32Bit.from_8bit([42, 34]);
34
+ assert.deepEqual(x.add(y).to_8bit(), [27, 0, 16, 51]);
35
+ // x, [26, 255, 230, 17]);
36
+ // y.should.eql([42, 34]);
37
+ });
38
+ it("Should add numbers, second longer than first", function () {
39
+ const x = CrunchyWith32Bit.from_8bit([234, 34]);
40
+ const y = CrunchyWith32Bit.from_8bit([255, 255, 230, 17]);
41
+ assert.deepEqual(x.add(y).to_8bit(), [1, 0, 0, 208, 51]);
42
+ // x.should.eql([234, 34]);
43
+ // y.should.eql([255, 255, 230, 17]);
44
+ });
45
+ it("Should add two identical numbers", function () {
46
+ const x = CrunchyWith32Bit.from_8bit([255, 255, 255, 255, 255, 255]);
47
+ const y = CrunchyWith32Bit.from_8bit([255, 255, 255, 255, 255, 255]);
48
+ assert.deepEqual(x.add(y).to_8bit(), [1, 255, 255, 255, 255, 255, 254]);
49
+ // x.should.eql([255, 255, 255, 255, 255, 255]);
50
+ // y.should.eql([255, 255, 255, 255, 255, 255]);
51
+ });
52
+ it("Should add positive to negative number expecting positive result", function () {
53
+ const x = CrunchyWith32Bit.from_8bit([51, 254, 144, 207]);
54
+ const y = CrunchyWith32Bit.from_8bit([-20, 89, 145, 32]);
55
+ assert.deepEqual(x.add(y).to_8bit(), [31, 164, 255, 175]);
56
+ // x.should.eql([51, 254, 144, 207]);
57
+ // y.should.eql([-20, 89, 145, 32]);
58
+ });
59
+ it("Should add positive to negative number expecting negative result", function () {
60
+ const x = CrunchyWith32Bit.from_8bit([242, 62]);
61
+ const y = CrunchyWith32Bit.from_8bit([-242, 64]);
62
+ assert.deepEqual(x.add(y).to_8bit(), [-2]);
63
+ // x.should.eql([242, 62]);
64
+ // y.should.eql([-242, 64]);
65
+ });
66
+ it("Should add negative to positive number expecting positive result", function () {
67
+ const x = CrunchyWith32Bit.from_8bit([-42, 2]);
68
+ const y = CrunchyWith32Bit.from_8bit([242, 62]);
69
+ assert.deepEqual(x.add(y).to_8bit(), [200, 60]);
70
+ // x, [-42, 2]);
71
+ // y, [242, 62]);
72
+ });
73
+ it("Should add negative to positive number expecting negative result", function () {
74
+ const x = CrunchyWith32Bit.from_8bit([-42, 2]);
75
+ const y = CrunchyWith32Bit.from_8bit([42, 0]);
76
+ assert.deepEqual(x.add(y).to_8bit(), [-2]);
77
+ // x, [-42, 2]);
78
+ // y, [42, 0]);
79
+ });
80
+ it("Should add two negative numbers expecting negative result", function () {
81
+ const x = CrunchyWith32Bit.from_8bit([-242, 62]);
82
+ const y = CrunchyWith32Bit.from_8bit([-42, 2]);
83
+ assert.deepEqual(x.add(y).to_8bit(), [-1, 28, 64]);
84
+ // x, [-242, 62]);
85
+ // y, [-42, 2]);
86
+ });
87
+ it("Should add zero to number", function () {
88
+ const x = CrunchyWith32Bit.from_8bit([26, 255, 230, 17]);
89
+ const y = CrunchyWith32Bit.from_8bit([0]);
90
+ assert.deepEqual(x.add(y).to_8bit(), [26, 255, 230, 17]);
91
+ // x, [26, 255, 230, 17]);
92
+ // y, [0]);
93
+ });
94
+ it("Should add number to zero", function () {
95
+ const x = CrunchyWith32Bit.from_8bit([0]);
96
+ const y = CrunchyWith32Bit.from_8bit([43, 123, 200, 1]);
97
+ assert.deepEqual(x.add(y).to_8bit(), [43, 123, 200, 1]);
98
+ // x, [0]);
99
+ // y, [43, 123, 200, 1]);
100
+ });
101
+ it("Should add two zeros", function () {
102
+ const x = CrunchyWith32Bit.from_8bit([0]);
103
+ const y = CrunchyWith32Bit.from_8bit([0]);
104
+ assert.deepEqual(x.add(y).to_8bit(), [0]);
105
+ // x.should.eql([0]);
106
+ // y.should.eql([0]);
107
+ });
108
+ });
109
+ describe("#add-from_string", function () {
110
+ it("Should add numbers", function () {
111
+ // console.log(new Date());
112
+ const x = c32.from_string("12345678911234567891");
113
+ // console.log(new Date());
114
+ const y = c32.from_string("12345678901234567890");
115
+ // console.log(new Date(), x, y);
116
+ // console.log("addddddd:", new Date(), x, x.toString(), y, y.toString(), x.add(y).toString());
117
+ assert.equal(x.add(y).toString(), "24691357812469135781");
118
+ // console.log(new Date());
119
+ // this.done();
120
+ });
121
+ it("Should add numbers, first longer than second", function () {
122
+ const x = c32.from_string("12345678901234567890");
123
+ const y = c32.from_string("1234567890");
124
+ assert.equal(x.add(y).toString(), "12345678902469135780");
125
+ // this.done();
126
+ });
127
+ it("Should add numbers, second longer than first", function () {
128
+ const x = c32.from_string("1234567890");
129
+ const y = c32.from_string("12345678901234567890");
130
+ assert.equal(x.add(y).toString(), "12345678902469135780");
131
+ // this.done();
132
+ });
133
+ it("Should add two identical numbers", function () {
134
+ const x = c32.from_string("12345678901234567890");
135
+ const y = c32.from_string("12345678901234567890");
136
+ assert.equal(x.add(y).toString(), "24691357802469135780");
137
+ // this.done();
138
+ });
139
+ it("Should add zero to number", function () {
140
+ const x = c32.from_string("12345678901234567890");
141
+ const y = c32.from_string("0");
142
+ assert.equal(x.add(y).toString(), "12345678901234567890");
143
+ // this.done();
144
+ });
145
+ it("Should add number to zero", function () {
146
+ const x = c32.from_string("0");
147
+ const y = c32.from_string("12345678901234567890");
148
+ assert.equal(x.add(y).toString(), "12345678901234567890");
149
+ // this.done();
150
+ });
151
+ it("Should add two zeros", function () {
152
+ const x = c32.from_string("0");
153
+ const y = c32.from_string("0");
154
+ assert.equal(x.add(y).toString(), "0");
155
+ // this.done();
156
+ });
157
+ });
158
+ describe("#sub", function () {
159
+ it("Should subtract numbers", function () {
160
+ const x = CrunchyWith32Bit.from_8bit([170, 1, 79, 119, 242, 62]);
161
+ const y = CrunchyWith32Bit.from_8bit([17, 241, 123, 250, 42, 2]);
162
+ // console.log("Should subtract numbers:", x, y, x.sub(y));
163
+ assert.deepEqual(x.sub(y).to_8bit(), [152, 15, 211, 125, 200, 60]);
164
+ // x.to_8bit().should.eql([170, 1, 79, 119, 242, 62]);
165
+ // y.should.eql([17, 241, 123, 250, 42, 2]);
166
+ });
167
+ it("Should subtract one", function () {
168
+ const x = CrunchyWith32Bit.from_8bit([240, 0, 0, 0, 0, 0]);
169
+ const y = CrunchyWith32Bit.from_8bit([1]);
170
+ assert.deepEqual(x.sub(y).to_8bit(), [239, 255, 255, 255, 255, 255]);
171
+ // x.should.eql([240, 0, 0, 0, 0, 0]);
172
+ // y.should.eql([1]);
173
+ });
174
+ it("Should subtract numbers expecting negative result", function () {
175
+ const x = CrunchyWith32Bit.from_8bit([26]);
176
+ const y = CrunchyWith32Bit.from_8bit([255]);
177
+ assert.deepEqual(x.sub(y).to_8bit(), [-229]);
178
+ // x.should.eql([26]);
179
+ // y.should.eql([255]);
180
+ });
181
+ it("Should subtract numbers expecting negative result", function () {
182
+ const x = CrunchyWith32Bit.from_8bit([188, 196]);
183
+ const y = CrunchyWith32Bit.from_8bit([188, 197]);
184
+ assert.deepEqual(x.sub(y).to_8bit(), [-1]);
185
+ // x.should.eql([188, 196]);
186
+ // y.should.eql([188, 197]);
187
+ });
188
+ it("Should subtract negative number from positive", function () {
189
+ const x = CrunchyWith32Bit.from_8bit([240, 0, 0, 0, 0, 0]);
190
+ const y = CrunchyWith32Bit.from_8bit([-1]);
191
+ // console.log("negative:", x, y, x.sub(y));
192
+ assert.deepEqual(x.sub(y).to_8bit(), [240, 0, 0, 0, 0, 1]);
193
+ // x.should.eql([240, 0, 0, 0, 0, 0]);
194
+ // y.should.eql([-1]);
195
+ });
196
+ it("Should subtract positive number from negative", function () {
197
+ const x = CrunchyWith32Bit.from_8bit([-240, 0, 0, 0, 0, 0]);
198
+ const y = CrunchyWith32Bit.from_8bit([1]);
199
+ assert.deepEqual(x.sub(y).to_8bit(), [-240, 0, 0, 0, 0, 1]);
200
+ // x.should.eql([-240, 0, 0, 0, 0, 0]);
201
+ // y.should.eql([1]);
202
+ });
203
+ it("Should subtract two negative numbers", function () {
204
+ const x = CrunchyWith32Bit.from_8bit([-240, 0, 0, 0, 0, 0]);
205
+ const y = CrunchyWith32Bit.from_8bit([-1]);
206
+ assert.deepEqual(x.sub(y).to_8bit(), [-239, 255, 255, 255, 255, 255]);
207
+ // x.should.eql([-240, 0, 0, 0, 0, 0]);
208
+ // y.should.eql([-1]);
209
+ });
210
+ it("Should subtract two equal numbers expecting zero", function () {
211
+ const x = CrunchyWith32Bit.from_8bit([20]);
212
+ const y = CrunchyWith32Bit.from_8bit([20]);
213
+ assert.deepEqual(x.sub(y).to_8bit(), [0]);
214
+ // x.should.eql([20]);
215
+ // y.should.eql([20]);
216
+ });
217
+ it("Should subtract zero", function () {
218
+ const x = CrunchyWith32Bit.from_8bit([244, 137, 7, 161]);
219
+ const y = CrunchyWith32Bit.from_8bit([0]);
220
+ assert.deepEqual(x.sub(y).to_8bit(), [244, 137, 7, 161]);
221
+ // x.should.eql([244, 137, 7, 161]);
222
+ // y.should.eql([0]);
223
+ });
224
+ it("Should subtract zero from zero", function () {
225
+ const x = CrunchyWith32Bit.from_8bit([0]);
226
+ const y = CrunchyWith32Bit.from_8bit([0]);
227
+ assert.deepEqual(x.sub(y).to_8bit(), [0]);
228
+ // x.should.eql([0]);
229
+ // y.should.eql([0]);
230
+ });
231
+ it("Should subtract from zero", function () {
232
+ const x = CrunchyWith32Bit.from_8bit([0]);
233
+ const y = CrunchyWith32Bit.from_8bit([15]);
234
+ assert.deepEqual(x.sub(y).to_8bit(), [-15]);
235
+ // x.should.eql([0]);
236
+ // y.should.eql([15]);
237
+ });
238
+ });
239
+ describe("#mul", function () {
240
+ it("Should multiply numbers", function () {
241
+ const x = CrunchyWith32Bit.from_8bit([162, 51, 95]);
242
+ const y = CrunchyWith32Bit.from_8bit([42, 18, 204]);
243
+ assert.deepEqual(x.mul(y).to_8bit(), [26, 168, 86, 115, 157, 180]);
244
+ });
245
+ it("Should multiply numbers", function () {
246
+ const x = CrunchyWith32Bit.from_8bit([255, 65, 34, 51, 95]);
247
+ const y = CrunchyWith32Bit.from_8bit([42, 18, 204]);
248
+ assert.deepEqual(x.mul(y).to_8bit(), [41, 243, 109, 152, 188, 115, 157, 180]);
249
+ });
250
+ it("Should multiply numbers", function () {
251
+ const x = CrunchyWith32Bit.from_8bit([255, 255, 255, 255]);
252
+ const y = CrunchyWith32Bit.from_8bit([255, 255, 255, 255]);
253
+ assert.deepEqual(x.mul(y).to_8bit(), [255, 255, 255, 254, 0, 0, 0, 1]);
254
+ });
255
+ it("Should multiply positive with negative number", function () {
256
+ const x = CrunchyWith32Bit.from_8bit([77, 242, 62]);
257
+ const y = CrunchyWith32Bit.from_8bit([-42, 2, 113, 43, 57, 65]);
258
+ assert.deepEqual(x.mul(y).to_8bit(), [-12, 202, 124, 133, 146, 125, 36, 79, 190]);
259
+ });
260
+ it("Should multiply negative with positive number", function () {
261
+ const x = CrunchyWith32Bit.from_8bit([-255, 17, 162, 62]);
262
+ const y = CrunchyWith32Bit.from_8bit([255, 17, 162, 62]);
263
+ assert.deepEqual(x.mul(y).to_8bit(), [-254, 36, 34, 110, 119, 14, 135, 4]);
264
+ });
265
+ it("Should multiply two negative numbers", function () {
266
+ const x = CrunchyWith32Bit.from_8bit([-162, 51, 95]);
267
+ const y = CrunchyWith32Bit.from_8bit([-42, 18, 204]);
268
+ assert.deepEqual(x.mul(y).to_8bit(), [26, 168, 86, 115, 157, 180]);
269
+ });
270
+ it("Should multiply by zero", function () {
271
+ const x = CrunchyWith32Bit.from_8bit([77, 242, 62]);
272
+ const y = CrunchyWith32Bit.from_8bit([0]);
273
+ assert.deepEqual(x.mul(y).to_8bit(), [0]);
274
+ });
275
+ it("Should multiply zero by number", function () {
276
+ const x = CrunchyWith32Bit.from_8bit([0]);
277
+ const y = CrunchyWith32Bit.from_8bit([77, 242, 62]);
278
+ assert.deepEqual(x.mul(y).to_8bit(), [0]);
279
+ });
280
+ it("Should multiply zeros", function () {
281
+ const x = CrunchyWith32Bit.from_8bit([0]);
282
+ const y = CrunchyWith32Bit.from_8bit([0]);
283
+ assert.deepEqual(x.mul(y).to_8bit(), [0]);
284
+ });
285
+ });
286
+ describe("#div", function () {
287
+ it("Should divide numbers", function () {
288
+ const x = CrunchyWith32Bit.from_8bit([52, 155, 168, 23, 6, 85]);
289
+ const y = CrunchyWith32Bit.from_8bit([19, 26, 247]);
290
+ assert.deepEqual(x.div(y).to_8bit(), [2, 192, 234, 136]);
291
+ });
292
+ it("Should divide by one", function () {
293
+ const x = CrunchyWith32Bit.from_8bit([15, 127, 73, 1]);
294
+ const y = CrunchyWith32Bit.from_8bit([1]);
295
+ assert.deepEqual(x.div(y).to_8bit(), [15, 127, 73, 1]);
296
+ });
297
+ it("Should divide by self", function () {
298
+ const x = CrunchyWith32Bit.from_8bit([15, 127, 73, 1]);
299
+ const y = CrunchyWith32Bit.from_8bit([15, 127, 73, 1]);
300
+ assert.deepEqual(x.div(y).to_8bit(), [1]);
301
+ });
302
+ it("Should divide negative number", function () {
303
+ const x = CrunchyWith32Bit.from_8bit([-170, 153, 136]);
304
+ const y = CrunchyWith32Bit.from_8bit([17, 68]);
305
+ assert.deepEqual(x.div(y).to_8bit(), [-9, 225]);
306
+ });
307
+ it("Should divide by negative number", function () {
308
+ const x = CrunchyWith32Bit.from_8bit([170, 153, 136, 119, 102, 85]);
309
+ const y = CrunchyWith32Bit.from_8bit([-17, 68]);
310
+ assert.deepEqual(x.div(y).to_8bit(), [-9, 225, 129, 255, 9]);
311
+ });
312
+ it("Should divide two negative numbers", function () {
313
+ const x = CrunchyWith32Bit.from_8bit([-52, 155, 168, 23, 6, 85]);
314
+ const y = CrunchyWith32Bit.from_8bit([-19, 26, 247]);
315
+ assert.deepEqual(x.div(y).to_8bit(), [2, 192, 234, 136]);
316
+ });
317
+ it("Should divide zero", function () {
318
+ const x = CrunchyWith32Bit.from_8bit([0]);
319
+ const y = CrunchyWith32Bit.from_8bit([17, 68]);
320
+ assert.deepEqual(x.div(y).to_8bit(), [0]);
321
+ });
322
+ it("Shouldn't divide by zero", function () {
323
+ const x = CrunchyWith32Bit.from_8bit([170, 153]);
324
+ const y = CrunchyWith32Bit.from_8bit([0]);
325
+ assert.isNotOk(x.div(y));
326
+ });
327
+ it("Shouldn't divide zero by zero", function () {
328
+ const x = CrunchyWith32Bit.from_8bit([0]);
329
+ const y = CrunchyWith32Bit.from_8bit([0]);
330
+ assert.isNotOk(x.div(y));
331
+ });
332
+ });
333
+ describe("#leftShift", function () {
334
+ it("Should left shift a number", function () {
335
+ const x = CrunchyWith32Bit.from_8bit([22, 11]);
336
+ const s = 5;
337
+ assert.deepEqual(x.shl(s).to_8bit(), [2, 193, 96]);
338
+ });
339
+ it("Should left shift a number more", function () {
340
+ const x = CrunchyWith32Bit.from_8bit([1]);
341
+ const s = 64;
342
+ assert.deepEqual(x.shl(s).to_8bit(), [1, 0, 0, 0, 0, 0, 0, 0, 0]);
343
+ });
344
+ it("Should left shift zero", function () {
345
+ const x = CrunchyWith32Bit.from_8bit([0]);
346
+ const s = 80;
347
+ assert.deepEqual(x.shl(s).to_8bit(), [0]);
348
+ });
349
+ it("Should left shift negative", function () {
350
+ const x = CrunchyWith32Bit.from_8bit([-3]);
351
+ const s = 8;
352
+ assert.deepEqual(x.shl(s).to_8bit(), [-3, 0]);
353
+ });
354
+ it("Should left shift one", function () {
355
+ const x = CrunchyWith32Bit.from_8bit([1, 0, 0, 0, 0]);
356
+ const s = 1;
357
+ assert.deepEqual(x.shl(s).to_8bit(), [2, 0, 0, 0, 0]);
358
+ });
359
+ });
360
+ describe("#rightShift", function () {
361
+ it("Should right shift a number to zero", function () {
362
+ const x = CrunchyWith32Bit.from_14bit([167837953]);
363
+ // assert.deepEqual(x.rsh(32).num, [0]);
364
+ assert.deepEqual(x.shr(32).num, [0]);
365
+ assert.deepEqual(x.shr(33).num, [0]);
366
+ });
367
+ it("Should right shift a number", function () {
368
+ const x = CrunchyWith32Bit.from_8bit([22, 11]);
369
+ const s = 5;
370
+ assert.deepEqual(x.shr(s).to_8bit(), [176]);
371
+ });
372
+ it("Should right shift a number out of existance", function () {
373
+ const x = CrunchyWith32Bit.from_8bit([22, 11]);
374
+ const s = 20;
375
+ assert.deepEqual(x.shr(s).to_8bit(), [0]);
376
+ });
377
+ it("Should right shift a negative number", function () {
378
+ const x = CrunchyWith32Bit.from_8bit([-3]);
379
+ const s = 8;
380
+ assert.deepEqual(x.shr(s).to_8bit(), [-0]);
381
+ });
382
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=test_ipaddress.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test_ipaddress.d.ts","sourceRoot":"","sources":["../../test/test_ipaddress.ts"],"names":[],"mappings":""}
@@ -0,0 +1,182 @@
1
+ import IPAddress from "../src/ipaddress.js";
2
+ // import Prefix32 from '../src/prefix32';
3
+ // import Crunchy from '../src/crunchy';
4
+ class IPAddressTest {
5
+ valid_ipv4 = "172.16.10.1/24";
6
+ valid_ipv6 = "2001:db8::8:800:200c:417a/64";
7
+ valid_mapped = "::13.1.68.3";
8
+ invalid_ipv4 = "10.0.0.256";
9
+ invalid_ipv6 = ":1:2:3:4:5:6:7";
10
+ invalid_mapped = "::1:2.3.4";
11
+ }
12
+ class RangeItem {
13
+ start;
14
+ target;
15
+ }
16
+ function Range(start, target) {
17
+ const ri = new RangeItem();
18
+ ri.start = start;
19
+ ri.target = target;
20
+ return ri;
21
+ }
22
+ describe("IPAddressTest", () => {
23
+ function assertArrayEqual(a, b) {
24
+ assert.equal(a.length, b.length, "length missmatch");
25
+ for (let i = 0; i < a.length; ++i) {
26
+ assert.equal(a[i], b[i]);
27
+ }
28
+ }
29
+ function setup() {
30
+ return new IPAddressTest();
31
+ }
32
+ it("test_method_ipaddress", () => {
33
+ assert.isOk(IPAddress.parse(setup().valid_ipv4));
34
+ assert.isOk(IPAddress.parse(setup().valid_ipv6));
35
+ assert.isOk(IPAddress.parse(setup().valid_mapped));
36
+ assert.isOk(IPAddress.parse(setup().valid_ipv4).is_ipv4());
37
+ assert.isOk(IPAddress.parse(setup().valid_ipv6).is_ipv6());
38
+ assert.isOk(IPAddress.parse(setup().valid_mapped).is_mapped());
39
+ assert.isNull(IPAddress.parse(setup().invalid_ipv4));
40
+ assert.isNull(IPAddress.parse(setup().invalid_ipv6));
41
+ assert.isNull(IPAddress.parse(setup().invalid_mapped));
42
+ });
43
+ it("test_module_method_valid", () => {
44
+ assert.equal(true, IPAddress.is_valid("10.0.0.1"));
45
+ assert.equal(true, IPAddress.is_valid("10.0.0.0"));
46
+ assert.equal(true, IPAddress.is_valid("2002::1"));
47
+ assert.equal(true, IPAddress.is_valid("dead:beef:cafe:babe::f0ad"));
48
+ assert.equal(false, IPAddress.is_valid("10.0.0.256"));
49
+ assert.equal(false, IPAddress.is_valid("10.0.0.0.0"));
50
+ assert.equal(true, IPAddress.is_valid("10.0.0"));
51
+ assert.equal(true, IPAddress.is_valid("10.0"));
52
+ assert.equal(false, IPAddress.is_valid("2002:516:2:200"));
53
+ assert.equal(false, IPAddress.is_valid("2002:::1"));
54
+ });
55
+ it("test_module_method_valid_ipv4_netmark", () => {
56
+ assert.equal(true, IPAddress.is_valid_netmask("255.255.255.0"));
57
+ assert.equal(false, IPAddress.is_valid_netmask("10.0.0.1"));
58
+ });
59
+ it("test_summarize", function () {
60
+ // this.timeout(15000);
61
+ const netstr = [];
62
+ for (const range of [Range(1, 10), Range(11, 127), Range(128, 169), Range(170, 172), Range(173, 192), Range(193, 224)]) {
63
+ for (let i = range.start; i < range.target; ++i) {
64
+ netstr.push(`${i}.0.0.0/8`);
65
+ }
66
+ }
67
+ for (let i = 0; i < 256; ++i) {
68
+ if (i != 254) {
69
+ netstr.push(`169.${i}.0.0/16`);
70
+ }
71
+ }
72
+ for (let i = 0; i < 256; ++i) {
73
+ if (i < 16 || i > 31) {
74
+ netstr.push(`172.${i}.0.0/16`);
75
+ }
76
+ }
77
+ for (let i = 0; i < 256; ++i) {
78
+ if (i != 168) {
79
+ netstr.push(`192.${i}.0.0/16`);
80
+ }
81
+ }
82
+ const ip_addresses = [];
83
+ for (const net of netstr) {
84
+ ip_addresses.push(IPAddress.parse(net));
85
+ }
86
+ const empty_vec = [];
87
+ assert.equal(IPAddress.summarize_str(empty_vec).length, 0);
88
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize_str(["10.1.0.4/24"])), ["10.1.0.0/24"]);
89
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize_str(["2000:1::4711/32"])), ["2000:1::/32"]);
90
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize_str(["10.1.0.4/24", "7.0.0.0/0", "1.2.3.4/4"])), ["0.0.0.0/0"]);
91
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize_str([
92
+ "2000:1::/32",
93
+ "3000:1::/32",
94
+ "2000:2::/32",
95
+ "2000:3::/32",
96
+ "2000:4::/32",
97
+ "2000:5::/32",
98
+ "2000:6::/32",
99
+ "2000:7::/32",
100
+ "2000:8::/32",
101
+ ])), ["2000:1::/32", "2000:2::/31", "2000:4::/30", "2000:8::/32", "3000:1::/32"]);
102
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize_str([
103
+ "10.0.1.1/24",
104
+ "30.0.1.0/16",
105
+ "10.0.2.0/24",
106
+ "10.0.3.0/24",
107
+ "10.0.4.0/24",
108
+ "10.0.5.0/24",
109
+ "10.0.6.0/24",
110
+ "10.0.7.0/24",
111
+ "10.0.8.0/24",
112
+ ])), ["10.0.1.0/24", "10.0.2.0/23", "10.0.4.0/22", "10.0.8.0/24", "30.0.0.0/16"]);
113
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize_str(["10.0.0.0/23", "10.0.2.0/24"])), [
114
+ "10.0.0.0/23",
115
+ "10.0.2.0/24",
116
+ ]);
117
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize_str(["10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/23"])), [
118
+ "10.0.0.0/22",
119
+ ]);
120
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize_str(["10.0.0.0/16", "10.0.2.0/24"])), ["10.0.0.0/16"]);
121
+ const cnt = 10;
122
+ for (let _ = 0; _ < cnt; ++_) {
123
+ assert.deepEqual(IPAddress.to_string_vec(IPAddress.summarize(ip_addresses)), [
124
+ "1.0.0.0/8",
125
+ "2.0.0.0/7",
126
+ "4.0.0.0/6",
127
+ "8.0.0.0/7",
128
+ "11.0.0.0/8",
129
+ "12.0.0.0/6",
130
+ "16.0.0.0/4",
131
+ "32.0.0.0/3",
132
+ "64.0.0.0/3",
133
+ "96.0.0.0/4",
134
+ "112.0.0.0/5",
135
+ "120.0.0.0/6",
136
+ "124.0.0.0/7",
137
+ "126.0.0.0/8",
138
+ "128.0.0.0/3",
139
+ "160.0.0.0/5",
140
+ "168.0.0.0/8",
141
+ "169.0.0.0/9",
142
+ "169.128.0.0/10",
143
+ "169.192.0.0/11",
144
+ "169.224.0.0/12",
145
+ "169.240.0.0/13",
146
+ "169.248.0.0/14",
147
+ "169.252.0.0/15",
148
+ "169.255.0.0/16",
149
+ "170.0.0.0/7",
150
+ "172.0.0.0/12",
151
+ "172.32.0.0/11",
152
+ "172.64.0.0/10",
153
+ "172.128.0.0/9",
154
+ "173.0.0.0/8",
155
+ "174.0.0.0/7",
156
+ "176.0.0.0/4",
157
+ "192.0.0.0/9",
158
+ "192.128.0.0/11",
159
+ "192.160.0.0/13",
160
+ "192.169.0.0/16",
161
+ "192.170.0.0/15",
162
+ "192.172.0.0/14",
163
+ "192.176.0.0/12",
164
+ "192.192.0.0/10",
165
+ "193.0.0.0/8",
166
+ "194.0.0.0/7",
167
+ "196.0.0.0/6",
168
+ "200.0.0.0/5",
169
+ "208.0.0.0/4",
170
+ ]);
171
+ }
172
+ // end
173
+ // printer = RubyProf.GraphPrinter.new(result)
174
+ // printer.print(STDOUT, {})
175
+ // test imutable input parameters
176
+ const a1 = IPAddress.parse("10.0.0.1/24");
177
+ const a2 = IPAddress.parse("10.0.1.1/24");
178
+ assertArrayEqual(IPAddress.to_string_vec(IPAddress.summarize([a1.clone(), a2.clone()])), ["10.0.0.0/23"]);
179
+ assert.equal("10.0.0.1/24", a1.to_string());
180
+ assert.equal("10.0.1.1/24", a2.to_string());
181
+ }, 15000);
182
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=test_ipv4.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test_ipv4.d.ts","sourceRoot":"","sources":["../../test/test_ipv4.ts"],"names":[],"mappings":""}