File: test_unit_creation.R

package info (click to toggle)
r-cran-units 0.8-7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,172 kB
  • sloc: xml: 2,437; cpp: 211; sh: 13; makefile: 2
file content (171 lines) | stat: -rw-r--r-- 4,483 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

test_that("parse_units() backticks strings correctly", {

  x <- matrix(ncol = 2, byrow = TRUE, c(
    "in",      "`in`",
    "`in`",    "`in`",
    "kelvin",  "`kelvin`",
    "%",       "`%`",
    "T",       "`T`",
    "'/s",     "`'`/`s`",
    "'",       "`'`" ,
    '"',       '`"`' ,
    '"/s' ,    '`"`/`s`',
    "s/'" ,    "`s`/`'`"  ,
    "C" ,      "`C`" ,
    "F" ,      "`F`",
    "\u00B0C", "`\u00B0C`",
    "\u2103" , "`\u2103`",
    "\u00B0F", "`\u00B0F`",
    "\u2109",  "`\u2109`",
    "log(ug)", "`log`(`ug`)",
    "log(ug/l)", "`log`(`ug`/`l`)",
    "kg*m/s^2", "`kg`*`m`/`s`^2"
  ))
  colnames(x) <- c("input", "expected_output")

  expect_identical(units:::backtick(x[,"input"]), x[,"expected_output"])
})

test_that("explicit exponents identified correctly", {
  expect_true( are_exponents_implicit("m s") )
  expect_true( are_exponents_implicit("m2") )
  expect_true( are_exponents_implicit("m-2") )
  expect_true( are_exponents_implicit("2 m") )
  expect_true( are_exponents_implicit("m s-2") )
  expect_true( are_exponents_implicit("m s-2 kg") )
  expect_true( are_exponents_implicit("2 m s") )

  expect_false( are_exponents_implicit("m") )
  expect_false( are_exponents_implicit("m/s") )
  expect_false( are_exponents_implicit("m^2") )
  expect_false( are_exponents_implicit("m*s") )
})

test_that("global options are respected", {
  # rt: round trip
  rt <- function(x) as.character(units(as_units(x)))

  expect_equal("g", rt("grams"))

  #op <- units_options(auto_convert_names_to_symbols = FALSE)
  units_options(auto_convert_names_to_symbols = FALSE)
  expect_equal("grams", rt("grams"))

  units_options(auto_convert_names_to_symbols = TRUE)
  expect_equal("g", rt("grams"))

  #units_options(op)
  expect_equal("g", rt("grams"))

  o = units_options("set_units_mode")
  g = set_units(1, g)
  units_options(set_units_mode = "standard")
  expect_equal(set_units(1, "g"), g)
  units_options(set_units_mode = o)

})


test_that("various forms of unit creation and destruction work", {

  #ox <- x <- 1L:4L # why should ints be preserved?
  ox <- x <- as.numeric(1:4)

  units(x) <- "m/s"
  expect_s3_class(x,"units")

  units(x) <- NULL
  expect_identical(x, ox)

  units(x) <- character(0)
  expect_identical(x, ox)

  ox <- x <- matrix(ox, ncol = 2)

  units(x) <- "m/s"
  expect_s3_class(x,"units")

  units(x) <- NULL
  expect_identical(x, ox)

  units(x) <- character(0)
  expect_identical(x, ox)

  ox <- y <- x <- as.numeric(1:4)
  units(x) <- "m/s"

  expect_identical(x, set_units(y, m/s))
  expect_identical(x, set_units(y, "m/s", mode = "standard"))

  expect_identical(ox, set_units(x, NULL))
  expect_identical(ox, set_units(x, NULL, mode = "standard"))


  ox <- x <- as.numeric(1:4)

  units(x) <- "m/s"
  expect_s3_class(x, "units")

  x <- drop_units(x)
  expect_identical(x, ox)
  expect_error(drop_units(x))


  ox <- x <- data.frame(x=ox, y=ox)

  units(x[[1]]) <- "m/s"
  expect_s3_class(x[[1]], "units")

  x <- drop_units(x)
  expect_identical(x, ox)


  meter <- units:::symbolic_unit("m")

  expect_identical(meter, make_units(m))
  expect_identical(meter, as_units("m"))
  expect_identical(meter, as_units(quote(m)))
  expect_identical(meter, as_units(expression(m)))

  meter_per_sec <- meter/units:::symbolic_unit("s")

  expect_identical(meter_per_sec, make_units(m/s))
  expect_identical(meter_per_sec, as_units("m/s"))
  expect_identical(meter_per_sec, as_units(" m / s "))
  expect_identical(meter_per_sec, as_units(quote(m/s)))
  expect_identical(meter_per_sec, as_units(expression(m/s)))
  expect_identical(meter_per_sec, as_units("m s-1"))

})

test_that("unitless objects can be created", {
  unit <- 1.0
  units(unit) <-  units:::unitless

  xul <- x <- as.numeric(2:5)
  units(xul) <-  units:::unitless


  expect_identical(unit, make_units())
  expect_identical(unit, make_units(1))

  expect_identical(unit, as_units(1))
  expect_identical(unit, as_units("1"))

  expect_identical(xul, set_units(x, 1))
  expect_identical(xul, set_units(x))
  expect_identical(xul, set_units(x, "1"))
  expect_identical(xul, set_units(x, "1", mode = "standard"))
  expect_identical(xul, set_units(x, mode = "standard"))
})

test_that("set_units default enforces NSE", {
  expect_error(set_units(1:3, as_units("m")))
  expect_error(set_units(1:3, as_units("m/s")))
  expect_error(set_units(1:3, make_units(m)))
  expect_error(set_units(1:3, make_units(m/s)))

  # is it bad if this works?
  # expect_error(set_units(1:3, "m/s"))
})