slice.js 958 B

12345678910111213141516171819202122232425262728293031323334353637
  1. if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
  2. var B = require('../').Buffer
  3. var test = require('tape')
  4. test('modifying buffer created by .slice() modifies original memory', function (t) {
  5. if (!B.TYPED_ARRAY_SUPPORT) return t.end()
  6. var buf1 = new B(26)
  7. for (var i = 0; i < 26; i++) {
  8. buf1[i] = i + 97 // 97 is ASCII a
  9. }
  10. var buf2 = buf1.slice(0, 3)
  11. t.equal(buf2.toString('ascii', 0, buf2.length), 'abc')
  12. buf2[0] = '!'.charCodeAt(0)
  13. t.equal(buf1.toString('ascii', 0, buf2.length), '!bc')
  14. t.end()
  15. })
  16. test('modifying parent buffer modifies .slice() buffer\'s memory', function (t) {
  17. if (!B.TYPED_ARRAY_SUPPORT) return t.end()
  18. var buf1 = new B(26)
  19. for (var i = 0; i < 26; i++) {
  20. buf1[i] = i + 97 // 97 is ASCII a
  21. }
  22. var buf2 = buf1.slice(0, 3)
  23. t.equal(buf2.toString('ascii', 0, buf2.length), 'abc')
  24. buf1[0] = '!'.charCodeAt(0)
  25. t.equal(buf2.toString('ascii', 0, buf2.length), '!bc')
  26. t.end()
  27. })