typst/tests/typ/code/ops.typ

148 lines
2.9 KiB
Plaintext
Raw Normal View History

2021-02-01 00:43:11 +03:00
// Test binary expressions.
// Ref: false
---
// Test template addition.
// Ref: true
{[*Hello ] + [world!*]}
---
// Test math operators.
2021-02-20 19:53:40 +03:00
// Test plus and minus.
2021-06-01 15:56:02 +03:00
#for v in (1, 3.14, 12pt, 45deg, 90%, 13% + 10pt, 6.3fr) {
2021-02-20 19:53:40 +03:00
// Test plus.
test(+v, v)
// Test minus.
test(-v, -1 * v)
test(--v, v)
// Test combination.
test(-++ --v, -v)
}
#test(-(4 + 2), 6-12)
2021-02-01 00:43:11 +03:00
// Addition.
2021-02-17 23:30:20 +03:00
#test(2 + 4, 6)
#test("a" + "b", "ab")
#test((1, 2) + (3, 4), (1, 2, 3, 4))
#test((a: 1) + (b: 2, c: 3), (a: 1, b: 2, c: 3))
2021-02-01 00:43:11 +03:00
// Subtraction.
2021-02-17 23:30:20 +03:00
#test(1-4, 3*-1)
#test(4cm - 2cm, 2cm)
#test(1e+2-1e-2, 99.99)
2021-02-01 00:43:11 +03:00
// Multiplication.
2021-02-17 23:30:20 +03:00
#test(2 * 4, 8)
2021-02-01 00:43:11 +03:00
// Division.
2021-02-17 23:30:20 +03:00
#test(12pt/.4, 30pt)
#test(7 / 2, 3.5)
2021-02-01 00:43:11 +03:00
// Combination.
2021-02-17 23:30:20 +03:00
#test(3-4 * 5 < -10, true)
#test({ let x; x = 1 + 4*5 >= 21 and { x = "a"; x + "b" == "ab" }; x }, true)
2021-02-01 00:43:11 +03:00
// Mathematical identities.
#let nums = (1, 3.14, 12pt, 45deg, 90%, 13% + 10pt)
2021-02-17 23:30:20 +03:00
#for v in nums {
2021-02-01 00:43:11 +03:00
// Test plus and minus.
test(v + v - v, v)
test(v - v - v, -v)
// Test plus/minus and multiplication.
test(v - v, 0 * v)
test(v + v, 2 * v)
// Integer addition does not give a float.
2021-02-17 23:30:20 +03:00
if type(v) != "integer" {
2021-02-01 00:43:11 +03:00
test(v + v, 2.0 * v)
}
// Linears cannot be divided by themselves.
2021-02-17 23:30:20 +03:00
if type(v) != "linear" {
2021-02-01 00:43:11 +03:00
test(v / v, 1.0)
test(v / v == 1, true)
}
}
// Make sure length, relative and linear
// - can all be added to / subtracted from each other,
// - multiplied with integers and floats,
// - divided by floats.
#let dims = (10pt, 30%, 50% + 3cm)
2021-02-17 23:30:20 +03:00
#for a in dims {
for b in dims {
2021-02-01 00:43:11 +03:00
test(type(a + b), type(a - b))
}
2021-02-17 23:30:20 +03:00
for b in (7, 3.14) {
2021-02-01 00:43:11 +03:00
test(type(a * b), type(a))
test(type(b * a), type(a))
test(type(a / b), type(a))
}
}
---
// Test boolean operators.
2021-02-20 19:53:40 +03:00
// Test not.
#test(not true, false)
#test(not false, true)
2021-02-01 00:43:11 +03:00
// And.
2021-02-17 23:30:20 +03:00
#test(false and false, false)
#test(false and true, false)
#test(true and false, false)
#test(true and true, true)
2021-02-01 00:43:11 +03:00
// Or.
2021-02-17 23:30:20 +03:00
#test(false or false, false)
#test(false or true, true)
#test(true or false, true)
#test(true or true, true)
2021-02-01 00:43:11 +03:00
// Short-circuiting.
2021-02-17 23:30:20 +03:00
#test(false and dont-care, false)
#test(true or dont-care, true)
2021-02-01 00:43:11 +03:00
---
// Test equality operators.
2021-02-17 23:30:20 +03:00
#test(1 == "hi", false)
#test(1 == 1.0, true)
#test(30% == 30% + 0cm, true)
#test(1in == 0% + 72pt, true)
#test(30% == 30% + 1cm, false)
#test("ab" == "a" + "b", true)
#test(() == (1,), false)
#test((1, 2, 3) == (1, 2.0) + (3,), true)
#test((:) == (a: 1), false)
#test((a: 2 - 1.0, b: 2) == (b: 2, a: 1), true)
#test("a" != "a", false)
2021-02-01 00:43:11 +03:00
---
// Test comparison operators.
2021-02-17 23:30:20 +03:00
#test(13 * 3 < 14 * 4, true)
#test(5 < 10, true)
#test(5 > 5, false)
#test(5 <= 5, true)
#test(5 <= 4, false)
#test(45deg < 1rad, true)
2021-02-01 00:43:11 +03:00
---
// Test assignment operators.
#let x = 0
2021-02-17 23:30:20 +03:00
{ x = 10 } #test(x, 10)
{ x -= 5 } #test(x, 5)
{ x += 1 } #test(x, 6)
{ x *= x } #test(x, 36)
{ x /= 2.0 } #test(x, 18.0)
{ x = "some" } #test(x, "some")
{ x += "thing" } #test(x, "something")