| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5514305 commit a53911b
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,6 +30,17 @@ let cntr = 0; | |||
| 30 | 30 | } | |
| 31 | 31 | } | |
| 32 | 32 | ||
| 33 | + { | ||
| 34 | + // Floats will be converted to integers via `Math.floor` | ||
| 35 | + b.fill(++cntr); | ||
| 36 | + c.fill(++cntr); | ||
| 37 | + const copied = b.copy(c, 0, 0, 512.5); | ||
| 38 | + assert.strictEqual(copied, 512); | ||
| 39 | + for (let i = 0; i < c.length; i++) { | ||
| 40 | + assert.strictEqual(c[i], b[i]); | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + | ||
| 33 | 44 | { | |
| 34 | 45 | // Copy c into b, without specifying sourceEnd | |
| 35 | 46 | b.fill(++cntr); | |
@@ -52,6 +63,17 @@ let cntr = 0; | |||
| 52 | 63 | } | |
| 53 | 64 | } | |
| 54 | 65 | ||
| 66 | + { | ||
| 67 | + // Copied source range greater than source length | ||
| 68 | + b.fill(++cntr); | ||
| 69 | + c.fill(++cntr); | ||
| 70 | + const copied = c.copy(b, 0, 0, c.length + 1); | ||
| 71 | + assert.strictEqual(copied, c.length); | ||
| 72 | + for (let i = 0; i < c.length; i++) { | ||
| 73 | + assert.strictEqual(b[i], c[i]); | ||
| 74 | + } | ||
| 75 | + } | ||
| 76 | + | ||
| 55 | 77 | { | |
| 56 | 78 | // Copy longer buffer b to shorter c without targetStart | |
| 57 | 79 | b.fill(++cntr); | |
@@ -107,6 +129,26 @@ bb.fill('hello crazy world'); | |||
| 107 | 129 | // Try to copy from before the beginning of b. Should not throw. | |
| 108 | 130 | b.copy(c, 0, 100, 10); | |
| 109 | 131 | ||
| 132 | + // Throw with invalid source type | ||
| 133 | + assert.throws( | ||
| 134 | + () => Buffer.prototype.copy.call(0), | ||
| 135 | + { | ||
| 136 | + code: 'ERR_INVALID_ARG_TYPE', | ||
| 137 | + name: 'TypeError', | ||
| 138 | + } | ||
| 139 | + ); | ||
| 140 | + | ||
| 141 | + // Copy throws at negative targetStart | ||
| 142 | + assert.throws( | ||
| 143 | + () => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), -1, 0), | ||
| 144 | + { | ||
| 145 | + code: 'ERR_OUT_OF_RANGE', | ||
| 146 | + name: 'RangeError', | ||
| 147 | + message: 'The value of "targetStart" is out of range. ' + | ||
| 148 | + 'It must be >= 0. Received -1' | ||
| 149 | + } | ||
| 150 | + ); | ||
| 151 | + | ||
| 110 | 152 | // Copy throws at negative sourceStart | |
| 111 | 153 | assert.throws( | |
| 112 | 154 | () => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -108,6 +108,13 @@ assert.strictEqual( | |||
| 108 | 108 | 3 | |
| 109 | 109 | ); | |
| 110 | 110 | ||
| 111 | + // Test base64url encoding | ||
| 112 | + assert.strictEqual( | ||
| 113 | + Buffer.from(b.toString('base64url'), 'base64url') | ||
| 114 | + .indexOf('ZA==', 0, 'base64url'), | ||
| 115 | + 3 | ||
| 116 | + ); | ||
| 117 | + | ||
| 111 | 118 | // test ascii encoding | |
| 112 | 119 | assert.strictEqual( | |
| 113 | 120 | Buffer.from(b.toString('ascii'), 'ascii') | |
@@ -180,15 +187,18 @@ assert.strictEqual(Buffer.from('aaaa0').indexOf('30', 'hex'), 4); | |||
| 180 | 187 | assert.strictEqual(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4); | |
| 181 | 188 | ||
| 182 | 189 | { | |
| 183 | - // test usc2 encoding | ||
| 184 | - const twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); | ||
| 185 | - | ||
| 186 | - assert.strictEqual(twoByteString.indexOf('\u0395', 4, 'ucs2'), 8); | ||
| 187 | - assert.strictEqual(twoByteString.indexOf('\u03a3', -4, 'ucs2'), 6); | ||
| 188 | - assert.strictEqual(twoByteString.indexOf('\u03a3', -6, 'ucs2'), 4); | ||
| 189 | - assert.strictEqual(twoByteString.indexOf( | ||
| 190 | - Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2'), 4); | ||
| 191 | - assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2')); | ||
| 190 | + // Test usc2 and utf16le encoding | ||
| 191 | + ['ucs2', 'utf16le'].forEach((encoding) => { | ||
| 192 | + const twoByteString = Buffer.from( | ||
| 193 | + '\u039a\u0391\u03a3\u03a3\u0395', encoding); | ||
| 194 | + | ||
| 195 | + assert.strictEqual(twoByteString.indexOf('\u0395', 4, encoding), 8); | ||
| 196 | + assert.strictEqual(twoByteString.indexOf('\u03a3', -4, encoding), 6); | ||
| 197 | + assert.strictEqual(twoByteString.indexOf('\u03a3', -6, encoding), 4); | ||
| 198 | + assert.strictEqual(twoByteString.indexOf( | ||
| 199 | + Buffer.from('\u03a3', encoding), -6, encoding), 4); | ||
| 200 | + assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, encoding)); | ||
| 201 | + }); | ||
| 192 | 202 | } | |
| 193 | 203 | ||
| 194 | 204 | const mixedByteStringUcs2 = | |
| Back | FazBrowse Home | New Git URL |
0 commit comments