1#include "duckdb/common/helper.hpp"
2#include "expression_helper.hpp"
3#include "duckdb/optimizer/rule/constant_folding.hpp"
4#include "duckdb/optimizer/rule/move_constants.hpp"
5
6using namespace duckdb;
7using namespace std;
8
9TEST_CASE("Test move constants", "[optimizer]") {
10 ExpressionHelper helper;
11
12 REQUIRE(helper.AddColumns("X INTEGER").empty());
13 helper.AddRule<ConstantFoldingRule>();
14 helper.AddRule<MoveConstantsRule>();
15
16 string input, expected_output;
17
18 // addition
19 input = "X+1=10";
20 expected_output = "X=9";
21 REQUIRE(helper.VerifyRewrite(input, expected_output));
22
23 input = "1+X=10";
24 expected_output = "X=9";
25 REQUIRE(helper.VerifyRewrite(input, expected_output));
26
27 input = "10=X+1";
28 expected_output = "9=X";
29 REQUIRE(helper.VerifyRewrite(input, expected_output));
30 // subtraction
31 input = "X-1=10";
32 expected_output = "X=11";
33 REQUIRE(helper.VerifyRewrite(input, expected_output));
34
35 input = "10-X=5";
36 expected_output = "X=5";
37 REQUIRE(helper.VerifyRewrite(input, expected_output));
38
39 input = "10-X<5";
40 expected_output = "X>5";
41 REQUIRE(helper.VerifyRewrite(input, expected_output));
42
43 input = "10-X>=5";
44 expected_output = "X<=5";
45 REQUIRE(helper.VerifyRewrite(input, expected_output));
46 // multiplication
47 input = "3*X=6";
48 expected_output = "X=2";
49 REQUIRE(helper.VerifyRewrite(input, expected_output));
50 input = "X*2=8";
51 expected_output = "X=4";
52 REQUIRE(helper.VerifyRewrite(input, expected_output));
53 input = "X*3>3";
54 expected_output = "X>1";
55 REQUIRE(helper.VerifyRewrite(input, expected_output));
56 // multiplication by negative value
57 input = "-1*X=-5";
58 expected_output = "X=5";
59 REQUIRE(helper.VerifyRewrite(input, expected_output));
60 input = "-1*X<-5";
61 expected_output = "X>5";
62 REQUIRE(helper.VerifyRewrite(input, expected_output));
63 // negation, FIXME:
64 // input = "-X=-5";
65 // expected_output = "X=5";
66 // REQUIRE(helper.VerifyRewrite(input, expected_output));
67 // input = "-X<-5";
68 // expected_output = "X>5";
69 // REQUIRE(helper.VerifyRewrite(input, expected_output));
70}
71