1#include "catch.hpp"
2#include "test_helpers.hpp"
3
4using namespace duckdb;
5using namespace std;
6
7TEST_CASE("Test unary operators", "[function]") {
8 DuckDB db(nullptr);
9 Connection con(db);
10 unique_ptr<QueryResult> result;
11 con.EnableQueryVerification();
12
13 REQUIRE_NO_FAIL(con.Query("CREATE TABLE test(i INTEGER)"));
14 REQUIRE_NO_FAIL(con.Query("INSERT INTO test VALUES (2)"));
15
16 result = con.Query("SELECT ++-++-+i FROM test");
17 REQUIRE(CHECK_COLUMN(result, 0, {2}));
18
19 //! test simple unary operators
20 result = con.Query("SELECT +i FROM test");
21 REQUIRE(CHECK_COLUMN(result, 0, {2}));
22
23 result = con.Query("SELECT -i FROM test");
24 REQUIRE(CHECK_COLUMN(result, 0, {-2}));
25
26 // we can also stack unary functions
27 result = con.Query("SELECT +++++++i FROM test");
28 REQUIRE(CHECK_COLUMN(result, 0, {2}));
29
30 result = con.Query("SELECT ++-++-+i FROM test");
31 REQUIRE(CHECK_COLUMN(result, 0, {2}));
32
33 result = con.Query("SELECT -+-+-+-+-i FROM test");
34 REQUIRE(CHECK_COLUMN(result, 0, {-2}));
35
36 // cannot apply these to a string
37 REQUIRE_FAIL(con.Query("SELECT +'hello'"));
38 REQUIRE_FAIL(con.Query("SELECT -'hello'"));
39
40 // cannot apply these to a date either
41 REQUIRE_NO_FAIL(con.Query("CREATE TABLE dates(d DATE)"));
42 REQUIRE_NO_FAIL(con.Query("INSERT INTO dates VALUES ('1992-02-02')"));
43
44 REQUIRE_FAIL(con.Query("SELECT +d FROM dates"));
45 REQUIRE_FAIL(con.Query("SELECT -d FROM dates"));
46
47 //! postfix operators not implemented
48 REQUIRE_FAIL(con.Query("SELECT 2!"));
49}
50