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

addr64 = require SRC_DIR + 'Addr64.coffee'
Addr64 = addr64.Addr64
LogCache = (require SRC_DIR + 'LogCache.coffee').LogCache

cache = new Object()

exports.setUp = (callback) ->
  cache = LogCache.create$String$int$int$int '/tmp/', 64, 16, 10
  callback()

exports.testCacheCreation = (test) ->
  test.expect 5
  test.equal cache.directory, '/tmp/'
  test.equal cache.pageSize, 16
  test.equal cache.pageMask.toFloat(), 15
  test.equal cache.fileSize, 64
  test.equal cache.fileMask.toFloat(), 63
  test.done()

exports.testParseAddrZero = (test) ->
  test.expect 4
  cache.parseAddr$Addr64(Addr64.create$int$int 0, 0)
  test.equal cache.pageOffset, 0
  test.equal cache.pageAddr.toFloat(), 0
  test.equal cache.fileOffset, 0
  test.equal cache.fileAddr.toFloat(), 0
  test.done()

exports.testParseAddrInsideFirstPage = (test) ->
  test.expect 4
  cache.parseAddr$Addr64(Addr64.create$int$int 0, 13)
  test.equal cache.pageOffset, 13
  test.equal cache.pageAddr.toFloat(), 0
  test.equal cache.fileOffset, 0
  test.equal cache.fileAddr.toFloat(), 0
  test.done()

exports.testParseAddrInsideThirdPage = (test) ->
  test.expect 4
  cache.parseAddr$Addr64(Addr64.create$int$int 0, 40)
  test.equal cache.pageOffset, 8
  test.equal cache.pageAddr.toFloat(), 32
  test.equal cache.fileOffset, 32
  test.equal cache.fileAddr.toFloat(), 0
  test.done()

exports.testParseAddrInsideSixthPage = (test) ->
  test.expect 4
  cache.parseAddr$Addr64(Addr64.create$int$int 0, 85)
  test.equal cache.pageOffset, 5
  test.equal cache.pageAddr.toFloat(), 80
  test.equal cache.fileOffset, 16
  test.equal cache.fileAddr.toFloat(), 64
  test.done()

exports.testParseAddrBeginOfEighthPage = (test) ->
  test.expect 4
  cache.parseAddr$Addr64(Addr64.create$int$int 0, 112)
  test.equal cache.pageOffset, 0
  test.equal cache.pageAddr.toFloat(), 112
  test.equal cache.fileOffset, 48
  test.equal cache.fileAddr.toFloat(), 64
  test.done()

