SRC_DIR = '../../src/log/'

fs = require 'fs'
utils = require '../TestUtils.coffee'
Addr64 = (require SRC_DIR + 'Addr64.coffee').Addr64
LogCache = (require SRC_DIR + 'LogCache.coffee').LogCache
LogUtil = (require SRC_DIR + 'LogUtil.coffee').LogUtil
DataIterator = (require SRC_DIR + 'DataIterator.coffee').DataIterator

it = new Object()
nullAddr = Addr64.create$int$int 0, 0

exports.setUp = (callback) ->
  path = utils.getNewDirName()
  try
    fs.mkdirSync path, '0777'
  catch e
  cache = LogCache.create$String$int$int$int path, 1024, 16, 4
  b = new Buffer 1024
  b.fill 0
  b.write "My first test file string for testing how cache reads files"
  f = "00000000000"
  fd = fs.openSync path + f + LogUtil.LOG_FILE_EXTENSION, 'w'
  fs.writeSync fd, b, 0, b.length, 0
  fs.closeSync fd
  it = DataIterator.create$LogCache$Addr64 cache, Addr64.create$int$int 0, 0
  callback()

exports.testCreation = (test) ->
  test.expect 3
  test.ok it.pageAddr.equals$Addr64 nullAddr
  test.equal it.offset, 0
  test.equal it.buffer, undefined
  test.done()

exports.testNext = (test) ->
  test.expect 21 * 2
  for i in [0..15]
    test.ok it.pageAddr.equals$Addr64 nullAddr
    test.equal it.offset, i
    it.moveNext()
  for i in [0..3]
    test.ok it.pageAddr.equals$Addr64 nullAddr.plus$int(16)
    test.equal it.offset, i
    it.moveNext()
  test.ok it.pageAddr.equals$Addr64 nullAddr.plus$int(16)
  test.equal it.offset, 4
  test.done()

exports.testSkip = (test) ->
  test.expect 5 * 3
  test.equal 10, it.skip$int 10
  test.ok it.pageAddr.equals$Addr64 nullAddr
  test.equal it.offset, 10
  test.equal 6, it.skip$int 6
  test.ok it.pageAddr.equals$Addr64 nullAddr.plus$int(16)
  test.equal it.offset, 0
  test.equal 7, it.skip$int 7
  test.ok it.pageAddr.equals$Addr64 nullAddr.plus$int(16)
  test.equal it.offset, 7
  test.equal 30, it.skip$int 30
  test.ok it.pageAddr.equals$Addr64 nullAddr.plus$int(48)
  test.equal it.offset, 5
  test.equal (1024 - 48 - 5), it.skip$int 1024
  test.ok it.pageAddr.equals$Addr64 nullAddr.plus$int(1024)
  test.equal it.offset, 0
  test.done()

exports.testCopy = (test) ->
  test.expect 3
  it.skip$int 10
  itNew = it.copy()
  test.ok itNew.pageAddr.equals$Addr64 nullAddr
  test.equal itNew.offset, 10
  test.equal itNew.buffer, undefined
  test.done()

exports.testGetValue = (test) ->
  test.expect 34
  itNew = it.copy()
  text = "My first test file string for testing how cache reads files"
  itNew.once 'next', (value) =>
    test.equal value, text.charCodeAt 0
    for i in [1..15]
      test.equal itNew.next$emit(), text.charCodeAt i
    itNew.once 'next', (value) =>
      test.equal value, text.charCodeAt 16
      for i in [17..31]
        test.equal itNew.next$emit(), text.charCodeAt i
      test.done()
    test.equal itNew.next$emit(), undefined
  test.equal itNew.next$emit(), undefined

exports.testGetData = (test) ->
  test.expect 5
  itNew = it.copy()
  itNew.skip$int 26
  itNew.once 'getData', (data) =>
    test.equal data.toString(), "for testing"
    itNew.skip$int 12
    test.equal itNew.getData$int$emit(3).toString(), "how"
    itNew.skip$int 4
    itNew.once 'getData', (data) =>
      test.equal data.toString(), "cache reads files"
      test.done()
    test.equal itNew.getData$int$emit(17), undefined
  test.equal itNew.getData$int$emit(11), undefined
