Skip to content

Commit

Permalink
Add cpp test (#3)
Browse files Browse the repository at this point in the history
* Added Cpp testing structure!

* Added Test Fixture Class

* Forced the cluster to only return OTU data, and created the fix test fixture with test!

* Created all of the test for the optimatrix adapter and removed the .dll from being tracked!

* Created all the test in the testfixture for listvectors and now I am adding them to the test files.

* Created base test for listVector

* Basic test for listVector are completed!

* Created the basic structure and test fixture for cluster commands

* Cluster Testing suite being pushed up!

* Fix cluster algorithm (#2)

* Fixed a slew of issues by adding bidirection distances.

* Fixed the clustering issue, clustering should be working correctly now!

* Generated Definitions for OptiCluster Test

* Finished creating unit test of the cluster command and opticluster

* Added OptiData test fixture

* Started creating the unit test for utils

* Utils command cluster and test work as expected!!

* Removed old code from one of the testing suites.

* Added another test in utils.

* Clustor has a 89% test coverage! Everything should be tested and ready to merge back!
  • Loading branch information
GregJohnsonJr authored May 17, 2024
1 parent 37ec06d commit 931b70b
Show file tree
Hide file tree
Showing 42 changed files with 1,417 additions and 108 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ __pycache__/

# Cpp Object files
*.o
*.dll

# Distribution / packaging
.Python
Expand Down Expand Up @@ -39,6 +40,9 @@ var/
pip-log.txt
pip-delete-this-directory.txt

# CMakeCheck list via Clion
CMakeLists.txt

# Unit test / coverage reports
htmlcov/
.tox/
Expand Down Expand Up @@ -186,4 +190,5 @@ fabric.properties
.idea/caches/build_file_checksums.ser

# idea folder, uncomment if you don't need it
# .idea
.idea
src/.DS_Store
19 changes: 19 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
58 changes: 58 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"files.associations": {
"__bit_reference": "cpp",
"__config": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__verbose_abort": "cpp",
"array": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"complex": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"execution": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"set": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"variant": "cpp",
"vector": "cpp",
"algorithm": "cpp"
}
}
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ Description: One paragraph description of what the package does as one
or more full sentences.
License: GPL (>= 2)
Imports: Rcpp (>= 1.0.12)
LinkingTo: Rcpp
LinkingTo: Rcpp,
testthat
Suggests:
testthat (>= 3.0.0)
testthat (>= 3.0.0),
xml2
Config/testthat/edition: 3
RoxygenNote: 7.3.1
8 changes: 4 additions & 4 deletions R/Cluster.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ OptiCluster <- function(sparse_matrix, cutoff, iterations)
indexOneList <- sparse_matrix@i

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.
indexTwoList <- sparse_matrix@j

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.
valueList <- sparse_matrix@x

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.
matrix_length <- length(indexOneList)
clustering_output_string <- MatrixToOpiMatrixCluster(indexOneList, indexTwoList, valueList, cutoff,

Check warning

Code scanning / lintr

no visible global function definition for 'MatrixToOpiMatrixCluster' Warning

no visible global function definition for 'MatrixToOpiMatrixCluster'

Check notice

Code scanning / lintr

Lines should not be more than 80 characters. This line is 101 characters. Note

Lines should not be more than 80 characters. This line is 101 characters.
matrix_length, matrix_length, iterations)
iterations)

Check notice

Code scanning / lintr

Hanging indent should be 55 spaces but is 35 spaces. Note

Hanging indent should be 55 spaces but is 35 spaces.
df <- t(read.table(text = clustering_output_string, sep = "\t", header = TRUE))

Check notice

Code scanning / lintr

Lines should not be more than 80 characters. This line is 81 characters. Note

Lines should not be more than 80 characters. This line is 81 characters.
df <- data.frame(df[-1,])

Check notice

Code scanning / lintr

Commas should always have a space after. Note

Commas should always have a space after.
colnames(df)[1] <- "cluster"
return(data.frame(df))
}
return(df)
}
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

MatrixToOpiMatrixCluster <- function(xPosition, yPosition, data, cutoff, rowSize, colSize, iterations = 2L) {
.Call(`_Opticluster_MatrixToOpiMatrixCluster`, xPosition, yPosition, data, cutoff, rowSize, colSize, iterations)
MatrixToOpiMatrixCluster <- function(xPosition, yPosition, data, cutoff, iterations = 2L) {

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Variable and function name style should match snake_case or symbols. Note

Variable and function name style should match snake_case or symbols.

Check notice

Code scanning / lintr

Lines should not be more than 80 characters. This line is 91 characters. Note

Lines should not be more than 80 characters. This line is 91 characters.
.Call(`_Opticluster_MatrixToOpiMatrixCluster`, xPosition, yPosition, data, cutoff, iterations)

Check notice

Code scanning / lintr

Indentation should be 2 spaces but is 4 spaces. Note

Indentation should be 2 spaces but is 4 spaces.

Check warning

Code scanning / lintr

no visible binding for global variable '_Opticluster_MatrixToOpiMatrixCluster' Warning

no visible binding for global variable '_Opticluster_MatrixToOpiMatrixCluster'

Check notice

Code scanning / lintr

Lines should not be more than 80 characters. This line is 98 characters. Note

Lines should not be more than 80 characters. This line is 98 characters.
}

6 changes: 6 additions & 0 deletions R/catch-routine-registration.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This dummy function definition is included with the package to ensure that
# 'tools::package_native_routine_registration_skeleton()' generates the required
# registration info for the 'run_testthat_tests' symbol.
(function() {
.Call("run_testthat_tests", FALSE, PACKAGE = "Opticluster")
})
4 changes: 3 additions & 1 deletion src/Adapters/OptimatrixAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>
#include <string>
#include <set>
#include <unordered_set>
#include <list>
#include "../MothurDependencies/OptiMatrix.h"
#include <fstream>
Expand All @@ -25,12 +26,13 @@ class OptimatrixAdapter {
this->cutoff = cutoff;
}
OptiMatrix* ConvertToOptimatrix(const std::vector<int>
&xPosition, const std::vector<int>& yPosition, const std::vector<double>& data, int rowSize, int colSize);
&xPosition, const std::vector<int>& yPosition, const std::vector<double>& data);
std::vector<std::set<long long>> GetCloseness() {return closeness;}
std::vector<std::string> GetNameList() {return nameList;}
std::vector<std::string> GetSingletons() {return singletons;}
private:
double cutoff;
//TODO Get rid of values inside of the adapter, it should not care about them
std::vector<std::set<long long>> closeness;
std::vector<std::string> nameList;
std::vector<std::string> singletons;
Expand Down
9 changes: 6 additions & 3 deletions src/ClusterCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ ClusterCommand::~ClusterCommand() {
/// Bad alloctions, returns basic_string, returns emptry string, returns non-utf8 characters, etc
/// @param optiMatrix
/// @return
int ClusterCommand::runOptiCluster(OptiMatrix *optiMatrix) {
std::string ClusterCommand::runOptiCluster(OptiMatrix *optiMatrix) {
std::string clusterOutputData;
std::string sensFile;
std::string outStep;
std::string clusterMatrixOutput;
if (!cutOffSet) {
clusterOutputData += ("\nYou did not set a cutoff, using 0.03.\n");
cutoff = 0.5;
Expand Down Expand Up @@ -122,7 +126,6 @@ int ClusterCommand::runOptiCluster(OptiMatrix *optiMatrix) {

clusterOutputData += "\n\n";
list = cluster.getList();
list->setLabel(std::to_string(cutoff));
//
if (printHeaders) {
//only print headers the first time
Expand All @@ -138,5 +141,5 @@ int ClusterCommand::runOptiCluster(OptiMatrix *optiMatrix) {
for (double result : results) { sensFile + std::to_string(result) + '\t'; }
}
delete matrix;
return 0;
return clusterMatrixOutput;
}
20 changes: 20 additions & 0 deletions src/ClusterCommandTestFixture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Created by Gregory Johnson on 5/1/24.
//

#include "tests/ClusterCommandTestFixture.h"

bool ClusterCommandTestFixture::TestSetIterationsWorksCorrectly(const int iterations, const bool expectResult) {
Setup();
const bool result = clusterCommand->SetMaxIterations(iterations);
TearDown();
return result == expectResult;
}

void ClusterCommandTestFixture::Setup() {
clusterCommand = std::make_unique<ClusterCommand>();
}

void ClusterCommandTestFixture::TearDown() {
clusterCommand.reset();
}
5 changes: 1 addition & 4 deletions src/ListVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ std::string ListVector::print(std::ostream &output, std::map<std::string, int> &
otuTag = "Otu";
std::string output_cluster;
printHeaders(output_cluster, ct, true);
output_cluster += label + "\t" + std::to_string(numBins);
//output_cluster += label + "\t" + std::to_string(numBins);
//TestHelper::Print(output_cluster);
Utils util;
std::vector<listCt> hold;
Expand Down Expand Up @@ -100,9 +100,7 @@ std::string ListVector::print(std::ostream &output) {
void ListVector::printHeaders(std::string &output, std::map<std::string, int> &ct, bool sortPlease) {
if (printListHeaders) {
if (binLabels.size() == 0) { sortPlease = false; } //we are creating arbitary otuNames
output = "hello\n";
std::vector<std::string> theseLabels = getLabels();
output = "label\tnum" + otuTag + "s";
if (sortPlease) {
Utils util;
std::vector<listCt> hold;
Expand Down Expand Up @@ -131,7 +129,6 @@ void ListVector::printHeaders(std::string &output, std::map<std::string, int> &c
}

output += "\n";

printListHeaders = false;
}
}
132 changes: 132 additions & 0 deletions src/ListVectorTestFixture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// Created by Gregory Johnson on 5/1/24.
//

#include "tests/ListVectorTestFixture.h"

bool ListVectorTestFixture::TestListVectorReturnsCorrectNumberOfBins(const std::vector<std::string>& mockListOfSequences,
const int expectedResult) {
Setup();
for (const auto& sequences: mockListOfSequences) {
listVector->push_back(sequences);
}
const bool result = listVector->getNumBins() == expectedResult;
TearDown();
return result;
}

bool ListVectorTestFixture::TestListVectorReturnsCorrectNumSeqs(const std::vector<std::string> &mockListOfSequences,
const int expectedResult) {
Setup();
for (const auto& sequences: mockListOfSequences) {
listVector->push_back(sequences);
}
const bool result = listVector->getNumSeqs() == expectedResult;
TearDown();
return result;
}

bool ListVectorTestFixture::TestListVectorReturnsCorrectMaxRank(const std::vector<std::string> &mockListOfSequences,
const int expectedResult) {
Setup();
for (const auto& sequences: mockListOfSequences) {
listVector->push_back(sequences);
}
const bool result = listVector->getMaxRank() == expectedResult;
TearDown();
return result;
}

bool ListVectorTestFixture::TestListVectorReturnsCorrectGetValue(const std::vector<std::string> &mockListOfSequences,
const int index, const std::string &expectedResult) {
Setup();
for (const auto& sequences: mockListOfSequences) {
listVector->push_back(sequences);
}
const bool result = listVector->get(index) == expectedResult;
TearDown();
return result;
}

bool ListVectorTestFixture::TestListVectorReturnsCorrectGetLabelsValue(
const std::vector<std::string> &mockListOfSequences, const int expectedResult) {
Setup();
for (const auto& sequences: mockListOfSequences) {
listVector->push_back(sequences);
}
const bool result = listVector->getLabels().size() == expectedResult;
TearDown();
return result;
}

bool ListVectorTestFixture::TestListVectorReturnsCorrectGetOtuNames(const std::vector<std::string> &mockListOfSequences,
const int binToTest, const std::string &expectedResult) {
Setup();
for (const auto& sequences: mockListOfSequences) {
listVector->push_back(sequences);
}
const bool result = listVector->getOTUName(binToTest) == expectedResult;
TearDown();
return result;
}

bool ListVectorTestFixture::TestListVectorSetsLabelsCorrectly(const std::vector<std::string> &mockListOfLabels,
const int expectedResult) {
Setup();
listVector->setLabels(mockListOfLabels);
const bool result = listVector->getLabels().size() == expectedResult;
TearDown();
return result;
}

bool ListVectorTestFixture::TestListVectorSetsPrintedLabelsCorrectly(const bool mockValue, const bool expectedResult) {
Setup();
const bool result = expectedResult == listVector->setPrintedLabels(mockValue);
TearDown();
return result;
}

bool ListVectorTestFixture::TestListVectorPushBackSetsDataCorrectly(const std::vector<std::string>& mockListOfSequences,
const int index, const std::string& expectedResult) {
Setup();
for (const auto& sequences: mockListOfSequences) {
listVector->push_back(sequences);
}
const bool result =listVector->get(index) == expectedResult;
TearDown();
return result;

}

bool ListVectorTestFixture::TestListVectorPrintDisplaysDataCorrectly(
const std::vector<std::string> &mockListOfSequences, const std::string& expectedResult) {
Setup();
std::ofstream stream;
for (const auto& sequences: mockListOfSequences) {
listVector->push_back(sequences);
}
// Get rid of ofstream in listVector
const bool result =listVector->print(stream) == expectedResult;
TearDown();
return result;
}


bool ListVectorTestFixture::TestListVectorReturnsCorrectGetOtuNamesSize(const std::vector<std::string> &mockListOfSequences,
const int binToTest, const int expectedResult) {
Setup();
for (const auto& sequences: mockListOfSequences) {
listVector->push_back(sequences);
}
const bool result = listVector->getOTUName(binToTest).size() == expectedResult;
TearDown();
return result;
}

void ListVectorTestFixture::Setup() {
listVector = std::make_unique<ListVector>();
}

void ListVectorTestFixture::TearDown() {
listVector.reset();
}
Loading

0 comments on commit 931b70b

Please sign in to comment.