I have a Unit Test which is used to test if a function works well over a series of input:
TEST_F( something, something) {
std::vector<int> inputFileNumber = { 0, 1 , 2, 3 };
for(auto i : inputFileNumber ) {
res = testMethodOverFile(i);
EXPECT_NEAR(res, results[i], 0.01);
logIfHasFailure(i);
}
}
I would like to know which of the inputs are failing, so I could use ::testing::Test::HasFailure. Unfortunately, once the flag is set, I cannot clear it and all the following indexes are logged as well.
Any ideas?
You should probably use parametrized test instead:
TEST_P( something, something)
{
auto i{GetParam()};
res = testMethodOverFile(i);
EXPECT_NEAR(res, results[i], 0.01);
}
INSTANTIATE_TEST_CASE_P(Default, something, testing::Values(0, 1, 2,3 ));
Related
I have the following definition of a directed graph in Kotlin. (I'm still learning Kotlin so please forgive any shortcomings. Improvements and suggestions are always welcome.) My goal is to have a method, reverse, which maintains the vertices and loops but swaps the directions of the other edges.
// We use an edge list because it makes it the easiest to swap.
data class ReversibleDirectedGraph<T>(val vertices: Set<T>, val edgeList: List<Pair<T,T>>) {
// This should be a self-inverting function.
fun reverse(): ReversibleDirectedGraph<T> {
// Make sure all vertices in edgeList are in vertices.
val allVertices = edgeList.flatMap { it.toList() }
require(allVertices.all { it in vertices }) { "Illegal graph specification" }
// Swap the edges.
val newEdgeList = edgeList.map { it.second to it.first }
return ReversibleDirectedGraph(allVertices.toSet(), newEdgeList)
}
}
fun main() {
// Example test: works correctly. Double edge reversal results in original graph.
val g = ReversibleDirectedGraph(setOf(0, 1, 2, 3),
listOf(0 to 1, 2 to 1, 3 to 2, 3 to 0, 1 to 3))
println(g)
val gr = g.reverse()
println(gr)
val grr = gr.reverse()
println(grr)
println(grr == g)
}
I'd like to use property-based testing to test this code using KotinTest, but I'm having trouble structuring it to properly produce random samples of undirected graphs. If I can achieve that point, I can reverse the edge direction twice and then ensure that the original graph is achieved.
I'm familiar with Gen.list, Gen.choose, etc, but I can't seem to fit the pieces together to get the final product, i.e. the random undirecteed graph.
I've gotten up to this, but this is clearly missing pieces, and I was hoping that someone might be able to assist. I suspect I could do it in Scala since I have more experience there, but I am determined to learn Kotlin. Ultimately, something along the lines of:
class ReversibleDirectedGraphTest: StringSpec() {
init {
"reversibleDirectedGraphTest" {
forAll { g: ReversibleDirectedGraph<Int> ->
assertEqual(g.reverse().reverse() == g) }
}
}
}
}
Any help / suggestions would be greatly appreciated. Thanks!
I ended up following the suggestion of #monkjack and creating my own Gen. I had to explicitly provide the Gen to the forAll, and a rare exception would come up with "bound must be greater than origin", but this works and the vast majority of test cases that are produced are valid and do not need to be intercepted by the try...catch.
class GraphGen: Gen<ReversibleDirectedGraph<Int>> {
override fun constants() =
listOf(
ReversibleDirectedGraph(emptySet(), emptySet()),
ReversibleDirectedGraph(setOf(0), setOf(0 to 0)),
ReversibleDirectedGraph(setOf(0, 1), emptySet()),
ReversibleDirectedGraph(setOf(0, 1), setOf(0 to 1))
)
override fun random(): Sequence<ReversibleDirectedGraph<Int>> = generateSequence {
val vertexGen = Gen.choose(0, 20)
val vertices = Gen.set(vertexGen).random().first()
val vertexList = vertices.toList()
val edgeGen = Gen.set(Gen.pair(Gen.from(vertexList), Gen.from(vertexList))).random()
// On rare occasions, this throws an exception with origin and bound, probably due
// to the number of sets. In those cases, we use an emptySet instead as a placeholder.
val edges = try { edgeGen.first() } catch (e: IllegalArgumentException) { null }
ReversibleDirectedGraph(vertices, edges?: emptySet())
}
}
class ReversibleDirectedGraphTest: StringSpec() {
init {
"reversibleDirectedGraphTest" {
forAll(GraphGen()) { g -> g.reverse().reverse() == g }
}
}
}
I'm using StencilJS v1.0.2 and it runs unit tests in Jest. I have the following code but it doesn't seem to behave as expected. Am I using the API correctly?
function ok( delay: number ) {
setTimeout(() => {
return 3;
}, delay);
}
test( 'timer', () => {
jest.useFakeTimers();
const result = ok( 1000 );
jest.advanceTimersByTime( 999 );
expect( result ).toBeUndefined();
jest.runAllTimers();
expect( result ).toBe( 3 );
} );
the test should pass but instead it fails on the last expect statement
This line:
const result = ok( 1000 );
...sets result to the return value of ok...
...but ok doesn't return anything so result is always undefined.
So the first expect passes:
expect( result ).toBeUndefined(); // Success!
...but the second expect fails since result will always be undefined:
expect( result ).toBe( 3 ); // <= FAILS since result is always undefined
The issue is that setTimeout just calls the function passed to it.
If the function returns a value then it just gets lost so this line:
return 3;
...doesn't actually do anything.
A simple way to fix the test is to have ok return an object...
...and have the function passed to setTimeout set a property on the object like this:
function ok(delay: number) {
const result: { val?: number } = {};
setTimeout(() => {
result.val = 3; // <= set a property on result
}, delay);
return result;
}
test('timer', () => {
jest.useFakeTimers();
const result = ok(1000);
jest.advanceTimersByTime(999);
expect(result.val).toBeUndefined(); // Success!
jest.runAllTimers();
expect(result.val).toBe(3); // Success!
});
I'd like to unit test a function with a set of different inputs and expected outputs.
My function is irrelevant thus I'll instead use an example function which counts english words with the following candidate implementation :
int countEnglishWords( const std::string& text )
{
return 5;
};
The following would be the set of test data. The end of the data is marked by an element with the word "END".
struct TestData {
std::string text;
int englishWords;
};
struct TestData data[] = // Mark end with "END"
{
{ "The car is very fast", 5 },
{ "El coche es muy rapido", 0 },
{ "The rain in Spain stays mainly in the plain", 9},
{ "XXXXX OOOOO TTTT", 0},
{ "Yes Si No No", 3},
{ "I have a cheerful live", 5},
{ "END", 0}
};
I could easily write 6 test cases and I would get the result I want. But this is not maintainable, since any further test added to the test cases would not be tested, it would require another test case to be written, which would be just boiler plate. Thus I've written a single test case which loops through all the test data like this :
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
class cppUnit_test: public CppUnit::TestFixture
{
private:
CPPUNIT_TEST_SUITE (cppUnit_test);
CPPUNIT_TEST(myTest);
CPPUNIT_TEST_SUITE_END();
public:
void myTest();
};
void cppUnit_test::myTest()
{
TestData* p = data;
while ( p->text != "END")
{
std::stringstream ss;
ss << "Text=\"" << p->text << "\" Counted=" <<
countEnglishWords(p->text) << " Expected=" << p->englishWords;
CPPUNIT_ASSERT_MESSAGE( ss.str().c_str(),
countEnglishWords(p->text) == p->englishWords );
++p;
}
}
int main()
{
CPPUNIT_TEST_SUITE_REGISTRATION (cppUnit_test);
CppUnit::Test *suite =
CppUnit::TestFactoryRegistry::getRegistry().makeTest();
CppUnit::TextUi::TestRunner runner;
runner.addTest(suite);
runner.run();
return 0;
}
The problem is that the previous code runs through the 1st test fine and also detects the error in the 2nd test but after that it stops testing. And the report is :
!!!FAILURES!!!
Test Results:
Run: 1 Failures: 1 Errors: 0
While the result I'd like to get is :
!!!FAILURES!!!
Test Results:
Run: 6 Failures: 4 Errors: 0
As I already mentioned in the comment cppunit 1.14.0 can support your use case.
I you want to reference an external array the quickest way is to use CPPUNIT_TEST_PARAMETERIZED. This macro expects two parameters: first similar to CPPUNIT_TEST a test method and then as a second parameter an iteratable.
Based on your code it would look like:
CPPUNIT_TEST_PARAMETERIZED(myTest, aData);
Now we need to adapt your myTest function a little bit.
void cppUnit_test::myTest(const TestData& data)
{
std::stringstream ss;
ss << "Text=\"" << data.text << "\" Counted=" <<
countEnglishWords(data.text) << " Expected=" << data.englishWords;
bool b = countEnglishWords(data.text) == data.englishWords;
std::string a = ss.str();
CPPUNIT_ASSERT_MESSAGE( a,
b);
}
Finally as the framework needs a way to report which test failed it expects that it can print the parameter that is passed to the test function. In this case the easiest way is to add a simple operator<< overload.
std::ostream& operator<<(std::ostream& strm, const TestData& data)
{
strm << data.text;
return strm;
}
If you combine these pieces you should quickly get a generic solution that will allow you to add as much data to your data array as you want without adapting the test code.
CPPUNIT_TEST_SUITE(TestSuite);
CPPUNIT_TEST_PARAMETERIZED(testMethod, {1, 2, 3, 4});
CPPUNIT_TEST_SUITE_END();
void testMethod(int /*val*/)
{
}
I am trying to use Lua for the configuration of a C++ application and am having trouble generating helpful messages when something is wrong in the configuration, not the the Lua syntax.
For example, suppose the following is a valid configuration:
foo = { a = 0, b = 'bar' }
but the user actually typed this:
foo = { a = 0, c = 'bar' }
Now, the app knows that foo can have fields a and b. It can load foo and get the value of a. It can even tell that b is not set and use a default. But I want to detect that c is present and report a warning.
Here is an extract of my attempt at that which blows up:
static void check_table(lua_State* L)
{
lua_pushnil(L);
while ( lua_next(L, -2) )
{
// key at -2 and value at -1
if ( lua_isstring(L, -2) )
{
const char* key = lua_tostring(L, -2);
// validate here; just printing key for now
cout << key << endl;
}
lua_pop(L, 1);
}
}
This works fine as long as the table is not actually an array. When I hit one of those, it dies on the second iteration with this:
...
1
PANIC: unprotected error in call to Lua API (invalid key to 'next')
which I attribute to this from the Lua reference page:
"If the value is a number, then lua_tolstring also changes the actual value in the
stack to a string. (This change confuses lua_next when lua_tolstring is applied to
keys during a table traversal.)"
Any way around this? I am open to alternate approaches. Ideally a message could be emitted like:
WARNING: conf.lua line 18: table foo does not use key 'c', ignored
(The Lua debug API doesn't give the file name and line number either, but that is a different topic.)
PS: I know, c could benign, but it could also be a typo. In a large configuration, ignoring such things could lead to hours of head scratching.
Validation will probably be much easier if written in Lua. I have something like this in mind:
local template = { a="number", b="string"}
local function validate(t)
for k,v in pairs(t) do
if template[k]==nil then
print("field "..k.." cannot be present")
elseif type(v)~=template[k] then
print("field "..k.." should be a "..template[k])
end
end
end
validate{ a = 0, b = 'bar' }
validate{ a = 0, b = 42 }
validate{ a = 0 }
validate{ a = 0, c = 'bar' }
lua_isstring is defined:
LUA_API int lua_isstring (lua_State *L, int idx) {
int t = lua_type(L, idx);
return (t == LUA_TSTRING || t == LUA_TNUMBER);
}
So instead of:
if ( lua_isstring(L, -2) )
use:
if ( lua_type(L, -2) == LUA_TSTRING )
I've written a simple, though highly multi-threaded, prime numbers generator.
The algorithm goes like this:
Thread 0: generates consecutive numbers.
Threads 1 .. N: filter out numbers that are not prime.
Upon each 'new' prime discovery, a new filter thread is added.
Take I: no flow control at all.
Thread 0 'send's numbers absolutely freely.
The program finishes with signal 11 (seg. fault), rarely signal 8, even more rarely finishes successfully.
Take II: flow control with 'setMaxMailboxSize' to 1.
Most of the time, everything works well.
Take III:
Now, if it all was a result of some internal unheld overflow, it should do well with 'setMaxMailboxSize' to 2 (or even 10), am I wrong ?
Thread 0 becomes stuck after it blocks for the first time.
Could someone please direct me what do I miss ?
Note 1:
I use DMD v2.053 under Ubuntu 10.04
Note 2:
This is my code:
#!/usr/bin/dmd -run
import std.stdio;
import std.conv;
import std.concurrency;
void main(string[] args)
{
/* parse command line arguments */
if (args.length < 2) {
writeln("Usage: prime <number of primes to generate>");
return;
}
auto nPrimes = to!int(args[1]);
auto tid = spawn(&generate, thisTid);
/* gather produced primes */
for (;;) {
auto prime = receiveOnly!int();
writeln(prime);
if (--nPrimes <= 0) {
break;
}
}
tid.send("stop");
}
void generate(Tid parentTid)
{
bool terminate = false;
// filter stage 1
auto tid = spawn(&filter_stage, parentTid);
/* WHAT DO I MISS HERE ? */
setMaxMailboxSize(tid, 1, OnCrowding.block);
for (int i = 2; !terminate; i++) {
receiveTimeout(0,
(string cmd) {
writeln(cmd);
terminate = true;
}
);
tid.send(i);
}
}
void filter_stage(Tid parentTid)
{
auto prime = receiveOnly!int();
parentTid.send(prime);
// filter stage 'N'
auto tid = spawn(&filter_stage, parentTid);
filter(prime, tid);
}
void filter(int prime, Tid tid)
{
for (;;) {
receive (
(int number) {
if (number % prime != 0) {
tid.send(number);
}
}
);
}
}
Sounds like a bug in std.concurrency. Try upgrading DMD to 2.055. I'm not sure if this specific bug is fixed but there are a lot of bug fixes between 2.053 and 2.055. If it's still broken then please file a bug report at http://d.puremagic.com/issues/.