Skip to content

Commit

Permalink
Introduced unit tests using GoogleTest framework.
Browse files Browse the repository at this point in the history
So far only two test suites implemented.
  • Loading branch information
MarioStanke committed Mar 6, 2019
1 parent 8d44316 commit a00fadb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ auxprogs/utrrnaseq/Debug/src/*.o
auxprogs/utrrnaseq/Debug/utrrnaseq
auxprogs/utrrnaseq/*.gff
src/*.o
src/googletest/*.o
auxprogs/bam2hints/bam2hints
auxprogs/bam2hints/bam2hints.o
auxprogs/bam2wig/bam2wig
Expand Down
24 changes: 24 additions & 0 deletions src/unittests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
include ../../common.mk
AINC = ../../include
CXX = g++
CXXFLAGS := -std=c++11 -Wall -pedantic -pthread ${CXXFLAGS}
LDFLAGS = -L../googletest -lgtest -lgtest_main
INCLS += -I$(AINC) -isystem ../googletest/include/

# Link all tests to a single test binary
unittests: lldouble_unittest.o fasta_unittest.o
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) $(INCLS) lldouble_unittest.o fasta_unittest.o ../lldouble.o ../fasta.o -o unittests

# Here follows each individual Unit Test
lldouble_unittest.o: lldouble_unittest.cc $(AINC)/lldouble.hh
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) $(INCLS) -c lldouble_unittest.cc

fasta_unittest.o: fasta_unittest.cc $(AINC)/fasta.hh
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) $(INCLS) -c fasta_unittest.cc

# execute all tests: run test binary
test: unittests
./unittests

clean:
rm -f *.o unittests
30 changes: 30 additions & 0 deletions src/unittests/fasta_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* fasta_unittest.cc
*
* License: Artistic License, see file LICENSE.TXT or
* https://opensource.org/licenses/artistic-license-1.0
*
* Description: Unit tests for FASTA input
*/

#include "fasta.hh"
#include "gtest/gtest.h"

#include <fstream>
#include <string>

namespace {
TEST(FastaTest, ReadFile) {
char *sequence = NULL, *name = NULL;
int length;
std::ifstream ifstrm("test.fa");
if (!ifstrm){
FAIL() << "Could not read unittests/test.fa file.";
} else {
readOneFastaSeq(ifstrm, sequence, name, length);
ASSERT_EQ(length, 34);
ASSERT_STREQ(name, "aVeryNiceGeneName");
ASSERT_STREQ(sequence, "atttgtttgtttgtttgctttgtttgttttgttt");
}
}
}
41 changes: 41 additions & 0 deletions src/unittests/lldouble_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* lldouble_unittest.cc
*
* License: Artistic License, see file LICENSE.TXT or
* https://opensource.org/licenses/artistic-license-1.0
*
* Description: Unit tests for LLDouble
*/

#include "lldouble.hh"
#include "gtest/gtest.h"

namespace {
TEST(LLDoubleTest, Summation) { // 1+3 = 4
long double a=1.0, b=3.0, s=a+b;
LLDouble lda(a), ldb(b);
LLDouble lds = lda + ldb;
EXPECT_DOUBLE_EQ(s, lds.doubleValue());
}
TEST(LLDoubleTest, SimpleMultiplication) { // 2*3=6
EXPECT_DOUBLE_EQ(6.0, (LLDouble(2.0) * LLDouble(3.0)).doubleValue());
}
TEST(LLDoubleTest, ChainMultiplication) {
// multiply by a many small factors and then divide by the same factors
// tests implicitly testPrecision
unsigned i, n=10000;
double* factors = new double[n];
for (unsigned i = 0; i<n; ++i){
factors[i] = (i+10000.0) / 45678.9;
}
LLDouble p(1.0);
for (i = 0; i<n; ++i){
p *= factors[i];
}
for (i = 0; i<n; ++i){
p /= factors[i];
}
EXPECT_NEAR(1.0, p.doubleValue(), 1e-8);
delete [] factors;
}
}
3 changes: 3 additions & 0 deletions src/unittests/test.fa
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
>aVeryNiceGeneName
atttgtttgtttgtttgctttgtttgt
tttgttt

0 comments on commit a00fadb

Please sign in to comment.