1+ # -------------------------------------------------------------------------------
2+ # Copyright (c) 2019-2022 University of Newcastle upon Tyne. All rights reserved.
3+ # Copyright (c) 2022-2025 Arjuna Technologies, Newcastle upon Tyne. All rights reserved.
4+ #
5+ # This program and the accompanying materials
6+ # are made available under the terms of the GNU Public License v3.0.
7+ #
8+ # You should have received a copy of the GNU General Public License
9+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
10+ # -------------------------------------------------------------------------------
11+
12+ #
13+ # Set up
14+ #
15+
16+ # context("meanSdGpDS::smk::setup")
17+
18+ set.standard.disclosure.settings()
19+
20+ #
21+ # Tests
22+ #
23+
24+ # context("meanSdGpDS::smk::numeric by factor")
25+ test_that(" simple meanSdGpDS, numeric by factor" , {
26+ x_var <- c(1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 , 9.0 , 10.0 )
27+ index_var <- as.factor(c(" A" , " A" , " A" , " A" , " A" , " B" , " B" , " B" , " B" , " B" ))
28+
29+ res <- meanSdGpDS(" x_var" , " index_var" )
30+
31+ expect_equal(class(res ), " list" )
32+ expect_true(res $ Table_valid )
33+ expect_equal(res $ Nvalid , 10 )
34+ expect_equal(res $ Nmissing , 0 )
35+ expect_equal(res $ Ntotal , 10 )
36+ expect_equal(res $ Mean_gp [[" A" ]], 3.0 )
37+ expect_equal(res $ Mean_gp [[" B" ]], 8.0 )
38+ })
39+
40+ test_that(" meanSdGpDS throws error when X does not exist" , {
41+ index_var <- as.factor(c(" A" , " A" , " B" , " B" ))
42+ expect_error(meanSdGpDS(" nonexistent_x" , " index_var" ), regexp = " does not exist" )
43+ })
44+
45+ test_that(" meanSdGpDS throws error when INDEX does not exist" , {
46+ x_var <- c(1.0 , 2.0 , 3.0 , 4.0 )
47+ expect_error(meanSdGpDS(" x_var" , " nonexistent_index" ), regexp = " does not exist" )
48+ })
49+
50+ test_that(" meanSdGpDS throws error when X is not numeric or integer" , {
51+ bad_x <- c(" a" , " b" , " c" , " d" )
52+ index_var <- as.factor(c(" A" , " A" , " B" , " B" ))
53+ expect_error(meanSdGpDS(" bad_x" , " index_var" ), regexp = " must be of type numeric or integer" )
54+ })
55+
56+ #
57+ # Done
58+ #
59+
60+ # context("meanSdGpDS::smk::shutdown")
61+
62+ # context("meanSdGpDS::smk::done")
0 commit comments