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/sqlite3_str_split.h"
18 
19 #include <sqlite3.h>
20 #include <string>
21 #include "perfetto/base/logging.h"
22 #include "src/trace_processor/scoped_db.h"
23 
24 #include "gtest/gtest.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_open(":memory:", &db) == SQLITE_OK);
35     db_.reset(db);
36     sqlite3_str_split_init(db_.get());
37   }
38 
SplitStmt(const std::string & str,const std::string & delim,int field)39   const char* SplitStmt(const std::string& str,
40                         const std::string& delim,
41                         int field) {
42     const std::string sql = "SELECT STR_SPLIT(\"" + str + "\", \"" + delim +
43                             "\", " + std::to_string(field) + ");";
44     sqlite3_stmt* stmt = nullptr;
45     PERFETTO_CHECK(sqlite3_prepare_v2(*db_, sql.c_str(),
46                                       static_cast<int>(sql.size()), &stmt,
47                                       nullptr) == SQLITE_OK);
48     stmt_.reset(stmt);
49     PERFETTO_CHECK(sqlite3_step(stmt) == SQLITE_ROW);
50     if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
51       return nullptr;
52     }
53     return reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
54   }
55 
56  protected:
57   ScopedDb db_;
58   ScopedStmt stmt_;
59 };
60 
TEST_F(Sqlite3StrSplitTest,SplitNoDelimiter)61 TEST_F(Sqlite3StrSplitTest, SplitNoDelimiter) {
62   ASSERT_STREQ(SplitStmt("abc", ":", 0), "abc");
63   ASSERT_EQ(SplitStmt("abc", ":", 1), nullptr);
64 }
65 
TEST_F(Sqlite3StrSplitTest,SplitSingleCharDelim)66 TEST_F(Sqlite3StrSplitTest, SplitSingleCharDelim) {
67   ASSERT_STREQ(SplitStmt("a:bc", ":", 0), "a");
68   ASSERT_STREQ(SplitStmt("a:bc", ":", 1), "bc");
69   ASSERT_EQ(SplitStmt("a:bc", ":", 2), nullptr);
70 }
71 
TEST_F(Sqlite3StrSplitTest,SplitInputConsecutiveDelim)72 TEST_F(Sqlite3StrSplitTest, SplitInputConsecutiveDelim) {
73   ASSERT_STREQ(SplitStmt("a::b::c", ":", 0), "a");
74   ASSERT_STREQ(SplitStmt("a::b::c", ":", 1), "");
75   ASSERT_STREQ(SplitStmt("a::b::c", ":", 2), "b");
76   ASSERT_STREQ(SplitStmt("a::b::c", ":", 3), "");
77   ASSERT_STREQ(SplitStmt("a::b::c", ":", 4), "c");
78   ASSERT_EQ(SplitStmt("a::b::c", ":", 5), nullptr);
79 }
80 
TEST_F(Sqlite3StrSplitTest,SplitStringDelim)81 TEST_F(Sqlite3StrSplitTest, SplitStringDelim) {
82   ASSERT_STREQ(SplitStmt("abczzdefzzghi", "zz", 0), "abc");
83   ASSERT_STREQ(SplitStmt("abczzdefzzghi", "zz", 1), "def");
84   ASSERT_STREQ(SplitStmt("abczzdefzzghi", "zz", 2), "ghi");
85 }
86 
TEST_F(Sqlite3StrSplitTest,SplitEmptyInput)87 TEST_F(Sqlite3StrSplitTest, SplitEmptyInput) {
88   ASSERT_STREQ(SplitStmt("", "zz", 0), "");
89   ASSERT_EQ(SplitStmt("", "zz", 1), nullptr);
90   ASSERT_EQ(SplitStmt("", "zz", 1000), nullptr);
91 }
92 
93 }  // namespace
94 }  // namespace trace_processor
95 }  // namespace perfetto
96