public
Created

  • Download Gist
Makefile
Makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
iteration.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#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;
}
iteration.h
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#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
main.cpp
C++
1 2 3 4 5 6 7 8 9 10
#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;
}
test.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.