| 1 | #include "catch.hpp" | 
|---|
| 2 | #include "test_helpers.hpp" | 
|---|
| 3 |  | 
|---|
| 4 | using namespace duckdb; | 
|---|
| 5 | using namespace std; | 
|---|
| 6 |  | 
|---|
| 7 | /* Test Case disclaimer | 
|---|
| 8 | * | 
|---|
| 9 | *  Assertions built using the Domain Testing technique | 
|---|
| 10 | *  at: https://bbst.courses/wp-content/uploads/2018/01/Kaner-Intro-to-Domain-Testing-2018.pdf | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 | TEST_CASE( "Suffix test", "[function]") { | 
|---|
| 14 | unique_ptr<QueryResult> result; | 
|---|
| 15 | DuckDB db(nullptr); | 
|---|
| 16 | Connection con(db); | 
|---|
| 17 | con.EnableQueryVerification(); | 
|---|
| 18 |  | 
|---|
| 19 | SECTION( "Short string (4bytes)") { | 
|---|
| 20 | result = con.Query( "SELECT suffix('abcd', 'd')"); | 
|---|
| 21 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 22 | result = con.Query( "SELECT suffix('abcd', 'cd')"); | 
|---|
| 23 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 24 | result = con.Query( "SELECT suffix('abcd', 'bcd')"); | 
|---|
| 25 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 26 | result = con.Query( "SELECT suffix('abcd', 'abcd')"); | 
|---|
| 27 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 28 | result = con.Query( "SELECT suffix('abcd', 'X')"); | 
|---|
| 29 | REQUIRE(CHECK_COLUMN(result, 0, {false})); | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | SECTION( "Medium string (8bytes)") { | 
|---|
| 33 | result = con.Query( "SELECT suffix('abcdefgh', 'h')"); | 
|---|
| 34 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 35 | result = con.Query( "SELECT suffix('abcdefgh', 'gh')"); | 
|---|
| 36 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 37 | result = con.Query( "SELECT suffix('abcdefgh', 'fgh')"); | 
|---|
| 38 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 39 | result = con.Query( "SELECT suffix('abcdefgh', 'efgh')"); | 
|---|
| 40 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 41 | result = con.Query( "SELECT suffix('abcdefgh', 'defgh')"); | 
|---|
| 42 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 43 | result = con.Query( "SELECT suffix('abcdefgh', 'X')"); | 
|---|
| 44 | REQUIRE(CHECK_COLUMN(result, 0, {false})); | 
|---|
| 45 | result = con.Query( "SELECT suffix('abcdefgh', 'abcdefgh')"); | 
|---|
| 46 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | SECTION( "Long string (> 15bytes)") { | 
|---|
| 50 | result = con.Query( "SELECT suffix('abcdefghijklmnopqrstuvwxyz', 'z')"); | 
|---|
| 51 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 52 | result = con.Query( "SELECT suffix('abcdefghijklmnopqrstuvwxyz', 'yz')"); | 
|---|
| 53 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 54 | result = con.Query( "SELECT suffix('abcdefghijklmnopqrstuvwxyz', 'xyz')"); | 
|---|
| 55 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 56 | result = con.Query( "SELECT suffix('abcdefghijklmnopqrstuvwxyz', 'wxyz')"); | 
|---|
| 57 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 58 | result = con.Query( "SELECT suffix('abcdefghijklmnopqrstuvwxyz', 'vwxyz')"); | 
|---|
| 59 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 60 | result = con.Query( "SELECT suffix('abcdefghijklmnopqrstuvwxyz', 'X')"); | 
|---|
| 61 | REQUIRE(CHECK_COLUMN(result, 0, {false})); | 
|---|
| 62 |  | 
|---|
| 63 | result = con.Query( "SELECT suffix('abcdefghijklmnopqrstuvwxyz', 'defghijklmnopqrstuvwxyz')"); | 
|---|
| 64 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | SECTION( "Empty string and suffix") { | 
|---|
| 68 | result = con.Query( "SELECT suffix('', 'aaa')"); | 
|---|
| 69 | REQUIRE(CHECK_COLUMN(result, 0, {false})); | 
|---|
| 70 |  | 
|---|
| 71 | result = con.Query( "SELECT suffix('aaa', '')"); | 
|---|
| 72 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | SECTION( "NULL string and suffix") { | 
|---|
| 76 | result = con.Query( "SELECT suffix(NULL, 'aaa')"); | 
|---|
| 77 | REQUIRE(CHECK_COLUMN(result, 0, {Value(nullptr)})); | 
|---|
| 78 |  | 
|---|
| 79 | result = con.Query( "SELECT suffix('aaa', NULL)"); | 
|---|
| 80 | REQUIRE(CHECK_COLUMN(result, 0, {Value(nullptr)})); | 
|---|
| 81 |  | 
|---|
| 82 | result = con.Query( "SELECT suffix(NULL, NULL)"); | 
|---|
| 83 | REQUIRE(CHECK_COLUMN(result, 0, {Value(nullptr)})); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | SECTION( "Suffix test with UTF8") { | 
|---|
| 87 | // inverse "átomo" (atom) | 
|---|
| 88 | result = con.Query( "SELECT suffix('omot\xc3\xa1', '\xc3\xa1')"); | 
|---|
| 89 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 90 | result = con.Query( "SELECT suffix('omot\xc3\xa1', 'á')"); | 
|---|
| 91 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 92 | result = con.Query( "SELECT suffix('omot\xc3\xa1', 'a')"); | 
|---|
| 93 | REQUIRE(CHECK_COLUMN(result, 0, {false})); | 
|---|
| 94 |  | 
|---|
| 95 | // inverse "olá mundo" (hello world) | 
|---|
| 96 | result = con.Query( "SELECT suffix('mundo ol\xc3\xa1', 'ol\xc3\xa1')"); | 
|---|
| 97 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 98 | result = con.Query( "SELECT suffix('mundo ol\xc3\xa1', 'olá')"); | 
|---|
| 99 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 100 | result = con.Query( "SELECT suffix('mundo olá', 'mundo olá')"); | 
|---|
| 101 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 102 | result = con.Query( "SELECT suffix('mundo ol\xc3\xa1', 'ola')"); | 
|---|
| 103 | REQUIRE(CHECK_COLUMN(result, 0, {false})); | 
|---|
| 104 |  | 
|---|
| 105 | // eftñ | 
|---|
| 106 | result = con.Query( "SELECT suffix('\x65\x66\x74\xc3\xb1', '\xc3\xb1')"); | 
|---|
| 107 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 108 | //ñeft | 
|---|
| 109 | result = con.Query( "SELECT suffix('\xc3\xb1\x65\x66\x74', 'ñeft')"); | 
|---|
| 110 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 111 | result = con.Query( "SELECT suffix('\xc3\xb1\x65\x66\x74', 'neft')"); | 
|---|
| 112 | REQUIRE(CHECK_COLUMN(result, 0, {false})); | 
|---|
| 113 |  | 
|---|
| 114 | // two ñ three ₡ four 🦆 end | 
|---|
| 115 | string str_utf8 = "'two \xc3\xb1 three \xE2\x82\xA1 four \xF0\x9F\xA6\x86 end'"; | 
|---|
| 116 |  | 
|---|
| 117 | result = con.Query( "SELECT suffix("+ str_utf8 + ", '\xF0\x9F\xA6\x86 end')"); | 
|---|
| 118 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 119 | result = con.Query( "SELECT suffix("+ str_utf8 + ", '🦆 end')"); | 
|---|
| 120 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 121 |  | 
|---|
| 122 | result = con.Query( "SELECT suffix("+ str_utf8 + ", 'three \xE2\x82\xA1 four 🦆 end')"); | 
|---|
| 123 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 124 | result = con.Query( "SELECT suffix("+ str_utf8 + ", 'three ₡ four 🦆 end')"); | 
|---|
| 125 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 126 |  | 
|---|
| 127 | result = con.Query( "SELECT suffix("+ str_utf8 + ", 'two \xc3\xb1 three ₡ four 🦆 end')"); | 
|---|
| 128 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 129 | result = con.Query( "SELECT suffix("+ str_utf8 + ", 'two ñ three ₡ four 🦆 end')"); | 
|---|
| 130 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 131 | result = con.Query( "SELECT suffix("+ str_utf8 + ", 'two ñ three \xE2\x82\xA1 four \xF0\x9F\xA6\x86 end')"); | 
|---|
| 132 | REQUIRE(CHECK_COLUMN(result, 0, {true})); | 
|---|
| 133 |  | 
|---|
| 134 | result = con.Query( "SELECT suffix("+ str_utf8 + ", 'two n three ₡ four 🦆 end')"); | 
|---|
| 135 | REQUIRE(CHECK_COLUMN(result, 0, {false})); | 
|---|
| 136 | result = con.Query( "SELECT suffix("+ str_utf8 + ", 'XXXtwo ñ three ₡ four 🦆 end')"); | 
|---|
| 137 | REQUIRE(CHECK_COLUMN(result, 0, {false})); | 
|---|
| 138 | } | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|