R Under development (unstable) (2024-08-23 r87049 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ################################################################################ > ## > ## $Id: portfolio.calcShares.test.R 1625 2010-02-18 19:44:29Z enos $ > ## > ## Tests "calcShares" method of "portfolio" > ## > ################################################################################ > > library(portfolio) Loading required package: grid Loading required package: lattice > > load("portfolio.calcShares.test.RData") > > ## save(truth, file = "portfolio.calcShares.test.RData", compress = TRUE) > > ## constructs a usable data.frame and portfolio > > data.0 <- data.frame(id = 1:20, symbol.var = letters[1:20], in.var = 1:20, + ret.var = 1:20, price.var = 1:20) > p.0 <- new("portfolio", id.var = "id", symbol.var = "symbol.var", + in.var = "in.var", ret.var = "ret.var", type = "equal", + size = "quintile", equity = 100000, + price.var = "price.var", data = data.0) > > ## tests output against precalculated values > > shares <- calcShares(p.0)@shares > shares <- shares[order(as.numeric(shares$id)),] > stopifnot(all.equal(truth, shares$shares)) > > ## Invalid Input: "price.var" not in names(data) > > p.0@price.var <- "foo" > > trial.0 <- try( + calcShares(p.0), + silent = TRUE + ) > > if(class(trial.0) == "try-error"){ + stopifnot(isTRUE(as.logical(grep("Error.*valid price\\.var!", trial.0[1])))) + } > > ## Corner Case: shares slot has 0 rows > > p.0@weights <- p.0@weights[0,] > > ## "calcShares" returns "object" when shares slot > ## has 0 rows > > stopifnot(class(calcShares(p.0)) == "portfolio") > > proc.time() user system elapsed 0.53 0.10 0.64