Skip to content

Commit

Permalink
added a test for some of the common functions. Might be extended? Or …
Browse files Browse the repository at this point in the history
…we could remove some unused functions
  • Loading branch information
kosloot authored and proycon committed Jul 3, 2023
1 parent a01fe64 commit a112be8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
8 changes: 5 additions & 3 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ void orderedinsert(std::list<double> & l, double value);
std::vector<std::string> & split(const std::string &s, char delim, std::vector<std::string> &elems);
std::vector<std::string> split(const std::string &s, char delim);

bool test_common();

class InternalError: public std::runtime_error {
public:
public:
explicit InternalError(): std::runtime_error("Colibri internal error") {}
};

class KeyError: public std::runtime_error {
public:
public:
explicit KeyError(): std::runtime_error("Colibri KeyError") {}
};

class UnknownTokenError: public std::runtime_error {
public:
public:
explicit UnknownTokenError(): std::runtime_error("Colibri TokenError: the input contained an unknown token") {}
};
#endif
41 changes: 31 additions & 10 deletions src/common.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "common.h"
#include <glob.h>
#include <sstream>
#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

Expand Down Expand Up @@ -32,19 +34,11 @@ bool strip_extension(std::string& filename, const std::string& extension) {
}

double listproduct(const vector<double> & l) {
double p = 1;
for ( const auto& iter : l ){
p *= iter;
}
return p;
return std::accumulate( l.begin(), l.end(), 1, std::multiplies<double>() );
}

double listsum(const vector<double> & l) {
double p = 0;
for ( const auto& iter: l ){
p += iter;
}
return p;
return std::accumulate( l.begin(), l.end(), 0 );
}

void orderedinsert(list<double> & l, double value) {
Expand Down Expand Up @@ -72,3 +66,30 @@ vector<string> split(const string &s, char delim) {
vector<string> elems;
return split(s, delim, elems);
}

bool test_common(){
int err_cnt = 0;
vector<double> v { 1.0, 2.0, 3.0, 4.0 };
if ( listproduct( v ) != 24.0 ) {
cerr << "listproduct problem: " << listproduct( v ) << " != 24.0" << endl;
++err_cnt;
}
if ( listsum( v ) != 10.0 ){
cerr << "listproduct problem: " << listsum( v ) << " != 10" << endl;
++err_cnt;
}
list<double> l { 1.0, 2.0, 3.0, 4.0 };
orderedinsert( l, 3.14 );
if ( l != list<double>({ 1.0, 2.0, 3.0, 3.14, 4.0 }) ){
cerr << "orderedinsert problem: { ";
for ( const auto& it : l ){
cerr << it;
if ( &it != &l.back() ){
cerr << ", ";
}
}
cerr << " } != { 1.0, 2.0, 3.0, 3.14, 4.0 }" << endl;
++err_cnt;
}
return err_cnt == 0;
}
4 changes: 3 additions & 1 deletion src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ void testgt(unsigned int value , unsigned int ref) {

int main( int argc, const char *argv[] ) {


//string model = argv[1];
//string classfile = argv[1];
bool verbose = false;
Expand Down Expand Up @@ -127,6 +126,9 @@ int main( int argc, const char *argv[] ) {
}
*/

{
cerr << " common functions "; test(test_common(),true);
}

int res = chdir("/tmp");
if ( res != 0 ){
Expand Down

0 comments on commit a112be8

Please sign in to comment.