#install.packages("compare")
library(compare)
source("check.R")
source("testFunctions.R")
options(show.error.messages =TRUE)
curwd = getwd()
data_dir = paste(curwd, "data/", sep="/")

setwd(data_dir)


correct_file = "practical3.R"
stud_ids = list.files(data_dir)


dd = initialiseStructure(stud_ids, correct_file)
##Add student name
stud_names = read.csv("../names.csv", header=FALSE, stringsAsFactors=FALSE,
  colClasses=c("character"))[,c(1,3)]

dd = addStudentName(dd, stud_names)

stud_paths =  paste(getwd(),  stud_names[,1], sep="/")
stud_paths = data.frame(stud_names[,1], stud_paths)
dd = addFilePath(dd, stud_paths)
dd[[1]]

for(id in stud_ids) {
  setwd(paste(data_dir, id, sep="/"))
  dd[[id]] = checkFilename(dd[[id]])
  dd[[id]] = checkFileSource(dd[[id]])
  dd[[id]] = checkAddVariables(dd[[id]])
}

dd = addFunction(dd, "MySD", 1)
dd = addFunction(dd, "MyDifference", 1)
dd = addFunction(dd, "MyMean2", 3)
dd = addFunction(dd, "IsEven", 2)
dd = addFunction(dd, "MyMedian", 2)



for(id in stud_ids) {
  x = c(-5.5, 10, 0,20)
  dd[[id]] = testFunction(dd[[id]], "MyMean", list(x=x), 1, mean(x))
  dd[[id]] = testFunction(dd[[id]], "MySD", list(x=x), 1, sd(x), round=3)
  y = c(-5.5, 0, 0, 10);
  dd[[id]] = testFunction(dd[[id]], "MyDifference", list(x=x, y=y), 1,
       mean(x)-mean(y))
  ##MyMean2
  dd[[id]] = testFunction(dd[[id]], "MyMean2", list(x=x, nonzero=TRUE), 1,
        mean(x[x>0]))
  dd[[id]] = testFunction(dd[[id]], "MyMean2", list(x=x, nonzero=FALSE), 2,
        mean(x))
  dd[[id]] = testFunction(dd[[id]], "MyMean2", list(x=x), 3,
      mean(x[x>0]))

  ##IsEven
  x = c(2, 3, 4)
  dd[[id]] = testFunction(dd[[id]], "IsEven", list(x=x), 1, FALSE)
  dd[[id]] = testFunction(dd[[id]], "IsEven", list(x=c(1,3,4,5)), 2, TRUE)

  ##MyMedian
  x = c(-5, 4, 1);
  dd[[id]] = testFunction(dd[[id]], "MyMedian", list(x=x), 1,median(x))
  x = c(4, -5, -1, 1);
  dd[[id]] = testFunction(dd[[id]], "MyMedian", list(x=x), 2,median(x))
}

dd[[1]]

setwd(data_dir)
setwd("../")
getwd()
save(dd, file="results.RData")


