File: rank_support_test.cpp

package info (click to toggle)
seqan3 3.0.2%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,052 kB
  • sloc: cpp: 144,641; makefile: 1,288; ansic: 294; sh: 228; xml: 217; javascript: 50; python: 27; php: 25
file content (192 lines) | stat: -rw-r--r-- 5,081 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "common.hpp"
#include "sdsl/bit_vectors.hpp"
#include "sdsl/rank_support.hpp"
#include "gtest/gtest.h"
#include <string>

using namespace sdsl;
using namespace std;

string test_file;
string temp_file;
string temp_dir;

namespace
{

template<class T>
class rank_support_test : public ::testing::Test { };

using testing::Types;

#ifdef FULL_TEST_SUITE

typedef Types<
    rank_support_il<1, 512>,
    rank_support_v<>,
    rank_support_v5<>,
    rank_support_sd<1>,
    rank_support_il<0, 512>,
    rank_support_rrr<0>,
    rank_support_v<0>,
    rank_support_v5<0>,
    rank_support_sd<0>,
    rank_support_hyb<1>,
    rank_support_hyb<0>,
    rank_support_v<10,2>,
    rank_support_v<01,2>,
    rank_support_v<00,2>,
    rank_support_v<11,2>,
    rank_support_v5<10,2>,
    rank_support_v5<01,2>,
    rank_support_v5<00,2>,
    rank_support_v5<11,2>,
    rank_support_il<1, 256>,
    rank_support_il<1, 1024>,
    rank_support_rrr<1, 64>,
    rank_support_rrr<1, 192>,
    rank_support_rrr<1, 256>,
    rank_support_rrr<1, 255>,
    rank_support_rrr<1, 15>,
    rank_support_rrr<1, 31>,
    rank_support_rrr<1, 63>,
    rank_support_rrr<1, 83>,
    rank_support_rrr<1, 127>,
    rank_support_rrr<1, 128>,
    rank_support_rrr<1, 129>,
    rank_support_il<0, 256>,
    rank_support_il<0, 1024>,
    rank_support_rrr<0, 64>,
    rank_support_rrr<0, 192>,
    rank_support_rrr<0, 256>,
    rank_support_rrr<0, 255>,
    rank_support_rrr<0, 15>,
    rank_support_rrr<0, 30>,
    rank_support_rrr<0, 63>,
    rank_support_rrr<0, 83>,
    rank_support_rrr<0, 127>,
    rank_support_rrr<0, 128>,
    rank_support_rrr<0, 129>
> Implementations;

#else

typedef Types<
    rank_support_il<1, 512>,
    rank_support_rrr<>,
    rank_support_v<>,
    rank_support_v5<>,
    rank_support_sd<1>,
    rank_support_il<0, 512>,
    rank_support_rrr<0>,
    rank_support_v<0>,
    rank_support_v5<0>,
    rank_support_sd<0>,
    rank_support_hyb<1>,
    rank_support_hyb<0>,
    rank_support_v<10,2>,
    rank_support_v<01,2>,
    rank_support_v<00,2>,
    rank_support_v<11,2>,
    rank_support_v5<10,2>,
    rank_support_v5<01,2>,
    rank_support_v5<00,2>,
    rank_support_v5<11,2>
> Implementations;

#endif

TYPED_TEST_CASE(rank_support_test, Implementations);

//! Test the rank method
TYPED_TEST(rank_support_test, rank_method)
{
    static_assert(sdsl::util::is_regular<TypeParam>::value, "Type is not regular");
    bit_vector bvec;
    ASSERT_TRUE(load_from_file(bvec, test_file));
    typename TypeParam::bit_vector_type bv(bvec);
    TypeParam rs(&bv);
    uint64_t rank=0;
    for (uint64_t j=0; j < bvec.size(); ++j) {
        ASSERT_EQ(rank, rs.rank(j));
        bool found = (j >= TypeParam::bit_pat_len-1);
        for (uint8_t k=0; found and k < TypeParam::bit_pat_len; ++k) {
            found &= bvec[j-k] == ((TypeParam::bit_pat>>k)&1);
        }
        rank += found;
    }
    EXPECT_EQ(rank, rs.rank(bvec.size()));
}

//! Test the rank method
TYPED_TEST(rank_support_test, store_and_load)
{
    bit_vector bvec;
    ASSERT_TRUE(load_from_file(bvec, test_file));
    typename TypeParam::bit_vector_type bv(bvec);
    TypeParam rs(&bv);
    TypeParam c_rs(&bv);
    ASSERT_TRUE(store_to_file(rs, temp_file));
    ASSERT_TRUE(load_from_file(c_rs, temp_file));
    c_rs.set_vector(&bv);

    uint64_t rank=0;
    for (uint64_t j=0; j < bvec.size(); ++j) {
        ASSERT_EQ(rank, c_rs.rank(j));
        bool found = (j >= TypeParam::bit_pat_len-1);
        for (uint8_t k=0; found and k < TypeParam::bit_pat_len; ++k) {
            found &= bvec[j-k] == ((TypeParam::bit_pat>>k)&1);
        }
        rank += found;
    }
    EXPECT_EQ(rank, c_rs.rank(bvec.size()));
    sdsl::remove(temp_file);
}

#if SDSL_HAS_CEREAL
template <typename in_archive_t, typename out_archive_t, typename TypeParam>
void do_serialisation(TypeParam const & l, typename TypeParam::bit_vector_type const & bv)
{
	{
		std::ofstream os{temp_file, std::ios::binary};
		out_archive_t oarchive{os};
		oarchive(l);
	}

	{
		TypeParam in_l{};
		std::ifstream is{temp_file, std::ios::binary};
		in_archive_t iarchive{is};
		iarchive(in_l);
		in_l.set_vector(&bv);
		EXPECT_EQ(l, in_l);
	}
}

TYPED_TEST(rank_support_test, cereal)
{
	if (temp_dir != "@/")
	{
		bit_vector bvec;
	        ASSERT_TRUE(load_from_file(bvec, test_file));
	        typename TypeParam::bit_vector_type bv(bvec);
	        TypeParam rs(&bv);

		do_serialisation<cereal::BinaryInputArchive,         cereal::BinaryOutputArchive>        (rs, bv);
		do_serialisation<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>(rs, bv);
		do_serialisation<cereal::JSONInputArchive,           cereal::JSONOutputArchive>          (rs, bv);
		do_serialisation<cereal::XMLInputArchive,            cereal::XMLOutputArchive>           (rs, bv);
	}
}
#endif // SDSL_HAS_CEREAL

}// end namespace

int main(int argc, char** argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    if ( init_2_arg_test(argc, argv, "RANK_SUPPORT", test_file, temp_dir, temp_file) != 0 ) {
        return 1;
    }
    return RUN_ALL_TESTS();
}