typst/tests/typ/compiler/ops.typ

304 lines
5.8 KiB
Plaintext
Raw Normal View History

2021-02-01 00:43:11 +03:00
// Test binary expressions.
// Ref: false
---
2022-03-11 14:59:55 +03:00
// Test adding content.
2021-02-01 00:43:11 +03:00
// Ref: true
2023-02-17 18:32:48 +03:00
#([*Hello* ] + [world!])
2021-02-01 00:43:11 +03:00
---
// 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-07-30 19:37:04 +03:00
// Test plus.
test(+v, v)
2021-02-20 19:53:40 +03:00
2021-07-30 19:37:04 +03:00
// Test minus.
test(-v, -1 * v)
test(--v, v)
2021-02-20 19:53:40 +03:00
2021-07-30 19:37:04 +03:00
// Test combination.
test(-++ --v, -v)
2021-02-20 19:53:40 +03:00
}
#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")
2022-05-16 16:55:50 +03:00
#test("a" + if false { "b" }, "a")
#test("a" + if true { "b" }, "ab")
2021-07-10 14:07:39 +03:00
#test(13 * "a" + "bbbbbb", "aaaaaaaaaaaaabbbbbb")
2021-02-17 23:30:20 +03:00
#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
2023-04-26 12:30:16 +03:00
---
// Error: 3-26 value is too large
#(9223372036854775807 + 1)
---
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
// With block.
#test(if true {
1
} + 2, 3)
2021-02-01 00:43:11 +03:00
// Mathematical identities.
2022-05-04 23:14:51 +03:00
#let nums = (
1, 3.14,
12pt, 3em, 12pt + 3em,
45deg,
90%,
13% + 10pt, 5% + 1em + 3pt,
2.3fr,
)
2021-02-17 23:30:20 +03:00
#for v in nums {
2021-07-30 19:37:04 +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.
2023-09-11 15:42:43 +03:00
if type(v) != int {
2021-07-30 19:37:04 +03:00
test(v + v, 2.0 * v)
}
2023-09-11 15:42:43 +03:00
if type(v) != relative and ("pt" not in repr(v) or "em" not in repr(v)) {
2021-07-30 19:37:04 +03:00
test(v / v, 1.0)
}
2021-02-01 00:43:11 +03:00
}
// Make sure length, ratio and relative length
2021-02-01 00:43:11 +03:00
// - can all be added to / subtracted from each other,
// - multiplied with integers and floats,
2022-05-04 23:14:51 +03:00
// - divided by integers and floats.
#let dims = (10pt, 1em, 10pt + 1em, 30%, 50% + 3cm, 40% + 2em + 1cm)
2021-02-17 23:30:20 +03:00
#for a in dims {
2021-07-30 19:37:04 +03:00
for b in dims {
test(type(a + b), type(a - b))
}
for b in (7, 3.14) {
test(type(a * b), type(a))
test(type(b * a), type(a))
test(type(a / b), type(a))
}
2021-02-01 00:43:11 +03:00
}
2022-05-04 23:14:51 +03:00
// Test division of different numeric types with zero components.
#for a in (0pt, 0em, 0%) {
for b in (10pt, 10em, 10%) {
test((2 * b) / b, 2)
test((a + b * 2) / b, 2)
test(b / (b * 2 + a), 0.5)
}
}
2023-04-04 16:22:48 +03:00
---
// Test numbers with alternative bases.
#test(0x10, 16)
#test(0b1101, 13)
#test(0xA + 0xa, 0x14)
---
// Error: 2-7 invalid binary number: 0b123
2023-04-04 16:22:48 +03:00
#0b123
---
// Error: 2-8 invalid hexadecimal number: 0x123z
2023-04-04 16:22:48 +03:00
#0x123z
---
// Test that multiplying infinite numbers by certain units does not crash.
#(float("inf") * 1pt)
#(float("inf") * 1em)
#(float("inf") * (1pt + 1em))
---
// Test that trying to produce a NaN scalar (such as in lengths) does not crash.
#let infpt = float("inf") * 1pt
#test(infpt - infpt, 0pt)
#test(infpt + (-infpt), 0pt)
// TODO: this result is surprising
#test(infpt / float("inf"), 0pt)
2021-02-01 00:43:11 +03:00
---
// 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.
// Most things compare by value.
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
// Functions compare by identity.
#test(test == test, true)
#test((() => {}) == (() => {}), false)
2023-03-10 22:47:23 +03:00
// Content compares field by field.
#let t = [a]
2023-03-10 22:47:23 +03:00
#test(t == t, true)
#test([] == [], true)
#test([a] == [a], true)
#test(grid[a] == grid[a], true)
#test(grid[a] == grid[b], 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)
#test(10% < 20%, true)
#test(50% < 40% + 0pt, false)
2022-04-30 15:12:28 +03:00
#test(40% + 0pt < 50% + 0pt, true)
#test(1em < 2em, true)
2021-02-01 00:43:11 +03:00
---
// Test assignment operators.
#let x = 0
2023-02-17 18:32:48 +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")
2021-06-26 19:07:05 +03:00
2023-04-25 12:22:12 +03:00
---
// Test destructuring assignments.
#let a = none
#let b = none
#let c = none
#((a,) = (1,))
#test(a, 1)
#((_, a, b, _) = (1, 2, 3, 4))
#test(a, 2)
#test(b, 3)
#((a, b, ..c) = (1, 2, 3, 4, 5, 6))
#test(a, 1)
#test(b, 2)
#test(c, (3, 4, 5, 6))
#((a: a, b, x: c) = (a: 1, b: 2, x: 3))
#test(a, 1)
#test(b, 2)
#test(c, 3)
#let a = (1, 2)
#((a: a.at(0), b) = (a: 3, b: 4))
#test(a, (3, 2))
#test(b, 4)
#let a = (1, 2)
#((a.at(0), b) = (3, 4))
#test(a, (3, 2))
#test(b, 4)
#((a, ..b) = (1, 2, 3, 4))
#test(a, 1)
#test(b, (2, 3, 4))
#let a = (1, 2)
#((b, ..a.at(0)) = (1, 2, 3, 4))
#test(a, ((2, 3, 4), 2))
#test(b, 1)
2022-11-29 15:37:25 +03:00
---
// Error: 3-6 cannot mutate a constant: box
2023-02-17 18:32:48 +03:00
#(box = 1)
2022-11-29 15:37:25 +03:00
2021-06-26 19:07:05 +03:00
---
2022-03-15 17:27:36 +03:00
// Test `in` operator.
#test("hi" in "worship", true)
#test("hi" in ("we", "hi", "bye"), true)
#test("Hey" in "abHeyCd", true)
#test("Hey" in "abheyCd", false)
#test(5 in range(10), true)
#test(12 in range(10), false)
#test("" in (), false)
#test("key" in (key: "value"), true)
#test("value" in (key: "value"), false)
#test("Hey" not in "abheyCd", true)
#test("a" not
/* fun comment? */ in "abc", false)
---
2023-01-27 13:54:30 +03:00
// Error: 10 expected keyword `in`
2023-02-17 18:32:48 +03:00
#("a" not)
2022-03-15 17:27:36 +03:00
---
2022-03-19 01:36:18 +03:00
// Test `with` method.
2021-06-26 19:07:05 +03:00
// Apply positional arguments.
#let add(x, y) = x + y
2022-03-19 01:36:18 +03:00
#test(add.with(2)(3), 5)
#test(add.with(2).with(3)(), 5)
#test((add.with(2))(4), 6)
#test((add.with(2).with(3))(), 5)
2021-06-26 19:07:05 +03:00
// Make sure that named arguments are overridable.
2021-11-17 19:09:19 +03:00
#let inc(x, y: 1) = x + y
#test(inc(1), 2)
2022-03-19 01:36:18 +03:00
#let inc2 = inc.with(y: 2)
#test(inc2(2), 4)
#test(inc2(2, y: 4), 6)