Skip to content

Commit

Permalink
Provide test for ecto::BREAK return value.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Görner authored and v4hn committed Sep 5, 2015
1 parent 2759c31 commit 2bd2664
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/cells/BreakEveryN.cpp
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2015, Michael 'v4hn' Goerner
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <ecto/ecto.hpp>
#include <ecto/registry.hpp>

using ecto::tendrils;
namespace ecto
{
struct BreakEveryN
{
static void declare_params(ecto::tendrils& p)
{
p.declare(&BreakEveryN::n_, "n", "Break on every nth process.", 1);
}

static void declare_io(const ecto::tendrils& parameters, ecto::tendrils& inputs, ecto::tendrils& outputs)
{
inputs.declare<tendril::none>("in", "Any input");
outputs.declare("out", inputs["in"]);
}

void configure(const tendrils& p, const tendrils& i, const tendrils& o)
{
i_ = 0;
}

int process(const tendrils& inputs, const tendrils& outputs)
{
if( ++i_ >= *n_ ){
i_ = 0;
return ecto::BREAK;
}
return ecto::OK;
}

size_t i_;

ecto::spore<size_t> n_;
};
}

ECTO_CELL(ecto_test, ecto::BreakEveryN, "BreakEveryN", "Pass on a values but break every Nth iteration.");

1 change: 1 addition & 0 deletions test/cells/CMakeLists.txt
Expand Up @@ -28,6 +28,7 @@
ectomodule(ecto_test
Accumulator.cpp
Add.cpp
BreakEveryN.cpp
BpObjectToCellPtr.cpp
CantCallMeFromTwoThreads.cpp
ConfigureCalledOnce.cpp
Expand Down
1 change: 1 addition & 0 deletions test/scripts/CMakeLists.txt
Expand Up @@ -36,6 +36,7 @@ foreach(file
test_async_multiple_sched
test_async
test_blackbox
test_break
#test_bp_to_cell_ptr
test_circular
test_constant
Expand Down
52 changes: 52 additions & 0 deletions test/scripts/test_break.py
@@ -0,0 +1,52 @@
#!/usr/bin/env python
#
# Copyright (c) 2015, Michael 'v4hn' Goerner
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import ecto
import ecto.ecto_test as ecto_test

# run the generator 10 times
gen = ecto_test.Generate(start= 0, step= 1, stop= 9)

# break on every second iteration
breaker= ecto_test.BreakEveryN(n= 2)

cnt = ecto.Counter()

plasm = ecto.Plasm()
plasm.connect([
gen[:] >> breaker[:],
breaker[:] >> cnt[:]
])

plasm.execute(niter= 0)

# as cnt depends on breaker it will only be processed every second iteration
result = cnt.outputs.count
print "Counter ran " + str(result) + " times"
assert(result == 5)

0 comments on commit 2bd2664

Please sign in to comment.