1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/trace_processor/perfetto_sql/intrinsics/functions/sqlite3_str_split.h"
18 
19 #include <sqlite3.h>
20 #include <string>
21 
22 #include "perfetto/base/logging.h"
23 #include "src/trace_processor/sqlite/scoped_db.h"
24 #include "test/gtest_and_gmock.h"
25 
26 namespace perfetto {
27 namespace trace_processor {
28 namespace {
29 
30 class Sqlite3StrSplitTest : public ::testing::Test {
31  public:
Sqlite3StrSplitTest()32   Sqlite3StrSplitTest() {
33     sqlite3* db = nullptr;
34     PERFETTO_CHECK(sqlite3_initialize() == SQLITE_OK);
35     PERFETTO_CHECK(sqlite3_open(":memory:", &db) == SQLITE_OK);
36     db_.reset(db);
37     sqlite3_str_split_init(db_.get());
38   }
39 
SplitStmt(const std::string & str,const std::string & delim,int field)40   const char* SplitStmt(const std::string& str,
41                         const std::string& delim,
42                         int field) {
43     const std::string sql = "SELECT STR_SPLIT(\"" + str + "\", \"" + delim +
44                             "\", " + std::to_string(field) + ");";
45     sqlite3_stmt* stmt = nullptr;
46     PERFETTO_CHECK(sqlite3_prepare_v2(*db_, sql.c_str(),
47                                       static_cast<int>(sql.size()), &stmt,
48                                       nullptr) == SQLITE_OK);
49     stmt_.reset(stmt);
50     PERFETTO_CHECK(sqlite3_step(stmt) == SQLITE_ROW);
51     if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
52       return nullptr;
53     }
54     return reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
55   }
56 
57  protected:
58   ScopedDb db_;
59   ScopedStmt stmt_;
60 };
61 
TEST_F(Sqlite3StrSplitTest,SplitNoDelimiter)62 TEST_F(Sqlite3StrSplitTest, SplitNoDelimiter) {
63   ASSERT_STREQ(SplitStmt("abc", ":", 0), "abc");
64   ASSERT_EQ(SplitStmt("abc", ":", 1), nullptr);
65 }
66 
TEST_F(Sqlite3StrSplitTest,SplitSingleCharDelim)67 TEST_F(Sqlite3StrSplitTest, SplitSingleCharDelim) {
68   ASSERT_STREQ(SplitStmt("a:bc", ":", 0), "a");
69   ASSERT_STREQ(SplitStmt("a:bc", ":", 1), "bc");
70   ASSERT_EQ(SplitStmt("a:bc", ":", 2), nullptr);
71 }
72 
TEST_F(Sqlite3StrSplitTest,SplitInputConsecutiveDelim)73 TEST_F(Sqlite3StrSplitTest, SplitInputConsecutiveDelim) {
74   ASSERT_STREQ(SplitStmt("a::b::c", ":", 0), "a");
75   ASSERT_STREQ(SplitStmt("a::b::c", ":", 1), "");
76   ASSERT_STREQ(SplitStmt("a::b::c", ":", 2), "b");
77   ASSERT_STREQ(SplitStmt("a::b::c", ":", 3), "");
78   ASSERT_STREQ(SplitStmt("a::b::c", ":", 4), "c");
79   ASSERT_EQ(SplitStmt("a::b::c", ":", 5), nullptr);
80 }
81 
TEST_F(Sqlite3StrSplitTest,SplitStringDelim)82 TEST_F(Sqlite3StrSplitTest, SplitStringDelim) {
83   ASSERT_STREQ(SplitStmt("abczzdefzzghi", "zz", 0), "abc");
84   ASSERT_STREQ(SplitStmt("abczzdefzzghi", "zz", 1), "def");
85   ASSERT_STREQ(SplitStmt("abczzdefzzghi", "zz", 2), "ghi");
86 }
87 
TEST_F(Sqlite3StrSplitTest,SplitEmptyInput)88 TEST_F(Sqlite3StrSplitTest, SplitEmptyInput) {
89   ASSERT_STREQ(SplitStmt("", "zz", 0), "");
90   ASSERT_EQ(SplitStmt("", "zz", 1), nullptr);
91   ASSERT_EQ(SplitStmt("", "zz", 1000), nullptr);
92 }
93 
94 }  // namespace
95 }  // namespace trace_processor
96 }  // namespace perfetto
97