Skip to content

Commit

Permalink
Merge pull request #16 from TravisWheelerLab/constGetHeaderSeq
Browse files Browse the repository at this point in the history
change FV struct to const when getting sequence/header
  • Loading branch information
Sawwave authored Mar 16, 2024
2 parents c86ef63 + 948f652 commit 7ac0534
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/FastaVector.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,9 @@ fastaVectorAddSequenceToList(struct FastaVector *fastaVector, char *header,

// return NULL for headerPtr and 0 for header length if the fastaVector does not
// have a header for the given headerIndex
void fastaVectorGetHeader(struct FastaVector *fastaVector, size_t headerIndex,
char **headerPtr, size_t *headerLength) {
void fastaVectorGetHeader(const struct FastaVector *const fastaVector,
size_t headerIndex, char **headerPtr,
size_t *headerLength) {
if (__builtin_expect(headerIndex >= fastaVector->metadata.count, false)) {
*headerLength = 0;
*headerPtr = NULL;
Expand All @@ -314,7 +315,7 @@ void fastaVectorGetHeader(struct FastaVector *fastaVector, size_t headerIndex,

// return NULL for headerPtr and 0 for header length if the fastaVector does not
// have a header for the given headerIndex
void fastaVectorGetSequence(struct FastaVector *fastaVector,
void fastaVectorGetSequence(const struct FastaVector *const fastaVector,
size_t sequenceIndex, char **sequencePtr,
size_t *sequenceLength) {
if (__builtin_expect(sequenceIndex >= fastaVector->metadata.count, false)) {
Expand Down
7 changes: 4 additions & 3 deletions src/FastaVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ fastaVectorAddSequenceToList(struct FastaVector *fastaVector, char *header,
*
*
*/
void fastaVectorGetHeader(struct FastaVector *fastaVector, size_t headerIndex,
char **headerPtr, size_t *headerLength);
void fastaVectorGetHeader(const struct FastaVector *const fastaVector,
size_t headerIndex, char **headerPtr,
size_t *headerLength);

/**
* @relates FastaVector
Expand All @@ -180,7 +181,7 @@ void fastaVectorGetHeader(struct FastaVector *fastaVector, size_t headerIndex,
* TODO: Should we return a static code in case the index was out of bounds?
*
*/
void fastaVectorGetSequence(struct FastaVector *fastaVector,
void fastaVectorGetSequence(const struct FastaVector *const fastaVector,
size_t sequenceIndex, char **sequencePtr,
size_t *sequenceLength);

Expand Down
5 changes: 2 additions & 3 deletions tests/fileReadTest/fileReadTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ void readTest(char **const testHeaderStrings, char **const testSequenceStrings,
"fail on reading test1.fasta for test 1");

for (size_t i = 0; i < numHeaders; i++) {
// add 1 to the strings for the null terminators
const size_t expectedHeaderLength = strlen(testHeaderStrings[i]) + 1;
const size_t expectedHeaderLength = strlen(testHeaderStrings[i]);
const size_t expectedSequenceLength = strlen(testSequenceStrings[i]);
size_t headerLength;
char *headerPtr;
Expand Down Expand Up @@ -51,7 +50,7 @@ void readTest(char **const testHeaderStrings, char **const testSequenceStrings,
testSequenceStrings[i], (int)sequenceLength, sequencePtr);
testAssertString(stringsMatch, buffer);

char terminator = headerPtr[headerLength - 1];
char terminator = headerPtr[headerLength];
sprintf(buffer,
"header index %zu was not null terminated! (found char %u after "
"the end of the header)",
Expand Down
6 changes: 3 additions & 3 deletions tests/fileWriteTest/fileWriteTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ void fastaVectorFileWriteTest(const size_t numSequences, const char *fileSrc) {
size_t headerLengthFromVector;
size_t sequenceLengthFromVector;
fastaVectorGetHeader(&fastaVector, sequenceNum, &headerFromVector,
&headerLengthFromVector);
&headerLengthFromVector);
fastaVectorGetSequence(&fastaVector, sequenceNum, &sequenceFromVector,
&sequenceLengthFromVector);
&sequenceLengthFromVector);
sprintf(buffer, "header was supposed to be length %zu, but got %zu.",
headerLength, headerLengthFromVector);
testAssertString((headerLength + 1) == headerLengthFromVector, buffer);
Expand Down Expand Up @@ -156,7 +156,7 @@ void fastaVectorFileWriteTest(const size_t numSequences, const char *fileSrc) {
size_t lastHeaderLen;
char *lastHeaderPtr;
fastaVectorGetHeader(&fastaVector, fastaVector.metadata.count - 1,
&lastHeaderPtr, &lastHeaderLen);
&lastHeaderPtr, &lastHeaderLen);
printf("headers did not match, \n h1: %.*s\n h2: %.*s\n hr: %.*s\n",
(int)fastaVector.header.count, fastaVector.header.charData,
(int)fastaReadVector.header.count, fastaReadVector.header.charData,
Expand Down
4 changes: 2 additions & 2 deletions tests/multiSequenceTest/multiSequenceTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ int main() {
for (uint8_t i = 0; i < 6; i++) {
size_t length;
char *charPtr;
fastaVectorFastaGetHeader(&fastaVector, i, &charPtr, &length);
fastaVectorGetHeader(&fastaVector, i, &charPtr, &length);
testAssertString(length == strlen(headers[i]),
"header lengths did not match");
testAssertString(strcmp(headers[i], charPtr) == 0,
"headers did not match exactly.");
fastaVectorFastaGetSequence(&fastaVector, i, &charPtr, &length);
fastaVectorGetSequence(&fastaVector, i, &charPtr, &length);
testAssertString(length == strlen(seqs[i]),
"sequence lengths did not match");
testAssertString(strcmp(seqs[i], charPtr) == 0,
Expand Down

0 comments on commit 7ac0534

Please sign in to comment.