aboutsummaryrefslogtreecommitdiffstats
path: root/test/libyul/Metrics.cpp
blob: 185a3755217c4d78dc2545892be573b30e1ff58c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * Unit tests for the code metrics.
 */

#include <test/Options.h>

#include <test/libyul/Common.h>

#include <libyul/optimiser/Metrics.h>
#include <libyul/AsmData.h>


using namespace std;
using namespace langutil;

namespace yul
{
namespace test
{

namespace
{

size_t codeSize(string const& _source)
{
    shared_ptr<Block> ast = parse(_source, false).first;
    BOOST_REQUIRE(ast);
    return CodeSize::codeSize(*ast);
}

}

BOOST_AUTO_TEST_SUITE(YulCodeSize)

BOOST_AUTO_TEST_CASE(empty_code)
{
    BOOST_CHECK_EQUAL(codeSize("{}"), 0);
}

BOOST_AUTO_TEST_CASE(nested_blocks)
{
    BOOST_CHECK_EQUAL(codeSize("{ {} {} {{ }} }"), 0);
}

BOOST_AUTO_TEST_CASE(instruction)
{
    BOOST_CHECK_EQUAL(codeSize("{ pop(calldatasize()) }"), 2);
}

BOOST_AUTO_TEST_CASE(variables_are_free)
{
    BOOST_CHECK_EQUAL(codeSize("{ let x let y let a, b, c }"), 0);
}

BOOST_AUTO_TEST_CASE(constants_cost_one)
{
    BOOST_CHECK_EQUAL(codeSize("{ let x := 3 }"), 1);
}

BOOST_AUTO_TEST_CASE(functions_are_skipped)
{
    BOOST_CHECK_EQUAL(codeSize("{ function f(x) -> r { r := mload(x) } }"), 0);
}

BOOST_AUTO_TEST_CASE(function_with_arguments)
{
    BOOST_CHECK_EQUAL(codeSize("{ function f(x) { sstore(x, 2) } f(2) }"), 2);
}

BOOST_AUTO_TEST_CASE(function_with_variables_as_arguments)
{
    BOOST_CHECK_EQUAL(codeSize("{ function f(x) { sstore(x, 2) } let y f(y) }"), 1);
}

BOOST_AUTO_TEST_CASE(function_with_variables_and_constants_as_arguments)
{
    BOOST_CHECK_EQUAL(codeSize(
        "{ function f(x, r) -> z { sstore(x, r) z := r } let y let t := f(y, 2) }"
    ), 2);
}

BOOST_AUTO_TEST_CASE(assignment)
{
    BOOST_CHECK_EQUAL(codeSize("{ let a a := 3 }"), 1);
}

BOOST_AUTO_TEST_CASE(assignments_between_vars_are_free)
{
    BOOST_CHECK_EQUAL(codeSize("{ let a let b := a a := b }"), 0);
}

BOOST_AUTO_TEST_CASE(assignment_complex)
{
    BOOST_CHECK_EQUAL(codeSize("{ let a let x := mload(a) a := sload(x) }"), 2);
}

BOOST_AUTO_TEST_SUITE_END()

}
}