diff options
Diffstat (limited to 'tests/01_arithmetic')
-rw-r--r-- | tests/01_arithmetic/00_value_conversion | 125 | ||||
-rw-r--r-- | tests/01_arithmetic/01_division | 53 | ||||
-rw-r--r-- | tests/01_arithmetic/02_modulo | 32 |
3 files changed, 210 insertions, 0 deletions
diff --git a/tests/01_arithmetic/00_value_conversion b/tests/01_arithmetic/00_value_conversion new file mode 100644 index 0000000..c44ad00 --- /dev/null +++ b/tests/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/01_arithmetic/01_division b/tests/01_arithmetic/01_division new file mode 100644 index 0000000..d4a2adb --- /dev/null +++ b/tests/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/01_arithmetic/02_modulo b/tests/01_arithmetic/02_modulo new file mode 100644 index 0000000..244d624 --- /dev/null +++ b/tests/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 -- |