summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-values/signs-abs-computed.html
blob: 8bad89e557b8e7a042a24732e380e815c12de2ab (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-values-4/#comp-func">
<link rel="help" href="https://drafts.csswg.org/css-values-4/#numbers">
<link rel="help" href="https://drafts.csswg.org/css-values-4/#calc-type-checking">
<link rel="author" title="Apple Inc">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../support/numeric-testcommon.js"></script>
<div id="container" style="font-size: 20px; width: 100px">
  <div id="target"></div>
</div>
<script>
function test_zero(expression, { is_negative }) {
  test_math_used(`calc(${expression})`, '0', {type:'integer'});
  // to test zero sign, make it to negative infinity and clamp it between -1 and 1
  test_math_used(`clamp(-1, calc( 1 / sign(${expression})), 1)`, (is_negative)? '-1' : '1', {type:'integer'});
}

function test_length_equals(value, expected, msgExtra) {
  test_math_used(value, expected, {msgExtra, type: 'integer'});
}

// Identity tests
test_math_used('abs(1)', '1', {type:'integer'});
test_math_used('sign(1)', '1', {type:'integer'});
test_math_used('abs(-1)', '1', {type:'integer'});
test_math_used('sign(-1)', '-1', {type:'integer'});

// Nestings
test_math_used('abs(sign(1))', '1', {type:'integer'});
test_math_used('abs(sign(sign(1)))', '1', {type:'integer'});
test_math_used('sign(sign(sign(1) + sign(1)))', '1', {type:'integer'});

// General calculations
test_math_used('calc(abs(0.1 + 0.2) + 0.05)', '0.35', {type:'number', approx:0.1});
test_math_used('calc(sign(0.1 + 0.2) - 0.05)', '0.95', {type:'number', approx:0.1});
test_math_used('calc(abs(0.1 + 0.2) * 2)', '0.6', {type:'number', approx:0.1});
test_math_used('calc(abs(sign(0.1) + 0.2) / 2)', '0.6', {type:'number', approx:0.1});
test_math_used('calc(abs(0.1 + 0.2) * -2)', '-0.6', {type:'number', approx:0.1});
test_math_used('calc(sign(0.1 - 0.2) - 0.05)', '-1.05', {type:'number', approx:0.1});
test_math_used('calc(sign(1) + sign(1) - 0.05)', '1.95', {type:'number', approx:0.1});

// Test with <length-percentage>
test_math_used('abs(10px)', '10px', {type:'length'});
test_math_used('abs(10%)', '10px', {type:'length'});
test_math_used('abs(10px + 10%)', '20px', {type:'length'});
test_math_used('calc(10px + abs(10%))', '20px', {type:'length'});
test_math_used('abs(-10px)', '10px', {type:'length'});
test_math_used('abs(-10%)', '10px', {type:'length'});
// (20px + 1px) * (sign(20px - 10px - 10px) + 1)
test_math_used('calc((1em + 1px) * (sign(1em - 10px - 10%) + 1))', '21px', {type:'length'});

// Test sign for zero
test_zero('calc(sign(-0))', {is_negative: true});
test_zero('calc(sign(0))', {is_negative: false});

// Test with NaN and inf
test_math_used('abs(infinity)', 'calc(infinity)', {type:'number'});
test_math_used('abs(-infinity)', 'calc(infinity)', {type:'number'});
test_math_used('abs(NaN)', 'calc(NaN)', {type:'number'});

// Test abs/sign with negate
test_math_used('calc(20 - abs(-10))', '10', {type:'number'});
test_math_used('calc(20 - abs(10))', '10', {type:'number'});
test_math_used('calc(10 - abs(10 - abs(-30))', '-10', {type:'number'});
test_math_used('calc(2 - sign(1))', '1', {type:'number'});
test_math_used('calc(2 - sign(-1))', '3', {type:'number'});
test_math_used('calc(2 - sign(1 - sign(-1)))', '1', {type:'number'});
test_math_used('calc(10 - abs(20 - sign(2 - abs(-20))))', '-11', {type:'number'});

// Type checking sign
test_math_used('sign(1px)', '1', {type:'integer'});
test_math_used('sign(1cm)', '1', {type:'integer'});
test_math_used('sign(1mm)', '1', {type:'integer'});
test_math_used('sign(1Q)', '1', {type:'integer'});
test_math_used('sign(1in)', '1', {type:'integer'});
test_math_used('sign(1pc)', '1', {type:'integer'});
test_math_used('sign(1pt)', '1', {type:'integer'});
test_math_used('sign(1em)', '1', {type:'integer'});
test_math_used('sign(1ex)', '1', {type:'integer'});
test_math_used('sign(1ch)', '1', {type:'integer'});
test_math_used('sign(1rem)', '1', {type:'integer'});
test_math_used('sign(1vh)', '1', {type:'integer'});
test_math_used('sign(1vw)', '1', {type:'integer'});
test_math_used('sign(1vmin)', '1', {type:'integer'});
test_math_used('sign(1vmax)', '1', {type:'integer'});
test_math_used('sign(-1px)', '-1', {type:'integer'});
test_math_used('sign(-1cm)', '-1', {type:'integer'});
test_math_used('sign(-1mm)', '-1', {type:'integer'});
test_math_used('sign(-1Q)', '-1', {type:'integer'});
test_math_used('sign(-1in)', '-1', {type:'integer'});
test_math_used('sign(-1pc)', '-1', {type:'integer'});
test_math_used('sign(-1pt)', '-1', {type:'integer'});
test_math_used('sign(-1em)', '-1', {type:'integer'});
test_math_used('sign(-1ex)', '-1', {type:'integer'});
test_math_used('sign(-1ch)', '-1', {type:'integer'});
test_math_used('sign(-1rem)', '-1', {type:'integer'});
test_math_used('sign(-1vh)', '-1', {type:'integer'});
test_math_used('sign(-1vw)', '-1', {type:'integer'});
test_math_used('sign(-1vmin)', '-1', {type:'integer'});
test_math_used('sign(-1vmax)', '-1', {type:'integer'});
test_math_used('sign(1s)', '1', {type:'integer'});
test_math_used('sign(1ms)', '1', {type:'integer'});
test_math_used('sign(-1s)', '-1', {type:'integer'});
test_math_used('sign(-1ms)', '-1', {type:'integer'});
test_math_used('sign(1deg)', '1', {type:'integer'});
test_math_used('sign(1grad)', '1', {type:'integer'});
test_math_used('sign(1rad)', '1', {type:'integer'});
test_math_used('sign(1turn)', '1', {type:'integer'});
test_math_used('sign(-1deg)', '-1', {type:'integer'});
test_math_used('sign(-1grad)', '-1', {type:'integer'});
test_math_used('sign(-1rad)', '-1', {type:'integer'});
test_math_used('sign(-1turn)', '-1', {type:'integer'});
test_zero('sign(0px)', {is_negative: false});
test_zero('sign(0cm)', {is_negative: false});
test_zero('sign(0mm)', {is_negative: false});
test_zero('sign(0Q)', {is_negative: false});
test_zero('sign(0in)', {is_negative: false});
test_zero('sign(0pc)', {is_negative: false});
test_zero('sign(0pt)', {is_negative: false});
test_zero('sign(0em)', {is_negative: false});
test_zero('sign(0ex)', {is_negative: false});
test_zero('sign(0ch)', {is_negative: false});
test_zero('sign(0rem)', {is_negative: false});
test_zero('sign(0vh)', {is_negative: false});
test_zero('sign(0vw)', {is_negative: false});
test_zero('sign(0vmin)', {is_negative: false});
test_zero('sign(0vmax)', {is_negative: false});
test_zero('sign(-0px)', {is_negative: true});
test_zero('sign(-0cm)', {is_negative: true});
test_zero('sign(-0mm)', {is_negative: true});
test_zero('sign(-0Q)', {is_negative: true});
test_zero('sign(-0in)', {is_negative: true});
test_zero('sign(-0pc)', {is_negative: true});
test_zero('sign(-0pt)', {is_negative: true});
test_zero('sign(-0em)', {is_negative: true});
test_zero('sign(-0ex)', {is_negative: true});
test_zero('sign(-0ch)', {is_negative: true});
test_zero('sign(-0rem)', {is_negative: true});
test_zero('sign(-0vh)', {is_negative: true});
test_zero('sign(-0vw)', {is_negative: true});
test_zero('sign(-0vmin)', {is_negative: true});
test_zero('sign(-0vmax)', {is_negative: true});
test_zero('sign(0s)', {is_negative: false});
test_zero('sign(0ms)', {is_negative: false});
test_zero('sign(-0s)', {is_negative: true});
test_zero('sign(-0ms)', {is_negative: true});
test_zero('sign(0deg)', {is_negative: false});
test_zero('sign(0grad)', {is_negative: false});
test_zero('sign(0rad)', {is_negative: false});
test_zero('sign(0turn)', {is_negative: false});
test_zero('sign(-0deg)', {is_negative: true});
test_zero('sign(-0grad)', {is_negative: true});
test_zero('sign(-0rad)', {is_negative: true});
test_zero('sign(-0turn)', {is_negative: true});

// Type checking abs
test_math_used('abs(1px)', '1px');
test_math_used('abs(1cm)', '1cm');
test_math_used('abs(1mm)', '1mm');
test_math_used('abs(1Q)', '1Q');
test_math_used('abs(1in)', '1in');
test_math_used('abs(1pc)', '1pc');
test_math_used('abs(1pt)', '1pt');
test_math_used('abs(1em)', '1em');
test_math_used('abs(1ex)', '1ex');
test_math_used('abs(1ch)', '1ch');
test_math_used('abs(1rem)', '1rem');
test_math_used('abs(1vh)', '1vh');
test_math_used('abs(1vw)', '1vw');
test_math_used('abs(1vmin)', '1vmin');
test_math_used('abs(1vmax)', '1vmax');
test_math_used('abs(-1px)', '1px');
test_math_used('abs(-1cm)', '1cm');
test_math_used('abs(-1mm)', '1mm');
test_math_used('abs(-1Q)', '1Q');
test_math_used('abs(-1in)', '1in');
test_math_used('abs(-1pc)', '1pc');
test_math_used('abs(-1pt)', '1pt');
test_math_used('abs(-1em)', '1em');
test_math_used('abs(-1ex)', '1ex');
test_math_used('abs(-1ch)', '1ch');
test_math_used('abs(-1rem)', '1rem');
test_math_used('abs(-1vh)', '1vh');
test_math_used('abs(-1vw)', '1vw');
test_math_used('abs(-1vmin)', '1vmin');
test_math_used('abs(-1vmax)', '1vmax');
test_math_used('abs(1s)', '1s', {type:'time'});
test_math_used('abs(1ms)', '1ms', {type:'time'});
test_math_used('abs(-1s)', '1s', {type:'time'});
test_math_used('abs(-1ms)', '1ms', {type:'time'});
test_math_used('abs(1deg)', '1deg', {type:'angle', approx:0.001});
test_math_used('abs(1grad)', '1grad', {type:'angle', approx:0.001});
test_math_used('abs(1rad)', '1rad', {type:'angle', approx:0.001});
test_math_used('abs(1turn)', '1turn', {type:'angle', approx:0.001});
test_math_used('abs(-1deg)', '1deg', {type:'angle', approx:0.001});
test_math_used('abs(-1grad)', '1grad', {type:'angle', approx:0.001});
test_math_used('abs(-1rad)', '1rad', {type:'angle', approx:0.001});
test_math_used('abs(-1turn)', '1turn', {type:'angle', approx:0.001});

// with relative length
document.getElementById('container').style.fontSize = '10px';
test_length_equals('sign(10px - 1em)', '0', 'fontSize=10px');
test_length_equals('sign(10px - 2em)', '-1', 'fontSize=10px');
document.getElementById('container').style.fontSize = '20px';

test_math_used('calc(sign(10%) * 100px)', '100px');

</script>