File: DNASequence_gtest.cpp

package info (click to toggle)
pbseqlib 0~20161219-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,924 kB
  • ctags: 5,123
  • sloc: cpp: 82,727; makefile: 305; python: 239; sh: 8
file content (279 lines) | stat: -rw-r--r-- 7,899 bytes parent folder | download | duplicates (3)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * =====================================================================================
 *
 *       Filename:  DNASequence_gtest.cpp
 *
 *    Description:  Test DNASequence.hpp
 *
 *        Version:  1.0
 *        Created:  10/27/2012 09:42:13 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Yuan Li (yli), yli@pacificbiosciences.com
 *        Company:  Pacific Biosciences
 *
 * =====================================================================================
 */

#include "gtest/gtest.h"
#include "DNASequence.hpp"
#include <climits>
#include <iostream>
#include <fstream>
using namespace std;

//Note ::testing::Test not ::testing::TEST
//SetUp() and TearDown(), not Setup() and Teardown()
class DNASequenceTest : public ::testing::Test {
public:
    DNASequence dnaOne;
};

//Test DNASequence constructor
TEST_F(DNASequenceTest, Constructor) {
    DNASequence dnaSeq;
    EXPECT_TRUE(dnaSeq.seq == NULL);
    EXPECT_TRUE(dnaSeq.length == 0);
    EXPECT_TRUE(dnaSeq.size() == dnaSeq.length);
    EXPECT_TRUE(dnaSeq.bitsPerNuc == 8);
    EXPECT_FALSE(dnaSeq.deleteOnExit);

    Nucleotide HKITTY[] = "HELLO,KITTY!";
    dnaSeq.seq = HKITTY;
    dnaSeq.length = sizeof(HKITTY)/sizeof(Nucleotide) - 1;
//    dnaSeq.Print(cout);
    EXPECT_EQ(dnaSeq.size(), 12);

    
    DNALength thisLen = 12;
    Nucleotide * thisNuc = new Nucleotide [thisLen];
    memcpy(thisNuc, HKITTY, thisLen);
    DNASequence newDnaSeq; 
    newDnaSeq.seq = thisNuc;
    newDnaSeq.length = thisLen;
//    newDnaSeq.Print(cout);
    EXPECT_EQ(memcmp(newDnaSeq.seq, dnaSeq.seq, thisLen), 0);
    EXPECT_EQ(newDnaSeq.length, thisLen);
    if (!thisNuc) delete thisNuc;

    DNASequence nnewDnaSeq;
    thisLen = 12;
    string atgc ("atgcatgcatgc");
    thisNuc = new Nucleotide [thisLen];
    for (int i = 0 ; i < thisLen; i++) {
        thisNuc[i] = atgc[i];
    }
    string ret;
    nnewDnaSeq.seq = thisNuc;
    nnewDnaSeq.length = thisLen;
    for (int i = 0 ; i < thisLen; i++) {
        ret += nnewDnaSeq.seq[i];
    }
    EXPECT_STREQ(ret.c_str(), atgc.c_str());
}

//Test DNASequence Append()
TEST_F(DNASequenceTest, Append) {
    DNALength oneLen = 10;
    Nucleotide * one = new Nucleotide [oneLen];

    string As("AAAAAAAAAA"); 
    for (int i = 0; i < oneLen; i++) {
        one[i] = As[i];
    }
    //Can not memcpy a string to a DNASequence directly 
    //such as memcpy(one, As.c_str(), oneLen), because 
    //DNASequence.seq is of type unsigned char, not char

    DNALength twoLen = 20;
    Nucleotide * two = new Nucleotide [twoLen];
    
    string Gs("GGGGGGGGGGGGGGGGGGGG");
    for (int i = 0; i < twoLen; i++) {
        two[i] = Gs[i];
    }
    //memcpy(two, Gs.c_str(), twoLen);

    Nucleotide * three = new Nucleotide [oneLen + twoLen];
    memcpy(three, one, oneLen);
    memcpy(three+oneLen, two, twoLen);

    DNASequence dnaTwo;
    dnaOne.seq = one; 
    dnaOne.length = oneLen;
    dnaOne.deleteOnExit = true;
    dnaTwo.seq = two;
    dnaTwo.length = twoLen;

    dnaOne.Append(dnaTwo, 0);
    EXPECT_EQ(dnaOne.length, oneLen + twoLen);
    EXPECT_EQ(memcmp(dnaOne.seq, three, dnaOne.length), 0);

    string AGs("AAAAAAAAAAGGGGGGGGGGGGGGGGGGGG");
    for (int i = 0; i < dnaOne.length; i++) {
        EXPECT_EQ(AGs[i], (char)dnaOne.seq[i]);
    }
  
    //if appendPos is positive, overwrite this sequence
    //from appendPos to the end
    AGs = "AAGGGGGGGGGGGGGGGGGGGG";
    DNALength appendPos = 2;
    dnaOne.Append(dnaTwo, appendPos);
    EXPECT_EQ(dnaOne.length, appendPos + twoLen);
    for (int i = 0; i < dnaOne.length; i++) {
        EXPECT_EQ(AGs[i], (char)dnaOne.seq[i]);
    }
    
    if(!one) delete one;
    if(!two) delete two;
    if(!three) delete three;
}

//Test DNASequence TakeOwnership
TEST_F(DNASequenceTest, TakeOwnership) {
    DNALength oneLen = 10;
    Nucleotide * one = new Nucleotide [oneLen];
    
    dnaOne.seq = one; 
    dnaOne.length = oneLen;

    DNASequence dnaTwo;
    
    //a bug may occur if deleteOneExit is true and 
    //TakeOwnership() is called twice. In that case, both
    //dnaOne and dnaTwo will become wild pointers 
    dnaTwo.deleteOnExit = true;
    dnaTwo.TakeOwnership(dnaOne);
    
    EXPECT_EQ(dnaTwo.length, dnaOne.length);
    EXPECT_EQ(dnaTwo.deleteOnExit, dnaOne.deleteOnExit);
    EXPECT_EQ(dnaTwo.seq, dnaOne.seq);

    if(!one) delete one;
}

//Test DNASequence ShallowCopy
TEST_F(DNASequenceTest, ShallowCopy) {
    DNALength oneLen = 10;
    Nucleotide * one = new Nucleotide [oneLen];

    string As("AAAAAAAAAA");
    for (int i = 0; i < oneLen; i++) {
        one[i] = As[i];
    }
    dnaOne.seq = one;
    dnaOne.length = oneLen;

    DNASequence dnaTwo;
    dnaTwo.ShallowCopy(dnaOne);

    EXPECT_EQ(dnaTwo.length, dnaOne.length);
    EXPECT_EQ(dnaTwo.seq   , dnaOne.seq);
    EXPECT_EQ(dnaTwo.deleteOnExit, dnaOne.deleteOnExit);
}


//Test DNASequence.Copy(const DNASequence rhs, 
//                      DNALength rhsPos,
//                      DNALength rhsLength)
TEST_F(DNASequenceTest, Copy) {
    DNALength oneLen = 10;
    Nucleotide * one = new Nucleotide [oneLen];

    string As("AGAAAAACAA");
    for (int i = 0; i < oneLen; i++) {
        one[i] = As[i];
    }

    dnaOne.seq = one;
    dnaOne.length = oneLen;

    DNASequence dnaTwo;
    dnaTwo.Copy(dnaOne);

    EXPECT_EQ(dnaTwo.length, dnaOne.length);
    EXPECT_NE(dnaTwo.seq   , dnaOne.seq);
    EXPECT_TRUE(dnaTwo.deleteOnExit); 
    EXPECT_EQ(memcmp(dnaTwo.seq, dnaOne.seq, dnaOne.length), 0);

    //if rhs.length is 0, return this * 
    DNASequence dnaThree;
    dnaTwo.Copy(dnaThree);
    //dnaTwo remains unchanged
    EXPECT_EQ(dnaTwo.length, 0);
    EXPECT_NE(dnaTwo.seq, dnaOne.seq);
    EXPECT_TRUE(dnaTwo.deleteOnExit); 
    EXPECT_TRUE(dnaTwo.seq == NULL);

    //if rhsPos is not 0 and rhsLength is 0
    dnaTwo.Copy(dnaOne, 2);
    EXPECT_EQ(dnaTwo.length, dnaOne.length - 2);
    EXPECT_TRUE(dnaTwo.deleteOnExit); 
    EXPECT_EQ(memcmp(dnaTwo.seq, dnaOne.seq + 2, dnaTwo.length), 0);


    //if the subsequence to copy is out of bounds
    EXPECT_GT(200, dnaOne.length);
    //EXPECT_EXIT(dnaTwo.Copy(dnaOne, 200), ::testing::ExitedWithCode(1), ""); 


    //if both rhsPos and rhsLength are less than MAXINT,
    //but rhsPos+ rhsLength > MAXINT
    DNALength rhsPos = 3;
    DNALength rhsLength = UINT_MAX -1;
    EXPECT_TRUE(rhsPos < UINT_MAX && rhsLength < UINT_MAX);
    EXPECT_TRUE(rhsLength > dnaOne.length + 1);
    //EXPECT_EXIT(dnaTwo.Copy(dnaOne, rhsPos, rhsLength), ::testing::ExitedWithCode(1), "");


    //if rhsPos > rhs.length
    //EXPECT_EXIT(dnaTwo.Copy(dnaOne, dnaOne.length + 1), ::testing::ExitedWithCode(1), "")
    //    << "Copy a subsequence which is out of bounds. This needs to be taken care of. See bug 21867.";

}

//Test DNASequence Allocate(DNALength)
TEST_F(DNASequenceTest, Allocate) {
    dnaOne.Allocate(0);
    EXPECT_EQ(dnaOne.length, 0);

    DNASequence dnaTwo;
    dnaTwo.Allocate(100);
    EXPECT_EQ(dnaTwo.length, 100);
}

//Test DNASequence ReferenceSubstring(rhs, pos, substrLength)
TEST_F(DNASequenceTest, ReferenceSubstring) {
    DNALength oneLen = 10;
    dnaOne.seq = new Nucleotide[oneLen];
    dnaOne.length = oneLen;

    DNASequence dnaTwo;
    dnaTwo.ReferenceSubstring(dnaOne);

    EXPECT_EQ(dnaOne.seq, dnaTwo.seq);
    EXPECT_EQ(dnaOne.length, dnaTwo.length);
    EXPECT_FALSE(dnaTwo.deleteOnExit);

//    EXPECT_DEATH_IF_SUPPORTED(dnaTwo.ReferenceSubstring(dnaOne, 100), "");
    delete [] dnaOne.seq;
}
/*
TEST_F(DNASequenceTest, CopyFromString) {
    // Test Copy(const std::string &)
    string str = "ATGCGGGCCTCGCCG";
    dnaOne.Copy(str);

    for (int i = 0; i < str.size(); i++) {
       EXPECT_EQ(dnaOne.seq[i], str[i]);
    }

    // Test operator = (const std::string)
    DNASequence dnaTwo;
    dnaTwo = str;
    for (int i = 0; i < str.size(); i++) {
       EXPECT_EQ(dnaOne.seq[i], str[i]);
    }
}
*/