From 0fe7d787f3e6a4f70001472e0ed4f3b2baafd240 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 23 Apr 2021 11:37:52 +0100 Subject: [PATCH] Add more tests --- .../semanticTests/fixedPoint/arrays.sol | 17 +++ .../semanticTests/fixedPoint/constants.sol | 13 ++ .../semanticTests/fixedPoint/conversion.sol | 16 +++ .../fixedPoint/inline_assembly.sol | 123 ++++++++++++++++++ .../fixedPoint/state_variables.sol | 14 ++ .../fixedPoint/state_variables_packed.sol | 31 +++++ .../semanticTests/fixedPoint/variables.sol | 13 ++ 7 files changed, 227 insertions(+) create mode 100644 test/libsolidity/semanticTests/fixedPoint/arrays.sol create mode 100644 test/libsolidity/semanticTests/fixedPoint/constants.sol create mode 100644 test/libsolidity/semanticTests/fixedPoint/conversion.sol create mode 100644 test/libsolidity/semanticTests/fixedPoint/inline_assembly.sol create mode 100644 test/libsolidity/semanticTests/fixedPoint/state_variables.sol create mode 100644 test/libsolidity/semanticTests/fixedPoint/state_variables_packed.sol create mode 100644 test/libsolidity/semanticTests/fixedPoint/variables.sol diff --git a/test/libsolidity/semanticTests/fixedPoint/arrays.sol b/test/libsolidity/semanticTests/fixedPoint/arrays.sol new file mode 100644 index 000000000..704dad316 --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/arrays.sol @@ -0,0 +1,17 @@ +contract C { + function f() public pure returns (fixed[2] memory ret) { + ret[0] = 0.00000000000001; + ret[1] = 11111111111111.1; + } + function g() public pure returns (fixed[] memory ret) { + ret = new fixed[](3); + ret[0] = 998.888888; + ret[1] = 44.1100000000011; + ret[2] = 888888888888888888.4400001; + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 10000, 11111111111111100000000000000000 +// g() -> 0x20, 3, 998888888000000000000, 44110000000001100000, 888888888888888888440000100000000000 diff --git a/test/libsolidity/semanticTests/fixedPoint/constants.sol b/test/libsolidity/semanticTests/fixedPoint/constants.sol new file mode 100644 index 000000000..89c3c7543 --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/constants.sol @@ -0,0 +1,13 @@ +contract C { + function f() public pure returns (fixed) { + return 99.101 * 3.1; + } + function g() public pure returns (fixed128x2) { + return fixed128x2(-1/3); + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 307.213100000000000000 +// g() -> -0.33 diff --git a/test/libsolidity/semanticTests/fixedPoint/conversion.sol b/test/libsolidity/semanticTests/fixedPoint/conversion.sol new file mode 100644 index 000000000..1e3e1987c --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/conversion.sol @@ -0,0 +1,16 @@ +contract C { + function test() public pure returns (uint a, uint b, uint c, uint d) { + fixed64x4 x = -1.1234; + a = uint64(bytes8(x)); + fixed64x2 y = fixed64x2(x); + b = uint64(bytes8(y)); + fixed16x4 z = fixed16x4(x); + c = uint16(bytes2(z)); + ufixed64x4 w = ufixed64x4(x); + d = uint64(bytes8(w)); + } +} +// ==== +// compileViaYul: also +// ---- +// test() -> 0xffffffffffffd41e, 0xffffffffffffff90, 0xd41e, 0xffffffffffffd41e diff --git a/test/libsolidity/semanticTests/fixedPoint/inline_assembly.sol b/test/libsolidity/semanticTests/fixedPoint/inline_assembly.sol new file mode 100644 index 000000000..1cc25530e --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/inline_assembly.sol @@ -0,0 +1,123 @@ +contract A { + function s128x18() public pure returns (fixed x) { + assembly {x := 1000000111000222000333} + } + + function s128x16() public pure returns (fixed128x16 x) { + assembly {x := 1000000111000222000333} + } + + function s128x8() public pure returns (fixed128x8 x) { + assembly {x := 1000000111000222000333} + } + + function s128x4() public pure returns (fixed128x4 x) { + assembly {x := 1000000111000222000333} + } + + function s128x2() public pure returns (fixed128x2 x) { + assembly {x := 1000000111000222000333} + } + + function s128x0() public pure returns (fixed128x0 x) { + assembly {x := 1000000111000222000333} + } + + function s128x0_2() public pure returns (fixed128x0 x, fixed128x2 y) { + assembly { + x := 1000000111000222000333 + y := 1000000111000222000333 + } + } + + function s128x2(fixed128x2 i) public pure returns (fixed128x2 x) { + assembly {x := i} + } + + function u128x2(ufixed128x2 i) public pure returns (ufixed128x2 x) { + assembly {x := i} + } + + function u128x0(ufixed128x0 i) public pure returns (ufixed128x0 x) { + assembly {x := i} + } + + function s128x2_add(fixed128x2 a, fixed128x2 b) public pure returns (fixed128x2 x) { + assembly {x := add(a, b)} + } + + function s128x0(fixed128x0 i) public pure returns (fixed128x0 x) { + assembly {x := i} + } + + function s32x0(fixed32x0 i) public pure returns (fixed32x0 x) { + assembly {x := i} + } + + function s16x0(fixed16x0 i) public pure returns (fixed16x0 x) { + assembly {x := i} + } + + function s8x0(fixed8x0 i) public pure returns (fixed8x0 x) { + assembly {x := i} + } + + function u32x0(ufixed32x0 i) public pure returns (ufixed32x0 x) { + assembly {x := i} + } + + function u16x0(ufixed16x0 i) public pure returns (ufixed16x0 x) { + assembly {x := i} + } + + function u8x0(ufixed8x0 i) public pure returns (ufixed8x0 x) { + assembly {x := i} + } + + function u32x2(ufixed32x2 i) public pure returns (ufixed32x2 x) { + assembly {x := i} + } + + function u16x2(ufixed16x2 i) public pure returns (ufixed16x2 x) { + assembly {x := i} + } + + function u8x2(ufixed8x2 i) public pure returns (ufixed8x2 x) { + assembly {x := i} + } + + function u32168x2(ufixed32x2 a, ufixed16x2 b, ufixed8x2 c) public pure returns (ufixed32x2 x) { + assembly {x := a} + } +} +// ==== +// compileViaYul: also +// ---- +// u128x0(ufixed128x0): 123 -> 123 +// u128x2(ufixed128x2): 1.23 -> 1.23 +// s128x2(fixed128x2): -2.34 -> -2.34 +// s128x2(fixed128x2): 2.34 -> 2.34 +// u128x0(ufixed128x0): 123 -> 123 +// s128x0(fixed128x0): -234 -> -234 +// s128x2_add(fixed128x2,fixed128x2): -1.23, -2.34 -> -3.57 +// s128x2_add(fixed128x2,fixed128x2): -1.23, 2.34 -> 1.11 +// s128x2_add(fixed128x2,fixed128x2): 1.23, -2.34 -> -1.11 +// s128x2_add(fixed128x2,fixed128x2): 1.23, 2.34 -> 3.57 +// s128x2_add(fixed128x2,fixed128x2): 1701411834604692317316873037158841057.27, -1701411834604692317316873037158841057.28 -> -0.01 +// u32x0(ufixed32x0): 0 -> 0 +// u16x0(ufixed16x0): 0 -> 0 +// u8x0(ufixed8x0): 0 -> 0 +// u32x0(ufixed32x0): 4294967295 -> 4294967295 +// u16x0(ufixed16x0): 65535 -> 65535 +// u8x0(ufixed8x0): 255 -> 255 +// u32x2(ufixed32x2): 42949672.95 -> 42949672.95 +// u16x2(ufixed16x2): 655.35 -> 655.35 +// u8x2(ufixed8x2): 2.55 -> 2.55 +// u32168x2(ufixed32x2,ufixed16x2,ufixed8x2): 42949672.95, 655.35, 2.55 -> 42949672.95 +// s128x18() -> 1000.000111000222000333 +// s128x16() -> 100000.0111000222000333 +// s128x8() -> 10000001110002.22000333 +// s128x4() -> 100000011100022200.0333 +// s128x2() -> 10000001110002220003.33 +// s128x0() -> 1000000111000222000333 +// s128x0_2() -> 1000000111000222000333, 10000001110002220003.33 diff --git a/test/libsolidity/semanticTests/fixedPoint/state_variables.sol b/test/libsolidity/semanticTests/fixedPoint/state_variables.sol new file mode 100644 index 000000000..d2deae6bb --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/state_variables.sol @@ -0,0 +1,14 @@ +contract C { + fixed x = 1.7; + fixed immutable y = -1.8; + fixed public z = - 9.3; + fixed constant w = -3.4; + function f() public view returns (fixed, fixed, fixed, fixed) { + return (x, y, this.z(), w); + } +} +// ==== +// compileViaYul: also +// ---- +// z() -> -9.300000000000000000 +// f() -> 1.700000000000000000, -1.800000000000000000, -9.300000000000000000, -3.400000000000000000 diff --git a/test/libsolidity/semanticTests/fixedPoint/state_variables_packed.sol b/test/libsolidity/semanticTests/fixedPoint/state_variables_packed.sol new file mode 100644 index 000000000..7f35842e4 --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/state_variables_packed.sol @@ -0,0 +1,31 @@ +contract C { + uint8 a; + fixed64x2 b; + fixed16x5 c; + uint8 d; + function set() public { + a = 1; + b = -104.5; + c = -0.1234; + d = 99; + } + function getLowLevel() public view returns (bytes32 r) { + assembly { r := sload(0) } + } + function getIntermediate() public view returns (uint8, bytes8, bytes2, uint8) { + return (a, bytes8(b), bytes2(c), d); + } + function get() public view returns (uint8, fixed64x2, fixed16x5, uint8) { + return (a, b, c, d); + } +} +// ==== +// compileViaYul: also +// ---- +// getLowLevel() -> 0x00 +// getIntermediate() -> 0, 0x00, 0x00, 0 +// get() -> 0, 0x00, 0x00, 0 +// set() -> +// getLowLevel() -> 0x63cfccffffffffffffd72e01 +// getIntermediate() -> 1, 0xffffffffffffd72e000000000000000000000000000000000000000000000000, 0xcfcc000000000000000000000000000000000000000000000000000000000000, 0x63 +// get() -> 1, -104.50, -0.12340, 0x63 diff --git a/test/libsolidity/semanticTests/fixedPoint/variables.sol b/test/libsolidity/semanticTests/fixedPoint/variables.sol new file mode 100644 index 000000000..5925344e5 --- /dev/null +++ b/test/libsolidity/semanticTests/fixedPoint/variables.sol @@ -0,0 +1,13 @@ +contract C { + function f() public pure returns (fixed) { + return 1.33; + } + function g(fixed f1, fixed f2) public pure returns (fixed, fixed, fixed) { + return (f2, 0.0000333, f1); + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 1.330000000000000000 +// g(fixed128x18,fixed128x18): 0.000000000000009871, 0.000000000888888880 -> 0.000000000888888880, 0.000033300000000000, 0.000000000000009871