diff options
author | Petr Štetiar <ynezz@true.cz> | 2021-03-19 16:54:55 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-04-23 00:42:30 +0200 |
commit | 2b59097c3f61fa901e91ac4cea48940760439578 (patch) | |
tree | 958d739a78f959dfcd55b3d76e6e970ca53fa1c6 /tests/custom/01_arithmetic | |
parent | 80393611fb6634abcc0da1dee2da7c4418dbde8d (diff) |
tests: create custom tests from current tests cases
Signed-off-by: Petr Štetiar <ynezz@true.cz>
Diffstat (limited to 'tests/custom/01_arithmetic')
-rw-r--r-- | tests/custom/01_arithmetic/00_value_conversion | 125 | ||||
-rw-r--r-- | tests/custom/01_arithmetic/01_division | 53 | ||||
-rw-r--r-- | tests/custom/01_arithmetic/02_modulo | 32 | ||||
-rw-r--r-- | tests/custom/01_arithmetic/03_bitwise | 54 | ||||
-rw-r--r-- | tests/custom/01_arithmetic/04_inc_dec | 49 |
5 files changed, 313 insertions, 0 deletions
diff --git a/tests/custom/01_arithmetic/00_value_conversion b/tests/custom/01_arithmetic/00_value_conversion new file mode 100644 index 0000000..c44ad00 --- /dev/null +++ b/tests/custom/01_arithmetic/00_value_conversion @@ -0,0 +1,125 @@ +Performing unary plus or minus, or performing arithmetic operations may +implicitly convert non-numeric values to numeric ones. + +If an addition is performed and either operand is a string, the other +operand is converted into a string as well and the addition result is a +concatenated string consisting of the two operand values. + +-- Expect stdout -- +HelloWorld +12 +34 +true +false +null +{ "some": "object" } +[ "some", "array" ] +function() { ... } +1.2 +Infinity +124 +123 +123 +NaN +NaN +NaN +124.2 +Infinity +1 +0 +0 +NaN +NaN +NaN +1.2 +Infinity +123 +12.3 +NaN +-1 +0 +0 +NaN +NaN +NaN +-1.2 +-Infinity +-123 +-12.3 +NaN +4.2 +9.6 +-- End -- + +-- Testcase -- +{% + print(join("\n", [ + + // Adding two strings concatenates them: + "Hello" + "World", + + // Adding a number to a string results in a string: + "1" + 2, + + // Adding a string to a number results in a string: + 3 + "4", + + // Adding any non-string value to a string or vice versa will + // force stringification of the non-string value + "" + true, + "" + false, + "" + null, + "" + { some: "object" }, + "" + [ "some", "array" ], + "" + function() {}, + "" + 1.2, + "" + (1 / 0), + + // Adding a numeric value to a non-string, non-numeric value + // or vice versa will convert the non-numeric argument to a + // number + 123 + true, + 123 + false, + 123 + null, + 123 + { some: "object" }, + 123 + [ "some", "array" ], + 123 + function() {}, + 123 + 1.2, + 123 + (1 / 0), + + // The unary "+" operator follows the same logic as adding a + // non-numeric, non-string value to a numeric one. Additionally + // the unary plus forces conversion of string values into numbers + +true, + +false, + +null, + +{ some: "object" }, + +[ "some", "array" ], + +function() {}, + +1.2, + +(1 / 0), + +"123", + +"12.3", + +"this is not a number", + + // The unary "-" operator functions like the unary "+" one and + // it additionally returns the negation of the numeric value + -true, + -false, + -null, + -{ some: "object" }, + -[ "some", "array" ], + -function() {}, + -1.2, + -(1 / 0), + -"123", + -"12.3", + -"this is not a number", + + // Adding a double to an integer or vice versa will force the + // result to a double as well + 1.2 + 3, + 4 + 5.6, + + "" ])); +-- End -- diff --git a/tests/custom/01_arithmetic/01_division b/tests/custom/01_arithmetic/01_division new file mode 100644 index 0000000..d4a2adb --- /dev/null +++ b/tests/custom/01_arithmetic/01_division @@ -0,0 +1,53 @@ +While arithmetic divisions generally follow the value conversion rules +outlined in the "00_value_conversion" test case, a number of additional +constraints apply. + +-- Expect stdout -- +Division by zero yields Infinity: +1 / 0 = Infinity + +Division by Infinity yields zero: +1 / Infinity = 0 + +Dividing Infinity yields Infinity: +Infinity / 1 = Infinity + +Dividing Infinity by Infinity yields NaN: +Infinity / Infinity = NaN + +If either operand is NaN, the result is NaN: +1 / NaN = NaN +NaN / 1 = NaN + +If both operands are integers, integer division is performed: +10 / 3 = 3 + +If either operand is a double, double division is performed: +10.0 / 3 = 3.33333 +10 / 3.0 = 3.33333 +-- End -- + +-- Testcase -- +Division by zero yields Infinity: +1 / 0 = {{ 1 / 0 }} + +Division by Infinity yields zero: +1 / Infinity = {{ 1 / Infinity }} + +Dividing Infinity yields Infinity: +Infinity / 1 = {{ Infinity / 1 }} + +Dividing Infinity by Infinity yields NaN: +Infinity / Infinity = {{ Infinity / Infinity }} + +If either operand is NaN, the result is NaN: +1 / NaN = {{ 1 / NaN }} +NaN / 1 = {{ NaN / 1 }} + +If both operands are integers, integer division is performed: +10 / 3 = {{ 10 / 3 }} + +If either operand is a double, double division is performed: +10.0 / 3 = {{ 10.0 / 3 }} +10 / 3.0 = {{ 10 / 3.0 }} +-- End -- diff --git a/tests/custom/01_arithmetic/02_modulo b/tests/custom/01_arithmetic/02_modulo new file mode 100644 index 0000000..244d624 --- /dev/null +++ b/tests/custom/01_arithmetic/02_modulo @@ -0,0 +1,32 @@ +The utpl language supports modulo divisions, however they're only defined +for integer values. + +-- Expect stdout -- +If both operands are integers or convertible to integers, +the modulo division yields the remainder: +10 % 4 = 2 +"10" % 4 = 2 +10 % "4" = 2 +"10" % "4" = 2 + +If either operand is a double value, the modulo operation +yields NaN: +10.0 % 4 = NaN +10 % 4.0 = NaN +"10.0" % 4 = NaN +-- End -- + +-- Testcase -- +If both operands are integers or convertible to integers, +the modulo division yields the remainder: +10 % 4 = {{ 10 % 4 }} +"10" % 4 = {{ "10" % 4 }} +10 % "4" = {{ 10 % 4 }} +"10" % "4" = {{ "10" % "4" }} + +If either operand is a double value, the modulo operation +yields NaN: +10.0 % 4 = {{ 10.0 % 4 }} +10 % 4.0 = {{ 10 % 4.0 }} +"10.0" % 4 = {{ "10.0" % 4 }} +-- End -- diff --git a/tests/custom/01_arithmetic/03_bitwise b/tests/custom/01_arithmetic/03_bitwise new file mode 100644 index 0000000..faf4ffd --- /dev/null +++ b/tests/custom/01_arithmetic/03_bitwise @@ -0,0 +1,54 @@ +Utpl implements C-style bitwise operations. One detail is that these operations +coerce their operands to signed 64bit integer values internally. + +-- Expect stdout -- +Left shift: +10 << 2 = 40 +3.3 << 4.1 = 48 + +Right shift: +10 >> 2 = 2 +3.3 >> 4.1 = 0 + +Bitwise and: +1234 & 200 = 192 +120.3 & 54.3 = 48 + +Bitwise xor: +1234 ^ 200 = 1050 +120.3 ^ 54.3 = 78 + +Bitwise or: +1234 | 200 = 1242 +120.3 | 54.3 = 126 + +Complement: +~0 = -1 +~10.4 = -11 +-- End -- + +-- Testcase -- +Left shift: +10 << 2 = {{ 10 << 2 }} +3.3 << 4.1 = {{ 3.3 << 4.1 }} + +Right shift: +10 >> 2 = {{ 10 >> 2 }} +3.3 >> 4.1 = {{ 3.3 >> 4.1 }} + +Bitwise and: +1234 & 200 = {{ 1234 & 200 }} +120.3 & 54.3 = {{ 120.3 & 54.3 }} + +Bitwise xor: +1234 ^ 200 = {{ 1234 ^ 200 }} +120.3 ^ 54.3 = {{ 120.3 ^ 54.3 }} + +Bitwise or: +1234 | 200 = {{ 1234 | 200 }} +120.3 | 54.3 = {{ 120.3 | 54.3 }} + +Complement: +~0 = {{ ~0 }} +~10.4 = {{ ~10.4 }} +-- End -- diff --git a/tests/custom/01_arithmetic/04_inc_dec b/tests/custom/01_arithmetic/04_inc_dec new file mode 100644 index 0000000..ae50ceb --- /dev/null +++ b/tests/custom/01_arithmetic/04_inc_dec @@ -0,0 +1,49 @@ +Utpl implements C-style pre- and postfix increment and decrement operators. + +Pre-increment or -decrement operations first mutate the value and then return +the resulting value while post-increment or -decrement operations first return +the initial value and then mutate the operand. + +Since the decrement and increment operators mutate their operand, they +may only be applied to variables, not constant literal expressions. + +If an undefined variable is incremented or decremented, its initial value +is assumed to be "0". + +If a non-numeric value is incremented or decremented, it is converted to a +number first. If the value is not convertible, the result of the increment +or decrement operation is NaN. + +-- Expect stdout -- +Incrementing a not existing variable assumes "0" as initial value: + + - Postfix increment result: 0, value after: 1 + - Prefix increment result: 1, value after: 1 + - Postfix decrement result: 0, value after: -1 + - Prefix decrement result: -1, value after: -1 + +Incrementing a non-numeric value will convert it to a number: + +124 +3.5 +2 +NaN +-- End -- + +-- Testcase -- +Incrementing a not existing variable assumes "0" as initial value: + + - Postfix increment result: {{ "" + a++ }}, value after: {{ a }} + - Prefix increment result: {{ "" + ++b }}, value after: {{ b }} + - Postfix decrement result: {{ "" + c-- }}, value after: {{ c }} + - Prefix decrement result: {{ "" + --d }}, value after: {{ d }} + +Incrementing a non-numeric value will convert it to a number: + +{% + n = "123"; n++; print(n, "\n"); + n = "4.5"; n--; print(n, "\n"); + n = true; n++; print(n, "\n"); + n = { some: "object" }; n--; print(n, "\n"); +%} +-- End -- |