1test_that("output test", {
2  expect_snapshot({
3    tibble::tibble(
4      x0 = num(9:11 * 100 + 0.5, sigfig = 3),
5      x1 = num(9:11 * 100 + 0.5, sigfig = 4),
6      x2 = num(9:11 * 100 + 0.5, sigfig = 5),
7    )
8
9    # Maximum digits after the decimal points
10    tibble::tibble(
11      x3 = num(9:11 * 100 + 0.5, digits = 0),
12      x4 = num(9:11 * 100 + 0.5, digits = -1),
13      x5 = num(9:11 * 100 + 0.5, digits = -2),
14    )
15
16    # Exact digits after the decimal point, with label
17    tibble::tibble(
18      usd = num(9:11 * 100 + 0.5, digits = 2, label = "USD"),
19      gbp = num(9:11 * 100 + 0.5, digits = 2, label = "£"),
20      chf = num(9:11 * 100 + 0.5, digits = 2, label = "SFr")
21    )
22
23    # Scale
24    tibble::tibble(
25      small  = num(9:11 / 1000 + 0.00005, label = "%", scale = 100),
26      medium = num(9:11 /  100 + 0.0005 , label = "%", scale = 100),
27      large  = num(9:11 /   10 + 0.005  , label = "%", scale = 100)
28    )
29
30    # Notation
31    tibble::tibble(
32      sci = num(10^(-13:6), notation = "sci"),
33      eng = num(10^(-13:6), notation = "eng"),
34      dec = num(10^(-13:6), notation = "dec"),
35      si  = num(10^(-13:6), notation = "si"),
36    )
37
38    # Fixed exponent notation
39    tibble::tibble(
40      scimin = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = -Inf),
41      engmin = num(10^(-7:6) * 123, notation = "eng", fixed_exponent = -Inf),
42      simin  = num(10^(-7:6) * 123, notation = "si",  fixed_exponent = -Inf)
43    )
44
45    tibble::tibble(
46      scismall = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = -3),
47      scilarge = num(10^(-7:6) * 123, notation = "eng", fixed_exponent = 3),
48      scimax   = num(10^(-7:6) * 123, notation = "si",  fixed_exponent = Inf)
49    )
50
51    # Extra significant figures
52    tibble::tibble(
53      default = num(100 + 1:3 * 0.001),
54      extra1 = num(100 + 1:3 * 0.001, extra_sigfig = TRUE),
55      extra2 = num(100 + 1:3 * 0.0001, extra_sigfig = TRUE),
56      extra3 = num(10000 + 1:3 * 0.00001, extra_sigfig = TRUE)
57    )
58  })
59})
60
61test_that("many digits", {
62  expect_snapshot({
63    num(123456789 * 10 ^ (-9:0))
64    num(123456789 * 10 ^ (-9:1))
65    num(123456789 * 10 ^ (-9:1), notation = "dec")
66    num(123456789 * 10 ^ (-9:1), notation = "sci")
67    num(123456789 * 10 ^ (-9:1), notation = "eng")
68    num(123456789 * 10 ^ (-9:1), notation = "si")
69    num(123456789 * 10 ^ (-9:1), notation = "sci", fixed_exponent = -Inf)
70    num(123456789 * 10 ^ (-9:1), notation = "eng", fixed_exponent = -Inf)
71    num(123456789 * 10 ^ (-9:1), notation = "si", fixed_exponent = -Inf)
72    num(123456789 * 10 ^ (-9:1), notation = "sci", fixed_exponent = -3)
73    num(123456789 * 10 ^ (-9:1), notation = "sci", fixed_exponent = 3)
74    num(123456789 * 10 ^ (-9:1), notation = "sci", fixed_exponent = Inf)
75  })
76})
77
78test_that("sigfig and digits", {
79  expect_snapshot({
80    num(c(578890.23, 240234.131, 40234.1))
81    num(c(578890.23, 240234.131, 40234.1), sigfig = 6)
82    num(c(578890.23, 240234.131, 40234.1), sigfig = 7)
83    num(c(578890.23, 240234.131, 40234.1), sigfig = 8)
84    num(c(578890.23, 240234.131, 40234.1), sigfig = 9)
85    num(c(578890.23, 240234.131, 40234.1), digits = 2)
86    num(c(578890.23, 240234.131, 40234.1), digits = 3)
87    num(c(578890.23, 240234.131, 40234.1), digits = 4)
88    num(c(578890.23, 240234.131, 40234.1), digits = -2)
89    num(c(578890.23, 240234.131, 40234.1), digits = -3)
90    num(c(578890.23, 240234.131, 40234.1), digits = -4)
91  })
92})
93
94test_that("forced digits", {
95  expect_snapshot({
96    pillar(num(1:3, digits = 2))
97    pillar(num(1:3, digits = 5))
98  })
99})
100
101test_that("all NA", {
102  expect_snapshot({
103    pillar(num(NA_real_, digits = 2))
104    pillar(num(NA_real_, notation = "si"))
105    pillar(num(NA_real_, notation = "sci"))
106    pillar(num(NA_real_, notation = "eng"))
107    pillar(num(NA_real_, notation = "sci", fixed_exponent = -1))
108    pillar(num(NA_real_, notation = "sci", fixed_exponent = -Inf))
109  })
110})
111
112test_that("some NA", {
113  expect_snapshot({
114    pillar(num(c(NA_real_, 1000), digits = 2))
115    pillar(num(c(NA_real_, 1000), notation = "si"))
116    pillar(num(c(NA_real_, 1000), notation = "sci"))
117    pillar(num(c(NA_real_, 1000), notation = "eng"))
118    pillar(num(c(NA_real_, 1000), notation = "sci", fixed_exponent = -1))
119    pillar(num(c(NA_real_, 1000), notation = "sci", fixed_exponent = -Inf))
120  })
121})
122
123test_that("coercion", {
124  expect_identical(
125    vec_c(num(1), num(2)),
126    num(as.numeric(1:2))
127  )
128  expect_identical(
129    vec_c(num(1, sigfig = 4), num(2, sigfig = 2, notation = "si")),
130    num(as.numeric(1:2), sigfig = 4)
131  )
132  expect_identical(
133    vec_c(num(1, label = "a"), 2),
134    num(as.numeric(1:2), label = "a")
135  )
136  expect_identical(
137    vec_c(1, num(2, label = "a")),
138    num(as.numeric(1:2), label = "a")
139  )
140  expect_error(
141    vec_c(num(1, label = "a"), num(2, label = "b"))
142  )
143})
144
145test_that("arithmetics", {
146  expect_snapshot({
147    num(1) + 2
148    1 + num(2)
149    1L + num(2)
150    num(3.23456, sigfig = 4) - num(2)
151    num(3, digits = 2) * num(4, sigfig = 2)
152    -num(2)
153  })
154})
155
156test_that("mathematics", {
157  expect_snapshot({
158    min(num(1:3, label = "$"))
159    mean(num(1:3, notation = "eng"))
160    sin(num(1:3, label = "%", scale = 100))
161  })
162})
163
164test_that("formatting", {
165  expect_snapshot({
166    format(num(-1:3))
167    format(num(-1:3), trim = TRUE)
168  })
169})
170
171test_that("attribute", {
172  expect_snapshot({
173    set_num_opts(1, sigfig = 2, fixed_exponent = -Inf)
174    set_num_opts(1000, digits = 2, notation = "eng")
175  })
176})
177