Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

TheUnbeliever

Member Since 19 Mar 2005
Offline Last Active Feb 28 2013 04:45 PM
*****

Topics I've Started

Testing - structure, how to?

01 March 2012 - 06:10 AM

So I've done a fair bit of hobby development, but never done anything beyond look furtively at any remotely formal testing. I 'check things work' and not much more. I've tried to do differently for my current project, partly because it's towards a dissertation and the assessment considers it, but also because the failure modes are sufficiently involved that it's a terrible idea not to.

How are tests normally structured? I wrote tests for components either beforehand or at least alongside, but I usually replaced these with functional code afterwards. So I suppose my regression testing is pretty limited, but the components are sufficiently orthogonal that I've been more-or-less able to write and debug them then leave alone. I have some Octave scripts that generate test cases for some elements. Do I just have a 'make tests' which runs those scripts to generate the test data files and swaps in an alternative main.cpp?

Also, how do I test e.g. stochastic model-fitting code? So far I've checked by eye that it looks sensible, but do I want to have some code which compares the fitted model to ground truth and checks it's within some tolerance, maybe with some sort of 'voting' system to account for the element of randomness?

C++11 variadic template beginner's problems

13 February 2012 - 06:05 PM

#include <iostream>



template <typename T>

T add(const T& a)

{ return a; }



template <typename T0, typename ... Ts>

auto add(const T0& a, const Ts& ... bs) -> decltype(a + add(bs ...))

{ return a + add(bs ...); }



int main()

{   

    std::cout << add(1, 2, 3) << "\n";

}


test.cpp: In function ‘int main()’:
test.cpp:13:29: error: no matching function for call to ‘add(int, int, int)’
test.cpp:13:29: note: candidates are:
test.cpp:4:3: note: template<class T> T add(const T&)
test.cpp:8:6: note: template<class T0, class ... Ts> decltype ((a + add(add::bs ...))) add(const T0&, const Ts& ...)

Works fine for add(1, 2). What's my mistake? I realize it's probably fairly fundamental. Thanks. :-)

SFINAE

25 October 2011 - 11:00 AM

I have something that looks like:

[source lang="cpp"]template <typename T>struct Traits{ typedef BOOST_TYPEOF(&T::operator()) MemOpPtr; // ... boost::function_types and mpl magic ...};template <typename O, typename I>struct Traits<O (*)(I)>{ /* ... */ };// overload Atemplate <typename T> void func(const T& o){ /* uses Traits<T> */ }// overload B - binds a boost::function holding T::operator() to ptemplate <typename T> void func(const boost::shared_ptr<T>& p){ /* uses Traits<T> */ }[/source]

How can I have overload B be used when A's Traits substitution fails? I can just rename the second overload, but would prefer to keep the same syntax if possible. At the moment, I'll get errors telling me that operator() isn't a member of boost::shared_ptr. BOOST_TYPEOF (alone) doesn't give SFINAE, but I think something like enable_if< is_defined<T::operator()> > on the default Traits would suffice?

Thanks.

PARTNERS