tests/testthat/test-signingUtils.R

context("Test functions for signing transactions.")

# https://stackoverflow.com/questions/12892348/convert-binary-string-to-binary-or-decimal-value

bitsToInt <- function(x){
  trim_0b = gsub("^(0b)[0]*", "", as.character(x), 2)
  return(sum(2^(which(rev(unlist(strsplit(trim_0b, "")) == 1))-1)))
}

test_that("digits() works as expected.", {

  short = digits(909887)
  medium = digits(1216133879)
  long = digits("123456789012345678901234567890")

  expect_length(short, 6)
  expect_length(medium, 10)
  expect_length(long, 30)

  expect_true(all(short[c(2,4,6)] == c(0,8,7)))
  expect_true(all(medium[c(3,5,8)] == c(1,1,8)))
  expect_true(all(long[c(8,14,26)] == c(8,4,6)))

})

test_that("Padding functionality works.", {
  x = 525600
  y = 3008
  xy = pad_digits(x, y)
  expect_equal(length(xy$x), length(xy$y))
  expect_true(all(xy$x == c(5,2,5,6,0,0)))
  expect_true(all(xy$y == c(0,0,3,0,0,8)))
})

test_that("User-defined bit shifting behave similarly to native R functions.", {
  x = 10001
  # The reason for not using native R functions is that they lose precision with large numbers.
  x0 = bitAND(x, 14)
  x1 = bitAND(x, 15)
  expect_equal(bitsToInt(x0), bitwAnd(x, 14))
  expect_equal(bitsToInt(x1), bitwAnd(x, 15))
})
froocpu/xlm documentation built on May 13, 2019, 4:02 a.m.