test_that("how to store data for multiple runs", {

    skip("WER")
    nReps <- 4
    K <- 100    
    nSNPs <- 10000

    ## so array option seems fine
    ## fast column access is dosage(iSNP) += arma::sum(gamma_t.col(iSNP) % eHapsCurrent_tc.slice(iRep).col(iSNP));

    eHapsCurrent_tc <- array(runif(nSNPs * K * nReps), c(K, nSNPs, nReps))
    list_of_eHapsCurrent_t <- lapply(1:nReps, function(iRep) {
        array(runif(nSNPs * K), c(K, nSNPs))
    })
    eHapsCurrent_t_input <- array(runif(nSNPs * K), c(K, nSNPs))
    gamma_t <- array(runif(nSNPs * K), c(K, nSNPs))
    
    library("microbenchmark")
    m <- microbenchmark(
        "1" = test_options(option = "1", eHapsCurrent_tc, list_of_eHapsCurrent_t, eHapsCurrent_t_input, gamma_t, nSNPs, K, nReps),
        "1B" = test_options(option = "1B", eHapsCurrent_tc, list_of_eHapsCurrent_t, eHapsCurrent_t_input, gamma_t, nSNPs, K, nReps),
        "1BB" = test_options(option = "1BB", eHapsCurrent_tc, list_of_eHapsCurrent_t, eHapsCurrent_t_input, gamma_t, nSNPs, K, nReps),         
        "1C" = test_options(option = "1C", eHapsCurrent_tc, list_of_eHapsCurrent_t, eHapsCurrent_t_input, gamma_t, nSNPs, K, nReps),
        "2" = test_options(option = "2", eHapsCurrent_tc, list_of_eHapsCurrent_t, eHapsCurrent_t_input, gamma_t, nSNPs, K, nReps),
        "3" = test_options(option = "3", eHapsCurrent_tc, list_of_eHapsCurrent_t, eHapsCurrent_t_input, gamma_t, nSNPs, K, nReps),
        "4" = test_options(option = "4", eHapsCurrent_tc, list_of_eHapsCurrent_t, eHapsCurrent_t_input, gamma_t, nSNPs, K, nReps),
        "4B" = test_options(option = "4B", eHapsCurrent_tc, list_of_eHapsCurrent_t, eHapsCurrent_t_input, gamma_t, nSNPs, K, nReps)        ,
        "4C" = test_options(option = "4C", eHapsCurrent_tc, list_of_eHapsCurrent_t, eHapsCurrent_t_input, gamma_t, nSNPs, K, nReps)
    )
    print(m)

})


test_that("faster forward haploid maybe", {

    skip("WER")
    
    for(gridWindowSize in c(NA, 100)) {
    test_package <- make_fb_test_package(
        K = 100,
        nReads = 1000,
        nSNPs = 10000,
        gridWindowSize = gridWindowSize
    )
    nSNPs <- test_package$nSNPs
    nGrids <- test_package$nGrids
    K <- test_package$K
    transMatRate_t_H <- test_package$transMatRate_t_H
    alphaMatCurrent_t <- test_package$alphaMatCurrent_t
    eMatHapSNP_t <- test_package$eMatHapSNP_t
    priorCurrent <- test_package$priorCurrent

    alphaHat_t <- array(0, c(K, nGrids))
    c <- array(0, nGrids)
    Rcpp_run_forward_haploid(
        alphaHat_t = alphaHat_t,
        c = c,
        eMatHapSNP_t = eMatHapSNP_t,
        alphaMat_t = alphaMatCurrent_t,
        transMatRate_t_H = transMatRate_t_H,
        T = nGrids,
        K = K,
        pi = priorCurrent,
        alphaStart = array(0, 100),
        run_fb_subset = FALSE
    )
    
    ## 
    ## c++ - pass by reference
    ##
    f1 <- function() {
        alphaHat_t <- array(0, c(K, nGrids))
        c <- array(0, nGrids)
        Rcpp_run_forward_haploid(
            alphaHat_t = alphaHat_t,
            c = c,
            eMatHapSNP_t = eMatHapSNP_t,
            alphaMat_t = alphaMatCurrent_t,
            transMatRate_t_H = transMatRate_t_H,
            T = nGrids,
            K = K,
            pi = priorCurrent,
            alphaStart = array(0, 100),
            run_fb_subset = FALSE
        )
        return(alphaHat_t)
    }
    f2 <- function() {
        alphaHat_t <- array(0, c(K, nGrids))
        c <- array(0, nGrids)
        Rcpp_run_forward_haploid_test(
            alphaHat_t = alphaHat_t,
            c = c,
            eMatHapSNP_t = eMatHapSNP_t,
            alphaMat_t = alphaMatCurrent_t,
            transMatRate_t_H = transMatRate_t_H,
            T = nGrids,
            K = K,
            pi = priorCurrent,
            alphaStart = array(0, 100),
            run_fb_subset = FALSE
        )
        return(alphaHat_t)
    }
    expect_equal(f1(), f2())

    b1 <- function() {
        betaHat_t <- array(0, c(K, nGrids))
        Rcpp_run_backward_haploid(
            betaHat_t = betaHat_t,
            c = c,
            eMatHapSNP_t = eMatHapSNP_t,
            alphaMat_t = alphaMatCurrent_t,    
            transMatRate_t_H = transMatRate_t_H
        )
        return(betaHat_t)
    }
    b2 <- function() {
        betaHat_t <- array(0, c(K, nGrids))
        Rcpp_run_backward_haploid_test(
            betaHat_t = betaHat_t,
            c = c,
            eMatHapSNP_t = eMatHapSNP_t,
            alphaMat_t = alphaMatCurrent_t,    
            transMatRate_t_H = transMatRate_t_H
        )
        return(betaHat_t)
    }
    expect_equal(b1(), b2())
    
    
    library("microbenchmark")
    ## m <- microbenchmark(f1(), f2(), f3(), unit = "us")
    m <- microbenchmark(b1(), b2(), unit = "us")    
    print(paste0("-----------------gridWindowSize = ", gridWindowSize))
    print(nGrids)
    print(m)

    
    }
    
})
