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

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

a = new Object()
b = new Object()
c = new Object()
d = new Object()

exports.setUp = (callback) ->
  a = Addr64.create$int$int 0, 0
  b = Addr64.create$int$int 8, 17
  c = Addr64.create$int$int 0, 15
  d = Addr64.create$int$int 3, 35
  callback()

exports.testToFloat = (test) ->
  test.expect 12
  test.equal a.left, 0
  test.equal a.right, 0
  test.equal a.toFloat(), 0
  test.equal b.left, 8
  test.equal b.right, 17
  test.equal b.toFloat(), (Math.pow 2, 35) + 17
  test.equal c.left, 0
  test.equal c.right, 15
  test.equal c.toFloat(), 15
  test.equal d.left, 3
  test.equal d.right, 35
  test.equal d.toFloat(), (Math.pow 2, 33) + (Math.pow 2, 32) + 35
  test.done()

exports.testDiff = (test) ->
  test.expect 5
  test.equal b.diff$Addr64(a), (Math.pow 2, 35) + 17
  test.equal c.diff$Addr64(a), 15
  test.equal d.diff$Addr64(a), (Math.pow 2, 33) + (Math.pow 2, 32) + 35
  test.equal b.diff$Addr64(c), (Math.pow 2, 35) + 2
  test.equal b.diff$Addr64(d), (Math.pow 2, 34) + (Math.pow 2, 32) + (17 - 35)
  test.done()

exports.testAdditionPositive = (test) ->
  test.expect 9
  a = a.plus$int(10)
  test.equal a.left, 0
  test.equal a.right, 10
  test.equal a.toFloat(), 10
  a = a.plus$int(Math.pow 2, 32).plus$int(1)
  test.equal a.left, 1
  test.equal a.right, 11
  test.equal a.toFloat(), (Math.pow 2, 32) + 11
  a = a.plus$int(Math.pow 2, 35).plus$int(17)
  test.equal a.left, 9
  test.equal a.right, 28
  test.equal a.toFloat(), (Math.pow 2, 35) + (Math.pow 2, 32) + 28
  test.done()

exports.testAdditionNegative = (test) ->
  test.expect 9
  b = b.plus$int(-10)
  test.equal b.left, 8
  test.equal b.right, 7
  test.equal b.toFloat(), (Math.pow 2, 35) + 7
  b = b.plus$int(-((Math.pow 2, 33)))
  test.equal b.left, 6
  test.equal b.right, 7
  test.equal b.toFloat(), (Math.pow 2, 34) + (Math.pow 2, 33) + 7
  b = b.plus$int(-20)
  test.equal b.left, 5
  test.equal b.right, (Math.pow 2, 32) - 13
  test.equal b.toFloat(), (Math.pow 2, 34) + (Math.pow 2, 33) - 13
  test.done()

exports.testAnd = (test) ->
  test.expect 3
  a = a.plus$int(Math.pow 2, 35).plus$int(31)
  test.equal a.logAnd$Addr64(b).toFloat(), (Math.pow 2, 35) + 17
  test.equal a.logAnd$Addr64(c).toFloat(), 15
  test.equal a.logAnd$Addr64(d).toFloat(), 3
  test.done()

exports.testAddr64ToString = (test) ->
  test.expect 4
  test.equal Addr64.addr64ToString$Addr64$int(a, 36), "0000000000000"
  a = a.plus$int(17)
  test.equal Addr64.addr64ToString$Addr64$int(a, 36), "000000000000h"
  a = a.plus$int(Math.pow 2, 35)
  test.equal Addr64.addr64ToString$Addr64$int(a, 36), "000000fs8wftd"
  a = a.plus$int(Math.pow 2, 33).plus$int(267)
  test.equal Addr64.addr64ToString$Addr64$int(a, 36), "000000jqb4jz0"
  test.done()

exports.testStringToAddr64 = (test) ->
  test.expect 4
  str = "0000000000000"
  test.equal Addr64.stringToAddr64$String$int(str, 36).toFloat(), 0
  str = "000000000000h"
  test.equal Addr64.stringToAddr64$String$int(str, 36).toFloat(), 17
  str = "000000fs8wftd"
  test.equal Addr64.stringToAddr64$String$int(str, 36).toFloat(),
               (Math.pow 2, 35) + 17
  str = "000000jqb4jz0"
  test.equal Addr64.stringToAddr64$String$int(str, 36).toFloat(),
          (Math.pow 2, 35) + (Math.pow 2, 33) + 17 + 267
  test.done()