ipaddress 0.2.6 → 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 (57) hide show
  1. package/dist/src/crunchy.d.ts +107 -33
  2. package/dist/src/crunchy.d.ts.map +1 -1
  3. package/dist/src/crunchy.js +274 -142
  4. package/dist/src/ip_bits.d.ts +2 -2
  5. package/dist/src/ip_bits.d.ts.map +1 -1
  6. package/dist/src/ipaddress.d.ts +24 -23
  7. package/dist/src/ipaddress.d.ts.map +1 -1
  8. package/dist/src/ipaddress.js +22 -7
  9. package/dist/src/ipv4.d.ts +3 -3
  10. package/dist/src/ipv4.d.ts.map +1 -1
  11. package/dist/src/ipv4.js +26 -15
  12. package/dist/src/ipv6.d.ts +4 -4
  13. package/dist/src/ipv6.d.ts.map +1 -1
  14. package/dist/src/ipv6.js +1 -1
  15. package/dist/src/ipv6_loopback.d.ts +1 -1
  16. package/dist/src/ipv6_loopback.d.ts.map +1 -1
  17. package/dist/src/ipv6_mapped.d.ts +1 -1
  18. package/dist/src/ipv6_mapped.d.ts.map +1 -1
  19. package/dist/src/ipv6_unspec.d.ts +1 -1
  20. package/dist/src/ipv6_unspec.d.ts.map +1 -1
  21. package/dist/src/prefix128.d.ts +1 -1
  22. package/dist/src/prefix128.d.ts.map +1 -1
  23. package/dist/src/prefix128.js +5 -1
  24. package/dist/src/prefix32.d.ts +1 -1
  25. package/dist/src/prefix32.d.ts.map +1 -1
  26. package/dist/src/prefix32.js +5 -1
  27. package/dist/test/test_crunchy_impls.d.ts +2 -0
  28. package/dist/test/test_crunchy_impls.d.ts.map +1 -0
  29. package/dist/test/test_crunchy_impls.js +150 -0
  30. package/dist/test/test_crunchy_with32bit.d.ts +2 -0
  31. package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
  32. package/dist/test/{test_crunchy.js → test_crunchy_with32bit.js} +113 -182
  33. package/dist/test/test_ipv4.js +1 -1
  34. package/dist/test/test_ipv6.js +1 -1
  35. package/package.json +10 -9
  36. package/src/crunchy.ts +365 -180
  37. package/src/ip_bits.ts +11 -11
  38. package/src/ipaddress.ts +49 -33
  39. package/src/ipv4.ts +32 -19
  40. package/src/ipv6.ts +6 -6
  41. package/src/ipv6_loopback.ts +1 -1
  42. package/src/ipv6_mapped.ts +1 -1
  43. package/src/ipv6_unspec.ts +1 -1
  44. package/src/prefix128.ts +6 -2
  45. package/src/prefix32.ts +6 -2
  46. package/test/test_crunchy_impls.ts +179 -0
  47. package/test/{test_crunchy.ts → test_crunchy_with32bit.ts} +131 -235
  48. package/test/test_ipaddress.ts +9 -9
  49. package/test/test_ipv4.ts +115 -115
  50. package/test/test_ipv6.ts +62 -62
  51. package/test/test_ipv6_loopback.ts +1 -1
  52. package/test/test_ipv6_mapped.ts +4 -4
  53. package/test/test_ipv6_unspec.ts +1 -1
  54. package/test/test_prefix128.ts +2 -2
  55. package/test/test_prefix32.ts +13 -13
  56. package/dist/test/test_crunchy.d.ts +0 -2
  57. package/dist/test/test_crunchy.d.ts.map +0 -1
@@ -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
+ });