Scilab: std_set example converted to test

This commit is contained in:
Simon Marchetto 2013-09-09 11:13:36 +02:00
commit 3abb810cc1
6 changed files with 52 additions and 83 deletions

View file

@ -0,0 +1,76 @@
%module li_std_set_as_argument
%{
#include <set>
#include <string>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <sstream>
%}
%include stl.i
namespace std
{
%template(IntSet) set<int>;
%template(StringSet) set<std::string>;
}
%ignore concat_set;
%inline %{
template <typename T>
std::set<T> concat_set(const std::set<T> set, const std::set<T> other_set)
{
std::set<T> out_set(set);
out_set.insert(other_set.begin(), other_set.end());
return out_set;
}
// int sets
std::set<int> create_integer_set(const int rangemin, const int rangemax)
{
std::set<int> out_set;
for (int i = rangemin; i <= rangemax; i++)
{
out_set.insert(i);
}
return out_set;
}
int sum_integer_set(const std::set<int>& set)
{
return std::accumulate(set.begin(), set.end(), 0);
}
std::set<int> concat_integer_set(const std::set<int> set, const std::set<int> other_set)
{
return concat_set<int>(set, other_set);
}
// string sets
std::set<std::string> create_string_set(const char* svalue)
{
std::set<std::string> out_set;
std::string str(svalue);
std::istringstream iss(str);
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::inserter<std::set<std::string> >(out_set, out_set.begin()));
return out_set;
}
std::set<std::string> concat_string_set(const std::set<std::string> set, const std::set<std::string> other_set)
{
return concat_set<std::string>(set, other_set);
}
%}

View file

@ -0,0 +1,35 @@
// Tests C++ fonctions with STL sets as arguments
exec("swigtest.start", -1);
// integer sets
// Example of passing matrices of int as set arguments of C++ functions.");
// get a set of int {1...4} from create_integer_set():");
iset = create_integer_set(1, 4);
if ~exists("iset") | (iset <> [1 2 3 4]) then swigtesterror(); end
// get the sum of this set elements with sum_integer_set():")
isum = sum_integer_set(iset);
if ~exists("isum") | (isum <> 10) then swigtesterror(); end
// get a set of of int {3...6} from create_integer_set():");
iset2 = create_integer_set(3, 6);
if ~exists("iset2") | (iset2 <> [3 4 5 6]) then swigtesterror(); end
// concat the two sets with concat_integer_set():");
iset3 = concat_integer_set(iset, iset2);
if ~exists("iset3") | (iset3 <> [1 2 3 4 5 6]) then swigtesterror(); end
// string sets
// Example of passing matrices of string as set arguments of C++ functions.");
// get a set of string {''aa'', ''bb'', ''cc'', ''dd''} with create_string_set():");
sset = create_string_set("aa bb cc dd");
if ~exists("sset") | (sset <> ["aa"; "bb"; "cc"; "dd"]) then swigtesterror(); end
// get a set of string {''cc'', ''dd'', ''ee'', ''ff''} with create_string_set():");
sset2 = create_string_set("cc dd ee ff");
if ~exists("sset2") | (sset2 <> ["cc"; "dd"; "ee"; "ff"]) then swigtesterror(); end
// concat the two sets with concat_string_set():");
sset3 = concat_string_set(sset, sset2);
if ~exists("sset3") | (sset3 <> ["aa"; "bb"; "cc"; "dd"; "ee"; "ff"]) then swigtesterror(); end
exec("swigtest.quit", -1);