xref: /aosp_15_r20/external/perfetto/src/trace_processor/db/runtime_table_unittest.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2023 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/db/runtime_table.h"
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "src/base/test/status_matchers.h"
24 #include "src/trace_processor/containers/string_pool.h"
25 #include "test/gtest_and_gmock.h"
26 
27 namespace perfetto::trace_processor {
28 namespace {
29 using base::gtest_matchers::IsOk;
30 using testing::Not;
31 
32 class RuntimeTableTest : public ::testing::Test {
33  protected:
34   StringPool pool_;
35   std::vector<std::string> names_{{"foo"}};
36   RuntimeTable::Builder builder_{&pool_, names_};
37 };
38 
TEST_F(RuntimeTableTest,DoubleThenIntValid)39 TEST_F(RuntimeTableTest, DoubleThenIntValid) {
40   ASSERT_OK(builder_.AddFloat(0, 1024.3));
41   ASSERT_OK(builder_.AddInteger(0, 1ll << 53));
42   ASSERT_OK_AND_ASSIGN(auto table, std::move(builder_).Build(2));
43 
44   const auto& col = table->columns()[0];
45   ASSERT_EQ(col.Get(0).AsDouble(), 1024.3);
46   ASSERT_EQ(col.Get(1).AsDouble(), static_cast<double>(1ll << 53));
47 }
48 
TEST_F(RuntimeTableTest,DoubleThenIntInvalid)49 TEST_F(RuntimeTableTest, DoubleThenIntInvalid) {
50   ASSERT_OK(builder_.AddFloat(0, 1024.0));
51   ASSERT_THAT(builder_.AddInteger(0, (1ll << 53) + 1), Not(IsOk()));
52   ASSERT_THAT(builder_.AddInteger(0, -(1ll << 53) - 1), Not(IsOk()));
53 }
54 
TEST_F(RuntimeTableTest,IntThenDouble)55 TEST_F(RuntimeTableTest, IntThenDouble) {
56   ASSERT_TRUE(builder_.AddInteger(0, 1024).ok());
57   ASSERT_TRUE(builder_.AddFloat(0, 1.3).ok());
58   ASSERT_OK_AND_ASSIGN(auto table, std::move(builder_).Build(2));
59 
60   const auto& col = table->columns()[0];
61   ASSERT_EQ(col.Get(0).AsDouble(), 1024.0);
62   ASSERT_EQ(col.Get(1).AsDouble(), 1.3);
63 }
64 
65 }  // namespace
66 }  // namespace perfetto::trace_processor
67