mirror of
https://github.com/Z3Prover/z3
synced 2025-05-08 00:05:46 +00:00
tested network sorting
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
31e2d823c9
commit
c57594d463
3 changed files with 140 additions and 42 deletions
|
@ -2,6 +2,9 @@
|
|||
#include "sorting_network.h"
|
||||
#include "vector.h"
|
||||
#include "ast.h"
|
||||
#include "ast_pp.h"
|
||||
#include "reg_decl_plugins.h"
|
||||
|
||||
|
||||
struct ast_ext {
|
||||
ast_manager& m;
|
||||
|
@ -38,23 +41,100 @@ struct unsigned_ext {
|
|||
}
|
||||
};
|
||||
|
||||
void tst_sorting_network() {
|
||||
svector<unsigned> vec;
|
||||
static void is_sorted(svector<unsigned> const& v) {
|
||||
for (unsigned i = 0; i + 1 < v.size(); ++i) {
|
||||
SASSERT(v[i] <= v[i+1]);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_sorting1() {
|
||||
svector<unsigned> in, out;
|
||||
unsigned_ext uext;
|
||||
sorting_network<unsigned_ext> sn(uext, vec);
|
||||
sorting_network<unsigned_ext> sn(uext);
|
||||
|
||||
svector<unsigned> in1;
|
||||
in1.push_back(0);
|
||||
in1.push_back(1);
|
||||
in1.push_back(0);
|
||||
in1.push_back(1);
|
||||
in1.push_back(1);
|
||||
in1.push_back(0);
|
||||
in.push_back(0);
|
||||
in.push_back(1);
|
||||
in.push_back(0);
|
||||
in.push_back(1);
|
||||
in.push_back(1);
|
||||
in.push_back(0);
|
||||
|
||||
sn(in1);
|
||||
sn(in, out);
|
||||
|
||||
for (unsigned i = 0; i < vec.size(); ++i) {
|
||||
std::cout << vec[i];
|
||||
is_sorted(out);
|
||||
for (unsigned i = 0; i < out.size(); ++i) {
|
||||
std::cout << out[i];
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
static void test_sorting2() {
|
||||
svector<unsigned> in, out;
|
||||
unsigned_ext uext;
|
||||
sorting_network<unsigned_ext> sn(uext);
|
||||
|
||||
in.push_back(0);
|
||||
in.push_back(1);
|
||||
in.push_back(2);
|
||||
in.push_back(1);
|
||||
in.push_back(1);
|
||||
in.push_back(3);
|
||||
|
||||
sn(in, out);
|
||||
|
||||
is_sorted(out);
|
||||
|
||||
for (unsigned i = 0; i < out.size(); ++i) {
|
||||
std::cout << out[i];
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
static void test_sorting4_r(unsigned i, svector<unsigned>& in) {
|
||||
if (i == in.size()) {
|
||||
svector<unsigned> out;
|
||||
unsigned_ext uext;
|
||||
sorting_network<unsigned_ext> sn(uext);
|
||||
sn(in, out);
|
||||
is_sorted(out);
|
||||
std::cout << "sorted\n";
|
||||
}
|
||||
else {
|
||||
in[i] = 0;
|
||||
test_sorting4_r(i+1, in);
|
||||
in[i] = 1;
|
||||
test_sorting4_r(i+1, in);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_sorting4() {
|
||||
svector<unsigned> in;
|
||||
in.resize(5);
|
||||
test_sorting4_r(0, in);
|
||||
}
|
||||
|
||||
void test_sorting3() {
|
||||
ast_manager m;
|
||||
reg_decl_plugins(m);
|
||||
expr_ref_vector in(m), out(m);
|
||||
for (unsigned i = 0; i < 7; ++i) {
|
||||
in.push_back(m.mk_fresh_const("a",m.mk_bool_sort()));
|
||||
}
|
||||
for (unsigned i = 0; i < in.size(); ++i) {
|
||||
std::cout << mk_pp(in[i].get(), m) << "\n";
|
||||
}
|
||||
ast_ext aext(m);
|
||||
sorting_network<ast_ext> sn(aext);
|
||||
sn(in, out);
|
||||
std::cout << "size: " << out.size() << "\n";
|
||||
for (unsigned i = 0; i < out.size(); ++i) {
|
||||
std::cout << mk_pp(out[i].get(), m) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void tst_sorting_network() {
|
||||
test_sorting1();
|
||||
test_sorting2();
|
||||
test_sorting3();
|
||||
test_sorting4();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue