Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/glacials/splits-io. a speedrunning data store and analysis engine splits.io
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
var assert = require('assert')var ref = require('../')
describe('C string', function () {
describe('readCString()', function () {
it('should return "" for a Buffer containing "\\0"', function () { var buf = new Buffer('\0') assert.strictEqual('', buf.readCString(0)) })
it('should return "hello" for a Buffer containing "hello\\0world"', function () { var buf = new Buffer('hello\0world') assert.strictEqual('hello', buf.readCString(0)) })
it('should throw an Error when reading from the NULL pointer', function () { assert.throws(function () { ref.NULL.readCString() }) })
})
describe('writeCString()', function () {
it('should write a C string (NULL terminated) to a Buffer', function () { var buf = new Buffer(20) var str = 'hello world' buf.writeCString(str) for (var i = 0; i < str.length; i++) { assert.equal(str.charCodeAt(i), buf[i]) } assert.equal(0, buf[str.length]) })
})
describe('allocCString()', function () {
it('should return a new Buffer containing the given string', function () { var buf = ref.allocCString('hello world') assert.strictEqual('hello world', buf.readCString()) })
it('should return the NULL pointer for `null` values', function () { var buf = ref.allocCString(null) assert(buf.isNull()) assert.strictEqual(0, buf.address()) })
it('should return the NULL pointer for `undefined` values', function () { var buf = ref.allocCString(undefined) assert(buf.isNull()) assert.strictEqual(0, buf.address()) })
it('should return the NULL pointer for a NULL pointer Buffer', function () { var buf = ref.allocCString(ref.NULL) assert(buf.isNull()) assert.strictEqual(0, buf.address()) })
})
describe('CString', function () {
it('should return JS `null` when given a pointer pointing to NULL', function () { var buf = ref.alloc(ref.types.CString) buf.writePointer(ref.NULL) assert.strictEqual(null, buf.deref())
// another version of the same test assert.strictEqual(null, ref.get(ref.NULL_POINTER, 0, ref.types.CString)) })
it('should read a utf8 string from a Buffer', function () { var str = 'hello world' var buf = ref.alloc(ref.types.CString) buf.writePointer(new Buffer(str + '\0')) assert.strictEqual(str, buf.deref()) })
// https://github.com/node-ffi/node-ffi/issues/169 it('should set a Buffer as backing store', function () { var str = 'hey!' var store = new Buffer(str + '\0') var buf = ref.alloc(ref.types.CString) ref.set(buf, 0, store)
assert.equal(str, ref.get(buf, 0)) })
})
})