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
|
# General Lua Libraries for Lua 5.1, 5.2 & 5.3
# Copyright (C) 2011-2018 stdlib authors
before:
base_module = 'math'
this_module = 'std.math'
global_table = '_G'
extend_base = {'floor', 'round'}
M = require(this_module)
specify std.math:
- context when required:
- context by name:
- it does not touch the global table:
expect(show_apis {added_to=global_table, by=this_module}).
to_equal {}
- it does not touch the core math table:
expect(show_apis {added_to=base_module, by=this_module}).
to_equal {}
- it contains apis from the core math table:
expect(show_apis {from=base_module, not_in=this_module}).
to_contain.a_permutation_of(extend_base)
- context via the std module:
- it does not touch the global table:
expect(show_apis {added_to=global_table, by='std'}).
to_equal {}
- it does not touch the core math table:
expect(show_apis {added_to=base_module, by='std'}).
to_equal {}
- describe floor:
- before:
f = M.floor
- context with bad arguments:
badargs.diagnose(f, 'std.math.floor(number, ?int)')
- it rounds to the nearest smaller integer:
expect(f(1.2)).to_be(1)
expect(f(1.9)).to_be(1)
expect(f(999e-2)).to_be(9)
expect(f(999e-3)).to_be(0)
- it rounds down to specified number of decimal places:
expect(f(1.2345, 0)).to_be(1.0)
expect(f(1.2345, 1)).to_be(1.2)
expect(f(1.2345, 2)).to_be(1.23)
expect(f(9.9999, 2)).to_be(9.99)
expect(f(99999e-3, 3)).to_be(99999e-3)
expect(f(99999e-4, 3)).to_be(9999e-3)
expect(f(99999e-5, 3)).to_be(999e-3)
- describe round:
- before:
f = M.round
- context with bad arguments:
badargs.diagnose(f, 'std.math.round(number, ?int)')
- it rounds to the nearest integer:
expect(f(1.2)).to_be(1)
expect(f(1.9)).to_be(2)
expect(f(949e-2)).to_be(9)
expect(f(999e-2)).to_be(10)
- it rounds to specified number of decimal places:
expect(f(1.234, 0)).to_be(1.0)
expect(f(5.678, 0)).to_be(6.0)
expect(f(1.234, 1)).to_be(1.2)
expect(f(5.678, 1)).to_be(5.7)
expect(f(1.234, 2)).to_be(1.23)
expect(f(5.678, 2)).to_be(5.68)
expect(f(9.999, 2)).to_be(10)
expect(f(11111e-2, 3)).to_be(11111e-2)
expect(f(99999e-2, 3)).to_be(99999e-2)
expect(f(11111e-3, 3)).to_be(11111e-3)
expect(f(99999e-3, 3)).to_be(99999e-3)
expect(f(11111e-4, 3)).to_be(1111e-3)
expect(f(99999e-4, 3)).to_be(10)
expect(f(99999e-5, 3)).to_be(1)
- it rounds negative values correctly:
expect(f(-1.234, 0)).to_be(-1.0)
expect(f(-5.678, 0)).to_be(-6.0)
expect(f(-1.234, 1)).to_be(-1.2)
expect(f(-5.678, 1)).to_be(-5.7)
expect(f(-1.234, 2)).to_be(-1.23)
expect(f(-5.678, 2)).to_be(-5.68)
expect(f(-9.999, 2)).to_be(-10)
expect(f(-11111e-2, 3)).to_be(-11111e-2)
expect(f(-99999e-2, 3)).to_be(-99999e-2)
expect(f(-11111e-3, 3)).to_be(-11111e-3)
expect(f(-99999e-3, 3)).to_be(-99999e-3)
expect(f(-11111e-4, 3)).to_be(-1111e-3)
expect(f(-99999e-4, 3)).to_be(-10)
expect(f(-99999e-5, 3)).to_be(-1)
|