string.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var assert = require('assert')
  2. var ref = require('../')
  3. describe('C string', function () {
  4. describe('readCString()', function () {
  5. it('should return "" for a Buffer containing "\\0"', function () {
  6. var buf = new Buffer('\0')
  7. assert.strictEqual('', buf.readCString(0))
  8. })
  9. it('should return "hello" for a Buffer containing "hello\\0world"', function () {
  10. var buf = new Buffer('hello\0world')
  11. assert.strictEqual('hello', buf.readCString(0))
  12. })
  13. it('should throw an Error when reading from the NULL pointer', function () {
  14. assert.throws(function () {
  15. ref.NULL.readCString()
  16. })
  17. })
  18. })
  19. describe('writeCString()', function () {
  20. it('should write a C string (NULL terminated) to a Buffer', function () {
  21. var buf = new Buffer(20)
  22. var str = 'hello world'
  23. buf.writeCString(str)
  24. for (var i = 0; i < str.length; i++) {
  25. assert.equal(str.charCodeAt(i), buf[i])
  26. }
  27. assert.equal(0, buf[str.length])
  28. })
  29. })
  30. describe('allocCString()', function () {
  31. it('should return a new Buffer containing the given string', function () {
  32. var buf = ref.allocCString('hello world')
  33. assert.strictEqual('hello world', buf.readCString())
  34. })
  35. it('should return the NULL pointer for `null` values', function () {
  36. var buf = ref.allocCString(null)
  37. assert(buf.isNull())
  38. assert.strictEqual(0, buf.address())
  39. })
  40. it('should return the NULL pointer for `undefined` values', function () {
  41. var buf = ref.allocCString(undefined)
  42. assert(buf.isNull())
  43. assert.strictEqual(0, buf.address())
  44. })
  45. it('should return the NULL pointer for a NULL pointer Buffer', function () {
  46. var buf = ref.allocCString(ref.NULL)
  47. assert(buf.isNull())
  48. assert.strictEqual(0, buf.address())
  49. })
  50. })
  51. describe('CString', function () {
  52. it('should return JS `null` when given a pointer pointing to NULL', function () {
  53. var buf = ref.alloc(ref.types.CString)
  54. buf.writePointer(ref.NULL)
  55. assert.strictEqual(null, buf.deref())
  56. // another version of the same test
  57. assert.strictEqual(null, ref.get(ref.NULL_POINTER, 0, ref.types.CString))
  58. })
  59. it('should read a utf8 string from a Buffer', function () {
  60. var str = 'hello world'
  61. var buf = ref.alloc(ref.types.CString)
  62. buf.writePointer(new Buffer(str + '\0'))
  63. assert.strictEqual(str, buf.deref())
  64. })
  65. // https://github.com/node-ffi/node-ffi/issues/169
  66. it('should set a Buffer as backing store', function () {
  67. var str = 'hey!'
  68. var store = new Buffer(str + '\0')
  69. var buf = ref.alloc(ref.types.CString)
  70. ref.set(buf, 0, store)
  71. assert.equal(str, ref.get(buf, 0))
  72. })
  73. })
  74. })