forked from tidyverse/readr
/
test-parsing-numeric.R
97 lines (71 loc) · 3.16 KB
/
test-parsing-numeric.R
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
context("Parsing, numeric")
es_MX <- locale("es", decimal_mark = ",")
test_that("non-numeric integer/double matches fail", {
expect_equal(n_problems(parse_double("d")), 1)
expect_equal(n_problems(parse_integer("d")), 1)
})
test_that("partial integer/double matches fail", {
expect_equal(n_problems(parse_double("3d")), 1)
expect_equal(n_problems(parse_integer("3d")), 1)
})
test_that("parse functions converts NAs", {
expect_equal(parse_double(c("1.5", "NA")), c(1.5, NA))
})
test_that("leading/trailing ws ignored when parsing", {
expect_equal(parse_double(c(" 1.5", "1.5", "1.5 ")), rep(1.5, 3))
expect_equal(read_csv("x\n 1.5\n1.5\n1.5 \n")$x, rep(1.5, 3))
})
test_that("lone - or decimal marks are not numbers", {
expect_equal(collector_guess("-"), "character")
expect_equal(collector_guess("."), "character")
expect_equal(collector_guess(",", locale = es_MX), "character")
expect_equal(n_problems(parse_numeric(c(".", "-"))), 2)
})
test_that("Numbers with trailing characters are parsed as characters", {
expect_equal(collector_guess("13T"), "character")
expect_equal(collector_guess(collector_guess(c("13T", "13T", "10N"))), "character")
})
# Leading zeros -----------------------------------------------------------
test_that("leading zeros are not numbers", {
expect_equal(collector_guess("0"), "integer")
expect_equal(collector_guess("0."), "double")
expect_equal(collector_guess("0001"), "character")
})
# Flexible number parsing -------------------------------------------------
test_that("col_number only takes first number", {
expect_equal(parse_number("XYZ 123,000 BLAH 456"), 123000)
})
test_that("col_number helps with currency", {
expect_equal(parse_number("$1,000,000.00"), 1e6)
expect_equal(parse_number("$1.000.000,00", locale = es_MX), 1e6)
})
test_that("invalid numbers don't parse", {
expect_warning(x <- parse_number(c("..", "--", "3.3.3", "4-1")))
expect_equal(n_problems(x), 4)
expect_equal(is.na(x), c(TRUE, TRUE, TRUE, TRUE))
})
# Decimal comma -----------------------------------------------------------
test_that("parse_vector passes along decimal_mark", {
expect_equal(parse_double("1,5", locale = es_MX), 1.5)
})
test_that("type_convert passes along decimal_mark", {
df <- data.frame(x = "1,5", stringsAsFactors = FALSE)
out <- type_convert(df, locale = es_MX)
expect_equal(out$x, 1.5)
})
test_that("read_tsv passes on decimal_mark", {
out <- read_tsv("x\n1,5", locale = es_MX)
expect_equal(out$x, 1.5)
})
# Negative numbers -----------------------------------------------------------
test_that("negative numbers return negative values", {
expect_equal(parse_number("-2"), -2)
expect_equal(parse_number("-100,000.00"), -100000)
})
# Large numbers -------------------------------------------------------------
test_that("large numbers are parsed properly", {
expect_equal(parse_double("100000000000000000000"), 1e20)
expect_equal(parse_double("1267650600228229401496703205376"), 1.267650600228229401496703205376e+30)
expect_equal(parse_double("100000000000000000000", locale = es_MX), 1e20)
expect_equal(parse_double("1267650600228229401496703205376", locale = es_MX), 1.267650600228229401496703205376e+30)
})