# Tests for the return type of IsingSampler().
# All three sampling methods ("MH", "CFTP", "direct") must return an unnamed
# matrix with n rows and ncol(graph) columns. Integer-valued response options
# yield integer storage; non-integer response options yield double storage.
# Plain stopifnot tests, no package dependencies beyond IsingSampler itself.

library("IsingSampler")

set.seed(4321)

## Small test model -----------------------------------------------------------
N <- 4
graph <- matrix(0, N, N)
graph[upper.tri(graph)] <- runif(N * (N - 1) / 2, -0.5, 1)
graph <- graph + t(graph)
thresholds <- runif(N, -1, 1)

nSamp <- 30

## 1) Integer responses: matrix, no dimnames, integer storage, right dims -----
for (method in c("MH", "CFTP", "direct")) {
  samples <- IsingSampler(nSamp, graph, thresholds, method = method,
                          responses = c(0L, 1L))
  stopifnot(is.matrix(samples))
  stopifnot(is.null(dimnames(samples)))
  stopifnot(storage.mode(samples) == "integer")
  stopifnot(nrow(samples) == nSamp, ncol(samples) == N)
}

## 2) Non-integer responses: matrix, no dimnames, double storage --------------
# CFTP is binary-only; non-integer c(0, 0.5) is still two response options so
# it is valid for all three methods.
for (method in c("MH", "CFTP", "direct")) {
  samples <- IsingSampler(nSamp, graph, thresholds, method = method,
                          responses = c(0, 0.5))
  stopifnot(is.matrix(samples))
  stopifnot(is.null(dimnames(samples)))
  stopifnot(storage.mode(samples) == "double")
  stopifnot(nrow(samples) == nSamp, ncol(samples) == N)
}

## 3) direct method WITH a constrain matrix still returns a proper matrix ------
constrain <- matrix(NA_real_, nSamp, N)
constrain[, 1] <- 1L
samples <- IsingSampler(nSamp, graph, thresholds, method = "direct",
                        responses = c(0L, 1L), constrain = constrain)
stopifnot(is.matrix(samples))
stopifnot(is.null(dimnames(samples)))
stopifnot(storage.mode(samples) == "integer")
stopifnot(nrow(samples) == nSamp, ncol(samples) == N)
stopifnot(all(samples[, 1] == 1L))

cat("All return-type tests passed.\n")
