ipaddress 0.2.5 → 0.2.6

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 (113) hide show
  1. package/{crunchy.d.ts → dist/src/crunchy.d.ts} +1 -0
  2. package/dist/src/crunchy.d.ts.map +1 -0
  3. package/dist/src/crunchy.js +484 -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} +4 -5
  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} +18 -16
  14. package/dist/src/ipaddress.d.ts.map +1 -0
  15. package/dist/src/ipaddress.js +1228 -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 +176 -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 +35 -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 +25 -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.d.ts +2 -0
  44. package/dist/test/test_crunchy.d.ts.map +1 -0
  45. package/dist/test/test_crunchy.js +451 -0
  46. package/dist/test/test_ipaddress.d.ts +2 -0
  47. package/dist/test/test_ipaddress.d.ts.map +1 -0
  48. package/dist/test/test_ipaddress.js +182 -0
  49. package/dist/test/test_ipv4.d.ts +2 -0
  50. package/dist/test/test_ipv4.d.ts.map +1 -0
  51. package/dist/test/test_ipv4.js +561 -0
  52. package/dist/test/test_ipv6.d.ts +2 -0
  53. package/dist/test/test_ipv6.d.ts.map +1 -0
  54. package/dist/test/test_ipv6.js +313 -0
  55. package/dist/test/test_ipv6_loopback.d.ts +2 -0
  56. package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
  57. package/dist/test/test_ipv6_loopback.js +40 -0
  58. package/dist/test/test_ipv6_mapped.d.ts +2 -0
  59. package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
  60. package/dist/test/test_ipv6_mapped.js +89 -0
  61. package/dist/test/test_ipv6_unspec.d.ts +2 -0
  62. package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
  63. package/dist/test/test_ipv6_unspec.js +39 -0
  64. package/dist/test/test_prefix128.d.ts +2 -0
  65. package/dist/test/test_prefix128.d.ts.map +1 -0
  66. package/dist/test/test_prefix128.js +35 -0
  67. package/dist/test/test_prefix32.d.ts +2 -0
  68. package/dist/test/test_prefix32.d.ts.map +1 -0
  69. package/dist/test/test_prefix32.js +126 -0
  70. package/dist/test/test_rle.d.ts +2 -0
  71. package/dist/test/test_rle.d.ts.map +1 -0
  72. package/dist/test/test_rle.js +141 -0
  73. package/dist/vitest.config.d.ts +3 -0
  74. package/dist/vitest.config.d.ts.map +1 -0
  75. package/dist/vitest.config.js +11 -0
  76. package/package.json +40 -37
  77. package/src/crunchy.ts +546 -0
  78. package/src/index.ts +23 -0
  79. package/src/ip_bits.ts +148 -0
  80. package/src/ip_version.ts +6 -0
  81. package/src/ipaddress.ts +1331 -0
  82. package/src/ipv4.ts +190 -0
  83. package/src/ipv6.ts +202 -0
  84. package/src/ipv6_loopback.ts +55 -0
  85. package/src/ipv6_mapped.ts +163 -0
  86. package/src/ipv6_unspec.ts +59 -0
  87. package/src/prefix.ts +170 -0
  88. package/src/prefix128.ts +38 -0
  89. package/src/prefix32.ts +28 -0
  90. package/src/rle.ts +85 -0
  91. package/test/test_crunchy.ts +614 -0
  92. package/test/test_ipaddress.ts +208 -0
  93. package/test/test_ipv4.ts +596 -0
  94. package/test/test_ipv6.ts +325 -0
  95. package/test/test_ipv6_loopback.ts +52 -0
  96. package/test/test_ipv6_mapped.ts +101 -0
  97. package/test/test_ipv6_unspec.ts +51 -0
  98. package/test/test_prefix128.ts +39 -0
  99. package/test/test_prefix32.ts +130 -0
  100. package/test/test_rle.ts +144 -0
  101. package/LICENSE +0 -20
  102. package/README.md +0 -960
  103. package/index.d.ts +0 -12
  104. package/ipaddress.js +0 -2869
  105. package/ipaddress.js.map +0 -1
  106. package/ipv4.d.ts +0 -14
  107. package/ipv6.d.ts +0 -12
  108. package/ipv6_loopback.d.ts +0 -5
  109. package/ipv6_mapped.d.ts +0 -5
  110. package/ipv6_unspec.d.ts +0 -5
  111. package/prefix128.d.ts +0 -6
  112. package/prefix32.d.ts +0 -6
  113. package/rle.d.ts +0 -14
@@ -0,0 +1,451 @@
1
+ import Crunchy from "../src/crunchy.js";
2
+ describe("#compare", function () {
3
+ it("Should confirm equality", function () {
4
+ const x = Crunchy.from_string("12345678901234567890");
5
+ const y = Crunchy.from_string("12345678901234567890");
6
+ assert.isTrue(x.eq(y));
7
+ // this.done();
8
+ });
9
+ it("Should find first is greater than second", function () {
10
+ const x = Crunchy.from_string("12345678901234567890");
11
+ const y = Crunchy.from_string("1234567890");
12
+ assert.isTrue(x.gt(y));
13
+ // this.done();
14
+ });
15
+ it("Should find first is less than second", function () {
16
+ const x = Crunchy.from_string("1234567890");
17
+ const y = Crunchy.from_string("12345678901234567890");
18
+ assert.isTrue(x.lt(y));
19
+ // this.done();
20
+ });
21
+ });
22
+ describe("#add", function () {
23
+ it("Should add numbers", function () {
24
+ const x = Crunchy.from_8bit([242, 62]);
25
+ const y = Crunchy.from_8bit([42, 2]);
26
+ assert.deepEqual(x.add(y).to_8bit(), [1, 28, 64]);
27
+ // x.should.eql([242, 62]);
28
+ // y.should.eql([42, 2]);
29
+ });
30
+ it("Should add numbers, first longer than second", function () {
31
+ const x = Crunchy.from_8bit([26, 255, 230, 17]);
32
+ const y = Crunchy.from_8bit([42, 34]);
33
+ assert.deepEqual(x.add(y).to_8bit(), [27, 0, 16, 51]);
34
+ // x, [26, 255, 230, 17]);
35
+ // y.should.eql([42, 34]);
36
+ });
37
+ it("Should add numbers, second longer than first", function () {
38
+ const x = Crunchy.from_8bit([234, 34]);
39
+ const y = Crunchy.from_8bit([255, 255, 230, 17]);
40
+ assert.deepEqual(x.add(y).to_8bit(), [1, 0, 0, 208, 51]);
41
+ // x.should.eql([234, 34]);
42
+ // y.should.eql([255, 255, 230, 17]);
43
+ });
44
+ it("Should add two identical numbers", function () {
45
+ const x = Crunchy.from_8bit([255, 255, 255, 255, 255, 255]);
46
+ const y = Crunchy.from_8bit([255, 255, 255, 255, 255, 255]);
47
+ assert.deepEqual(x.add(y).to_8bit(), [1, 255, 255, 255, 255, 255, 254]);
48
+ // x.should.eql([255, 255, 255, 255, 255, 255]);
49
+ // y.should.eql([255, 255, 255, 255, 255, 255]);
50
+ });
51
+ it("Should add positive to negative number expecting positive result", function () {
52
+ const x = Crunchy.from_8bit([51, 254, 144, 207]);
53
+ const y = Crunchy.from_8bit([-20, 89, 145, 32]);
54
+ assert.deepEqual(x.add(y).to_8bit(), [31, 164, 255, 175]);
55
+ // x.should.eql([51, 254, 144, 207]);
56
+ // y.should.eql([-20, 89, 145, 32]);
57
+ });
58
+ it("Should add positive to negative number expecting negative result", function () {
59
+ const x = Crunchy.from_8bit([242, 62]);
60
+ const y = Crunchy.from_8bit([-242, 64]);
61
+ assert.deepEqual(x.add(y).to_8bit(), [-2]);
62
+ // x.should.eql([242, 62]);
63
+ // y.should.eql([-242, 64]);
64
+ });
65
+ it("Should add negative to positive number expecting positive result", function () {
66
+ const x = Crunchy.from_8bit([-42, 2]);
67
+ const y = Crunchy.from_8bit([242, 62]);
68
+ assert.deepEqual(x.add(y).to_8bit(), [200, 60]);
69
+ // x, [-42, 2]);
70
+ // y, [242, 62]);
71
+ });
72
+ it("Should add negative to positive number expecting negative result", function () {
73
+ const x = Crunchy.from_8bit([-42, 2]);
74
+ const y = Crunchy.from_8bit([42, 0]);
75
+ assert.deepEqual(x.add(y).to_8bit(), [-2]);
76
+ // x, [-42, 2]);
77
+ // y, [42, 0]);
78
+ });
79
+ it("Should add two negative numbers expecting negative result", function () {
80
+ const x = Crunchy.from_8bit([-242, 62]);
81
+ const y = Crunchy.from_8bit([-42, 2]);
82
+ assert.deepEqual(x.add(y).to_8bit(), [-1, 28, 64]);
83
+ // x, [-242, 62]);
84
+ // y, [-42, 2]);
85
+ });
86
+ it("Should add zero to number", function () {
87
+ const x = Crunchy.from_8bit([26, 255, 230, 17]);
88
+ const y = Crunchy.from_8bit([0]);
89
+ assert.deepEqual(x.add(y).to_8bit(), [26, 255, 230, 17]);
90
+ // x, [26, 255, 230, 17]);
91
+ // y, [0]);
92
+ });
93
+ it("Should add number to zero", function () {
94
+ const x = Crunchy.from_8bit([0]);
95
+ const y = Crunchy.from_8bit([43, 123, 200, 1]);
96
+ assert.deepEqual(x.add(y).to_8bit(), [43, 123, 200, 1]);
97
+ // x, [0]);
98
+ // y, [43, 123, 200, 1]);
99
+ });
100
+ it("Should add two zeros", function () {
101
+ const x = Crunchy.from_8bit([0]);
102
+ const y = Crunchy.from_8bit([0]);
103
+ assert.deepEqual(x.add(y).to_8bit(), [0]);
104
+ // x.should.eql([0]);
105
+ // y.should.eql([0]);
106
+ });
107
+ });
108
+ describe("#add-from_string", function () {
109
+ it("Should add numbers", function () {
110
+ // console.log(new Date());
111
+ const x = Crunchy.from_string("12345678911234567891");
112
+ // console.log(new Date());
113
+ const y = Crunchy.from_string("12345678901234567890");
114
+ // console.log(new Date(), x, y);
115
+ // console.log("addddddd:", new Date(), x, x.toString(), y, y.toString(), x.add(y).toString());
116
+ assert.equal(x.add(y).toString(), "24691357812469135781");
117
+ // console.log(new Date());
118
+ // this.done();
119
+ });
120
+ it("Should add numbers, first longer than second", function () {
121
+ const x = Crunchy.from_string("12345678901234567890");
122
+ const y = Crunchy.from_string("1234567890");
123
+ assert.equal(x.add(y).toString(), "12345678902469135780");
124
+ // this.done();
125
+ });
126
+ it("Should add numbers, second longer than first", function () {
127
+ const x = Crunchy.from_string("1234567890");
128
+ const y = Crunchy.from_string("12345678901234567890");
129
+ assert.equal(x.add(y).toString(), "12345678902469135780");
130
+ // this.done();
131
+ });
132
+ it("Should add two identical numbers", function () {
133
+ const x = Crunchy.from_string("12345678901234567890");
134
+ const y = Crunchy.from_string("12345678901234567890");
135
+ assert.equal(x.add(y).toString(), "24691357802469135780");
136
+ // this.done();
137
+ });
138
+ it("Should add zero to number", function () {
139
+ const x = Crunchy.from_string("12345678901234567890");
140
+ const y = Crunchy.from_string("0");
141
+ assert.equal(x.add(y).toString(), "12345678901234567890");
142
+ // this.done();
143
+ });
144
+ it("Should add number to zero", function () {
145
+ const x = Crunchy.from_string("0");
146
+ const y = Crunchy.from_string("12345678901234567890");
147
+ assert.equal(x.add(y).toString(), "12345678901234567890");
148
+ // this.done();
149
+ });
150
+ it("Should add two zeros", function () {
151
+ const x = Crunchy.from_string("0");
152
+ const y = Crunchy.from_string("0");
153
+ assert.equal(x.add(y).toString(), "0");
154
+ // this.done();
155
+ });
156
+ });
157
+ describe("#sub", function () {
158
+ it("Should subtract numbers", function () {
159
+ const x = Crunchy.from_8bit([170, 1, 79, 119, 242, 62]);
160
+ const y = Crunchy.from_8bit([17, 241, 123, 250, 42, 2]);
161
+ // console.log("Should subtract numbers:", x, y, x.sub(y));
162
+ assert.deepEqual(x.sub(y).to_8bit(), [152, 15, 211, 125, 200, 60]);
163
+ // x.to_8bit().should.eql([170, 1, 79, 119, 242, 62]);
164
+ // y.should.eql([17, 241, 123, 250, 42, 2]);
165
+ });
166
+ it("Should subtract one", function () {
167
+ const x = Crunchy.from_8bit([240, 0, 0, 0, 0, 0]);
168
+ const y = Crunchy.from_8bit([1]);
169
+ assert.deepEqual(x.sub(y).to_8bit(), [239, 255, 255, 255, 255, 255]);
170
+ // x.should.eql([240, 0, 0, 0, 0, 0]);
171
+ // y.should.eql([1]);
172
+ });
173
+ it("Should subtract numbers expecting negative result", function () {
174
+ const x = Crunchy.from_8bit([26]);
175
+ const y = Crunchy.from_8bit([255]);
176
+ assert.deepEqual(x.sub(y).to_8bit(), [-229]);
177
+ // x.should.eql([26]);
178
+ // y.should.eql([255]);
179
+ });
180
+ it("Should subtract numbers expecting negative result", function () {
181
+ const x = Crunchy.from_8bit([188, 196]);
182
+ const y = Crunchy.from_8bit([188, 197]);
183
+ assert.deepEqual(x.sub(y).to_8bit(), [-1]);
184
+ // x.should.eql([188, 196]);
185
+ // y.should.eql([188, 197]);
186
+ });
187
+ it("Should subtract negative number from positive", function () {
188
+ const x = Crunchy.from_8bit([240, 0, 0, 0, 0, 0]);
189
+ const y = Crunchy.from_8bit([-1]);
190
+ // console.log("negative:", x, y, x.sub(y));
191
+ assert.deepEqual(x.sub(y).to_8bit(), [240, 0, 0, 0, 0, 1]);
192
+ // x.should.eql([240, 0, 0, 0, 0, 0]);
193
+ // y.should.eql([-1]);
194
+ });
195
+ it("Should subtract positive number from negative", function () {
196
+ const x = Crunchy.from_8bit([-240, 0, 0, 0, 0, 0]);
197
+ const y = Crunchy.from_8bit([1]);
198
+ assert.deepEqual(x.sub(y).to_8bit(), [-240, 0, 0, 0, 0, 1]);
199
+ // x.should.eql([-240, 0, 0, 0, 0, 0]);
200
+ // y.should.eql([1]);
201
+ });
202
+ it("Should subtract two negative numbers", function () {
203
+ const x = Crunchy.from_8bit([-240, 0, 0, 0, 0, 0]);
204
+ const y = Crunchy.from_8bit([-1]);
205
+ assert.deepEqual(x.sub(y).to_8bit(), [-239, 255, 255, 255, 255, 255]);
206
+ // x.should.eql([-240, 0, 0, 0, 0, 0]);
207
+ // y.should.eql([-1]);
208
+ });
209
+ it("Should subtract two equal numbers expecting zero", function () {
210
+ const x = Crunchy.from_8bit([20]);
211
+ const y = Crunchy.from_8bit([20]);
212
+ assert.deepEqual(x.sub(y).to_8bit(), [0]);
213
+ // x.should.eql([20]);
214
+ // y.should.eql([20]);
215
+ });
216
+ it("Should subtract zero", function () {
217
+ const x = Crunchy.from_8bit([244, 137, 7, 161]);
218
+ const y = Crunchy.from_8bit([0]);
219
+ assert.deepEqual(x.sub(y).to_8bit(), [244, 137, 7, 161]);
220
+ // x.should.eql([244, 137, 7, 161]);
221
+ // y.should.eql([0]);
222
+ });
223
+ it("Should subtract zero from zero", function () {
224
+ const x = Crunchy.from_8bit([0]);
225
+ const y = Crunchy.from_8bit([0]);
226
+ assert.deepEqual(x.sub(y).to_8bit(), [0]);
227
+ // x.should.eql([0]);
228
+ // y.should.eql([0]);
229
+ });
230
+ it("Should subtract from zero", function () {
231
+ const x = Crunchy.from_8bit([0]);
232
+ const y = Crunchy.from_8bit([15]);
233
+ assert.deepEqual(x.sub(y).to_8bit(), [-15]);
234
+ // x.should.eql([0]);
235
+ // y.should.eql([15]);
236
+ });
237
+ });
238
+ describe("#mul", function () {
239
+ it("Should multiply numbers", function () {
240
+ const x = Crunchy.from_8bit([162, 51, 95]);
241
+ const y = Crunchy.from_8bit([42, 18, 204]);
242
+ assert.deepEqual(x.mul(y).to_8bit(), [26, 168, 86, 115, 157, 180]);
243
+ });
244
+ it("Should multiply numbers", function () {
245
+ const x = Crunchy.from_8bit([255, 65, 34, 51, 95]);
246
+ const y = Crunchy.from_8bit([42, 18, 204]);
247
+ assert.deepEqual(x.mul(y).to_8bit(), [41, 243, 109, 152, 188, 115, 157, 180]);
248
+ });
249
+ it("Should multiply numbers", function () {
250
+ const x = Crunchy.from_8bit([255, 255, 255, 255]);
251
+ const y = Crunchy.from_8bit([255, 255, 255, 255]);
252
+ assert.deepEqual(x.mul(y).to_8bit(), [255, 255, 255, 254, 0, 0, 0, 1]);
253
+ });
254
+ it("Should multiply positive with negative number", function () {
255
+ const x = Crunchy.from_8bit([77, 242, 62]);
256
+ const y = Crunchy.from_8bit([-42, 2, 113, 43, 57, 65]);
257
+ assert.deepEqual(x.mul(y).to_8bit(), [-12, 202, 124, 133, 146, 125, 36, 79, 190]);
258
+ });
259
+ it("Should multiply negative with positive number", function () {
260
+ const x = Crunchy.from_8bit([-255, 17, 162, 62]);
261
+ const y = Crunchy.from_8bit([255, 17, 162, 62]);
262
+ assert.deepEqual(x.mul(y).to_8bit(), [-254, 36, 34, 110, 119, 14, 135, 4]);
263
+ });
264
+ it("Should multiply two negative numbers", function () {
265
+ const x = Crunchy.from_8bit([-162, 51, 95]);
266
+ const y = Crunchy.from_8bit([-42, 18, 204]);
267
+ assert.deepEqual(x.mul(y).to_8bit(), [26, 168, 86, 115, 157, 180]);
268
+ });
269
+ it("Should multiply by zero", function () {
270
+ const x = Crunchy.from_8bit([77, 242, 62]);
271
+ const y = Crunchy.from_8bit([0]);
272
+ assert.deepEqual(x.mul(y).to_8bit(), [0]);
273
+ });
274
+ it("Should multiply zero by number", function () {
275
+ const x = Crunchy.from_8bit([0]);
276
+ const y = Crunchy.from_8bit([77, 242, 62]);
277
+ assert.deepEqual(x.mul(y).to_8bit(), [0]);
278
+ });
279
+ it("Should multiply zeros", function () {
280
+ const x = Crunchy.from_8bit([0]);
281
+ const y = Crunchy.from_8bit([0]);
282
+ assert.deepEqual(x.mul(y).to_8bit(), [0]);
283
+ });
284
+ });
285
+ describe("#div", function () {
286
+ it("Should divide numbers", function () {
287
+ const x = Crunchy.from_8bit([52, 155, 168, 23, 6, 85]);
288
+ const y = Crunchy.from_8bit([19, 26, 247]);
289
+ assert.deepEqual(x.div(y).to_8bit(), [2, 192, 234, 136]);
290
+ });
291
+ it("Should divide by one", function () {
292
+ const x = Crunchy.from_8bit([15, 127, 73, 1]);
293
+ const y = Crunchy.from_8bit([1]);
294
+ assert.deepEqual(x.div(y).to_8bit(), [15, 127, 73, 1]);
295
+ });
296
+ it("Should divide by self", function () {
297
+ const x = Crunchy.from_8bit([15, 127, 73, 1]);
298
+ const y = Crunchy.from_8bit([15, 127, 73, 1]);
299
+ assert.deepEqual(x.div(y).to_8bit(), [1]);
300
+ });
301
+ it("Should divide negative number", function () {
302
+ const x = Crunchy.from_8bit([-170, 153, 136]);
303
+ const y = Crunchy.from_8bit([17, 68]);
304
+ assert.deepEqual(x.div(y).to_8bit(), [-9, 225]);
305
+ });
306
+ it("Should divide by negative number", function () {
307
+ const x = Crunchy.from_8bit([170, 153, 136, 119, 102, 85]);
308
+ const y = Crunchy.from_8bit([-17, 68]);
309
+ assert.deepEqual(x.div(y).to_8bit(), [-9, 225, 129, 255, 9]);
310
+ });
311
+ it("Should divide two negative numbers", function () {
312
+ const x = Crunchy.from_8bit([-52, 155, 168, 23, 6, 85]);
313
+ const y = Crunchy.from_8bit([-19, 26, 247]);
314
+ assert.deepEqual(x.div(y).to_8bit(), [2, 192, 234, 136]);
315
+ });
316
+ it("Should divide zero", function () {
317
+ const x = Crunchy.from_8bit([0]);
318
+ const y = Crunchy.from_8bit([17, 68]);
319
+ assert.deepEqual(x.div(y).to_8bit(), [0]);
320
+ });
321
+ it("Shouldn't divide by zero", function () {
322
+ const x = Crunchy.from_8bit([170, 153]);
323
+ const y = Crunchy.from_8bit([0]);
324
+ assert.isNotOk(x.div(y));
325
+ });
326
+ it("Shouldn't divide zero by zero", function () {
327
+ const x = Crunchy.from_8bit([0]);
328
+ const y = Crunchy.from_8bit([0]);
329
+ assert.isNotOk(x.div(y));
330
+ });
331
+ });
332
+ // describe("#mod", function() {
333
+ // it("Should calculate modulo", function() {
334
+ // let x = [52, 155, 168, 23, 6, 85],
335
+ // y = [19, 26, 247];
336
+ // Crunch.mod(x, y).should.eql([10, 237, 29]);
337
+ // x.should.eql([52, 155, 168, 23, 6, 85]);
338
+ // y.should.eql([19, 26, 247]);
339
+ // });
340
+ // it("Should calculate modulo of number smaller than modulus", function() {
341
+ // let x = [1, 0],
342
+ // y = [1, 241];
343
+ // Crunch.mod(x, y).should.eql([1, 0]);
344
+ // x.should.eql([1, 0]);
345
+ // y.should.eql([1, 241]);
346
+ // });
347
+ // it("Should calculate modulo of modulus", function() {
348
+ // let x = [1, 241],
349
+ // y = [1, 241];
350
+ // Crunch.mod(x, y).should.eql([0]);
351
+ // x.should.eql([1, 241]);
352
+ // y.should.eql([1, 241]);
353
+ // });
354
+ // it("Should calculate modulo of zero", function() {
355
+ // let x = [0],
356
+ // y = [1, 241];
357
+ // Crunch.mod(x, y).should.eql([0]);
358
+ // x.should.eql([0]);
359
+ // y.should.eql([1, 241]);
360
+ // });
361
+ // // it("Should calculate modulo of negative", function() {
362
+ // // let x = [-4],
363
+ // // y = [3];
364
+ // // Crunch.mod(x, y).should.eql([2]);
365
+ // // x.should.eql([-4]);
366
+ // // y.should.eql([3]);
367
+ // // });
368
+ // // it("Should calculate modulo of negative", function() {
369
+ // // let x = [ -10, 18 ],
370
+ // // y = [ 123 ];
371
+ // // Crunch.mod(x, y).should.eql([ 5 ]);
372
+ // // x.should.eql([ -10, 18 ]);
373
+ // // y.should.eql([ 123 ]);
374
+ // // });
375
+ // // it("Should calculate modulo of negative", function() {
376
+ // // let x = [ -15, 62, 62, 18, 176 ],
377
+ // // y = [ 139, 207, 194, 82 ];
378
+ // // Crunch.mod(x, y).should.eql([ 12, 123, 46, 72 ]);
379
+ // // x.should.eql([ -15, 62, 62, 18, 176 ]);
380
+ // // y.should.eql([ 139, 207, 194, 82 ]);
381
+ // // });
382
+ // });
383
+ describe("#leftShift", function () {
384
+ it("Should left shift a number", function () {
385
+ const x = Crunchy.from_8bit([22, 11]);
386
+ const s = 5;
387
+ assert.deepEqual(x.shl(s).to_8bit(), [2, 193, 96]);
388
+ });
389
+ it("Should left shift a number more", function () {
390
+ const x = Crunchy.from_8bit([1]);
391
+ const s = 64;
392
+ assert.deepEqual(x.shl(s).to_8bit(), [1, 0, 0, 0, 0, 0, 0, 0, 0]);
393
+ });
394
+ it("Should left shift zero", function () {
395
+ const x = Crunchy.from_8bit([0]);
396
+ const s = 80;
397
+ assert.deepEqual(x.shl(s).to_8bit(), [0]);
398
+ });
399
+ it("Should left shift negative", function () {
400
+ const x = Crunchy.from_8bit([-3]);
401
+ const s = 8;
402
+ assert.deepEqual(x.shl(s).to_8bit(), [-3, 0]);
403
+ });
404
+ it("Should left shift one", function () {
405
+ const x = Crunchy.from_8bit([1, 0, 0, 0, 0]);
406
+ const s = 1;
407
+ assert.deepEqual(x.shl(s).to_8bit(), [2, 0, 0, 0, 0]);
408
+ });
409
+ });
410
+ describe("#rightShift", function () {
411
+ it("Should right shift a number to zero", function () {
412
+ const x = Crunchy.from_14bit([167837953]);
413
+ // assert.deepEqual(x.rsh(32).num, [0]);
414
+ assert.deepEqual(x.shr(32).num, [0]);
415
+ assert.deepEqual(x.shr(33).num, [0]);
416
+ });
417
+ it("Should right shift a number", function () {
418
+ const x = Crunchy.from_8bit([22, 11]);
419
+ const s = 5;
420
+ assert.deepEqual(x.shr(s).to_8bit(), [176]);
421
+ });
422
+ it("Should right shift a number out of existance", function () {
423
+ const x = Crunchy.from_8bit([22, 11]);
424
+ const s = 20;
425
+ assert.deepEqual(x.shr(s).to_8bit(), [0]);
426
+ });
427
+ it("Should right shift a negative number", function () {
428
+ const x = Crunchy.from_8bit([-3]);
429
+ const s = 8;
430
+ assert.deepEqual(x.shr(s).to_8bit(), [-0]);
431
+ });
432
+ });
433
+ // describe("#parse", function() {
434
+ // it("Should convert string to integer", function() {
435
+ // let s = "6315359056060240643798024";
436
+ // Crunchy.parse(s).to_8bit().should.eql([5, 57, 84, 76, 233, 0, 120, 91, 180, 180, 8]);
437
+ // s.should.equal("6315359056060240643798024");
438
+ // });
439
+ // it("Should convert string negative number to integer", function() {
440
+ // let s = "-256";
441
+ // Crunchy.parse(s).to_8bit().should.eql([-1, 0]);
442
+ // s.should.equal("-256");
443
+ // });
444
+ // });
445
+ // describe("#stringify", function() {
446
+ // it("Should convert integer to string", function() {
447
+ // let x = [5, 57, 84, 76, 233, 0, 120, 91, 180, 180, 8];
448
+ // Crunchy.from_8bit(x).to_8bit().should.equal("6315359056060240643798024");
449
+ // x.should.eql([5, 57, 84, 76, 233, 0, 120, 91, 180, 180, 8]);
450
+ // });
451
+ // });
@@ -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":""}