From 97c0c8337f4acae114ecc2b6b99e9f0e2ca185d5 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 29 Jul 2021 11:21:51 +0200 Subject: [PATCH] Fixed point conversion tests. --- .../fixed_point/conversion_bytes.sol | 2 ++ .../fixed_point/conversion_bytes_invalid.sol | 18 +++++++++++++ .../fixed_point/conversion_fixed.sol | 13 ++++++++++ .../fixed_point/conversion_fixed_invalid.sol | 26 +++++++++++++++++++ .../fixed_point/conversion_integer.sol | 2 ++ .../conversion_integer_invalid.sol | 5 ++++ .../fixed_point/conversion_rational.sol | 7 +++++ .../conversion_rational_invalid.sol | 21 +++++++++++++++ .../fixed_point/conversion_to_integer.sol | 2 ++ .../conversion_to_integer_invalid.sol | 5 ++++ .../fixed_point/implicit_conversion_bytes.sol | 5 ++++ 11 files changed, 106 insertions(+) create mode 100644 test/libsolidity/syntaxTests/fixed_point/conversion_bytes.sol create mode 100644 test/libsolidity/syntaxTests/fixed_point/conversion_bytes_invalid.sol create mode 100644 test/libsolidity/syntaxTests/fixed_point/conversion_fixed.sol create mode 100644 test/libsolidity/syntaxTests/fixed_point/conversion_fixed_invalid.sol create mode 100644 test/libsolidity/syntaxTests/fixed_point/conversion_integer.sol create mode 100644 test/libsolidity/syntaxTests/fixed_point/conversion_integer_invalid.sol create mode 100644 test/libsolidity/syntaxTests/fixed_point/conversion_rational.sol create mode 100644 test/libsolidity/syntaxTests/fixed_point/conversion_rational_invalid.sol create mode 100644 test/libsolidity/syntaxTests/fixed_point/conversion_to_integer.sol create mode 100644 test/libsolidity/syntaxTests/fixed_point/conversion_to_integer_invalid.sol create mode 100644 test/libsolidity/syntaxTests/fixed_point/implicit_conversion_bytes.sol diff --git a/test/libsolidity/syntaxTests/fixed_point/conversion_bytes.sol b/test/libsolidity/syntaxTests/fixed_point/conversion_bytes.sol new file mode 100644 index 000000000..b33072218 --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/conversion_bytes.sol @@ -0,0 +1,2 @@ +fixed constant x = fixed(bytes16(fixed(1))); +ufixed16x1 constant y = ufixed16x1(bytes2(ufixed16x1(1))); \ No newline at end of file diff --git a/test/libsolidity/syntaxTests/fixed_point/conversion_bytes_invalid.sol b/test/libsolidity/syntaxTests/fixed_point/conversion_bytes_invalid.sol new file mode 100644 index 000000000..1d98682a8 --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/conversion_bytes_invalid.sol @@ -0,0 +1,18 @@ +fixed constant a = fixed(bytes17(0)); +bytes17 constant b = bytes17(fixed(0)); +fixed constant c = fixed(bytes15(0)); +bytes15 constant d = bytes15(fixed(0)); + +fixed16x1 constant e = fixed16x1(bytes3(0)); +bytes3 constant f = bytes3(fixed16x1(0)); +fixed16x1 constant g = fixed16x1(bytes1(0)); +bytes1 constant h = bytes1(fixed16x1(0)); +// ---- +// TypeError 9640: (19-36): Explicit type conversion not allowed from "bytes17" to "fixed128x18". +// TypeError 9640: (59-76): Explicit type conversion not allowed from "fixed128x18" to "bytes17". +// TypeError 9640: (97-114): Explicit type conversion not allowed from "bytes15" to "fixed128x18". +// TypeError 9640: (137-154): Explicit type conversion not allowed from "fixed128x18" to "bytes15". +// TypeError 9640: (180-200): Explicit type conversion not allowed from "bytes3" to "fixed16x1". +// TypeError 9640: (222-242): Explicit type conversion not allowed from "fixed16x1" to "bytes3". +// TypeError 9640: (267-287): Explicit type conversion not allowed from "bytes1" to "fixed16x1". +// TypeError 9640: (309-329): Explicit type conversion not allowed from "fixed16x1" to "bytes1". diff --git a/test/libsolidity/syntaxTests/fixed_point/conversion_fixed.sol b/test/libsolidity/syntaxTests/fixed_point/conversion_fixed.sol new file mode 100644 index 000000000..f560980ca --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/conversion_fixed.sol @@ -0,0 +1,13 @@ +// implicit conversions +fixed128x2 constant a = fixed64x1(0); +fixed128x1 constant b = fixed64x1(0); +fixed128x1 constant c = ufixed64x1(0); +fixed128x3 constant d = ufixed64x1(0); +// explicit conversions +// precision reduction +ufixed64x1 constant r = ufixed64x1(ufixed64x2(0)); +// sign change +ufixed64x2 constant s = ufixed64x2(fixed64x2(0)); +// bit reduction +fixed32x2 constant t = fixed32x2(fixed64x2(0)); +// ---- diff --git a/test/libsolidity/syntaxTests/fixed_point/conversion_fixed_invalid.sol b/test/libsolidity/syntaxTests/fixed_point/conversion_fixed_invalid.sol new file mode 100644 index 000000000..9c2ec5b8c --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/conversion_fixed_invalid.sol @@ -0,0 +1,26 @@ +// implicit conversions +// precision reduction +fixed128x1 constant a = fixed64x2(0); +// sign change +fixed64x1 constant b = ufixed64x1(0); +ufixed256x3 constant c = fixed64x1(0); +// width reduction +fixed32x1 constant d = ufixed64x1(0); + +// explicit conversions +// precision and sign +fixed256x1 constant e = fixed256x1(ufixed64x2(0)); +// precision and value range +ufixed32x1 constant f = ufixed32x1(ufixed64x2(0)); +// value range and sign +fixed32x2 constant g = fixed32x2(ufixed64x2(0)); +ufixed32x2 constant h = ufixed32x2(fixed64x2(0)); +// ---- +// TypeError 7407: (71-83): Type fixed64x2 is not implicitly convertible to expected type fixed128x1. Conversion would incur precision loss - use explicit conversion instead. +// TypeError 7407: (123-136): Type ufixed64x1 is not implicitly convertible to expected type fixed64x1. +// TypeError 7407: (163-175): Type fixed64x1 is not implicitly convertible to expected type ufixed256x3. +// TypeError 7407: (219-232): Type ufixed64x1 is not implicitly convertible to expected type fixed32x1. +// TypeError 9640: (305-330): Explicit type conversion not allowed from "ufixed64x2" to "fixed256x1". Can only change one of precision, number of bits and signedness at the same time. +// TypeError 9640: (385-410): Explicit type conversion not allowed from "ufixed64x2" to "ufixed32x1". Can only change one of precision, number of bits and signedness at the same time. +// TypeError 9640: (459-483): Explicit type conversion not allowed from "ufixed64x2" to "fixed32x2". Can only change one of precision, number of bits and signedness at the same time. +// TypeError 9640: (509-533): Explicit type conversion not allowed from "fixed64x2" to "ufixed32x2". Can only change one of precision, number of bits and signedness at the same time. diff --git a/test/libsolidity/syntaxTests/fixed_point/conversion_integer.sol b/test/libsolidity/syntaxTests/fixed_point/conversion_integer.sol new file mode 100644 index 000000000..094bb7e28 --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/conversion_integer.sol @@ -0,0 +1,2 @@ +ufixed32x1 constant x = ufixed32x1(uint8(0)); +fixed32x1 constant y = fixed32x1(uint8(0)); diff --git a/test/libsolidity/syntaxTests/fixed_point/conversion_integer_invalid.sol b/test/libsolidity/syntaxTests/fixed_point/conversion_integer_invalid.sol new file mode 100644 index 000000000..1a481f527 --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/conversion_integer_invalid.sol @@ -0,0 +1,5 @@ +ufixed16x1 constant x = ufixed16x1(uint16(0)); +ufixed64x1 constant y = ufixed64x1(int8(0)); +// ---- +// TypeError 9640: (24-45): Explicit type conversion not allowed from "uint16" to "ufixed16x1". +// TypeError 9640: (71-90): Explicit type conversion not allowed from "int8" to "ufixed64x1". diff --git a/test/libsolidity/syntaxTests/fixed_point/conversion_rational.sol b/test/libsolidity/syntaxTests/fixed_point/conversion_rational.sol new file mode 100644 index 000000000..f2fbd6b74 --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/conversion_rational.sol @@ -0,0 +1,7 @@ +// implicit conversions +fixed64x2 constant a = 1.23; +ufixed64x2 constant b = 1.23; + +// explicit conversions +fixed constant c = fixed(1/3); +// ---- diff --git a/test/libsolidity/syntaxTests/fixed_point/conversion_rational_invalid.sol b/test/libsolidity/syntaxTests/fixed_point/conversion_rational_invalid.sol new file mode 100644 index 000000000..af8e0e2e6 --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/conversion_rational_invalid.sol @@ -0,0 +1,21 @@ +// implicit conversions +fixed64x2 constant a = 1.123; +ufixed64x2 constant b = -1.123; +fixed64x28 constant c = 1; +fixed64x28 constant d = -1; +ufixed256x77 constant e = 1/3; +ufixed256x77 constant f = -1; + +// explicit conversions +fixed64x2 constant g = fixed64x2(2**64); +ufixed64x2 constant h = ufixed64x2(-1); + +// ---- +// TypeError 2326: (47-52): Type rational_const 1123 / 1000 is not implicitly convertible to expected type fixed64x2. Try converting to type ufixed16x3 or use an explicit conversion. +// TypeError 2326: (78-84): Type rational_const -1123 / 1000 is not implicitly convertible to expected type ufixed64x2. Try converting to type fixed16x3 or use an explicit conversion. +// TypeError 7407: (110-111): Type int_const 1 is not implicitly convertible to expected type fixed64x28. +// TypeError 7407: (137-139): Type int_const -1 is not implicitly convertible to expected type fixed64x28. +// TypeError 4426: (167-170): Type rational_const 1 / 3 is not implicitly convertible to expected type ufixed256x77, but it can be explicitly converted. +// TypeError 7407: (198-200): Type int_const -1 is not implicitly convertible to expected type ufixed256x77. +// TypeError 9640: (250-266): Explicit type conversion not allowed from "int_const 18446744073709551616" to "fixed64x2". Value is too large. +// TypeError 9640: (292-306): Explicit type conversion not allowed from "int_const -1" to "ufixed64x2". Value is too small. diff --git a/test/libsolidity/syntaxTests/fixed_point/conversion_to_integer.sol b/test/libsolidity/syntaxTests/fixed_point/conversion_to_integer.sol new file mode 100644 index 000000000..a2377594d --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/conversion_to_integer.sol @@ -0,0 +1,2 @@ +uint constant x = uint(ufixed(0)); +int constant y = int(fixed(0)); diff --git a/test/libsolidity/syntaxTests/fixed_point/conversion_to_integer_invalid.sol b/test/libsolidity/syntaxTests/fixed_point/conversion_to_integer_invalid.sol new file mode 100644 index 000000000..fb88befbf --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/conversion_to_integer_invalid.sol @@ -0,0 +1,5 @@ +uint constant x = uint(fixed8x1(0)); +uint248 constant y = uint248(ufixed256x1(0)); +// ---- +// TypeError 9640: (18-35): Explicit type conversion not allowed from "fixed8x1" to "uint256". +// TypeError 9640: (58-81): Explicit type conversion not allowed from "ufixed256x1" to "uint248". diff --git a/test/libsolidity/syntaxTests/fixed_point/implicit_conversion_bytes.sol b/test/libsolidity/syntaxTests/fixed_point/implicit_conversion_bytes.sol new file mode 100644 index 000000000..d80cc729d --- /dev/null +++ b/test/libsolidity/syntaxTests/fixed_point/implicit_conversion_bytes.sol @@ -0,0 +1,5 @@ +bytes32 constant x = ufixed256x5(0); +bytes16 constant y = fixed(0); +// ---- +// TypeError 7407: (21-35): Type ufixed256x5 is not implicitly convertible to expected type bytes32. +// TypeError 7407: (58-66): Type fixed128x18 is not implicitly convertible to expected type bytes16.