1 /*
2 * Copyright (C) 2018 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 "utils/container/bit-vector.h"
18
19 #include <memory>
20
21 #include "utils/container/bit-vector_generated.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24
25 namespace libtextclassifier3 {
26 namespace {
27
TEST(BitVectorTest,Dense)28 TEST(BitVectorTest, Dense) {
29 std::vector<bool> data = {false, true, true, true, false,
30 false, true, false, false, true};
31
32 std::unique_ptr<BitVectorDataT> mutable_bit_vector_data =
33 BitVector::CreateDenseBitVectorData(data);
34 flatbuffers::FlatBufferBuilder builder;
35 builder.Finish(BitVectorData::Pack(builder, mutable_bit_vector_data.get()));
36 const flatbuffers::DetachedBuffer buffer = builder.Release();
37 const BitVectorData* bit_vector_data =
38 flatbuffers::GetRoot<BitVectorData>(buffer.data());
39
40 BitVector bit_vector(bit_vector_data);
41 EXPECT_EQ(bit_vector[0], false);
42 EXPECT_EQ(bit_vector[1], true);
43 EXPECT_EQ(bit_vector[2], true);
44 EXPECT_EQ(bit_vector[3], true);
45 EXPECT_EQ(bit_vector[4], false);
46 EXPECT_EQ(bit_vector[5], false);
47 EXPECT_EQ(bit_vector[6], true);
48 EXPECT_EQ(bit_vector[7], false);
49 EXPECT_EQ(bit_vector[8], false);
50 EXPECT_EQ(bit_vector[9], true);
51 EXPECT_EQ(bit_vector[10], false);
52 }
53
TEST(BitVectorTest,Sparse)54 TEST(BitVectorTest, Sparse) {
55 std::vector<int32> sorted_indices = {3, 7};
56
57 std::unique_ptr<BitVectorDataT> mutable_bit_vector_data =
58 BitVector::CreateSparseBitVectorData(sorted_indices);
59 flatbuffers::FlatBufferBuilder builder;
60 builder.Finish(BitVectorData::Pack(builder, mutable_bit_vector_data.get()));
61 const flatbuffers::DetachedBuffer buffer = builder.Release();
62 const BitVectorData* bit_vector_data =
63 flatbuffers::GetRoot<BitVectorData>(buffer.data());
64
65 BitVector bit_vector(bit_vector_data);
66 EXPECT_EQ(bit_vector[0], false);
67 EXPECT_EQ(bit_vector[1], false);
68 EXPECT_EQ(bit_vector[2], false);
69 EXPECT_EQ(bit_vector[3], true);
70 EXPECT_EQ(bit_vector[4], false);
71 EXPECT_EQ(bit_vector[5], false);
72 EXPECT_EQ(bit_vector[6], false);
73 EXPECT_EQ(bit_vector[7], true);
74 EXPECT_EQ(bit_vector[8], false);
75 }
76
TEST(BitVectorTest,Null)77 TEST(BitVectorTest, Null) {
78 BitVector bit_vector(nullptr);
79
80 EXPECT_EQ(bit_vector[0], false);
81 EXPECT_EQ(bit_vector[1], false);
82 }
83
84 } // namespace
85 } // namespace libtextclassifier3
86