Skip to content

Instantly share code, notes, and snippets.

@daxim
Created January 19, 2014 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daxim/8508188 to your computer and use it in GitHub Desktop.
Save daxim/8508188 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include "iteration.h"
struct func_val {
float operator() (int a, int b) {
return a / (b + 3.141592);
}
float operator() (const triple& a) {
return this->operator()( a.a, a.b );
}
};
val_vec_t fill() {
val_vec_t out;
for (int i=0; i<1000; ++i) {
triple t {i, -i, 0.0f};
out.push_back(t);
}
return out;
}
val_vec_t iteration() {
val_vec_t t = fill();
for( std::vector<triple>::iterator it = t.begin(); it != t.end(); ++it ) {
it->res = it->a / ( it->b + 3.141592 );
}
for( auto it = t.begin(); it != t.end(); ++it ) {
it->res = it->a / ( it->b + 3.141592 );
}
func_val f;
for( auto it = t.begin(); it != t.end(); ++it ) {
it->res = f( *it );
}
std::for_each( t.begin(), t.end(), func_val() );
std::for_each( t.begin(), t.end(), []( triple& it ) {
it.res = it.a / (it.b + 3.141592 );
} );
return t;
}
#ifndef ITERATION_H_
#define ITERATION_H_
#include <string>
#include <vector>
struct triple {
int a;
int b;
float res;
bool operator==(const triple& rhs) {
return a == rhs.a && b == rhs.b && res == rhs.res;
}
std::string to_string() {
return std::to_string(a) + " | " + std::to_string(b) + " | " + std::to_string(res);
}
};
typedef std::vector<triple> val_vec_t;
val_vec_t iteration();
#endif
#include <iostream>
#include "iteration.h"
int main( int argc, char** argv ) {
auto v = iteration();
for( val_vec_t::iterator it = v.begin(); it != v.end(); ++it ) {
std::cout << (*it).to_string() << std::endl;
}
return 0;
}
CXXFLAGS = -O0 -g -Wall -fmessage-length=0 -pedantic -std=c++11 -Wextra
all: iteration main.cpp
$(CXX) $(CXXFLAGS) -c -o main.o main.cpp
$(CXX) -o iteration main.o iteration.o
iteration: iteration.cpp
$(CXX) $(CXXFLAGS) -c -o iteration.o iteration.cpp
test: iteration test.cpp
$(CXX) $(CXXFLAGS) -c -o test.o test.cpp
$(CXX) -o test test.o iteration.o
prove -e '' ./test
clean:
rm -f *.o ./iteration ./test
#include "iteration.h"
#include <string>
#include <iostream>
// simple testing functions with TAP output
/**
* @brief running number of tests
*/
static int counter = 0;
/**
* @brief compares two values and accordingly prints ok or not ok
*
* @param got the value from the library under test
* @param expected the value that you expect to get from the library under test
* @return void
*/
template<typename T>
void is( T got, T expected) {
counter++;
if( got == expected ) {
std::cout << "ok " << counter << std::endl;
return;
}
std::cout << "not ok " << counter << std::endl;
std::cout << "# Failed test #" << counter << std::endl;
std::cout << "# got: " << got.to_string() << std::endl;
std::cout << "# expected: " << expected.to_string() << std::endl;
return;
}
/**
* @brief compares two values and accordingly prints ok or not ok
*
* @param got the value from the library under test
* @param expected the value that you expect to get from the library under test
* @param comment human readable description of the test
* @return void
*/
template<typename T>
void is( T got, T expected, std::string comment ) {
counter++;
if( got == expected ) {
std::cout << "ok " << counter << " - " << comment << std::endl;
return;
}
std::cout << "not ok " << counter << " - " << comment << std::endl;
std::cout << "# Failed test #" << counter << " - '" << comment << "'" << std::endl;
std::cout << "# got: " << got.to_string() << std::endl;
std::cout << "# expected: " << expected.to_string() << std::endl;
return;
}
/**
* @brief finalise TAP stream
*
* @return void
*/
void done_testing() {
std::cout << "1.." << counter << std::endl;
return;
}
int main() {
// fetch data to test
val_vec_t res = iteration();
// our tests
is(res.at(0), triple {0,0,0});
is(res.at(1), triple {1,-1,0.46694234}, "complicated case");
done_testing();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment