forked from simongog/sdsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsaIntTest.cpp
211 lines (187 loc) · 5.95 KB
/
CsaIntTest.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "sdsl/suffix_arrays.hpp"
#include "gtest/gtest.h"
#include <cstdlib>
#include <vector>
#include <string>
namespace
{
using namespace sdsl;
using namespace std;
typedef int_vector<>::size_type size_type;
tMSS test_case_file_map;
string test_file;
uint8_t num_bytes;
string temp_file;
string temp_dir;
template<class T>
class CsaIntTest : public ::testing::Test { };
using testing::Types;
typedef Types< csa_wt<wt_int<>, 32, 32, sa_order_sa_sampling<>, int_vector<>, int_alphabet<> >,
csa_sada<enc_vector<>, 32, 32, sa_order_sa_sampling<>, int_vector<>, int_alphabet<> >,
csa_bitcompressed<int_alphabet<> >,
csa_wt<wt_int<rrr_vector<63> >, 8, 8, sa_order_sa_sampling<>, int_vector<>, int_alphabet<> >,
csa_wt<wt_int<>, 32, 32, text_order_sa_sampling<>, int_vector<>, int_alphabet<> >,
csa_sada<enc_vector<>, 32, 32, text_order_sa_sampling<>, int_vector<>, int_alphabet<> >
> Implementations;
TYPED_TEST_CASE(CsaIntTest, Implementations);
TYPED_TEST(CsaIntTest, CreateAndStoreTest)
{
TypeParam csa;
cache_config config(false, temp_dir, util::basename(test_file));
construct(csa, test_file, config, num_bytes);
test_case_file_map = config.file_map;
bool success = store_to_file(csa, temp_file);
ASSERT_EQ(true, success);
}
//! Test access methods
TYPED_TEST(CsaIntTest, Sigma)
{
TypeParam csa;
ASSERT_EQ(true, load_from_file(csa, temp_file));
int_vector<> text;
load_vector_from_file(text, test_file, num_bytes);
text.resize(text.size()+1);
text[text.size()-1] = 0; // add 0-character at the end
size_type n = text.size();
ASSERT_EQ(n, csa.size());
std::set<uint64_t> occur;
size_type sigma = 0;
for (size_type j=0; j<n; ++j) {
if (occur.end() == occur.find(text[j])) {
occur.insert(text[j]);
++sigma;
}
}
ASSERT_EQ(sigma, csa.sigma);
}
//! Test suffix array access methods
TYPED_TEST(CsaIntTest, SaAccess)
{
TypeParam csa;
ASSERT_EQ(true, load_from_file(csa, temp_file));
int_vector<> sa;
load_from_file(sa, test_case_file_map[constants::KEY_SA]);
size_type n = sa.size();
ASSERT_EQ(n, csa.size());
for (size_type j=0; j<n; ++j) {
ASSERT_EQ(sa[j], csa[j])<<" j="<<j;
}
}
//! Test inverse suffix access methods
TYPED_TEST(CsaIntTest, IsaAccess)
{
TypeParam csa;
ASSERT_EQ(true, load_from_file(csa, temp_file));
int_vector<> isa;
size_type n = 0;
{
int_vector<> sa;
load_from_file(sa, test_case_file_map[constants::KEY_SA]);
n = sa.size();
ASSERT_EQ(n, csa.size());
isa = sa;
for (size_type j=0; j<n; ++j) {
isa[sa[j]] = j; // calculate inverse suffix array
}
}
for (size_type j=0; j<n; ++j) {
ASSERT_EQ(isa[j], csa(j))<<" j="<<j;
}
}
//! Test Burrows-Wheeler access methods
TYPED_TEST(CsaIntTest, BwtAccess)
{
if (test_case_file_map.end() != test_case_file_map.find(constants::KEY_BWT_INT)) {
TypeParam csa;
ASSERT_EQ(true, load_from_file(csa, temp_file));
int_vector<> bwt;
load_from_file(bwt, test_case_file_map[constants::KEY_BWT_INT]);
size_type n = bwt.size();
ASSERT_EQ(n, csa.size());
for (size_type j=0; j<n; ++j) {
ASSERT_EQ(bwt[j], csa.bwt[j])<<" j="<<j;
}
}
}
//! Test text access methods
TYPED_TEST(CsaIntTest, TextAccess)
{
if (test_case_file_map.end() != test_case_file_map.find(constants::KEY_TEXT_INT)) {
TypeParam csa;
ASSERT_EQ(true, load_from_file(csa, temp_file));
int_vector<> text;
load_from_file(text, test_case_file_map[constants::KEY_TEXT_INT]);
size_type n = text.size();
ASSERT_EQ(n, csa.size());
for (size_type j=0; j<n; ++j) {
ASSERT_EQ(text[j], csa.text[j])<<" j="<<j;
}
}
}
//! Test Psi access methods
TYPED_TEST(CsaIntTest, PsiAccess)
{
if (test_case_file_map.end() != test_case_file_map.find(constants::KEY_PSI)) {
TypeParam csa;
ASSERT_EQ(true, load_from_file(csa, temp_file));
int_vector<> psi;
load_from_file(psi, test_case_file_map[constants::KEY_PSI]);
size_type n = psi.size();
ASSERT_EQ(n, csa.size());
for (size_type j=0; j<n; ++j) {
ASSERT_EQ(psi[j], csa.psi[j])<<" j="<<j;
}
}
}
//! Test if Psi[LF[i]]=i
TYPED_TEST(CsaIntTest, PsiLFAccess)
{
TypeParam csa;
ASSERT_EQ(true, load_from_file(csa, temp_file));
for (size_type j=0; j<csa.size(); ++j) {
size_type lf = csa.psi(j);
ASSERT_TRUE(lf >= 0);
ASSERT_TRUE(lf < csa.size());
ASSERT_EQ(j, csa.psi[lf])<<" j="<<j;
}
}
//! Test access after swap
TYPED_TEST(CsaIntTest, SwapTest)
{
TypeParam csa1;
ASSERT_EQ(true, load_from_file(csa1, temp_file));
TypeParam csa2;
csa1.swap(csa2);
int_vector<> sa;
load_from_file(sa, test_case_file_map[constants::KEY_SA]);
size_type n = sa.size();
ASSERT_EQ(n, csa2.size());
for (size_type j=0; j<n; ++j) {
ASSERT_EQ((typename TypeParam::value_type)sa[j], csa2[j]);
}
}
TYPED_TEST(CsaIntTest, DeleteTest)
{
std::remove(temp_file.c_str());
util::delete_all_files(test_case_file_map);
}
} // namespace
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
if (argc < 4) {
cout << "Usage: " << argv[0] << " test_file num_bytes temp_file tmp_dir" << endl;
cout << " (1) Generates a CSA out of test_file; stores it in temp_file." << endl;
cout << " Temporary files (SA/BWT/TEXT) are stored in tmp_dir." << endl;
cout << " num_bytes specifies who many bytes make a symbol in the"<< endl;
cout << " input sequence" << endl;
cout << " (2) Performs tests." << endl;
cout << " (3) Deletes temp_file." << endl;
return 1;
}
test_file = argv[1];
num_bytes = atoi(argv[2]);
temp_file = argv[3];
temp_dir = argv[4];
return RUN_ALL_TESTS();
}